示例#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
文件: test_utils.py 项目: mme/vergeml
def test_has_path_false():
    dic = {'x': {'y': {'z': 1}}}
    assert not dict_has_path(dic, 'x.z.y')
示例#5
0
文件: test_utils.py 项目: mme/vergeml
def test_has_path_top():
    dic = {'x': {'y': {'z': 1}}}
    assert dict_has_path(dic, 'x')
示例#6
0
文件: test_utils.py 项目: mme/vergeml
def test_has_path():
    dic = {'x': {'y': {'z': 1}}}
    assert dict_has_path(dic, 'x.y.z')
示例#7
0
def test_has_path_false():
    d = {'x': {'y': {'z': 1}}}
    assert dict_has_path(d, 'x.z.y') == False
示例#8
0
def test_has_path_top():
    d = {'x': {'y': {'z': 1}}}
    assert dict_has_path(d, 'x') == True