ignorelib
Use the syntax and semantics of gitignore with custom ignore file names and locations
git has a comprehensive mechanism for selecting files to ignore inside repositories.
ignorelib lets you use the same system, customized to your own needs.
You can read about the semantics of gitignore here: https://git-scm.com/docs/gitignore
This library is a lightly-modified version of the gitignore implementation in dulwich, a pure Python implementation of git.
Installation
python -m pip install ignorelib
Usage
Setup
The primary entrypoint is the class factory method IgnoreFilterManager.build(), with the following inputs:
path: the root path (required). All path checks you make are relative to this path.global_ignore_file_paths: an optional list of file paths to attempt to load global ignore patterns from.- Relative paths are relative to the root path (for git, this would be
.git/info/exclude) - User expansion is performed, so paths like (for git)
~/.config/git/ignorework. - Files that cannot be loaded are silently ignored, so you don't need to check if they exist or not.
- Files earlier in the list take precedence, and these files take precendence over the patterns in
global_patterns.
- Relative paths are relative to the root path (for git, this would be
global_patterns: an optional list of global ignore patterns. These are the things that should always be ignored (for git, this would be.gitto exclude the repo directory)ignore_file_name: an optional file name for the per-directory ignore file (for git, this would be.gitignore).ignore_case: an optional boolean for specifying whether to ignore case, defaulting to false.
Use
You check if a given path is ignored with the is_ignored() method of an IgnoreFilterManager object, which takes a (relative) path and returns True if it matches an ignore pattern.
It returns False if it is explicitly not ignored (using a pattern starting with !), or None if the file does not match any patterns.
Note that this allows you to distinguish between the default state (not ignoring) and actually matching a pattern that prevents it from being ignored.
To iterate over not-ignored files, IgnoreFilterManager.walk() has the same interface as os.walk() but without taking a root path, as this comes from the the IgnoreFilterManager.
After using an IgnoreFilterManager instance to get a number of paths, you can extract the state (i.e., all loaded patterns with their sources) in a JSON-serializable format with the IgnoreFilterManager.to_dict() method.
Example
To replicate the behavior of git, you would do something like:
import os.path
import ignorelib
def get_filter_manager_for_path(path):
return ignorelib.IgnoreFilterManager(path,
global_ignore_file_paths=[
os.path.join('.git', 'info', 'exclude'), # relative to input path, so within the repo
os.path.expanduser(os.path.join('~', '.config', 'git', 'ignore')) # absolute
],
global_patterns=['.git'],
ignore_file_name='.gitignore')
Metadata
Release files for ignorelib 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ignorelib-0.3.0.tar.gz | 10.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ignorelib-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.9 kB
Release files / ignorelib-0.3.0.tar.gz
| Download URL | ignorelib-0.3.0.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
712ae752cdf1717c53c5009169b167f4e23c963e5405f86af1078329923e08fd
|
|
BLAKE2b-256 checksum How to use checksums |
d04a4d76ff7d494ce1299e735e5d3a199d973e73c5bf5f39b927826063ed86cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.8.3 Linux/4.19.128-microsoft-standard
|
Release files / ignorelib-0.3.0-py3-none-any.whl
| Download URL | ignorelib-0.3.0-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2575083ff18534a8a3925a2a531b0d2f9d326efe550dc54e726ab5d6a6598d0c
|
|
BLAKE2b-256 checksum How to use checksums |
0fafb9dd9b4421238e979932f949192aa9dba3689eedbc8a76d95042150a4c28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.8.3 Linux/4.19.128-microsoft-standard
|