DirConfMixin

class tollan.utils.dirconf.DirConfMixin[source]

Bases: object

A mix-in class for managing configuration files stored in a directory.

The class implements the logic to setup a directory and populate a set of pre-defined items.

The class manages a set of configuration files recognized by the _config_file_pattern, provides methods to load and merge the configuration in memory.

The mix-in class expects the following class attributes from the instrumenting class:

  • _contents

    A dictionary {<attr>: dict} that defines the content of the directory generated populate_dir. The dict for each attr should have the following keys:

    • path: The path name.

    • type: one of file or dir

    • backup_enabled: True or False.

The mix-in class also expects the following property:

  • rootpath

    The path of the mapped directory.

Attributes Summary

logger

Methods Summary

collect_config_files()

The list of configuration files present in the directory.

collect_config_from_files(config_files[, …])

Load config from config_files and merge by the order.

get_config_schema()

Return a schema.Schema object that validates the config.

populate_dir(dirpath[, create, force, …])

Populate dirpath with the defined items.

to_dict()

Return a dict representation of the contents.

validate_config(cfg)

write_config_to_yaml(config, filepath[, …])

Attributes Documentation

logger = <Logger DirConfMixin (WARNING)>

Methods Documentation

collect_config_files()[source]

The list of configuration files present in the directory.

Files with names match _config_file_pattern are returned.

The returned files are sorted according to the sort key _config_file_sort_key.

classmethod collect_config_from_files(config_files, validate=True)[source]

Load config from config_files and merge by the order.

Parameters
config_fileslist

A list of configuration file paths.

validatebool, optional

If True, the configuration is validated using validate_config().

classmethod get_config_schema()[source]

Return a schema.Schema object that validates the config.

This combines all extend_config_schema defined in all base classes.

classmethod populate_dir(dirpath, create=False, force=False, overwrite=False, dry_run=False, **kwargs)[source]

Populate dirpath with the defined items.

Parameters
dirpathpathlib.Path, str

The path to the work directory.

createbool

When set to False, raise DirConfigError if path does not already have all content items. Otherwise, create the missing ones.

forcebool

When False, raise DirConfigError if dirpath is not empty

overwritebool

When False, backups for existing files is created.

dry_runbool

If True, no actual file system changed is made.

kwargsdict

Keyword arguments passed directly into the created config file.

to_dict()[source]

Return a dict representation of the contents.

classmethod validate_config(cfg)[source]
classmethod write_config_to_yaml(config, filepath, overwrite=False)[source]