API¶
pathspec¶
The pathspec package provides pattern matching for file paths. So far this only includes Git’s wildmatch pattern matching (the style used for “.gitignore” files).
The following classes are imported and made available from the root of the pathspec package:
pathspec.gitignore.GitIgnoreSpec
pathspec.pathspec.PathSpec
pathspec.pattern.Pattern
pathspec.pattern.RegexPattern
pathspec.util.RecursionError
The following functions are also imported:
The following deprecated functions are also imported to maintain backward compatibility:
pathspec.util.iter_tree()
which is an alias forpathspec.util.iter_tree_files()
.pathspec.util.match_files()
pathspec.pathspec¶
This module provides an object oriented interface for pattern matching of files.
-
class
pathspec.pathspec.
PathSpec
(patterns: Iterable[pathspec.pattern.Pattern])[source]¶ Bases:
object
The
PathSpec
class is a wrapper around a list of compiledPattern
instances.-
__init__
(patterns: Iterable[pathspec.pattern.Pattern]) → None[source]¶ Initializes the
PathSpec
instance.patterns (
Collection
orIterable
) yields each compiled pattern (Pattern
).
-
patterns
= None¶ patterns (
Collection
ofPattern
) contains the compiled patterns.
-
__eq__
(other: object) → bool[source]¶ Tests the equality of this path-spec with other (
PathSpec
) by comparing theirpatterns
attributes.
-
classmethod
from_lines
(pattern_factory: Union[str, Callable[[AnyStr], pathspec.pattern.Pattern]], lines: Iterable[AnyStr]) → Self[source]¶ Compiles the pattern lines.
pattern_factory can be either the name of a registered pattern factory (
str
), or aCallable
used to compile patterns. It must accept an uncompiled pattern (str
) and return the compiled pattern (Pattern
).lines (
Iterable
) yields each uncompiled pattern (str
). This simply has to yield each line so it can be aio.TextIOBase
(e.g., fromopen()
orio.StringIO
) or the result fromstr.splitlines()
.Returns the
PathSpec
instance.
-
match_entries
(entries: Iterable[pathspec.util.TreeEntry], separators: Optional[Collection[str]] = None) → Iterator[pathspec.util.TreeEntry][source]¶ Matches the entries to this path-spec.
entries (
Iterable
ofTreeEntry
) contains the entries to be matched againstself.patterns
.separators (
Collection
ofstr
; orNone
) optionally contains the path separators to normalize. Seenormalize_file()
for more information.Returns the matched entries (
Iterator
ofTreeEntry
).
-
match_file
(file: Union[str, os.PathLike], separators: Optional[Collection[str]] = None) → bool[source]¶ Matches the file to this path-spec.
file (
str
oros.PathLike[str]
) is the file path to be matched againstself.patterns
.separators (
Collection
ofstr
) optionally contains the path separators to normalize. Seenormalize_file()
for more information.
-
match_files
(files: Iterable[Union[str, os.PathLike]], separators: Optional[Collection[str]] = None) → Iterator[Union[str, os.PathLike]][source]¶ Matches the files to this path-spec.
files (
Iterable
ofstr
oros.PathLike[str]
) contains the file paths to be matched againstself.patterns
.separators (
Collection
ofstr
; orNone
) optionally contains the path separators to normalize. Seenormalize_file()
for more information.Returns the matched files (
Iterator
ofstr
oros.PathLike[str]
).
-
match_tree_entries
(root: Union[str, os.PathLike], on_error: Optional[Callable] = None, follow_links: Optional[bool] = None) → Iterator[pathspec.util.TreeEntry][source]¶ Walks the specified root path for all files and matches them to this path-spec.
root (
str
oros.PathLike[str]
) is the root directory to search.on_error (
Callable
orNone
) optionally is the error handler for file-system exceptions. Seeiter_tree_entries()
for more information.follow_links (
bool
orNone
) optionally is whether to walk symbolic links that resolve to directories. Seeiter_tree_files()
for more information.
-
match_tree_files
(root: Union[str, os.PathLike], on_error: Optional[Callable] = None, follow_links: Optional[bool] = None) → Iterator[str][source]¶ Walks the specified root path for all files and matches them to this path-spec.
root (
str
oros.PathLike[str]
) is the root directory to search for files.on_error (
Callable
orNone
) optionally is the error handler for file-system exceptions. Seeiter_tree_files()
for more information.follow_links (
bool
orNone
) optionally is whether to walk symbolic links that resolve to directories. Seeiter_tree_files()
for more information.
-
match_tree
(root: Union[str, os.PathLike], on_error: Optional[Callable] = None, follow_links: Optional[bool] = None) → Iterator[str]¶ Walks the specified root path for all files and matches them to this path-spec.
root (
str
oros.PathLike[str]
) is the root directory to search for files.on_error (
Callable
orNone
) optionally is the error handler for file-system exceptions. Seeiter_tree_files()
for more information.follow_links (
bool
orNone
) optionally is whether to walk symbolic links that resolve to directories. Seeiter_tree_files()
for more information.
-
pathspec.gitignore¶
This module provides GitIgnoreSpec
which replicates
.gitignore behavior.
-
class
pathspec.gitignore.
GitIgnoreSpec
(patterns: Iterable[pathspec.pattern.Pattern])[source]¶ Bases:
pathspec.pathspec.PathSpec
The
GitIgnoreSpec
class extendsPathSpec
to replicate .gitignore behavior.-
__eq__
(other: object) → bool[source]¶ Tests the equality of this gitignore-spec with other (
GitIgnoreSpec
) by comparing theirpatterns
attributes. A non-GitIgnoreSpec
will not compare equal.
-
classmethod
from_lines
(lines: Iterable[AnyStr], pattern_factory: Union[str, Callable[[AnyStr], pathspec.pattern.Pattern], None] = None) → Self[source]¶ Compiles the pattern lines.
lines (
Iterable
) yields each uncompiled pattern (str
). This simply has to yield each line so it can be aio.TextIOBase
(e.g., fromopen()
orio.StringIO
) or the result fromstr.splitlines()
.pattern_factory can be
None
, the name of a registered pattern factory (str
), or aCallable
used to compile patterns. The callable must accept an uncompiled pattern (str
) and return the compiled pattern (Pattern
). Default isNone
forGitWildMatchPattern
).Returns the
GitIgnoreSpec
instance.
-
pathspec.pattern¶
This module provides the base definition for patterns.
-
class
pathspec.pattern.
Pattern
(include: Optional[bool])[source]¶ Bases:
object
The
Pattern
class is the abstract definition of a pattern.-
__init__
(include: Optional[bool]) → None[source]¶ Initializes the
Pattern
instance.include (
bool
orNone
) is whether the matched files should be included (True
), excluded (False
), or is a null-operation (None
).
-
include
¶ include (
bool
orNone
) is whether the matched files should be included (True
), excluded (False
), or is a null-operation (None
).
-
match
(files: Iterable[str]) → Iterator[str][source]¶ DEPRECATED: This method is no longer used and has been replaced by
match_file()
. Use thematch_file()
method with a loop for similar results.Matches this pattern against the specified files.
files (
Iterable
ofstr
) contains each file relative to the root directory (e.g.,"relative/path/to/file"
).
-
-
class
pathspec.pattern.
RegexPattern
(pattern: Union[AnyStr, Pattern[AnyStr]], include: Optional[bool] = None)[source]¶ Bases:
pathspec.pattern.Pattern
The
RegexPattern
class is an implementation of a pattern using regular expressions.-
__init__
(pattern: Union[AnyStr, Pattern[AnyStr]], include: Optional[bool] = None) → None[source]¶ Initializes the
RegexPattern
instance.pattern (
str
,bytes
,re.Pattern
, orNone
) is the pattern to compile into a regular expression.include (
bool
orNone
) must beNone
unless pattern is a precompiled regular expression (re.Pattern
) in which case it is whether matched files should be included (True
), excluded (False
), or is a null operation (None
).Note
Subclasses do not need to support the include parameter.
-
regex
¶ regex (
re.Pattern
) is the regular expression for the pattern.
-
__eq__
(other: pathspec.pattern.RegexPattern) → bool[source]¶ Tests the equality of this regex pattern with other (
RegexPattern
) by comparing theirinclude
andregex
attributes.
-
match_file
(file: str) → Optional[pathspec.pattern.RegexMatchResult][source]¶ Matches this pattern against the specified file.
file (
str
) contains each file relative to the root directory (e.g., “relative/path/to/file”).Returns the match result (
RegexMatchResult
) if file matched; otherwise,None
.
-
classmethod
pattern_to_regex
(pattern: str) → Tuple[str, bool][source]¶ Convert the pattern into an uncompiled regular expression.
pattern (
str
) is the pattern to convert into a regular expression.Returns the uncompiled regular expression (
str
orNone
), and whether matched files should be included (True
), excluded (False
), or is a null-operation (None
).Note
The default implementation simply returns pattern and
True
.
-
-
class
pathspec.pattern.
RegexMatchResult
(match: Match[AnyStr])[source]¶ Bases:
object
The
RegexMatchResult
data class is used to return information about the matched regular expression.-
match
¶ match (
re.Match
) is the regex match result.
-
pathspec.patterns.gitwildmatch¶
This module implements Git’s wildmatch pattern matching which itself is derived from Rsync’s wildmatch. Git uses wildmatch for its “.gitignore” files.
-
class
pathspec.patterns.gitwildmatch.
GitWildMatchPattern
(pattern: Union[AnyStr, Pattern[AnyStr]], include: Optional[bool] = None)[source]¶ Bases:
pathspec.pattern.RegexPattern
The
GitWildMatchPattern
class represents a compiled Git wildmatch pattern.-
classmethod
pattern_to_regex
(pattern: AnyStr) → Tuple[Optional[AnyStr], Optional[bool]][source]¶ Convert the pattern into a regular expression.
pattern (
str
orbytes
) is the pattern to convert into a regular expression.Returns the uncompiled regular expression (
str
,bytes
, orNone
); and whether matched files should be included (True
), excluded (False
), or if it is a null-operation (None
).
-
static
escape
(s: AnyStr) → AnyStr[source]¶ Escape special characters in the given string.
s (
str
orbytes
) a filename or a string that you want to escape, usually before adding it to a “.gitignore”.
-
match
(files: Iterable[str]) → Iterator[str]¶ DEPRECATED: This method is no longer used and has been replaced by
match_file()
. Use thematch_file()
method with a loop for similar results.Matches this pattern against the specified files.
files (
Iterable
ofstr
) contains each file relative to the root directory (e.g.,"relative/path/to/file"
).
-
classmethod
pathspec.util¶
This module provides utility methods for dealing with path-specs.
-
pathspec.util.
NORMALIZE_PATH_SEPS
= []¶ NORMALIZE_PATH_SEPS (
list
ofstr
) contains the path separators that need to be normalized to the POSIX separator for the current operating system. The separators are determined by examiningos.sep
andos.altsep
.
-
pathspec.util.
append_dir_sep
(path: pathlib.Path) → str[source]¶ Appends the path separator to the path if the path is a directory. This can be used to aid in distinguishing between directories and files on the file-system by relying on the presence of a trailing path separator.
path (
pathlib.path
) is the path to use.Returns the path (
str
).
-
pathspec.util.
detailed_match_files
(patterns: Iterable[pathspec.pattern.Pattern], files: Iterable[str], all_matches: Optional[bool] = None) → Dict[str, pathspec.util.MatchDetail][source]¶ Matches the files to the patterns, and returns which patterns matched the files.
patterns (
Iterable
ofPattern
) contains the patterns to use.files (
Iterable
ofstr
) contains the normalized file paths to be matched against patterns.all_matches (
boot
orNone
) is whether to return all matches patterns (True
), or only the last matched pattern (False
). Default isNone
forFalse
.Returns the matched files (
dict
) which maps each matched file (str
) to the patterns that matched in order (MatchDetail
).
-
pathspec.util.
iter_tree_entries
(root: Union[str, os.PathLike], on_error: Optional[Callable] = None, follow_links: Optional[bool] = None) → Iterator[pathspec.util.TreeEntry][source]¶ Walks the specified directory for all files and directories.
root (
str
oros.PathLike[str]
) is the root directory to search.on_error (
Callable
orNone
) optionally is the error handler for file-system exceptions. It will be called with the exception (OSError
). Reraise the exception to abort the walk. Default isNone
to ignore file-system exceptions.follow_links (
bool
orNone
) optionally is whether to walk symbolic links that resolve to directories. Default isNone
forTrue
.Raises
RecursionError
if recursion is detected.Returns an
Iterator
yielding each file or directory entry (TreeEntry
) relative to root.
-
pathspec.util.
iter_tree_files
(root: Union[str, os.PathLike], on_error: Optional[Callable] = None, follow_links: Optional[bool] = None) → Iterator[str][source]¶ Walks the specified directory for all files.
root (
str
oros.PathLike[str]
) is the root directory to search for files.on_error (
Callable
orNone
) optionally is the error handler for file-system exceptions. It will be called with the exception (OSError
). Reraise the exception to abort the walk. Default isNone
to ignore file-system exceptions.follow_links (
bool
orNone
) optionally is whether to walk symbolic links that resolve to directories. Default isNone
forTrue
.Raises
RecursionError
if recursion is detected.Returns an
Iterator
yielding the path to each file (str
) relative to root.
-
pathspec.util.
iter_tree
(root, on_error=None, follow_links=None)[source]¶ DEPRECATED: The
iter_tree()
function is an alias for theiter_tree_files()
function.
-
pathspec.util.
lookup_pattern
(name: str) → Callable[[AnyStr], pathspec.pattern.Pattern][source]¶ Lookups a registered pattern factory by name.
name (
str
) is the name of the pattern factory.Returns the registered pattern factory (
Callable
). If no pattern factory is registered, raisesKeyError
.
-
pathspec.util.
match_file
(patterns: Iterable[pathspec.pattern.Pattern], file: str) → bool[source]¶ Matches the file to the patterns.
patterns (
Iterable
ofPattern
) contains the patterns to use.file (
str
) is the normalized file path to be matched against patterns.
-
pathspec.util.
match_files
(patterns: Iterable[pathspec.pattern.Pattern], files: Iterable[str]) → Set[str][source]¶ DEPRECATED: This is an old function no longer used. Use the
match_file()
function with a loop for better results.Matches the files to the patterns.
patterns (
Iterable
ofPattern
) contains the patterns to use.files (
Iterable
ofstr
) contains the normalized file paths to be matched against patterns.
-
pathspec.util.
normalize_file
(file: Union[str, os.PathLike], separators: Optional[Collection[str]] = None) → str[source]¶ Normalizes the file path to use the POSIX path separator (i.e.,
'/'
), and make the paths relative (remove leading'/'
).file (
str
oros.PathLike[str]
) is the file path.separators (
Collection
ofstr
; orNone
) optionally contains the path separators to normalize. This does not need to include the POSIX path separator ('/'
), but including it will not affect the results. Default isNone
forNORMALIZE_PATH_SEPS
. To prevent normalization, pass an empty container (e.g., an empty tuple()
).Returns the normalized file path (
str
).
-
pathspec.util.
normalize_files
(files: Iterable[Union[str, os.PathLike]], separators: Optional[Collection[str]] = None) → Dict[str, List[Union[str, os.PathLike]]][source]¶ DEPRECATED: This function is no longer used. Use the
normalize_file()
function with a loop for better results.Normalizes the file paths to use the POSIX path separator.
files (
Iterable
ofstr
oros.PathLike[str]
) contains the file paths to be normalized.separators (
Collection
ofstr
; orNone
) optionally contains the path separators to normalize. Seenormalize_file()
for more information.Returns a
dict
mapping each normalized file path (str
) to the original file paths (list
ofstr
oros.PathLike[str]
).
-
pathspec.util.
register_pattern
(name: str, pattern_factory: Callable[[AnyStr], pathspec.pattern.Pattern], override: Optional[bool] = None) → None[source]¶ Registers the specified pattern factory.
name (
str
) is the name to register the pattern factory under.pattern_factory (
Callable
) is used to compile patterns. It must accept an uncompiled pattern (str
) and return the compiled pattern (Pattern
).override (
bool
orNone
) optionally is whether to allow overriding an already registered pattern under the same name (True
), instead of raising anAlreadyRegisteredError
(False
). Default isNone
forFalse
.
-
exception
pathspec.util.
AlreadyRegisteredError
(name: str, pattern_factory: Callable[[AnyStr], pathspec.pattern.Pattern])[source]¶ Bases:
Exception
The
AlreadyRegisteredError
exception is raised when a pattern factory is registered under a name already in use.
-
exception
pathspec.util.
RecursionError
(real_path: str, first_path: str, second_path: str)[source]¶ Bases:
Exception
The
RecursionError
exception is raised when recursion is detected.-
first_path
¶ first_path (
str
) is the first path encountered forself.real_path
.
-
second_path
¶ second_path (
str
) is the second path encountered forself.real_path
.
-
-
class
pathspec.util.
MatchDetail
(patterns: Sequence[pathspec.pattern.Pattern])[source]¶ Bases:
object
The
MatchDetail
class contains information about
-
class
pathspec.util.
TreeEntry
(name: str, path: str, lstat: os.stat_result, stat: os.stat_result)[source]¶ Bases:
object
The
TreeEntry
class contains information about a file-system entry.-
is_dir
(follow_links: Optional[bool] = None) → bool[source]¶ Get whether the entry is a directory.
follow_links (
bool
orNone
) is whether to follow symbolic links. If this isTrue
, a symlink to a directory will result inTrue
. Default isNone
forTrue
.Returns whether the entry is a directory (
bool
).
-
is_file
(follow_links: Optional[bool] = None) → bool[source]¶ Get whether the entry is a regular file.
follow_links (
bool
orNone
) is whether to follow symbolic links. If this isTrue
, a symlink to a regular file will result inTrue
. Default isNone
forTrue
.Returns whether the entry is a regular file (
bool
).
-
stat
(follow_links: Optional[bool] = None) → os.stat_result[source]¶ Get the cached stat result for the entry.
follow_links (
bool
orNone
) is whether to follow symbolic links. If this isTrue
, the stat result of the linked file will be returned. Default isNone
forTrue
.Returns that stat result (
os.stat_result
).
-