API Reference¶
Directory¶
- class Dir(entries=None)[source]¶
Directory layout class. See Use cases for examples.
Example
>>> from dirlay import Dir
>>> Dir({'docs/index.rst': '', 'src': {}, 'pyproject.toml': '\n'}).data {'docs': {'index.rst': ''}, 'src': {}, 'pyproject.toml': '\n'}
>>> Dir({'a/b/c/d/e/f.txt': '', 'a/b/c/d/ee': {}}).data {'a': {'b': {'c': {'d': {'e': {'f.txt': ''}, 'ee': {}}}}}}
- __eq__(other)[source]¶
Two directory layouts are equal if they have:
equal files and directories (both path and data)
equal
basedir
- __floordiv__(path)[source]¶
Return absolute
Pathobject for sub-path; equivalent totree[path].abspath.Directory layout must be linked to the file system.
- __or__(entries)[source]¶
Append dict of entries to a copy of self.
Equivalent to
x = self.copy(); x.update(entries).
- as_rich(real_basedir=False, show_data=False, **kwargs)[source]¶
Return
Treerepresentation; rich must be installed. See Print as tree for examples.
- chdir(path=None)[source]¶
Change current directory to a subdirectory relative to layout base.
- Parameters:
path (
Path|str|None) – Relative path to subdirectory to be chdir’ed to; ifNone(default),basedirwill be used.- Returns:
None- Raises:
ValueError – If
pathis absolute.
- mktree(basedir=None, chdir=None)[source]¶
Create directories and files in given or temporary directory.
- Parameters:
basedir (
Path|str|None, optional) – Path to base directory under which directories and files will be created; ifNone(default), temporary directory is used. After the directory structure is created,basedirvalue is available asbasedirattribute.chdir (
Path|str|bool|None, optional) – Change the current directory to given path. IfNone(default) orFalse, directory is not changed;Trueis equivalent to'.'.
- Returns:
None- Raises:
FileExistsError – If
basedirpath already exists.
- print_rich(real_basedir=False, show_data=False, **kwargs)[source]¶
Print
Treerepresentation. Seeas_rich.- Returns:
None
- rmtree()[source]¶
Remove directory and all its contents.
If
basedirwas created, it will be removed. Ifchdirargument was passed, current working directory will be restored to the original one.- Returns:
None
- property basedir¶
Base filesystem directory as
Pathobject.When
None, directory layout object is not instantiated (not created on the file system).
- property data¶
Internal data mapping.
Node¶
- class Node(key, base, basedir)[source]¶
Node proxy object representing directory or file.
- key¶
String path relative to
Dirroot, and also a mapping key:>>> tree = Dir({'a': {'b.md': 'B'}}) >>> tree[tree['a/b.md'].key] == tree['a/b.md'] True
- Type:
str
- data¶
In-memory file content if the node is a file, or, if
isdirisTrue, a dictionary, representing directory structure.- Type:
str | dict[str, str | dict]
- abspath¶
Absolute node path, if directory layout is linked to the filesystem, or
Noneotherwise.- Type:
Path|None
- isdir¶
Whether the node is a directory.
- Type:
bool
Utilities¶
Type aliases¶
>>> from dirlay.types import DictTree, DictNode