def __init__(self, name=None, plugins=PLUGINS): self._values = {} self._options = Option.discover(self, plugins=plugins) self.plugins = plugins self.name = name for option in self._options: if option.alias is None: dict_set_path(self._values, option.name, option.default)
def configure(self, option, value): try: value = option.cast_value(value) value = option.transform_value(value) option.validate_value(value) if isinstance(value, dict): if dict_has_path(self._values, option.name): dict_merge(dict_get_path(self._values, option.name), value) else: dict_set_path(self._values, option.name, value) except VergeMLError as err: # set help topic err.help_topic = self.name raise err
def _normalize(raw, validators): raw = deepcopy(raw) res = {} for _, conf in validators.items(): options = Option.discover(conf) aliases = [opt for opt in options if opt.alias] for alias in aliases: if dict_has_path(raw, alias.name): v = dict_get_path(raw, alias.name) if not alias.type or isinstance(v, alias.type): dict_set_path(res, alias.alias, v) dict_del_path(raw, alias.name) for k, v in deepcopy(raw).items(): if "." in k: dict_set_path(res, k, v) del raw[k] return dict_merge(res, raw)
def set(self, path, value): """Set a value by its path. """ dict_set_path(self._config, path, value)
def test_set_path(): dic = {} dict_set_path(dic, "x.y.z", 1) assert dic == {'x': {'y': {'z': 1}}}
def test_set_path_existing(): dic = {'x': {'y1': 1}} dict_set_path(dic, "x.y.z", 1) assert dic == {'x': {'y': {'z': 1}, 'y1': 1}}