]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/pathspec/gitignore.py
2 This module provides :class:`.GitIgnoreSpec` which replicates
15 from .pathspec
import (
17 from .pattern
import (
19 from .patterns
.gitwildmatch
import (
21 GitWildMatchPatternError
,
26 Self
= TypeVar("Self", bound
="GitIgnoreSpec")
28 :class:`GitIgnoreSpec` self type hint to support Python v<3.11 using PEP
33 class GitIgnoreSpec(PathSpec
):
35 The :class:`GitIgnoreSpec` class extends :class:`PathSpec` to
36 replicate *.gitignore* behavior.
39 def __eq__(self
, other
: object) -> bool:
41 Tests the equality of this gitignore-spec with *other*
42 (:class:`GitIgnoreSpec`) by comparing their :attr:`~PathSpec.patterns`
43 attributes. A non-:class:`GitIgnoreSpec` will not compare equal.
45 if isinstance(other
, GitIgnoreSpec
):
46 return super().__eq
__(other
)
47 elif isinstance(other
, PathSpec
):
55 lines
: Iterable
[AnyStr
],
56 pattern_factory
: Union
[str, Callable
[[AnyStr
], Pattern
], None] = None,
59 Compiles the pattern lines.
61 *lines* (:class:`~collections.abc.Iterable`) yields each uncompiled
62 pattern (:class:`str`). This simply has to yield each line so it can
63 be a :class:`io.TextIOBase` (e.g., from :func:`open` or
64 :class:`io.StringIO`) or the result from :meth:`str.splitlines`.
66 *pattern_factory* can be :data:`None`, the name of a registered
67 pattern factory (:class:`str`), or a :class:`~collections.abc.Callable`
68 used to compile patterns. The callable must accept an uncompiled
69 pattern (:class:`str`) and return the compiled pattern (:class:`.Pattern`).
70 Default is :data:`None` for :class:`.GitWildMatchPattern`).
72 Returns the :class:`GitIgnoreSpec` instance.
74 if pattern_factory
is None:
75 pattern_factory
= GitWildMatchPattern
77 elif (isinstance(lines
, str) or callable(lines
)) and _is_iterable(pattern_factory
):
78 # Support reversed order of arguments from PathSpec.
79 pattern_factory
, lines
= lines
, pattern_factory
81 self
= super().from_lines(pattern_factory
, lines
)
82 return self
# type: ignore
86 patterns
: Collection
[GitWildMatchPattern
],
90 Matches the file to the patterns.
92 .. NOTE:: Subclasses of :class:`.PathSpec` may override this
93 method as an instance method. It does not have to be a static
96 *patterns* (:class:`~collections.abc.Iterable` of :class:`~pathspec.pattern.Pattern`)
97 contains the patterns to use.
99 *file* (:class:`str`) is the normalized file path to be matched
102 Returns :data:`True` if *file* matched; otherwise, :data:`False`.
106 for pattern
in patterns
:
107 if pattern
.include
is not None:
108 match
= pattern
.match_file(file)
109 if match
is not None:
112 # Check for directory marker.
114 dir_mark
= match
.match
.group(_DIR_MARK
)
115 except IndexError as e
:
116 # NOTICE: The exact content of this error message is subject
118 raise GitWildMatchPatternError((
119 f
"Invalid git pattern: directory marker regex group is missing. "
120 f
"Debug: file={file!r} regex={pattern.regex!r} "
121 f
"group={_DIR_MARK!r} match={match.match!r}."
125 # Pattern matched by a directory pattern.
128 # Pattern matched by a file pattern.
131 if pattern
.include
and dir_mark
:
132 out_matched
= pattern
.include
133 out_priority
= priority
134 elif priority
>= out_priority
:
135 out_matched
= pattern
.include
136 out_priority
= priority