Пример #1
0
def _make_parser(**kwargs):
    """
    :return: (keyword args to be used, parser object)
    """
    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = filter_options(("defaults", "dict_type", "allow_no_value"),
                              kwargs)
    kwargs_1 = filter_options(("filename", ), kwargs)

    try:
        parser = configparser.SafeConfigParser(**kwargs_0)
    except TypeError:
        # .. note::
        #    It seems ConfigParser.*ConfigParser in python 2.6 does not support
        #    'allow_no_value' option parameter, and TypeError will be thrown.
        kwargs_0 = filter_options(("defaults", "dict_type"), kwargs)
        parser = configparser.SafeConfigParser(**kwargs_0)

    return (kwargs_1, parser)
Пример #2
0
def _load_impl(config_fp, sep=_SEP, **kwargs):
    """
    :param config_fp: File or file-like object provides ini-style conf
    :return: Dict or dict-like object represents config values
    """
    config = dict()

    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = Base.mk_opt_args(("defaults", "dict_type", "allow_no_value"),
                                kwargs)
    kwargs_1 = Base.mk_opt_args(("filename", ), kwargs)

    try:
        try:
            parser = configparser.SafeConfigParser(**kwargs_0)
        except TypeError:
            # It seems ConfigPaser.*ConfigParser in python 2.6 does not support
            # 'allow_no_value' option parameter, and TypeError will be thrown.
            kwargs_0 = Base.mk_opt_args(("defaults", "dict_type"), kwargs)
            parser = configparser.SafeConfigParser(**kwargs_0)

        parser.readfp(config_fp, **kwargs_1)

        if parser.defaults():
            config["DEFAULT"] = dict()

            for k, v in iteritems(parser.defaults()):
                config["DEFAULT"][k] = _parse(v, sep)

        for s in parser.sections():
            config[s] = dict()

            for k, v in parser.items(s):
                config[s][k] = _parse(v, sep)

    except Exception:
        logging.warn(sys.exc_info()[-1])
        raise

    return config
Пример #3
0
def _load(stream, to_container=dict, sep=_SEP, **kwargs):
    """
    :param stream: File or file-like object provides ini-style conf
    :param to_container: any callable to make container
    :param sep: Seprator string

    :return: Dict or dict-like object represents config values
    """
    _parse_val = _parse if kwargs.get("ac_parse_value", False) else _noop

    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = mk_opt_args(("defaults", "dict_type", "allow_no_value"), kwargs)
    kwargs_1 = mk_opt_args(("filename", ), kwargs)

    try:
        parser = configparser.SafeConfigParser(**kwargs_0)
    except TypeError:
        # .. note::
        #    It seems ConfigPaser.*ConfigParser in python 2.6 does not support
        #    'allow_no_value' option parameter, and TypeError will be thrown.
        kwargs_0 = mk_opt_args(("defaults", "dict_type"), kwargs)
        parser = configparser.SafeConfigParser(**kwargs_0)

    cnf = to_container()
    parser.readfp(stream, **kwargs_1)

    # .. note:: Process DEFAULT config parameters as special ones.
    defaults = parser.defaults()
    if defaults:
        cnf["DEFAULT"] = to_container()
        for key, val in iteritems(defaults):
            cnf["DEFAULT"][key] = _parse_val(val, sep)

    for sect in parser.sections():
        cnf[sect] = to_container()
        for key, val in parser.items(sect):
            cnf[sect][key] = _parse_val(val, sep)

    return cnf