### Build and Install pathspec from Source Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Build and install pathspec from its source code. This requires the 'build' package and is useful for development or when installing from a local copy. ```bash python -m build pip install dist/pathspec-*-py3-none-any.whl ``` -------------------------------- ### Install pathspec using pip Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Install the pathspec library from PyPI using pip. This is the recommended method for most users. ```bash pip install pathspec ``` -------------------------------- ### Basic PathSpec Initialization with Gitignore Patterns Source: https://github.com/cpburnz/python-pathspec/blob/master/README-dist.rst Initialize a PathSpec object from a list of gitignore-style patterns. This is useful for defining inclusion/exclusion rules for file paths. ```python from pathspec import PathSpec # The gitignore-style patterns for files to select, but we're including # instead of ignoring. spec_text = """ # This is a comment because the line begins with a hash: "#" # Include several project directories (and all descendants) relative to # the current directory. To reference only a directory you must end with a # slash: "/" /project-a/ /project-b/ /project-c/ # Patterns can be negated by prefixing with exclamation mark: "!" # Ignore temporary files beginning or ending with "~" and ending with # ".swp". !~* !*~ !*.swp # These are python projects so ignore compiled python files from # testing. !*.pyc # Ignore the build directories but only directly under the project # directories. !/*/build/ """ spec = PathSpec.from_lines('gitignore', spec_text.splitlines()) ``` -------------------------------- ### Create GitIgnoreSpec for Git-like Behavior Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Instantiate GitIgnoreSpec to more closely replicate Git's behavior, including edge cases like including files from excluded directories. ```python from pathspec import GitIgnoreSpec spec = GitIgnoreSpec.from_lines(spec_text.splitlines()) ``` -------------------------------- ### Compile Gitignore Patterns from Lines Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Compile gitignore-style patterns from a list of strings using the PathSpec class. This is a convenient class method for initializing PathSpec. ```python >>> spec = PathSpec.from_lines('gitignore', spec_text.splitlines()) ``` -------------------------------- ### Load Gitignore Patterns from File Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Load gitignore-style patterns directly from a file object using PathSpec.from_lines. Ensure the file is opened in read mode. ```python >>> with open('patterns.list', 'r') as fh: >>> spec = PathSpec.from_lines('gitignore', fh) ``` -------------------------------- ### Initialize GitIgnoreSpec Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Initialize GitIgnoreSpec with patterns to more closely replicate Git's behavior, including edge cases not covered by standard gitignore documentation. This spec always uses GitIgnoreSpecPattern internally. ```python >>> from pathspec import GitIgnoreSpec >>> spec = GitIgnoreSpec.from_lines(spec_text.splitlines()) ``` -------------------------------- ### Match Files in Directory Tree Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Perform matching on a whole directory tree using PathSpec.match_tree_files. Returns a set of matched file paths. ```python >>> matches = set(spec.match_tree_files('path/to/directory')) ``` -------------------------------- ### Compile Gitignore Patterns Manually Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Manually compile gitignore-style patterns using GitIgnoreBasicPattern and then initializing PathSpec. This provides more control over pattern compilation. ```python >>> from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern >>> patterns = map(GitIgnoreBasicPattern, spec_text.splitlines()) >>> spec = PathSpec(patterns) ``` -------------------------------- ### Load PathSpec from File Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Load gitignore patterns directly from a file object into a PathSpec. Ensure the file is opened in read mode. ```python with open('patterns.list', 'r') as fh: spec = PathSpec.from_lines('gitignore', fh) ``` -------------------------------- ### Define Gitignore Patterns Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Define gitignore-style patterns for file selection. Patterns can include comments, directory inclusions, negations, and specific file exclusions. ```python >>> from pathspec import PathSpec >>> # The gitignore-style patterns for files to select, but we're including >>> # instead of ignoring. >>> spec_text = """ ... # This is a comment because the line begins with a hash: "#" ... # Include several project directories (and all descendants) relative to ... # the current directory. To reference only a directory you must end with a ... # slash: "/" ... /project-a/ ... /project-b/ ... /project-c/ ... # Patterns can be negated by prefixing with exclamation mark: "!" ... # Ignore temporary files beginning or ending with "~" and ending with ... # ".swp". ... !~* ... !*~ ... !*.swp ... # These are python projects so ignore compiled python files from ... # testing. ... !*.pyc ... # Ignore the build directories but only directly under the project ... # directories. ... !/*/build/ ... """ ``` -------------------------------- ### Match Specific File Paths Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Perform matching on a specific set of file paths using PathSpec.match_files. Returns a set of matched file paths. ```python >>> matches = set(spec.match_files(file_paths)) ``` -------------------------------- ### Match Files in Directory Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Use `match_tree_files` to find files that match or do not match a given pathspec within a directory. This is useful for selectively including or excluding files. ```python >>> keep_files = set(spec.match_tree_files('path/to/directory', negate=True)) >>> ignore_files = set(spec.match_tree_files('path/to/directory')) ``` -------------------------------- ### Match a Set of File Paths Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Perform matching on a predefined list or set of file paths using a PathSpec object. ```python matches = set(spec.match_files(file_paths)) ``` -------------------------------- ### Match an Individual File Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Check if a single, specific file path matches the patterns defined in a PathSpec object. ```python is_matched = spec.match_file(file_path) ``` -------------------------------- ### Flipping Results with negate=True Source: https://github.com/cpburnz/python-pathspec/blob/master/README-dist.rst Use the 'negate=True' option with GitIgnoreSpec to invert the matching results. This is useful for scenarios where you want to include files that would normally be excluded, such as ignoring *.gitignore files. ```python from pathspec import GitIgnoreSpec spec = GitIgnoreSpec.from_lines([...]) # To find files to keep, and exclude files like *.gitignore, set negate=True # to flip the results. ``` -------------------------------- ### Check Individual File Match Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Check if an individual file matches the defined patterns using PathSpec.match_file. Returns a boolean. ```python >>> is_matched = spec.match_file(file_path) ``` -------------------------------- ### Match Files in a Directory Tree Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Perform matching on all files within a specified directory tree using a PathSpec object. ```python matches = set(spec.match_tree_files('path/to/directory')) ``` -------------------------------- ### Ignoring Files with GitIgnoreSpec Source: https://github.com/cpburnz/python-pathspec/blob/master/doc/source/readme.md Use GitIgnoreSpec with negate=True to find files to keep, excluding patterns like .gitignore. This is useful for managing file inclusion/exclusion based on gitignore-style patterns. ```python from pathspec import GitIgnoreSpec spec = GitIgnoreSpec.from_lines([...]) keep_files = set(spec.match_tree_files('path/to/directory', negate=True)) ignore_files = set(spec.match_tree_files('path/to/directory')) ``` -------------------------------- ### Invert PathSpec Results Source: https://github.com/cpburnz/python-pathspec/blob/master/README.rst Flip the results of a PathSpec or GitIgnoreSpec to positively match files instead of excluding them. Use negate=True when initializing or when calling methods. ```python from pathspec import GitIgnoreSpec spec = GitIgnoreSpec.from_lines([...]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.