Module dirct.dirct

Classes

class Dirct (path: str | os.PathLike[str], loaders: Iterable[Callable[[pathlib.Path], Any]] = (<dirct.loaders.TomlLoader object>, <dirct.loaders.YamlLoader object>, <dirct.loaders.JsonLoader object>, <dirct.loaders.TextLoader object>, <dirct.loaders.BinaryLoader object>), key_mapper: KeyMapper = <dirct.key_mappers.exact_key_mapper.ExactKeyMapper object>)

A dict that reflects the contents of a directory.

The keys are the names of the files/subdirectories in the directory (subject to the rules imposed by the given key_mapper). The values are the parsed contents of the files, or nested Dirct objects for subdirectories.

This class does no caching, so it will reflect any changes to the directory's contents. If you want to load the contents of the directory once and then use them, use to_dict() method.

Additionally, if the directory contains a file named self.* (where * is any file extension supported by the parsers), the contents of that file will be added to the Dirct as well. This allows you to keep some of the keys in a single file and the rest in separate files or subdirectories.

The loaders parameter can be passed to specify which loaders to use to try to load files. A loader is a callable that takes a Path and returns the parsed contents. Loaders are tried in order. If a loader raises UnsupportedFileError, the next loader is tried, but if it raises any other kind of exception, it is propagated. If no loader can load a file, UnsupportedFileError is raised.

Args

path
The path to the directory.
loaders
The callables to use to try to load files. Defaults to DEFAULT_LOADERS, which supports JSON, YAML, TOML, plain text, and binary files.
key_converter
A key converter to use to convert keys to paths and vice versa. Defaults to an ExactKeyMapper that uses the exact file names as keys.

Ancestors

  • dirct._base_dirct.BaseDirct
  • collections.abc.Mapping
  • collections.abc.Collection
  • collections.abc.Sized
  • collections.abc.Iterable
  • collections.abc.Container
  • typing.Generic

Methods

def to_dict(self) ‑> dict[str, typing.Any]

Convert this Dirct to a plain dict, recursively converting all subdirectories to dicts as well.