Skip to content

Add filter_glob and exclude_glob to fs.walk #464

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

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added `filter_glob` and `exclude_glob` parameters to `fs.walk.Walker`.
Closes [#459](https://github.com/PyFilesystem/pyfilesystem2/issues/459).

### Fixed
- Elaborated documentation of `filter_dirs` and `exclude_dirs` in `fs.walk.Walker`.
Closes [#371](https://github.com/PyFilesystem/pyfilesystem2/issues/371).


## [2.4.16] - 2022-05-02

Expand Down
59 changes: 58 additions & 1 deletion fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from contextlib import closing
from functools import partial, wraps

from . import copy, errors, fsencode, iotools, tools, walk, wildcard
from . import copy, errors, fsencode, iotools, tools, walk, wildcard, glob
from .copy import copy_modified_time
from .glob import BoundGlobber
from .mode import validate_open_mode
Expand Down Expand Up @@ -1696,6 +1696,63 @@ def match(self, patterns, name):
matcher = wildcard.get_matcher(patterns, case_sensitive)
return matcher(name)

def match_glob(self, patterns, path, accept_prefix=False):
# type: (Optional[Iterable[Text]], Text, bool) -> bool
"""Check if a path matches any of a list of glob patterns.

If a filesystem is case *insensitive* (such as Windows) then
this method will perform a case insensitive match (i.e. ``*.py``
will match the same names as ``*.PY``). Otherwise the match will
be case sensitive (``*.py`` and ``*.PY`` will match different
names).

Arguments:
patterns (list, optional): A list of patterns, e.g.
``['*.py']``, or `None` to match everything.
Comment on lines +1710 to +1711
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at just the doc-strings, it's hard to see how match_glob() is different from match() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The summaries (first sentences) differ: Check if a name matches any of a list of wildcards. and Check if a path matches any of a list of glob patterns..

path (str): A resource path, starting with "/".
accept_prefix (bool): If ``True``, the path is
not required to match the patterns themselves
but only need to be a prefix of a string that does.

Returns:
bool: `True` if ``path`` matches any of the patterns.

Raises:
TypeError: If ``patterns`` is a single string instead of
a list (or `None`).
ValueError: If ``path`` is not a string starting with "/".

Example:
>>> my_fs.match_glob(['*.py'], '/__init__.py')
True
>>> my_fs.match_glob(['*.jpg', '*.png'], '/foo.gif')
False
>>> my_fs.match_glob(['dir/file.txt'], '/dir/', accept_prefix=True)
True
>>> my_fs.match_glob(['dir/file.txt'], '/dir/', accept_prefix=False)
False
>>> my_fs.match_glob(['dir/file.txt'], '/dir/gile.txt', accept_prefix=True)
False

Note:
If ``patterns`` is `None` (or ``['*']``), then this
method will always return `True`.

"""
if patterns is None:
return True
if not path or path[0] != "/":
raise ValueError("%s needs to be a string starting with /" % path)
if isinstance(patterns, six.text_type):
raise TypeError("patterns must be a list or sequence")
case_sensitive = not typing.cast(
bool, self.getmeta().get("case_insensitive", False)
)
matcher = glob.get_matcher(
patterns, case_sensitive, accept_prefix=accept_prefix
)
return matcher(path)

def tree(self, **kwargs):
# type: (**Any) -> None
"""Render a tree view of the filesystem to stdout or a file.
Expand Down
17 changes: 17 additions & 0 deletions fs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"OperationFailed",
"OperationTimeout",
"PathError",
"PatternError",
"PermissionDenied",
"RemoteConnectionError",
"RemoveRootError",
Expand Down Expand Up @@ -346,3 +347,19 @@ class UnsupportedHash(ValueError):
not supported by hashlib.

"""


class PatternError(ValueError):
"""A string pattern with invalid syntax was given."""

default_message = "pattern '{pattern}' is invalid at position {position}"

def __init__(self, pattern, position, exc=None, msg=None): # noqa: D107
# type: (Text, int, Optional[Exception], Optional[Text]) -> None
self.pattern = pattern
self.position = position
self.exc = exc
super(ValueError, self).__init__()

def __reduce__(self):
return type(self), (self.path, self.position, self.exc, self._msg)
Loading