]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/pathspec/pattern.py
5222ec0899980236d1ce343e2c6b70e88d7239fc
2 This module provides the base definition for patterns.
15 Pattern
as PatternHint
,
20 class Pattern(object):
22 The :class:`Pattern` class is the abstract definition of a pattern.
25 # Make the class dict-less.
26 __slots__
= ('include',)
28 def __init__(self
, include
: Optional
[bool]) -> None:
30 Initializes the :class:`Pattern` instance.
32 *include* (:class:`bool` or :data:`None`) is whether the matched
33 files should be included (:data:`True`), excluded (:data:`False`),
34 or is a null-operation (:data:`None`).
37 self
.include
= include
39 *include* (:class:`bool` or :data:`None`) is whether the matched
40 files should be included (:data:`True`), excluded (:data:`False`),
41 or is a null-operation (:data:`None`).
44 def match(self
, files
: Iterable
[str]) -> Iterator
[str]:
46 DEPRECATED: This method is no longer used and has been replaced by
47 :meth:`.match_file`. Use the :meth:`.match_file` method with a loop
50 Matches this pattern against the specified files.
52 *files* (:class:`~collections.abc.Iterable` of :class:`str`)
53 contains each file relative to the root directory (e.g.,
54 :data:`"relative/path/to/file"`).
56 Returns an :class:`~collections.abc.Iterable` yielding each matched
57 file path (:class:`str`).
60 "{0.__module__}.{0.__qualname__}.match() is deprecated. Use "
61 "{0.__module__}.{0.__qualname__}.match_file() with a loop for "
63 ).format(self
.__class
__), DeprecationWarning, stacklevel
=2)
66 if self
.match_file(file) is not None:
69 def match_file(self
, file: str) -> Optional
[Any
]:
71 Matches this pattern against the specified file.
73 *file* (:class:`str`) is the normalized file path to match against.
75 Returns the match result if *file* matched; otherwise, :data:`None`.
77 raise NotImplementedError((
78 "{0.__module__}.{0.__qualname__} must override match_file()."
79 ).format(self
.__class
__))
82 class RegexPattern(Pattern
):
84 The :class:`RegexPattern` class is an implementation of a pattern
85 using regular expressions.
88 # Keep the class dict-less.
89 __slots__
= ('regex',)
93 pattern
: Union
[AnyStr
, PatternHint
],
94 include
: Optional
[bool] = None,
97 Initializes the :class:`RegexPattern` instance.
99 *pattern* (:class:`str`, :class:`bytes`, :class:`re.Pattern`, or
100 :data:`None`) is the pattern to compile into a regular expression.
102 *include* (:class:`bool` or :data:`None`) must be :data:`None`
103 unless *pattern* is a precompiled regular expression (:class:`re.Pattern`)
104 in which case it is whether matched files should be included
105 (:data:`True`), excluded (:data:`False`), or is a null operation
108 .. NOTE:: Subclasses do not need to support the *include*
112 if isinstance(pattern
, (str, bytes
)):
113 assert include
is None, (
114 "include:{!r} must be null when pattern:{!r} is a string."
115 ).format(include
, pattern
)
116 regex
, include
= self
.pattern_to_regex(pattern
)
117 # NOTE: Make sure to allow a null regular expression to be
118 # returned for a null-operation.
119 if include
is not None:
120 regex
= re
.compile(regex
)
122 elif pattern
is not None and hasattr(pattern
, 'match'):
123 # Assume pattern is a precompiled regular expression.
124 # - NOTE: Used specified *include*.
127 elif pattern
is None:
128 # NOTE: Make sure to allow a null pattern to be passed for a
130 assert include
is None, (
131 "include:{!r} must be null when pattern:{!r} is null."
132 ).format(include
, pattern
)
135 raise TypeError("pattern:{!r} is not a string, re.Pattern, or None.".format(pattern
))
137 super(RegexPattern
, self
).__init
__(include
)
139 self
.regex
: PatternHint
= regex
141 *regex* (:class:`re.Pattern`) is the regular expression for the
145 def __eq__(self
, other
: 'RegexPattern') -> bool:
147 Tests the equality of this regex pattern with *other* (:class:`RegexPattern`)
148 by comparing their :attr:`~Pattern.include` and :attr:`~RegexPattern.regex`
151 if isinstance(other
, RegexPattern
):
152 return self
.include
== other
.include
and self
.regex
== other
.regex
154 return NotImplemented
156 def match_file(self
, file: str) -> Optional
['RegexMatchResult']:
158 Matches this pattern against the specified file.
160 *file* (:class:`str`)
161 contains each file relative to the root directory (e.g., "relative/path/to/file").
163 Returns the match result (:class:`RegexMatchResult`) if *file*
164 matched; otherwise, :data:`None`.
166 if self
.include
is not None:
167 match
= self
.regex
.match(file)
168 if match
is not None:
169 return RegexMatchResult(match
)
174 def pattern_to_regex(cls
, pattern
: str) -> Tuple
[str, bool]:
176 Convert the pattern into an uncompiled regular expression.
178 *pattern* (:class:`str`) is the pattern to convert into a regular
181 Returns the uncompiled regular expression (:class:`str` or :data:`None`),
182 and whether matched files should be included (:data:`True`),
183 excluded (:data:`False`), or is a null-operation (:data:`None`).
185 .. NOTE:: The default implementation simply returns *pattern* and
191 @dataclasses.dataclass()
192 class RegexMatchResult(object):
194 The :class:`RegexMatchResult` data class is used to return information
195 about the matched regular expression.
198 # Keep the class dict-less.
205 *match* (:class:`re.Match`) is the regex match result.