Skip to content

Commit

Permalink
Merge pull request #122 from jaraco/bugfix/121-glob-dirs
Browse files Browse the repository at this point in the history
Fix directory matching in glob
  • Loading branch information
jaraco authored Aug 11, 2024
2 parents 21c6bfd + 5d89a1c commit 579be51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/121.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Also match directories in Path.glob.
12 changes: 12 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,18 @@ def test_glob_recursive(self, alpharep):

assert list(root.glob("**/*.txt")) == list(root.rglob("*.txt"))

@pass_alpharep
def test_glob_dirs(self, alpharep):
root = zipfile.Path(alpharep)
assert list(root.glob('b')) == [zipfile.Path(alpharep, "b/")]
assert list(root.glob('b*')) == [zipfile.Path(alpharep, "b/")]

@pass_alpharep
def test_glob_subdir(self, alpharep):
root = zipfile.Path(alpharep)
assert list(root.glob('g/h')) == [zipfile.Path(alpharep, "g/h/")]
assert list(root.glob('g*/h*')) == [zipfile.Path(alpharep, "g/h/")]

@pass_alpharep
def test_glob_subdirs(self, alpharep):
root = zipfile.Path(alpharep)
Expand Down
3 changes: 1 addition & 2 deletions zipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ def glob(self, pattern):
prefix = re.escape(self.at)
tr = Translator(seps='/')
matches = re.compile(prefix + tr.translate(pattern)).fullmatch
names = (data.filename for data in self.root.filelist)
return map(self._next, filter(matches, names))
return map(self._next, filter(matches, self.root.namelist()))

def rglob(self, pattern):
return self.glob(f'**/{pattern}')
Expand Down
10 changes: 9 additions & 1 deletion zipp/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def translate(self, pattern):
"""
Given a glob pattern, produce a regex that matches it.
"""
return self.extend(self.translate_core(pattern))
return self.extend(self.match_dirs(self.translate_core(pattern)))

def extend(self, pattern):
r"""
Expand All @@ -41,6 +41,14 @@ def extend(self, pattern):
"""
return rf'(?s:{pattern})\Z'

def match_dirs(self, pattern):
"""
Ensure that zipfile.Path directory names are matched.
zipfile.Path directory names always end in a slash.
"""
return rf'{pattern}[/]?'

def translate_core(self, pattern):
r"""
Given a glob pattern, produce a regex that matches it.
Expand Down

0 comments on commit 579be51

Please sign in to comment.