Skip to content

Commit 0cba208

Browse files
committed
Add "matchcase" option to iterchildren and matchglob
1 parent 0140979 commit 0cba208

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

domdf_python_tools/paths.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ def iterchildren(
774774
self: _PP,
775775
exclude_dirs: Optional[Iterable[str]] = unwanted_dirs,
776776
match: Optional[str] = None,
777+
matchcase: bool = True,
777778
) -> Iterator[_PP]:
778779
"""
779780
Returns an iterator over all children (files and directories) of the current path object.
@@ -784,6 +785,11 @@ def iterchildren(
784785
together with their children.
785786
:param match: A pattern to match filenames against.
786787
The pattern should be in the format taken by :func:`~.matchglob`.
788+
:param matchcase: Whether the filename's case should match the pattern.
789+
790+
:rtype:
791+
792+
.. versionchanged:: 2.5.0 Added the ``matchcase`` option.
787793
"""
788794

789795
if not self.is_dir():
@@ -801,12 +807,12 @@ def iterchildren(
801807
if any(d in parts for d in exclude_dirs):
802808
continue
803809

810+
if match is None or (match is not None and matchglob(file, match, matchcase)):
811+
yield file
812+
804813
if file.is_dir():
805814
yield from file.iterchildren(exclude_dirs, match)
806815

807-
if match is None or (match is not None and matchglob(file, match)):
808-
yield file
809-
810816

811817
class PosixPathPlus(PathPlus, pathlib.PurePosixPath):
812818
"""
@@ -878,7 +884,7 @@ def traverse_to_file(base_directory: _P, *filename: PathLike, height: int = -1)
878884
raise FileNotFoundError(f"'{filename[0]!s}' not found in {base_directory}")
879885

880886

881-
def matchglob(filename: PathLike, pattern: str):
887+
def matchglob(filename: PathLike, pattern: str, matchcase: bool = True):
882888
"""
883889
Given a filename and a glob pattern, return whether the filename matches the glob.
884890
@@ -888,12 +894,19 @@ def matchglob(filename: PathLike, pattern: str):
888894
:param pattern: A pattern structured like a filesystem path, where each element consists of the glob syntax.
889895
Each element is matched by :mod:`fnmatch`.
890896
The special element ``**`` matches zero or more files or directories.
897+
:param matchcase: Whether the filename's case should match the pattern.
898+
899+
:rtype:
891900
892901
.. seealso::
893902
894903
:wikipedia:`Glob (programming)#Syntax` on Wikipedia
904+
905+
.. versionchanged:: 2.5.0 Added the ``matchcase`` option.
895906
"""
896907

908+
match_func = fnmatch.fnmatchcase if matchcase else fnmatch.fnmatch
909+
897910
filename = PathPlus(filename)
898911

899912
pattern_parts = deque(pathlib.PurePath(pattern).parts)
@@ -927,16 +940,16 @@ def matchglob(filename: PathLike, pattern: str):
927940
# Filename must match everything after **
928941
return False
929942

930-
if fnmatch.fnmatchcase(filename_part, pattern_part):
943+
if match_func(filename_part, pattern_part):
931944
continue
932945
else:
933-
while not fnmatch.fnmatchcase(filename_part, pattern_part):
946+
while not match_func(filename_part, pattern_part):
934947
if not filename_parts:
935948
return False
936949

937950
filename_part = filename_parts.popleft()
938951

939-
elif fnmatch.fnmatchcase(filename_part, pattern_part):
952+
elif match_func(filename_part, pattern_part):
940953
continue
941954
else:
942955
return False

0 commit comments

Comments
 (0)