Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 2.4
2 Name: pathspec
3 Version: 1.1.1
4 Summary: Utility library for gitignore style pattern matching of file paths.
5 Author-email: "Caleb P. Burns" <[email protected]>
6 Requires-Python: >=3.9
7 Description-Content-Type: text/x-rst
8 Classifier: Development Status :: 5 - Production/Stable
9 Classifier: Intended Audience :: Developers
10 Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
11 Classifier: Operating System :: OS Independent
12 Classifier: Programming Language :: Python
13 Classifier: Programming Language :: Python :: 3
14 Classifier: Programming Language :: Python :: 3.9
15 Classifier: Programming Language :: Python :: 3.10
16 Classifier: Programming Language :: Python :: 3.11
17 Classifier: Programming Language :: Python :: 3.12
18 Classifier: Programming Language :: Python :: 3.13
19 Classifier: Programming Language :: Python :: 3.14
20 Classifier: Programming Language :: Python :: Implementation :: CPython
21 Classifier: Programming Language :: Python :: Implementation :: PyPy
22 Classifier: Topic :: Software Development :: Libraries :: Python Modules
23 Classifier: Topic :: Utilities
24 License-File: LICENSE
25 Requires-Dist: hyperscan >=0.7 ; extra == "hyperscan"
26 Requires-Dist: typing-extensions >=4 ; extra == "optional"
27 Requires-Dist: google-re2 >=1.1 ; extra == "re2"
28 Project-URL: Change Log, https://python-path-specification.readthedocs.io/en/latest/changes.html
29 Project-URL: Documentation, https://python-path-specification.readthedocs.io/en/latest/index.html
30 Project-URL: Issue Tracker, https://github.com/cpburnz/python-pathspec/issues
31 Project-URL: Source Code, https://github.com/cpburnz/python-pathspec
32 Provides-Extra: hyperscan
33 Provides-Extra: optional
34 Provides-Extra: re2
35
36
37 PathSpec
38 ========
39
40 *pathspec* is a utility library for pattern matching of file paths. So far this
41 only includes Git's `gitignore`_ pattern matching.
42
43 .. _`gitignore`: http://git-scm.com/docs/gitignore
44
45
46 Tutorial
47 --------
48
49 Say you have a "Projects" directory and you want to back it up, but only
50 certain files, and ignore others depending on certain conditions::
51
52 >>> from pathspec import PathSpec
53 >>> # The gitignore-style patterns for files to select, but we're including
54 >>> # instead of ignoring.
55 >>> spec_text = """
56 ...
57 ... # This is a comment because the line begins with a hash: "#"
58 ...
59 ... # Include several project directories (and all descendants) relative to
60 ... # the current directory. To reference only a directory you must end with a
61 ... # slash: "/"
62 ... /project-a/
63 ... /project-b/
64 ... /project-c/
65 ...
66 ... # Patterns can be negated by prefixing with exclamation mark: "!"
67 ...
68 ... # Ignore temporary files beginning or ending with "~" and ending with
69 ... # ".swp".
70 ... !~*
71 ... !*~
72 ... !*.swp
73 ...
74 ... # These are python projects so ignore compiled python files from
75 ... # testing.
76 ... !*.pyc
77 ...
78 ... # Ignore the build directories but only directly under the project
79 ... # directories.
80 ... !/*/build/
81 ...
82 ... """
83
84 The ``PathSpec`` class provides an abstraction around pattern implementations,
85 and we want to compile our patterns as "gitignore" patterns. You could call it a
86 wrapper for a list of compiled patterns::
87
88 >>> spec = PathSpec.from_lines('gitignore', spec_text.splitlines())
89
90 If we wanted to manually compile the patterns, we can use the ``GitIgnoreBasicPattern``
91 class directly. It is used in the background for "gitignore" which internally
92 converts patterns to regular expressions::
93
94 >>> from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern
95 >>> patterns = map(GitIgnoreBasicPattern, spec_text.splitlines())
96 >>> spec = PathSpec(patterns)
97
98 ``PathSpec.from_lines()`` is a class method which simplifies that.
99
100 If you want to load the patterns from file, you can pass the file object
101 directly as well::
102
103 >>> with open('patterns.list', 'r') as fh:
104 >>> spec = PathSpec.from_lines('gitignore', fh)
105
106 You can perform matching on a whole directory tree with::
107
108 >>> matches = set(spec.match_tree_files('path/to/directory'))
109
110 Or you can perform matching on a specific set of file paths with::
111
112 >>> matches = set(spec.match_files(file_paths))
113
114 Or check to see if an individual file matches::
115
116 >>> is_matched = spec.match_file(file_path)
117
118 There's actually two implementations of "gitignore". The basic implementation is
119 used by ``PathSpec`` and follows patterns as documented by `gitignore`_.
120 However, Git's behavior differs from the documented patterns. There's some
121 edge-cases, and in particular, Git allows including files from excluded
122 directories which appears to contradict the documentation. ``GitIgnoreSpec``
123 handles these cases to more closely replicate Git's behavior::
124
125 >>> from pathspec import GitIgnoreSpec
126 >>> spec = GitIgnoreSpec.from_lines(spec_text.splitlines())
127
128 You do not specify the style of pattern for ``GitIgnoreSpec`` because it should
129 always use ``GitIgnoreSpecPattern`` internally.
130
131
132 Performance
133 -----------
134
135 Running lots of regular expression matches against thousands of files in Python
136 is slow. Alternate regular expression backends can be used to improve
137 performance. ``PathSpec`` and ``GitIgnoreSpec`` both accept a ``backend``
138 parameter to control the backend. The default is "best" to automatically choose
139 the best available backend. There are currently 3 backends.
140
141 The "simple" backend is the default and it simply uses Python's ``re.Pattern``
142 objects that are normally created. This can be the fastest when there's only 1
143 or 2 patterns.
144
145 The "hyperscan" backend uses the `hyperscan`_ library. Hyperscan tends to be at
146 least 2 times faster than "simple", and generally slower than "re2". This can be
147 faster than "re2" under the right conditions with pattern counts of 1-25.
148
149 The "re2" backend uses the `google-re2`_ library (not to be confused with the
150 *re2* library on PyPI which is unrelated and abandoned). Google's re2 tends to
151 be significantly faster than "simple", and 3 times faster than "hyperscan" at
152 high pattern counts.
153
154 See `benchmarks_backends.md`_ for comparisons between native Python regular
155 expressions and the optional backends.
156
157
158 .. _`benchmarks_backends.md`: https://github.com/cpburnz/python-pathspec/blob/master/benchmarks_backends.md
159 .. _`google-re2`: https://pypi.org/project/google-re2/
160 .. _`hyperscan`: https://pypi.org/project/hyperscan/
161
162
163 FAQ
164 ---
165
166
167 1. How do I ignore files like *.gitignore*?
168 +++++++++++++++++++++++++++++++++++++++++++
169
170 ``GitIgnoreSpec`` (and ``PathSpec``) positively match files by default. To find
171 the files to keep, and exclude files like *.gitignore*, you need to set
172 ``negate=True`` to flip the results::
173
174 >>> from pathspec import GitIgnoreSpec
175 >>> spec = GitIgnoreSpec.from_lines([...])
176 >>> keep_files = set(spec.match_tree_files('path/to/directory', negate=True))
177 >>> ignore_files = set(spec.match_tree_files('path/to/directory'))
178
179
180 License
181 -------
182
183 *pathspec* is licensed under the `Mozilla Public License Version 2.0`_. See
184 `LICENSE`_ or the `FAQ`_ for more information.
185
186 In summary, you may use *pathspec* with any closed or open source project
187 without affecting the license of the larger work so long as you:
188
189 - give credit where credit is due,
190
191 - and release any custom changes made to *pathspec*.
192
193 .. _`Mozilla Public License Version 2.0`: http://www.mozilla.org/MPL/2.0
194 .. _`LICENSE`: LICENSE
195 .. _`FAQ`: http://www.mozilla.org/MPL/2.0/FAQ.html
196
197
198 Source
199 ------
200
201 The source code for *pathspec* is available from the GitHub repo
202 `cpburnz/python-pathspec`_.
203
204 .. _`cpburnz/python-pathspec`: https://github.com/cpburnz/python-pathspec
205
206
207 Installation
208 ------------
209
210 *pathspec* is available for install through `PyPI`_::
211
212 pip install pathspec
213
214 *pathspec* can also be built from source. The following packages will be
215 required:
216
217 - `build`_ (>=0.6.0)
218
219 *pathspec* can then be built and installed with::
220
221 python -m build
222 pip install dist/pathspec-*-py3-none-any.whl
223
224 The following optional dependencies can be installed:
225
226 - `google-re2`_: Enables optional "re2" backend.
227 - `hyperscan`_: Enables optional "hyperscan" backend.
228 - `typing-extensions`_: Improves some type hints.
229
230 .. _`PyPI`: http://pypi.python.org/pypi/pathspec
231 .. _`build`: https://pypi.org/project/build/
232 .. _`typing-extensions`: https://pypi.org/project/typing-extensions/
233
234
235 Documentation
236 -------------
237
238 Documentation for *pathspec* is available on `Read the Docs`_.
239
240 The full change history can be found in `CHANGES.rst`_ and `Change History`_.
241
242 An upgrade guide is available in `UPGRADING.rst`_ and `Upgrade Guide`_.
243
244 .. _`CHANGES.rst`: https://github.com/cpburnz/python-pathspec/blob/master/CHANGES.rst
245 .. _`Change History`: https://python-path-specification.readthedocs.io/en/stable/changes.html
246 .. _`Read the Docs`: https://python-path-specification.readthedocs.io
247 .. _`UPGRADING.rst`: https://github.com/cpburnz/python-pathspec/blob/master/UPGRADING.rst
248 .. _`Upgrade Guide`: https://python-path-specification.readthedocs.io/en/stable/upgrading.html
249
250
251 Other Languages
252 ---------------
253
254 The related project `pathspec-ruby`_ (by *highb*) provides a similar library as
255 a `Ruby gem`_.
256
257 .. _`pathspec-ruby`: https://github.com/highb/pathspec-ruby
258 .. _`Ruby gem`: https://rubygems.org/gems/pathspec
259
260
261 Change History
262 ==============
263
264
265 1.1.1 (2026-04-26)
266 ------------------
267
268 Improvements:
269
270 - Improved type checking with mypy and pyright.
271
272 Bug fixes:
273
274 - Fixed typing on `PathSpec[TPattern]` to `PathSpec[TPattern_co]`.
275 - Added missing variant type-hint `type[Pattern]` to `PathSpec.from_lines()` parameter `pattern_factory`.
276 - Fixed possible type error when using `+` and `+=` operators on `PathSpec`.
277
278
279 1.1.0 (2026-04-22)
280 ------------------
281
282 New features:
283
284 - `Issue #108`_: Specialize pattern type for `PathSpec` as `PathSpec[TPattern]` for better debugging of `PathSpec().patterns`.
285
286 Bug fixes:
287
288 - `Issue #93`_: Git discards invalid range notation. `GitIgnoreSpecPattern` now discards patterns with invalid range notation like Git.
289 - `Pull #106`_: Fix escape() not escaping backslash characters.
290
291 Improvements:
292
293 - `Pull #110`_: Nicer debug print outs (and str for regex pattern).
294
295
296 .. _`Pull #106`: https://github.com/cpburnz/python-pathspec/pull/106
297 .. _`Issue #108`: https://github.com/cpburnz/python-pathspec/issues/108
298 .. _`Pull #110`: https://github.com/cpburnz/python-pathspec/pull/110
299
300
301 1.0.4 (2026-01-26)
302 ------------------
303
304 Bug fixes:
305
306 - `Issue #103`_: Using re2 fails if pyre2 is also installed.
307
308 .. _`Issue #103`: https://github.com/cpburnz/python-pathspec/issues/103
309
310
311 1.0.3 (2026-01-09)
312 ------------------
313
314 Bug fixes:
315
316 - `Issue #101`_: pyright strict errors with pathspec >= 1.0.0.
317 - `Issue #102`_: No module named 'tomllib'.
318
319
320 .. _`Issue #101`: https://github.com/cpburnz/python-pathspec/issues/101
321 .. _`Issue #102`: https://github.com/cpburnz/python-pathspec/issues/102
322
323
324 1.0.2 (2026-01-07)
325 ------------------
326
327 Bug fixes:
328
329 - Type hint `collections.abc.Callable` does not properly replace `typing.Callable` until Python 3.9.2.
330
331
332 1.0.1 (2026-01-06)
333 ------------------
334
335 Bug fixes:
336
337 - `Issue #100`_: ValueError(f"{patterns=!r} cannot be empty.") when using black.
338
339
340 .. _`Issue #100`: https://github.com/cpburnz/python-pathspec/issues/100
341
342
343 1.0.0 (2026-01-05)
344 ------------------
345
346 Major changes:
347
348 - `Issue #91`_: Dropped support of EoL Python 3.8.
349 - Added concept of backends to allow for faster regular expression matching. The backend can be controlled using the `backend` argument to `PathSpec()`, `PathSpec.from_lines()`, `GitIgnoreSpec()`, and `GitIgnoreSpec.from_lines()`.
350 - Renamed "gitwildmatch" pattern back to "gitignore". The "gitignore" pattern behaves slightly differently when used with `PathSpec` (*gitignore* as documented) than with `GitIgnoreSpec` (replicates *Git*'s edge cases).
351
352 API changes:
353
354 - Breaking: protected method `pathspec.pathspec.PathSpec._match_file()` (with a leading underscore) has been removed and replaced by backends. This does not affect normal usage of `PathSpec` or `GitIgnoreSpec`. Only custom subclasses will be affected. If this breaks your usage, let me know by `opening an issue <https://github.com/cpburnz/python-pathspec/issues>`_.
355 - Deprecated: "gitwildmatch" is now an alias for "gitignore".
356 - Deprecated: `pathspec.patterns.GitWildMatchPattern` is now an alias for `pathspec.patterns.gitignore.spec.GitIgnoreSpecPattern`.
357 - Deprecated: `pathspec.patterns.gitwildmatch` module has been replaced by the `pathspec.patterns.gitignore` package.
358 - Deprecated: `pathspec.patterns.gitwildmatch.GitWildMatchPattern` is now an alias for `pathspec.patterns.gitignore.spec.GitIgnoreSpecPattern`.
359 - Deprecated: `pathspec.patterns.gitwildmatch.GitWildMatchPatternError` is now an alias for `pathspec.patterns.gitignore.GitIgnorePatternError`.
360 - Removed: `pathspec.patterns.gitwildmatch.GitIgnorePattern` has been deprecated since v0.4 (2016-07-15).
361 - Signature of method `pathspec.pattern.RegexPattern.match_file()` has been changed from `def match_file(self, file: str) -> RegexMatchResult | None` to `def match_file(self, file: AnyStr) -> RegexMatchResult | None` to reflect usage.
362 - Signature of class method `pathspec.pattern.RegexPattern.pattern_to_regex()` has been changed from `def pattern_to_regex(cls, pattern: str) -> tuple[str, bool]` to `def pattern_to_regex(cls, pattern: AnyStr) -> tuple[AnyStr | None, bool | None]` to reflect usage and documentation.
363
364 New features:
365
366 - Added optional "hyperscan" backend using `hyperscan`_ library. It will automatically be used when installed. This dependency can be installed with ``pip install 'pathspec[hyperscan]'``.
367 - Added optional "re2" backend using the `google-re2`_ library. It will automatically be used when installed. This dependency can be installed with ``pip install 'pathspec[re2]'``.
368 - Added optional dependency on `typing-extensions`_ library to improve some type hints.
369
370 Bug fixes:
371
372 - `Issue #93`_: Do not remove leading spaces.
373 - `Issue #95`_: Matching for files inside folder does not seem to behave like .gitignore's.
374 - `Issue #98`_: UnboundLocalError in RegexPattern when initialized with `pattern=None`.
375 - Type hint on return value of `pathspec.pattern.RegexPattern.match_file()` to match documentation.
376
377 Improvements:
378
379 - Mark Python 3.13 and 3.14 as supported.
380 - No-op patterns are now filtered out when matching files, slightly improving performance.
381 - Fix performance regression in `iter_tree_files()` from v0.10.
382
383
384 .. _`Issue #38`: https://github.com/cpburnz/python-pathspec/issues/38
385 .. _`Issue #91`: https://github.com/cpburnz/python-pathspec/issues/91
386 .. _`Issue #93`: https://github.com/cpburnz/python-pathspec/issues/93
387 .. _`Issue #95`: https://github.com/cpburnz/python-pathspec/issues/95
388 .. _`Issue #98`: https://github.com/cpburnz/python-pathspec/issues/98
389 .. _`google-re2`: https://pypi.org/project/google-re2/
390 .. _`hyperscan`: https://pypi.org/project/hyperscan/
391 .. _`typing-extensions`: https://pypi.org/project/typing-extensions/
392