Skip to main content

A spec-compliant `.gitignore` parser for Python

Project description

gitignorefile

Black Linux OS X Windows PyPI

A spec-compliant .gitignore parser for Python.

Installation

pip3 install gitignorefile

Usage

gitignorefile.parse()

Parses single .gitignore file. Suppose /home/michael/project/.gitignore contains the following:

__pycache__/
*.py[cod]

Then:

import gitignorefile

matches = gitignorefile.parse("/home/michael/project/.gitignore")
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True

gitignorefile.ignore()

shutil.copytree() ignore function which checks if file is ignored by any .gitignore in the directory tree.

Example:

import shutil
import gitignorefile

shutil.copytree("/source", "/destination", ignore=gitignorefile.ignore())

gitignorefile.ignored()

Checks if file is ignored by any .gitignore in the directory tree.

import gitignorefile

gitignorefile.ignored("/home/michael/project/main.py") # False

gitignorefile.Cache

Caches .gitignore rules discovered in the directory tree.

import gitignorefile

matches = gitignorefile.Cache()
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True

Custom ignore file sources

You could override files, that will be used to fetch ignore rules. Default value is [".gitignore", ".git/info/exclude"].

import gitignorefile

matches = gitignorefile.Cache(ignore_names=[".mylovelytoolignore"])
matches("/home/michael/project/main.py") # False
matches("/home/michael/project/main.pyc") # True
matches("/home/michael/project/dir/main.pyc") # True
matches("/home/michael/project/__pycache__") # True

Credits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gitignorefile-1.1.2.tar.gz (12.2 kB view details)

Uploaded Source

File details

Details for the file gitignorefile-1.1.2.tar.gz.

File metadata

  • Download URL: gitignorefile-1.1.2.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for gitignorefile-1.1.2.tar.gz
Algorithm Hash digest
SHA256 ce1f6c97d46d7684d3dd3cf768185a3b252a715e026b6fc90103b64d826e4656
MD5 2c1ee729c45e71c5520faf0e0395d094
BLAKE2b-256 91aed983db08d9c5f3dd593b46316d115b8988c25c554072293603a7bc7d52ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page