ConfigInit

Contents

ConfigInit#

class processor_tools.config.init_config.ConfigInit(package_name: str, configs: Dict[str, str | dict | Callable], config_directory: str | None = None, config_directory_file_path: str | None = None)[source]#

Defines and initialises a set of configuration files for a package.

Config filenames are specified relative to a config directory whose location is resolved at init time. Two standard locations are provided:

An explicit path can also be passed directly to init().

Parameters:
  • package_name – package name, used to derive the standard config directory names

  • configs

    dict mapping config filename to its template. Template can be:

    • dict — written as a new config file with those values

    • str — path to an existing file that is copied to the target location

    • callable — called at init time with no arguments, must return a dict

Example:

config_init = ConfigInit(
    package_name="mypackage",
    configs={
        "settings.yaml": {"db_host": "localhost", "debug": False},
        "logging.yaml": "/path/to/default_logging.yaml",
        "env.yaml": lambda: {"hostname": os.uname().nodename},
    },
)

config_init.init()                                    # -> ~/.config/<package_name>/
config_init.init(config_init.project_dir())          # -> <cwd>/.<package_name>/
config_init.init(config_init.project_dir(__file__))  # -> <this file's dir>/.<package_name>/
config_init.init("/explicit/path")                    # -> /explicit/path/

Methods

__init__

Initialise the ConfigInit instance.

cli

Command-line interface for initialising the config directory.

get_config_directory

Get the config directory path from the config directory file, or return the default home directory if the file does not exist.

home_dir

Return the standard user-home config directory: ~/.config/<package_name>.

init

Create all defined config files in the given directory.

is_initialised

Return True if all defined config files are present in the given directory.

list_config

Return a list of config filenames present in the given directory.

missing

Return a list of config filenames not present in the given directory.

project_dir

Return the standard project config directory: <base>/.<package_name>.

reset_config_directory

Reset the config directory by deleting the config directory file and all config files in the config directory.

set_config_directory

Set the config directory path in the config directory file.