How to use the anyconfig.compat.iteritems function in anyconfig

To help you get started, we’ve selected a few anyconfig examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ssato / python-anyconfig / src / anyconfig / backend / shellvars.py View on Github external
def dump_to_stream(self, cnf, stream, **kwargs):
        """
        Dump config 'cnf' to a file or file-like object 'stream'.

        :param cnf: Shell variables data to dump
        :param stream: Shell script file or file like object
        :param kwargs: backend-specific optional keyword parameters :: dict
        """
        for key, val in anyconfig.compat.iteritems(cnf):
            stream.write("%s='%s'%s" % (key, val, os.linesep))
github ssato / python-anyconfig / src / anyconfig / backend / properties.py View on Github external
def dump_to_stream(self, cnf, stream, **kwargs):
        """
        Dump config 'cnf' to a file or file-like object 'stream'.

        :param cnf: Java properties config data to dump
        :param stream: Java properties file or file like object
        :param kwargs: backend-specific optional keyword parameters :: dict
        """
        for key, val in anyconfig.compat.iteritems(cnf):
            stream.write("%s = %s%s" % (key, escape(val), os.linesep))
github ssato / python-anyconfig / src / anyconfig / backend / ini.py View on Github external
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.
github ssato / python-anyconfig / src / anyconfig / backend / xml.py View on Github external
def _elem_set_attrs(obj, parent, to_str):
    """
    :param obj: Container instance gives attributes of XML Element
    :param parent: XML ElementTree parent node object
    :param to_str: Callable to convert value to string or None
    :param options: Keyword options, see :func:`container_to_etree`

    :return: None but parent will be modified
    """
    for attr, val in anyconfig.compat.iteritems(obj):
        parent.set(attr, to_str(val))
github ssato / python-anyconfig / anyconfig / mergeabledict.py View on Github external
:param opts: Extra optional keyword arguments such as ac_ordered:

        - ac_ordered: Create an OrderedDict object instead of dict if True

    :return: A dict or namedtuple object if to_namedtuple is True
    """
    cls = OrderedDict if opts.get("ac_ordered", False) else dict
    if is_dict_like(obj):
        if to_namedtuple:
            _name = obj.get(_ac_ntpl_cls_key, "NamedTuple")
            _keys = [k for k in obj.keys() if k != _ac_ntpl_cls_key]
            _vals = [convert_to(obj[k], to_namedtuple, _ac_ntpl_cls_key,
                                **opts) for k in _keys]
            return collections.namedtuple(_name, _keys)(*_vals)
        else:
            return cls((k, convert_to(v, **opts)) for k, v in iteritems(obj))
    elif is_namedtuple(obj):
        if to_namedtuple:
            return obj  # Nothing to do if it's nested n.t. (it should be).
        else:
            return cls((k, convert_to(getattr(obj, k))) for k in obj._fields)
    elif is_iterable(obj):
        return type(obj)(convert_to(v, to_namedtuple, _ac_ntpl_cls_key, **opts)
                         for v in obj)
    else:
        return obj
github ssato / python-anyconfig / anyconfig / backend / properties_.py View on Github external
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)
github ssato / python-anyconfig / src / anyconfig / backend / xml.py View on Github external
- tags: Dict of tags for special nodes to keep XML info, attributes,
          text and children nodes, e.g. {"attrs": "@attrs", "text": "#text"}
    """
    if to_str is None:
        to_str = _to_str_fn(**options)

    if not anyconfig.utils.is_dict_like(obj):
        if parent is not None and obj:
            parent.text = to_str(obj)  # Parent is a leaf text node.
        return parent  # All attributes and text should be set already.

    options = _complement_tag_options(options)
    (attrs, text, children) = operator.itemgetter(*_ATC)(options)

    for key, val in anyconfig.compat.iteritems(obj):
        if key == attrs:
            _elem_set_attrs(val, parent, to_str)
        elif key == text:
            parent.text = to_str(val)
        elif key == children:
            for celem in _elem_from_descendants(val, **options):
                parent.append(celem)
        else:
            parent = _get_or_update_parent(key, val, to_str, parent=parent,
                                           **options)

    return ET.ElementTree(parent)
github ssato / python-anyconfig / src / anyconfig / backend / ini.py View on Github external
: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
github ssato / python-anyconfig / anyconfig / mergeabledict.py View on Github external
if ac_merge not in MERGE_STRATEGIES:
        raise ValueError("Wrong merge strategy: %r" % ac_merge)

    if getattr(options, "ac_namedtuple", False):
        ac_ordered = True  # To keep the order of items.

    cls = _get_mdict_class(ac_merge=ac_merge, ac_ordered=ac_ordered)
    if obj is None:
        return cls()

    opts = dict(ac_ordered=ac_ordered, _ac_ntpl_cls_key=_ac_ntpl_cls_key)
    opts.update(options)

    if is_dict_like(obj):
        return cls((k, None if v is None else create_from(v, **opts)) for k, v
                   in iteritems(obj))
    elif is_namedtuple(obj):
        ocls = _MDICTS_CLASS_MAP[cls]
        mdict = ocls((k, create_from(getattr(obj, k), **opts)) for k
                     in obj._fields)
        mdict[_ac_ntpl_cls_key] = obj.__class__.__name__
        return mdict
    elif is_iterable(obj):
        return type(obj)(create_from(v, **opts) for v in obj)
    else:
        return obj