Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds improved matching for globs #134

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
23 changes: 21 additions & 2 deletions src/django_watchfiles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
from __future__ import annotations

import re
import sys
import threading
from collections.abc import Generator
from collections.abc import Iterable
from fnmatch import fnmatch
from fnmatch import translate
from pathlib import Path
from typing import Callable

from django.utils import autoreload
from watchfiles import Change
from watchfiles import watch

if sys.version_info >= (3, 13):

def full_match(relative_path_str: str, glob: str) -> bool:
return Path(relative_path_str).full_match(glob)

MATCH_METHOD = full_match
else:

def full_match_backport(relative_path_str: str, glob: str) -> bool:
# Full-match backport for python <= 3.12 with support for ** directories
# Not perfect; too greedy with `*/`, but much improved over fnmatch()
glob = glob.replace("**/", "**")
regex = re.compile(translate(glob))
return bool(regex.match(relative_path_str))

MATCH_METHOD = full_match_backport


class MutableWatcher:
"""
Expand Down Expand Up @@ -71,7 +90,7 @@ def file_filter(self, change: Change, filename: str) -> bool:
else:
relative_path_str = str(relative_path)
for glob in globs:
if fnmatch(relative_path_str, glob):
if MATCH_METHOD(relative_path_str, glob):
return True
return False

Expand Down