示例#1
0
def apply_config(raw, validators):
    raw = _normalize(raw, validators)

    # find invalid options
    invalid = deepcopy(raw)

    for k, config in validators.items():
        options = [opt for opt in config.options() if opt.alias is None]

        for option in options:
            if dict_has_path(invalid, option.name):
                dict_del_path(invalid, option.name)

        if k in invalid or (not k and invalid):
            first = dict_paths(invalid, k)[0]
            candidates = [opt.name for opt in options]
            suggestion = did_you_mean(candidates, first)
            raise _invalid_option(first, help_topic=config.name, suggestion=suggestion, kind='key')
    
        # validate and cast options
        for option in options:
            if dict_has_path(raw, option.name):
                value = dict_get_path(raw, option.name)
                dict_del_path(raw, option.name)
                config.configure(option, value)
    
    return raw
示例#2
0
    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
示例#3
0
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)
示例#4
0
文件: env.py 项目: zennsocial/vergeml
    def get(self, path):
        """Get a value by its path.

        For example, to access the variable 'id' in the dict 'device', use device.id as path.
        """
        return dict_get_path(self._config, path, None)
示例#5
0
文件: test_utils.py 项目: mme/vergeml
def test_get_path_top():
    dic = {'x': {'y': {'z': 1}}}
    assert dict_get_path(dic, 'x') == {'y': {'z': 1}}
示例#6
0
文件: test_utils.py 项目: mme/vergeml
def test_get_path():
    dic = {'x': {'y': {'z': 1}}}
    assert dict_get_path(dic, 'x.y.z') == 1