API Reference¶
dirconf.config
¶
DirConfig
dataclass
¶
A base dataclass representing a collection of configuration files.
This either be subclassed explicitly, i.e. via class MyConfig(DirConfig): ...,
or through the make_dirconfig
function.
All fields are expected to be instances of Node.
nodes
¶
An iterator over all nodes (fields) in the configuration.
Parameters:
-
recurse(bool, default:False) –In cases where one or more of the nodes of the
DirConfigis a directory whoseHandleris itself instance ofDirConfig, passingrecurse=Truewill also yield the nodes from these children.
Yields:
read
¶
Read a configuration from a given path and return its contents as a dict.
Parameters:
Returns:
tree
¶
Returns a tree-like representation of the configuration.
Parameters:
-
max_depth(int | None, default:None) –Optionally truncate the tree at a certain depth.
-
details(bool, default:True) –If set to
False, details about the path and handler are suppressed.
Returns:
-
tree_repr(str) –A printable tree-like representation of the configuration.
write
¶
Write a configuration to a given path.
Parameters:
-
path(str | PathLike) –A path to a directory where the configuration will be written. The directory need not yet exist.
-
data(dict[str, Any]) –A
dictwhose keys match the field names for thisDirConfig, and whose values contain the data to be written. -
overwrite_ok(bool, default:False) –A flag indicating whether overwriting existing files is acceptable. Nothing is done with this argument other than to pass it to the
writemethod for all of the handlers.
make_dirconfig
¶
A function that generates subclasses of DirConfig.
This is a wrapper around
dataclasses.make_dataclass
that sets the base class to dirconf.config.DirConfig and constructs
fields using the provided spec.
Parameters:
-
cls_name(str) –A name for the class being created.
-
spec(dict | str | PathLike) –A dict specifying the node (field) names, paths and handlers.
-
kwargs(Any, default:{}) –Additional arguments to pass to
make_dataclass.
Returns:
dirconf.filter
¶
MISSING
module-attribute
¶
A sentinel value that may be used in place of missing data.
MissingWarning
¶
Bases: Warning
A warning indicating that a filter test has failed, perhaps unexpectedly.
filter
¶
filter(
read: Callable[[Path], bool] | None = None,
write: Callable[..., bool] | None = None,
label: str | None = None,
warn: bool = False,
) -> Callable[[Any], Any]
A decorator for classes satisfying the Handler protocol.
This provides an alternative to decorating both the read method
with filter_read and the write
method with filter_write.
Parameters:
-
read(Callable[[Path], bool] | None, default:None) –A test to run when the
readmethod is called. -
write(Callable[..., bool] | None, default:None) –A test to run when the
writemethod is called. -
warn(bool, default:False) –If
True, emit a warning when a test fails.
See Also
filter_missing
¶
Filter out non-existent paths from read and MISSING data from `write.
This is implemented purely for convenience, since it simply calls
filter with two specific test functions.
filter_read
¶
filter_read(
test: Callable[[Path], bool], label: str | None = None, warn: bool = False
) -> Callable[[ReadMethod], ReadMethod]
A decorator for read methods that allows for filtering.
The path-like argument to read is first cast to a pathlib.Path before
being passed into test. If the output of test(path) is True, the wrapped
read method is called and nothing else is done. If the output is False,
then the read is not attempted and instead a special value,
MISSING, is returned. In the latter case, a
MissingWarning is also emitted if
warn=True.
Parameters:
-
test(Callable[[Path], bool]) –A function which causes the
readto be skipped ifFalseis returned. -
label(str | None, default:None) –An optional label for this filter. If blank or
Noneone will be generated automatically. -
warn(bool, default:False) –If
True, test failure causes a warning is emitted.
filter_write
¶
filter_write(
test: Callable[..., bool], label: str | None = None, warn: bool = False
) -> Callable[[WriteMethod], WriteMethod]
A decorator for write methods that allows for filtering.
The path-like argument to write is first cast to a pathlib.Path before
being passed into test along with data and the remaining argument(s).
If the output of test(path, data, **kwargs) is True, the wrapped
write method is called and nothing else is done. If the output is False,
then the write is not attempted and the function simply returns. In the
latter case, a MissingWarning is also
emitted if warn=True.
Parameters:
-
test(Callable[..., bool]) –A function which causes the
writeto be skipped ifFalseis returned. -
label(str | None, default:None) –An optional label for this filter. If blank or
Noneone will be generated automatically. -
warn(bool, default:False) –If
True, test failure causes a warning is emitted.
dirconf.handler
¶
HandlerFactory
¶
Type alias for a zero-argument callable that returns a Handler.
ReadMethod
¶
Type alias for the read method of a handler.
Expected signature: (self, path: str | PathLike) -> Any
WriteMethod
¶
WriteMethod = Callable[..., None]
Type alias for the write method of a handler.
Expected signature
(self, path: str | PathLike, data: Any, *, overwrite_ok: bool) -> None
Note: Callable cannot express keyword-only parameters or self binding.
The overwrite_ok parameter must be passed as a keyword argument at runtime.
Handler
¶
Bases: Protocol
A Protocol for all valid handlers.
As with any Protocol, it is not necessary to subclass Handler. However, any
valid handler must implement the read and write methods with signatures
matching the abstract methods given here.
infer_handler_from_path
¶
infer_handler_from_path(path: str | PathLike) -> HandlerFactory
Infers the desired HandlerFactory based on the file extension.
parse_handler
¶
parse_handler(input: str | HandlerFactory) -> HandlerFactory
Returns a HandlerFactory given any valid input.
register_handler
¶
register_handler(
name: str, handler: HandlerFactory, extensions: list[str] | None = None
) -> None
Add a handler factory to the registry.
dirconf.node
¶
Node
dataclass
¶
Node(path: str | PathLike, handler: HandlerFactory)
A dataclass representing a file or directory.
handler
instance-attribute
¶
handler: HandlerFactory
A HandlerFactory that produces valid
handlers with read and write methods for the file or directory.
dirconf.utils
¶
dict_to_namespace
¶
dict_to_namespace(dict_: dict) -> SimpleNamespace
Converts a dict into a SimpleNamespace supporting 'dot' access.
namespace_to_dict
¶
namespace_to_dict(namespace: SimpleNamespace) -> dict
Converts a SimpleNamespace back to a dict.
tree
¶
Constructs a tree-like representation of a directory.
This is primarily for sanity-checking by comparing the output of
DirConfig.tree with an actual
directory.
Note
This is inspired by GNU tree and
is an adaptation of
this stackoverflow answer
by Aaron Hall.