Exemple #1
0
def _dumps_itr(cnf, dkey=DEFAULTSECT):
    """
    :param cnf: Configuration data to dump
    """
    for sect, params in iteritems(cnf):
        yield "[%s]" % sect

        for key, val in iteritems(params):
            if sect != dkey and dkey in cnf and cnf[dkey].get(key) == val:
                continue  # It should be in [DEFAULT] section.

            yield "%s = %s" % (key, _to_s(val))

        yield ''  # it will be a separator between each sections.
Exemple #2
0
def _dumps_itr(cnf, dkey=DEFAULTSECT):
    """
    :param cnf: Configuration data to dump
    """
    for sect, params in iteritems(cnf):
        yield "[%s]" % sect

        for key, val in iteritems(params):
            if sect != dkey and dkey in cnf and cnf[dkey].get(key) == val:
                continue  # It should be in [DEFAULT] section.

            yield "%s = %s" % (key, _to_s(val))

        yield ''  # it will be a separator between each sections.
def mk_lines_g(data):
    has_default = "DEFAULT" in data

    def is_inherited_from_default(k, v):
        return has_default and data["DEFAULT"].get(k, None) == v

    for sect, params in iteritems(data):
        yield "[%s]\n" % sect

        for k, v in iteritems(params):
            if sect != "DEFAULT" and is_inherited_from_default(k, v):
                continue

            yield "%s = %s\n" % (k, _to_s(v))

        yield "\n"  # put an empty line just after each sections.
Exemple #4
0
def mk_lines_g(data):
    has_default = "DEFAULT" in data

    def is_inherited_from_default(k, v):
        return has_default and data["DEFAULT"].get(k, None) == v

    for sect, params in iteritems(data):
        yield "[%s]\n" % sect

        for k, v in iteritems(params):
            if sect != "DEFAULT" and is_inherited_from_default(k, v):
                continue

            yield "%s = %s\n" % (k, _to_s(v))

        yield "\n"  # put an empty line just after each sections.
Exemple #5
0
def _load(stream, container, sep=_SEP, dkey=DEFAULTSECT, **kwargs):
    """
    :param stream: File or file-like object provides ini-style conf
    :param container: any callable to make container
    :param sep: Seprator string
    :param dkey: Default section name

    :return: Dict or dict-like object represents config values
    """
    (kwargs_1, psr) = _make_parser(**kwargs)
    if IS_PYTHON_3:
        psr.read_file(stream, **kwargs_1)
    else:
        psr.readfp(stream, **kwargs_1)

    cnf = container()
    kwargs["sep"] = sep

    defaults = psr.defaults()
    if defaults:
        cnf[dkey] = container(_parsed_items(iteritems(defaults), **kwargs))

    for sect in psr.sections():
        cnf[sect] = container(_parsed_items(psr.items(sect), **kwargs))

    return cnf
def etree_to_container(root, container):
    """
    Convert XML ElementTree to a collection of container objects.

    :param root: etree root object or None
    :param container: A nested dict like objects
    """
    tree = container()
    if root is None:
        return tree

    tree[root.tag] = container()

    if root.attrib:
        tree[root.tag]["attrs"] = container(AC.iteritems(root.attrib))

    if root.text and root.text.strip():
        tree[root.tag]["text"] = root.text.strip()

    if len(root):  # It has children.
        # FIXME: Configuration item cannot have both attributes and
        # values (list) at the same time in current implementation:
        tree[root.tag]["children"] = [etree_to_container(c, container) for c
                                      in root]

    return tree
def _load(stream, container, sep=_SEP, dkey=DEFAULTSECT, **kwargs):
    """
    :param stream: File or file-like object provides ini-style conf
    :param container: any callable to make container
    :param sep: Seprator string
    :param dkey: Default section name

    :return: Dict or dict-like object represents config values
    """
    (kwargs_1, psr) = _make_parser(**kwargs)
    if IS_PYTHON_3:
        psr.read_file(stream, **kwargs_1)
    else:
        psr.readfp(stream, **kwargs_1)

    cnf = container()
    kwargs["sep"] = sep

    defaults = psr.defaults()
    if defaults:
        cnf[dkey] = container(_parsed_items(iteritems(defaults), **kwargs))

    for sect in psr.sections():
        cnf[sect] = container(_parsed_items(psr.items(sect), **kwargs))

    return cnf
Exemple #8
0
def etree_to_container(root, container):
    """
    Convert XML ElementTree to a collection of container objects.

    :param root: etree root object or None
    :param container: A nested dict like objects
    """
    tree = container()
    if root is None:
        return tree

    tree[root.tag] = container()

    if root.attrib:
        tree[root.tag]["attrs"] = container(AC.iteritems(root.attrib))

    if root.text and root.text.strip():
        tree[root.tag]["text"] = root.text.strip()

    if len(root):  # It has children.
        # FIXME: Configuration item cannot have both attributes and
        # values (list) at the same time in current implementation:
        tree[root.tag]["children"] = [
            etree_to_container(c, container) for c in root
        ]

    return tree
def dump_impl(data, config_fp):
    """TODO: How to encode nested dicts?
    """
    p = pyjavaproperties.Properties()
    for k, v in iteritems(data):
        p.setProperty(k, v)

    p.store(config_fp)
Exemple #10
0
def mk_lines_g(data):
    """
    Make lines from given `data`
    """
    has_default = "DEFAULT" in data

    def is_inherited_from_default(key, val):
        """
        :return: True if (key, val) pair in defaults.
        """
        return has_default and data["DEFAULT"].get(key, None) == val

    for sect, params in iteritems(data):
        yield "[%s]\n" % sect

        for key, val in iteritems(params):
            if sect != "DEFAULT" and is_inherited_from_default(key, val):
                continue

            yield "%s = %s\n" % (key, _to_s(val))

        yield "\n"  # put an empty line just after each sections.
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
Exemple #12
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
Exemple #13
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
Exemple #14
0
def _load(filepath=None, stream=None, sep=_SEP, **kwargs):
    """
    :param filepath: Config file path
    :param stream: File or file-like object provides ini-style conf
    :param sep: Seprator string

    :return: Dict or dict-like object represents config values
    """
    # 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)

    container = dict
    cnf = container()
    (fname, arg) = _switch_read_fn_arg(filepath, stream)
    getattr(parser, fname)(arg, **kwargs_1)

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

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

    return cnf