How to use the anyconfig.backend 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 / tests / api.py View on Github external
def test_14__single_load__ac_parser_by_id(self):
        cid = anyconfig.backend.json.Parser.cid()
        res = TT.single_load(self.a_path, ac_parser=cid)
        self.assert_dicts_equal(res, self.cnf)
github ssato / python-anyconfig / src / anyconfig / backend / yaml / pyyaml.py View on Github external
return yml_fnc("dump", data, stream, **options)


class Parser(common.Parser):
    """
    Parser for YAML files.
    """
    _cid = "pyyaml"
    _priority = 30  # Higher priority than ruamel.yaml.
    _load_opts = ["Loader", "ac_safe", "ac_dict"]
    _dump_opts = ["stream", "ac_safe", "Dumper", "default_style",
                  "default_flow_style", "canonical", "indent", "width",
                  "allow_unicode", "line_break", "encoding", "explicit_start",
                  "explicit_end", "version", "tags"]

    load_from_stream = anyconfig.backend.base.to_method(yml_load)
    dump_to_stream = anyconfig.backend.base.to_method(yml_dump)
github ssato / python-anyconfig / src / anyconfig / backend / json / default.py View on Github external
import anyconfig.backend.base

from .common import Parser as BaseParser


class Parser(BaseParser):
    """
    Parser for JSON files.
    """
    _cid = "std.json"
    _priority = 30  # Higher priority than others.

    _load_from_string_fn = anyconfig.backend.base.to_method(json.loads)
    _load_from_stream_fn = anyconfig.backend.base.to_method(json.load)
    _dump_to_string_fn = anyconfig.backend.base.to_method(json.dumps)
    _dump_to_stream_fn = anyconfig.backend.base.to_method(json.dump)
github ssato / python-anyconfig / anyconfig / backend / backends.py View on Github external
from __future__ import absolute_import

import itertools
import logging
import operator
import pkg_resources

import anyconfig.compat
import anyconfig.utils

import anyconfig.backend.ini
import anyconfig.backend.json
import anyconfig.backend.xml

LOGGER = logging.getLogger(__name__)
PARSERS = [anyconfig.backend.ini.Parser, anyconfig.backend.json.Parser,
           anyconfig.backend.xml.Parser]

try:
    import anyconfig.backend.yaml
    PARSERS.append(anyconfig.backend.yaml.Parser)
except ImportError:
    LOGGER.warn("YAML module is not available. Disabled its support.")


for e in pkg_resources.iter_entry_points("anyconfig_backends"):
    try:
        PARSERS.append(e.load())
    except ImportError:
        continue
github ssato / python-anyconfig / anyconfig / backend / bson.py View on Github external
_CO_OPTIONS = ("document_class", "tz_aware", "uuid_representation",
               "unicode_decode_error_handler", "tzinfo")


def _codec_options(**options):
    """
    bson.BSON.{decode{,_all},encode} can receive bson.CodecOptions.

    :return: :class:`~bson.CodecOptions`
    """
    opts = anyconfig.utils.filter_options(_CO_OPTIONS, options)
    return bson.CodecOptions(**opts)


class Parser(anyconfig.backend.base.StringParser,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Loader/Dumper of BSON files.
    """
    _type = "bson"
    _extensions = ["bson", "bsn"]  # Temporary.
    _load_opts = [] if bson.has_c() else ["codec_options"]
    _dump_opts = [] if bson.has_c() else ["check_keys", "codec_options"]
    _ordered = not bson.has_c()
    _dict_opts = [] if bson.has_c() else ["document_class"]

    def _load_options(self, container, **options):
        """
        :param container: callble to make a container object later
        """
        if "codec_options" not in options:
github ssato / python-anyconfig / anyconfig / backend / winreg.py View on Github external
"""
    Decode :class:`Registry.Registry.RegistryKey` object.

    :param rkey: :class:`Registry.Registry.RegistryKey` object
    :param to_container: any callable to make container

    :return: Dict or dict-like object represents this object
    """
    skeys = [decode(sk, to_container) for sk in rkey.subkeys()]
    vals = [decode_0(v, to_container) for v in rkey.values()]
    val = to_container(subkeys=skeys, values=vals,
                       timestamp=str(rkey.timestamp()))
    return to_container({rkey.path(): val})


class Parser(anyconfig.backend.base.FromStreamLoader):
    """
    Windows registry files parser.
    """
    _type = "registry"
    _extensions = [".reg"]
    _open_flags = ('rb', 'wb')

    def load_from_stream(self, stream, to_container, **options):
        """
        Load config from given file like object `stream`.

        :param stream:  Config file or file like object
        :param to_container: callble to make a container object
        :param options: optional keyword arguments

        :return: Dict-like object holding config parameters
github ssato / python-anyconfig / anyconfig / backend / configobj.py View on Github external
return cobj


def load(path_or_strm, container, **opts):
    """
    :param path_or_strm: input config file path or file/file-like object
    :param container: callble to make a container object
    :param opts: keyword options passed to :class:`configobj.ConfigObj`

    :return: Mapping object
    """
    return container(configobj.ConfigObj(path_or_strm, **opts))


class Parser(anyconfig.backend.base.StreamParser,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Parser for Ini-like config files which configobj supports.
    """
    _cid = "configobj"
    _type = "configobj"
    _priority = 10
    _load_opts = _LOAD_OPTS  # options on dump will be just ignored.
    _dump_opts = _LOAD_OPTS  # Likewise.
    _ordered = True

    load_from_path = load_from_stream = anyconfig.backend.base.to_method(load)

    def dump_to_string(self, cnf, **kwargs):
        """
        Dump config 'cnf' to a string.
github ssato / python-anyconfig / anyconfig / backend / configobj.py View on Github external
return container(configobj.ConfigObj(path_or_strm, **opts))


class Parser(anyconfig.backend.base.StreamParser,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Parser for Ini-like config files which configobj supports.
    """
    _cid = "configobj"
    _type = "configobj"
    _priority = 10
    _load_opts = _LOAD_OPTS  # options on dump will be just ignored.
    _dump_opts = _LOAD_OPTS  # Likewise.
    _ordered = True

    load_from_path = load_from_stream = anyconfig.backend.base.to_method(load)

    def dump_to_string(self, cnf, **kwargs):
        """
        Dump config 'cnf' to a string.

        :param cnf: Configuration data to dump
        :param kwargs: backend-specific optional keyword parameters :: dict

        :return: string represents the configuration
        """
        return os.linesep.join(make_configobj(cnf, **kwargs).write())

    def dump_to_stream(self, cnf, stream, **kwargs):
        """
        :param cnf: Configuration data to dump
        :param stream: Config file or file-like object
github ssato / python-anyconfig / src / anyconfig / backend / pickle.py View on Github external
except ImportError:
    import pickle

import anyconfig.backend.base
import anyconfig.compat


if anyconfig.compat.IS_PYTHON_3:
    LOAD_OPTS = ["fix_imports", "encoding", "errors"]
    DUMP_OPTS = ["protocol", "fix_imports"]
else:
    LOAD_OPTS = []
    DUMP_OPTS = ["protocol"]


class Parser(anyconfig.backend.base.StringStreamFnParser,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Parser for Pickle files.
    """
    _cid = "pickle"
    _type = "pickle"
    _extensions = ["pkl", "pickle"]
    _load_opts = LOAD_OPTS
    _dump_opts = DUMP_OPTS
    _allow_primitives = True

    _load_from_string_fn = anyconfig.backend.base.to_method(pickle.loads)
    _load_from_stream_fn = anyconfig.backend.base.to_method(pickle.load)
    _dump_to_string_fn = anyconfig.backend.base.to_method(pickle.dumps)
    _dump_to_stream_fn = anyconfig.backend.base.to_method(pickle.dump)