Last active
January 17, 2023 12:26
-
-
Save christabor/6f9c16293ae7d575d36e99d351ad094d to your computer and use it in GitHub Desktop.
Generator stream gitignore filter python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gitignore2list(directory): | |
"""Get a list of gitignore entries from a file.""" | |
try: | |
with open('{}/.gitignore'.format(directory), 'r+') as ignorefile: | |
yield (x.rstrip() for x in ignorefile.read().split('\n') | |
if x and not x.startswith('#')) | |
except OSError: | |
yield None | |
def ignore(to_ignore): | |
"""Return a customized filtering generator that will filter based on a list of given names.""" | |
def _f(name): | |
for item in to_ignore: | |
# Filter star args (e.g *.pyc) | |
if item.startswith('*.'): | |
if name.endswith(item[1:]): | |
yield False | |
if name.endswith(item): | |
yield False | |
yield True | |
return _f | |
def gitignore(path): | |
"""Create a function to filter based on gitignore file entries. | |
Note: wildcard/regexes are not supported. | |
""" | |
return ignore(gitignore2list(path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment