|
21 | 21 | from contextlib import closing
|
22 | 22 | from functools import partial, wraps
|
23 | 23 |
|
24 |
| -from . import copy, errors, fsencode, iotools, tools, walk, wildcard |
| 24 | +from . import copy, errors, fsencode, iotools, tools, walk, wildcard, glob |
25 | 25 | from .copy import copy_modified_time
|
26 | 26 | from .glob import BoundGlobber
|
27 | 27 | from .mode import validate_open_mode
|
@@ -1696,6 +1696,63 @@ def match(self, patterns, name):
|
1696 | 1696 | matcher = wildcard.get_matcher(patterns, case_sensitive)
|
1697 | 1697 | return matcher(name)
|
1698 | 1698 |
|
| 1699 | + def match_glob(self, patterns, path, accept_prefix=False): |
| 1700 | + # type: (Optional[Iterable[Text]], Text, bool) -> bool |
| 1701 | + """Check if a path matches any of a list of glob patterns. |
| 1702 | +
|
| 1703 | + If a filesystem is case *insensitive* (such as Windows) then |
| 1704 | + this method will perform a case insensitive match (i.e. ``*.py`` |
| 1705 | + will match the same names as ``*.PY``). Otherwise the match will |
| 1706 | + be case sensitive (``*.py`` and ``*.PY`` will match different |
| 1707 | + names). |
| 1708 | +
|
| 1709 | + Arguments: |
| 1710 | + patterns (list, optional): A list of patterns, e.g. |
| 1711 | + ``['*.py']``, or `None` to match everything. |
| 1712 | + path (str): A resource path, starting with "/". |
| 1713 | + accept_prefix (bool): If ``True``, the path is |
| 1714 | + not required to match the patterns themselves |
| 1715 | + but only need to be a prefix of a string that does. |
| 1716 | +
|
| 1717 | + Returns: |
| 1718 | + bool: `True` if ``path`` matches any of the patterns. |
| 1719 | +
|
| 1720 | + Raises: |
| 1721 | + TypeError: If ``patterns`` is a single string instead of |
| 1722 | + a list (or `None`). |
| 1723 | + ValueError: If ``path`` is not a string starting with "/". |
| 1724 | +
|
| 1725 | + Example: |
| 1726 | + >>> my_fs.match_glob(['*.py'], '/__init__.py') |
| 1727 | + True |
| 1728 | + >>> my_fs.match_glob(['*.jpg', '*.png'], '/foo.gif') |
| 1729 | + False |
| 1730 | + >>> my_fs.match_glob(['dir/file.txt'], '/dir/', accept_prefix=True) |
| 1731 | + True |
| 1732 | + >>> my_fs.match_glob(['dir/file.txt'], '/dir/', accept_prefix=False) |
| 1733 | + False |
| 1734 | + >>> my_fs.match_glob(['dir/file.txt'], '/dir/gile.txt', accept_prefix=True) |
| 1735 | + False |
| 1736 | +
|
| 1737 | + Note: |
| 1738 | + If ``patterns`` is `None` (or ``['*']``), then this |
| 1739 | + method will always return `True`. |
| 1740 | +
|
| 1741 | + """ |
| 1742 | + if patterns is None: |
| 1743 | + return True |
| 1744 | + if not path or path[0] != "/": |
| 1745 | + raise ValueError("%s needs to be a string starting with /" % path) |
| 1746 | + if isinstance(patterns, six.text_type): |
| 1747 | + raise TypeError("patterns must be a list or sequence") |
| 1748 | + case_sensitive = not typing.cast( |
| 1749 | + bool, self.getmeta().get("case_insensitive", False) |
| 1750 | + ) |
| 1751 | + matcher = glob.get_matcher( |
| 1752 | + patterns, case_sensitive, accept_prefix=accept_prefix |
| 1753 | + ) |
| 1754 | + return matcher(path) |
| 1755 | + |
1699 | 1756 | def tree(self, **kwargs):
|
1700 | 1757 | # type: (**Any) -> None
|
1701 | 1758 | """Render a tree view of the filesystem to stdout or a file.
|
|
0 commit comments