configloader.py 文件源码

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

项目:kscore 作者: liuyichen 项目源码 文件源码
def raw_config_parse(config_filename):
    """Returns the parsed INI config contents.

    Each section name is a top level key.

    :returns: A dict with keys for each profile found in the config
        file and the value of each key being a dict containing name
        value pairs found in that profile.

    :raises: ConfigNotFound, ConfigParseError
    """
    config = {}
    path = config_filename
    if path is not None:
        path = os.path.expandvars(path)
        path = os.path.expanduser(path)
        if not os.path.isfile(path):
            raise kscore.exceptions.ConfigNotFound(path=path)
        cp = configparser.RawConfigParser()
        try:
            cp.read(path)
        except configparser.Error:
            raise kscore.exceptions.ConfigParseError(path=path)
        else:
            for section in cp.sections():
                config[section] = {}
                for option in cp.options(section):
                    config_value = cp.get(section, option)
                    if config_value.startswith('\n'):
                        # Then we need to parse the inner contents as
                        # hierarchical.  We support a single level
                        # of nesting for now.
                        try:
                            config_value = _parse_nested(config_value)
                        except ValueError:
                            raise kscore.exceptions.ConfigParseError(
                                path=path)
                    config[section][option] = config_value
    return config
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号