Skip to content

Commit

Permalink
Re-implement glob directly, adding compatibility for older Pythons an…
Browse files Browse the repository at this point in the history
…d avoids platform-specific concerns.
  • Loading branch information
jaraco committed Nov 25, 2022
1 parent 519640e commit e587f31
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions zipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import itertools
import contextlib
import pathlib
import re
import fnmatch

from .py310compat import text_encoding

Expand Down Expand Up @@ -327,36 +329,25 @@ def match(self, path_pattern):
def is_symlink(self):
return False # See #82102

@contextlib.contextmanager
def _scandir(self): # Needed for glob
yield self.iterdir()

def _make_child_relpath(self, relpath): # Needed for glob
return self / relpath
def _descendants(self):
for child in self.iterdir():
yield child
if child.is_dir():
yield from child._descendants()

def glob(self, pattern):
if not pattern:
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
path = pathlib.Path(self.at)
drv, root, pattern_parts = path._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (path._flavour.sep, path._flavour.altsep):
pattern_parts.append('')
selector = pathlib._make_selector(tuple(pattern_parts), path._flavour)
for p in selector.select_from(self):
yield p

matches = re.compile(fnmatch.translate(pattern)).fullmatch
return (
child
for child in self._descendants()
if matches(str(child.relative_to(self)))
)

def rglob(self, pattern):
path = pathlib.Path(self.at)
drv, root, pattern_parts = path._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern and pattern[-1] in (path._flavour.sep, path._flavour.altsep):
pattern_parts.append('')
selector = pathlib._make_selector(("**",) + tuple(pattern_parts), path._flavour)
for p in selector.select_from(self):
yield p
return self.glob(f'**/{pattern}')

def relative_to(self, *other, **kwargs):
other = (pathlib.Path(each.at) for each in other)
Expand Down

0 comments on commit e587f31

Please sign in to comment.