API Reference¶
- class crabwalk.Walk(*paths, max_depth=None, follow_symlinks=False, max_filesize=None, global_ignore_files=None, custom_ignore_filenames=None, overrides=None, types=None, hidden=True, parents=True, ignore=True, git_global=True, git_ignore=True, git_exclude=True, require_git=True, ignore_case_insensitive=False, sort=None, same_file_system=False, skip_stdout=False, filter_entry=None, onerror=None)¶
Recursive directory iterator which yields
DirEntryobjects.If
Walkis not closed (either by using awithstatement or callingclose()explicitly) then aResourceWarningwill be emitted in its destructor.- Parameters:
paths (Union[str, os.PathLike[str]]) – Paths to iterate recursively.
follow_symlinks (bool) – Whether to follow symbolic links or not.
max_filesize (Optional[int]) – Whether to ignore files above the specified limit.
global_ignore_files (Sequence[Union[str, os.PathLike[str]]]) – Paths to global ignore files. These have lower precedence than all other sources of ignore rules.
custom_ignore_filenames (Sequence[str]) – Custom ignore file names. These have higher precedence than all other ignore files.
hidden (bool) – Enables ignoring hidden files.
parents (bool) – Enables reading ignore files from parent directories. When enabled,
.gitignorefiles in parent directories of each file path given are respected. Otherwise, they are ignored.ignore (bool) –
Enables reading
.ignorefiles..ignorefiles have the same semantics as gitignore files and are supported by search tools such as ripgrep and The Silver Searcher.git_global (bool) –
Enables reading a global gitignore file, whose path is specified in git’s
core.excludesFileconfig option.Git’s config file location is
$HOME/.gitconfig. If$HOME/.gitconfigdoes not exist or does not specifycore.excludesFile, then$XDG_CONFIG_HOME/git/ignoreis read. If$XDG_CONFIG_HOMEis not set or is empty, then$HOME/.config/git/ignoreis used instead.git_ignore (bool) – Enables reading
.gitignorefiles.git_exclude (bool) –
Enables reading
.git/info/excludefiles..git/info/excludefiles have match semantics as described in the gitignore man page.require_git (bool) – Whether a git repository is required to apply git-related ignore rules (global rules,
.gitignoreand local exclude rules).ignore_case_insensitive (bool) – Process ignore files case insensitively.
sort (Union[Callable[[str], SupportsRichComparison], bool]) – May be true to sort entries by file path, or a callable to extract a comparison key based on the file path (like the
keyargument tosorted()).same_file_system (bool) – Do not cross file system boundaries.
skip_stdout (bool) –
Do not yield directory entries that are believed to correspond to stdout.
This is useful when a command is invoked via shell redirection to a file that is also being read. For example,
grep -r foo ./ > resultsmight end up trying to searchresultseven though it is also writing to it, which could cause an unbounded feedback loop. Setting this option prevents this from happening by skipping over theresultsfile.filter_entry (Optional[Callable[[DirEntry], bool]]) – Yields only entries which satisfy the given predicate and skips descending into directories that do not satify the given predicate.
onerror (Optional[Callable[[Exception], None]]) – By default, errors are ignored. You may specify a function to either log the error or re-raise it.
- disable_standard_filters() None¶
Disable the
hidden,parents,ignore,git_ignore,git_global, andgit_excludefilters.
- class crabwalk.DirEntry¶
Object yielded by
Walkto expose the file path and other file attributes of a directory entry.The interface is similar—but not identical—to
os.DirEntry.DirEntryimplements theos.PathLikeinterface.- name: str¶
Return the base filename of this entry.
os.DirEntrydifferenceIf this entry has no file name (e.g.,
/), then the full path is returned.
- path: str¶
The entry’s full path name. The path is only absolute if the
Walkpath argument was absolute.
- inode() int¶
Return the inode number of the entry.
If
follow_symlinks=Trueand this entry is a symbolic link, the inode number of the target is returned.os.DirEntrydifferenceIn contrast,
os.DirEntryalways returns the inode number of the symbolic link itself.Use
os.stat(entry, follow_symlinks=False).st_inoif that’s what you want.On the first, uncached call, a system call is required on Windows but not on Unix.
- is_dir() bool¶
Returns whether this entry is a directory or if
Walkwas configured withfollow_symlinks=Trueand this is a symbolic link pointing to a directory.Never makes any system calls.
os.DirEntrydifferenceThere is no
follow_symlinksargument, as it is configured viaWalk.
- is_file() bool¶
Returns whether this entry is a file or if
Walkwas configured withfollow_symlinks=Trueand this is a symbolic link pointing to a file.Never makes any system calls.
os.DirEntrydifferenceThere is no
follow_symlinksargument, as it is configured viaWalk.
- stat() os.stat_result¶
Returns a
stat_resultobject for this entry. Follows symbolic links ifWalkwas configured withfollow_symlinks=True.Always makes a system call.
os.DirEntrydifferenceThere is no
follow_symlinksargument, as it is configured viaWalk.
- class crabwalk.Types(initial=None, /, **kwargs)¶
A collection of type definitions with selections and negations.
Typesimplements theMutableMappinginterface.Typical usage:
types = Types() types.add_defaults() types.select("...") types.negate("...") with Walk(..., types=types) as walk: ...
- add(name: str, glob: str) None¶
Add
globto type with namename.>>> types = Types() >>> types.add("py", "*.py") >>> types.add("py", "*.pyi") >>> types["py"] ('*.py', '*.pyi')
- class crabwalk.Override(glob, case_insensitive=False)¶
A
namedtupleused byOverrides.- Parameters:
glob (str) – A glob with the same semantics as a single line in a
.gitignorefile, where the meaning of!is inverted: namely,!at the beginning of a glob will ignore a file. Without!, all matches of the glob provided are treated as whitelist matches.case_insensitive (bool) – Whether this glob should be matched case insensitively or not.
- class crabwalk.Overrides(overrides=(), *, path)¶
A
MutableSequenceofOverridetuples.Strings and tuples will be coerced to
Overrideinstances.- Parameters:
overrides (Iterable[str | tuple[str, bool]]) –
An iterable of globs,
(glob, case_insensitive)tuples, orOverridenamedtuples.>>> o = Overrides(["*.py", ("*.pyi", True), Override("!*.pyc")], path=".") >>> assert o[0] == Override("*.py", False) >>> assert o[1] == Override("*.pyi", True) >>> assert o[2] == Override("!*.pyc", False)
path (Union[str, os.PathLike[str]]) – Globs are matched relative to this path.
Exceptions¶
- exception crabwalk.WalkError¶
Base class for all exceptions raised by the
crabwalkpackage.- line¶
A line number if this error is associated with one.
- path¶
A file path is this error is associated with one.
- depth¶
A directory depth if this error is associated with one while recursively walking a directory.
- exception crabwalk.LoopError¶
An error that occurs when a file loop is detected when traversing symbolic links.
- exception crabwalk.GlobError¶
An error that occurs when trying to parse a glob.
- exception crabwalk.PartialError¶
A collection of “soft” errors. These occur when adding an ignore file partially succeeded.
- exception crabwalk.InvalidDefinitionError¶
A user specified file type definition could not be parsed.
- exception crabwalk.UnrecognizedFileTypeError¶
A type selection for a file type that is not defined.