]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/pathspec/pathspec.py
93f2f6098938537b827fd257e463cb7309944796
2 This module provides an object oriented interface for pattern matching of files.
5 from collections
.abc
import (
6 Collection
as CollectionType
)
7 from itertools
import (
21 from .pattern
import (
31 Self
= TypeVar("Self", bound
="PathSpec")
33 :class:`PathSpec` self type hint to support Python v<3.11 using PEP 673
38 class PathSpec(object):
40 The :class:`PathSpec` class is a wrapper around a list of compiled
41 :class:`.Pattern` instances.
44 def __init__(self
, patterns
: Iterable
[Pattern
]) -> None:
46 Initializes the :class:`PathSpec` instance.
48 *patterns* (:class:`~collections.abc.Collection` or :class:`~collections.abc.Iterable`)
49 yields each compiled pattern (:class:`.Pattern`).
52 self
.patterns
= patterns
if isinstance(patterns
, CollectionType
) else list(patterns
)
54 *patterns* (:class:`~collections.abc.Collection` of :class:`.Pattern`)
55 contains the compiled patterns.
58 def __eq__(self
, other
: object) -> bool:
60 Tests the equality of this path-spec with *other* (:class:`PathSpec`)
61 by comparing their :attr:`~PathSpec.patterns` attributes.
63 if isinstance(other
, PathSpec
):
64 paired_patterns
= zip_longest(self
.patterns
, other
.patterns
)
65 return all(a
== b
for a
, b
in paired_patterns
)
69 def __len__(self
) -> int:
71 Returns the number of compiled patterns this path-spec contains
74 return len(self
.patterns
)
76 def __add__(self
: Self
, other
: "PathSpec") -> Self
:
78 Combines the :attr:`Pathspec.patterns` patterns from two
79 :class:`PathSpec` instances.
81 if isinstance(other
, PathSpec
):
82 return self
.__class
__(self
.patterns
+ other
.patterns
)
86 def __iadd__(self
: Self
, other
: "PathSpec") -> Self
:
88 Adds the :attr:`Pathspec.patterns` patterns from one :class:`PathSpec`
89 instance to this instance.
91 if isinstance(other
, PathSpec
):
92 self
.patterns
+= other
.patterns
100 pattern_factory
: Union
[str, Callable
[[AnyStr
], Pattern
]],
101 lines
: Iterable
[AnyStr
],
104 Compiles the pattern lines.
106 *pattern_factory* can be either the name of a registered pattern factory
107 (:class:`str`), or a :class:`~collections.abc.Callable` used to compile
108 patterns. It must accept an uncompiled pattern (:class:`str`) and return the
109 compiled pattern (:class:`.Pattern`).
111 *lines* (:class:`~collections.abc.Iterable`) yields each uncompiled pattern
112 (:class:`str`). This simply has to yield each line so that it can be a
113 :class:`io.TextIOBase` (e.g., from :func:`open` or :class:`io.StringIO`) or
114 the result from :meth:`str.splitlines`.
116 Returns the :class:`PathSpec` instance.
118 if isinstance(pattern_factory
, str):
119 pattern_factory
= util
.lookup_pattern(pattern_factory
)
121 if not callable(pattern_factory
):
122 raise TypeError(f
"pattern_factory:{pattern_factory!r} is not callable.")
124 if not _is_iterable(lines
):
125 raise TypeError(f
"lines:{lines!r} is not an iterable.")
127 patterns
= [pattern_factory(line
) for line
in lines
if line
]
132 entries
: Iterable
[TreeEntry
],
133 separators
: Optional
[Collection
[str]] = None,
135 negate
: Optional
[bool] = None,
136 ) -> Iterator
[TreeEntry
]:
138 Matches the entries to this path-spec.
140 *entries* (:class:`~collections.abc.Iterable` of :class:`~util.TreeEntry`)
141 contains the entries to be matched against :attr:`self.patterns <PathSpec.patterns>`.
143 *separators* (:class:`~collections.abc.Collection` of :class:`str`; or
144 :data:`None`) optionally contains the path separators to normalize. See
145 :func:`~pathspec.util.normalize_file` for more information.
147 *negate* (:class:`bool` or :data:`None`) is whether to negate the match
148 results of the patterns. If :data:`True`, a pattern matching a file will
149 exclude the file rather than include it. Default is :data:`None` for
152 Returns the matched entries (:class:`~collections.abc.Iterator` of
153 :class:`~util.TreeEntry`).
155 if not _is_iterable(entries
):
156 raise TypeError(f
"entries:{entries!r} is not an iterable.")
158 use_patterns
= _filter_patterns(self
.patterns
)
159 for entry
in entries
:
160 norm_file
= normalize_file(entry
.path
, separators
)
161 is_match
= self
._match
_file
(use_patterns
, norm_file
)
164 is_match
= not is_match
169 # Match files using the `match_file()` utility function. Subclasses may
170 # override this method as an instance method. It does not have to be a static
172 _match_file
= staticmethod(match_file
)
177 separators
: Optional
[Collection
[str]] = None,
180 Matches the file to this path-spec.
182 *file* (:class:`str` or :class:`os.PathLike[str]`) is the file path to be
183 matched against :attr:`self.patterns <PathSpec.patterns>`.
185 *separators* (:class:`~collections.abc.Collection` of :class:`str`)
186 optionally contains the path separators to normalize. See
187 :func:`~pathspec.util.normalize_file` for more information.
189 Returns :data:`True` if *file* matched; otherwise, :data:`False`.
191 norm_file
= util
.normalize_file(file, separators
=separators
)
192 return self
._match
_file
(self
.patterns
, norm_file
)
196 files
: Iterable
[StrPath
],
197 separators
: Optional
[Collection
[str]] = None,
199 negate
: Optional
[bool] = None,
200 ) -> Iterator
[StrPath
]:
202 Matches the files to this path-spec.
204 *files* (:class:`~collections.abc.Iterable` of :class:`str` or
205 :class:`os.PathLike[str]`) contains the file paths to be matched against
206 :attr:`self.patterns <PathSpec.patterns>`.
208 *separators* (:class:`~collections.abc.Collection` of :class:`str`; or
209 :data:`None`) optionally contains the path separators to normalize. See
210 :func:`~pathspec.util.normalize_file` for more information.
212 *negate* (:class:`bool` or :data:`None`) is whether to negate the match
213 results of the patterns. If :data:`True`, a pattern matching a file will
214 exclude the file rather than include it. Default is :data:`None` for
217 Returns the matched files (:class:`~collections.abc.Iterator` of
218 :class:`str` or :class:`os.PathLike[str]`).
220 if not _is_iterable(files
):
221 raise TypeError(f
"files:{files!r} is not an iterable.")
223 use_patterns
= _filter_patterns(self
.patterns
)
224 for orig_file
in files
:
225 norm_file
= normalize_file(orig_file
, separators
)
226 is_match
= self
._match
_file
(use_patterns
, norm_file
)
229 is_match
= not is_match
234 def match_tree_entries(
237 on_error
: Optional
[Callable
] = None,
238 follow_links
: Optional
[bool] = None,
240 negate
: Optional
[bool] = None,
241 ) -> Iterator
[TreeEntry
]:
243 Walks the specified root path for all files and matches them to this
246 *root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
249 *on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally
250 is the error handler for file-system exceptions. See
251 :func:`~pathspec.util.iter_tree_entries` for more information.
253 *follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
254 symbolic links that resolve to directories. See
255 :func:`~pathspec.util.iter_tree_files` for more information.
257 *negate* (:class:`bool` or :data:`None`) is whether to negate the match
258 results of the patterns. If :data:`True`, a pattern matching a file will
259 exclude the file rather than include it. Default is :data:`None` for
262 Returns the matched files (:class:`~collections.abc.Iterator` of
263 :class:`.TreeEntry`).
265 entries
= util
.iter_tree_entries(root
, on_error
=on_error
, follow_links
=follow_links
)
266 yield from self
.match_entries(entries
, negate
=negate
)
268 def match_tree_files(
271 on_error
: Optional
[Callable
] = None,
272 follow_links
: Optional
[bool] = None,
274 negate
: Optional
[bool] = None,
277 Walks the specified root path for all files and matches them to this
280 *root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
283 *on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally
284 is the error handler for file-system exceptions. See
285 :func:`~pathspec.util.iter_tree_files` for more information.
287 *follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
288 symbolic links that resolve to directories. See
289 :func:`~pathspec.util.iter_tree_files` for more information.
291 *negate* (:class:`bool` or :data:`None`) is whether to negate the match
292 results of the patterns. If :data:`True`, a pattern matching a file will
293 exclude the file rather than include it. Default is :data:`None` for
296 Returns the matched files (:class:`~collections.abc.Iterable` of
299 files
= util
.iter_tree_files(root
, on_error
=on_error
, follow_links
=follow_links
)
300 yield from self
.match_files(files
, negate
=negate
)
302 # Alias `match_tree_files()` as `match_tree()` for backward compatibility
304 match_tree
= match_tree_files