How to use the anyconfig.backend.base 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 / properties.py View on Github external
if line.endswith("\\"):
            prev += line.rstrip(" \\")
            continue

        (key, val) = _parseline(line)
        if key is None:
            LOGGER.warning("Failed to parse the line: %s", line)
            continue

        ret[key] = unescape(val)

    return ret


class Parser(anyconfig.backend.base.StreamParser):
    """
    Parser for Java properties files.
    """
    _cid = "properties"
    _type = "properties"
    _extensions = ["properties"]
    _ordered = True
    _dict_opts = ["ac_dict"]

    def load_from_stream(self, stream, container, **kwargs):
        """
        Load config from given file like object 'stream'.

        :param stream: A file or file like object of Java properties files
        :param container: callble to make a container object
        :param kwargs: optional keyword parameters (ignored)
github ssato / python-anyconfig / src / anyconfig / backend / shellvars.py View on Github external
for line in stream.readlines():
        line = line.rstrip()
        if line is None or not line:
            continue

        (key, val) = _parseline(line)
        if key is None:
            LOGGER.warning("Empty val in the line: %s", line)
            continue

        ret[key] = val

    return ret


class Parser(anyconfig.backend.base.StreamParser):
    """
    Parser for Shell variable definition files.
    """
    _cid = "shellvars"
    _type = "shellvars"
    _ordered = True
    _dict_opts = ["ac_dict"]

    def load_from_stream(self, stream, container, **kwargs):
        """
        Load config from given file like object 'stream'.

        :param stream:
            A file or file like object of shell scripts define shell variables
        :param container: callble to make a container object
        :param kwargs: optional keyword parameters (ignored)
github ssato / python-anyconfig / src / anyconfig / backend / ini.py View on Github external
yield ''  # it will be a separator between each sections.


def _dumps(cnf, **kwargs):
    """
    :param cnf: Configuration data to dump
    :param kwargs: optional keyword parameters to be sanitized :: dict

    :return: String representation of 'cnf' object in INI format
    """
    return os.linesep.join(l for l in _dumps_itr(cnf))


class Parser(anyconfig.backend.base.Parser,
             anyconfig.backend.base.FromStreamLoaderMixin,
             anyconfig.backend.base.ToStringDumperMixin):
    """
    Ini config files parser.
    """
    _cid = "ini"
    _type = "ini"
    _extensions = ["ini"]
    _load_opts = ["defaults", "dict_type", "allow_no_value", "filename",
                  "ac_parse_value"]
    _dict_opts = ["dict_type"]

    dump_to_string = anyconfig.backend.base.to_method(_dumps)
    load_from_stream = anyconfig.backend.base.to_method(_load)
github ssato / python-anyconfig / anyconfig / backend / msgpack.py View on Github external
Changelog:

    .. versionadded:: 0.0.11
"""
from __future__ import absolute_import

import msgpack
import anyconfig.backend.base
import anyconfig.compat

from anyconfig.backend.base import to_method


class Parser(anyconfig.backend.base.StringStreamFnParser,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Loader/Dumper for MessagePack files.
    """
    _type = "msgpack"
    _extensions = []
    _load_opts = ["read_size", "use_list", "object_hook", "list_hook",
                  "encoding", "unicode_errors", "max_buffer_size", "ext_hook",
                  "max_str_len", "max_bin_len", "max_array_len", "max_map_len",
                  "max_ext_len", "object_pairs_hook"]
    _dump_opts = ["default", "encoding", "unicode_errors", "use_single_float",
                  "autoreset", "use_bin_type"]
    _ordered = not anyconfig.compat.IS_PYTHON_3  # TODO.
    _dict_opts = ["object_pairs_hook"]  # Exclusive with object_hook

    _load_from_string_fn = to_method(msgpack.unpackb)
    _load_from_stream_fn = to_method(msgpack.unpack)
github ssato / python-anyconfig / src / anyconfig / backend / pickle.py View on Github external
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)
github ssato / python-anyconfig / src / anyconfig / backend / json / default.py View on Github external
from __future__ import absolute_import

import json
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 / 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:
            options.setdefault("document_class", container)
github ssato / python-anyconfig / src / anyconfig / backend / ini.py View on Github external
yield ''  # it will be a separator between each sections.


def _dumps(cnf, **kwargs):
    """
    :param cnf: Configuration data to dump
    :param kwargs: optional keyword parameters to be sanitized :: dict

    :return: String representation of 'cnf' object in INI format
    """
    return os.linesep.join(l for l in _dumps_itr(cnf))


class Parser(anyconfig.backend.base.Parser,
             anyconfig.backend.base.FromStreamLoaderMixin,
             anyconfig.backend.base.ToStringDumperMixin):
    """
    Ini config files parser.
    """
    _cid = "ini"
    _type = "ini"
    _extensions = ["ini"]
    _load_opts = ["defaults", "dict_type", "allow_no_value", "filename",
                  "ac_parse_value"]
    _dict_opts = ["dict_type"]

    dump_to_string = anyconfig.backend.base.to_method(_dumps)
    load_from_stream = anyconfig.backend.base.to_method(_load)
github ssato / python-anyconfig / src / anyconfig / backend / json / common.py View on Github external
JSON_LOAD_OPTS = ["cls", "object_hook", "parse_float", "parse_int",
                  "parse_constant", "object_pairs_hook"]

JSON_DUMP_OPTS = ["skipkeys", "ensure_ascii", "check_circular", "allow_nan",
                  "cls", "indent", "separators", "default", "sort_keys"]

JSON_DICT_OPTS = ["object_pairs_hook", "object_hook"]

# It seems that 'encoding' argument is not allowed in json.load[s] and
# json.dump[s] in JSON module in python 3.x.
if not anyconfig.compat.IS_PYTHON_3:
    JSON_LOAD_OPTS.append("encoding")
    JSON_DUMP_OPTS.append("encoding")


class Parser(anyconfig.backend.base.StringStreamFnParser):
    """
    Parser for JSON files.
    """
    _cid = "std.json"
    _type = "json"
    _extensions = ["json", "jsn", "js"]
    _ordered = True
    _allow_primitives = True

    # .. note:: These may be overwritten.
    _load_opts = JSON_LOAD_OPTS
    _dump_opts = JSON_DUMP_OPTS
    _dict_opts = JSON_DICT_OPTS
github ssato / python-anyconfig / src / anyconfig / backend / xml.py View on Github external
def etree_write(tree, stream):
    """
    Write XML ElementTree 'root' content into 'stream'.

    :param tree: XML ElementTree object
    :param stream: File or file-like object can write to
    """
    try:
        tree.write(stream, encoding="utf-8", xml_declaration=True)
    except TypeError:
        tree.write(stream, encoding="unicode", xml_declaration=True)


class Parser(anyconfig.backend.base.Parser,
             anyconfig.backend.base.ToStreamDumperMixin,
             anyconfig.backend.base.BinaryFilesMixin):
    """
    Parser for XML files.
    """
    _cid = "xml"
    _type = "xml"
    _extensions = ["xml"]
    _load_opts = _dump_opts = ["tags", "merge_attrs", "ac_parse_value"]
    _ordered = True
    _dict_opts = ["ac_dict"]

    def load_from_string(self, content, container, **opts):
        """
        Load config from XML snippet (a string 'content').