config_file.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:pyera 作者: pyera 项目源码 文件源码
def set_config_entry_value(configentry, v):
    if configentry.fixed:
        warning(ParserError(parser.filename, 0, 'Tried to overwrite a fixed configuration.'))
        return False
    if v == '':
        warning(ParserError(parser.filename, 0, 'The configuration value is an empty string.'))
        return False
    v = v.strip()
    if configentry.dtype == bool:
        try:
            configentry.value = (int(dtype) != 0)
            return True
        except:
            pass
        if v in ('NO', 'FALSE', '?'):
            configentry.value = False
            return True
        elif v in ('YES', 'TRUE', '?'):
            configentry.value = True
            return True
        else:
            raise ValueError
    if configentry.dtype == config.Color:
        rgb = v.split(',')
        if len(rgb) < 3:
            raise ValueError
        elif len(rgb) > 3:
            warning(ParserError(parser.filename, 0, 'The value of configuration %s (\'%s\') has more than 3 entries. Truncating.' % (k, v)))
            rgb = rgb[:3]
        rgb = tuple(int(i) for i in rgb)
        for i in rgb:
            if not (0 <= i <= 255):
                raise ValueError
        configentry.value = config.Color(r = rgb[0], g = rgb[1], b = rgb[2])
        return True
    elif configentry.dtype == config.char:
        if len(v) != 1:
            return False
        configentry.value = v
        return True
    elif configentry.dtype == int:
        configentry.value = int(v)
        return True
    elif configentry.dtype == str:
        configentry.value = v
        return True
    elif configentry.dtype == typing.List[int]:
        #Bug in Emuera1821: it will set the value to the point where parse is successful.
        configentry.value = []
        for segment in v.split('/'):
            configentry.value.append(int(segment.strip()))
        return True
    elif type(configentry.dtype) == enum.EnumMeta:
        configentry.value = configentry.dtype[v]
        return True
    else:
        raise RuntimeError('Cannot reach here')

#Config object generater, based on parsed result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号