diff --git a/shepherd/config.py b/shepherd/config.py index ab7f4fd..e8d152c 100644 --- a/shepherd/config.py +++ b/shepherd/config.py @@ -1,7 +1,13 @@ +""" +Configuration managment module. Enables configuration to be validated against +requirement definitions before being loaded and used. +""" + + import re import toml -from shepherd.freezedry import freezedryable, rehydrate +from .freezedry import freezedryable, rehydrate @@ -172,6 +178,13 @@ class ConfigManager(): self.root_config = {} def load(self, source): + """ + Load a config source into the ConfigManager. + + Args: + source: Either a dict config to load directly, a filepath to a TOML file, + or a open file. + """ if isinstance(source, dict): # load from dict self.root_config = source elif isinstance(source, str): # load from pathname @@ -181,6 +194,17 @@ class ConfigManager(): self.root_config = toml.load(source) def get_config(self, table_name, conf_def): + """ + Get a config dict called ``table_name`` and validate + it against ``conf_def`` before returning it. + + Note that as part of validation, optional keys that are missing will be + filled in with their default values (see ``TableDef``). + + Args: + table_name: (str) Name of the config dict to find. + conf_def: (ConfDefinition) Config definition to validate against. + """ if not isinstance(conf_def, ConfDefinition): raise TypeError("Supplied config definition must be an instance " "of ConfDefinition") @@ -195,6 +219,16 @@ class ConfigManager(): return self.root_config[table_name] def get_configs(self, conf_defs): + """ + Get multiple configs at once, validating each one. + + Args: + conf_defs: (dict) A dictionary of ConfigDefinitions. The keys are used + as the name to find each config dict, which is then validated against + the corresponding conf def. + Returns: + A dict of config dicts, with keys matching those passed in ``conf_defs``. + """ config_values = {} for name, conf_def in conf_defs.items(): config_values[name] = self.get_config(name, conf_def)