Improve docs

master
Tom Wilson 6 years ago
parent 4987b672d7
commit e4d1e145e4

@ -1,7 +1,13 @@
"""
Configuration managment module. Enables configuration to be validated against
requirement definitions before being loaded and used.
"""
import re import re
import toml import toml
from shepherd.freezedry import freezedryable, rehydrate from .freezedry import freezedryable, rehydrate
@ -172,6 +178,13 @@ class ConfigManager():
self.root_config = {} self.root_config = {}
def load(self, source): 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 if isinstance(source, dict): # load from dict
self.root_config = source self.root_config = source
elif isinstance(source, str): # load from pathname elif isinstance(source, str): # load from pathname
@ -181,6 +194,17 @@ class ConfigManager():
self.root_config = toml.load(source) self.root_config = toml.load(source)
def get_config(self, table_name, conf_def): 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): if not isinstance(conf_def, ConfDefinition):
raise TypeError("Supplied config definition must be an instance " raise TypeError("Supplied config definition must be an instance "
"of ConfDefinition") "of ConfDefinition")
@ -195,6 +219,16 @@ class ConfigManager():
return self.root_config[table_name] return self.root_config[table_name]
def get_configs(self, conf_defs): 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 = {} config_values = {}
for name, conf_def in conf_defs.items(): for name, conf_def in conf_defs.items():
config_values[name] = self.get_config(name, conf_def) config_values[name] = self.get_config(name, conf_def)

Loading…
Cancel
Save