anyconfig.backend.base

Abstract implementation of backend modules:

Backend module must implement a parser class inherits Parser or its children classes of this module and override all or some of the methods as needed:

  • load_from_string(): Load config from string

  • load_from_stream(): Load config from a file or file-like object

  • load_from_path(): Load config from file of given path

  • dump_to_string(): Dump config as a string

  • dump_to_stream(): Dump config to a file or file-like object

  • dump_to_path(): Dump config to a file of given path

Changelog:

    Changed in version 0.9.5:
  • Make Parser inherited from Processor

  • introduce the member _allow_primitives and the class method allow_primitives to Parser to allow parsers to load and return data of primitive types other than mapping objects

    Changed in version 0.9.1:
  • Rename the member _dict_options to _dict_opts to make consistent w/ other members such as _load_opts.

    Changed in version 0.8.3:
  • Add _ordered membmer and a class method :meth:` ordered to Parser.

  • Add _dict_options member to the class Parser.

    Changed in version 0.2:
  • The methods load_impl(), dump_impl() are deprecated and replaced with load_from_stream() and load_from_path(), dump_to_string() and dump_to_path() respectively.

anyconfig.backend.base.ensure_outdir_exists(filepath)

Make dir to dump filepath if that dir does not exist.

Parameters

filepath – path of file to dump

anyconfig.backend.base.to_method(func)

Lift func() to a method; it will be called with the first argument self ignored.

Parameters

func – Any callable object

class anyconfig.backend.base.TextFilesMixin

Bases: object

Mixin class to open configuration files as a plain text.

Arguments of open() is different depends on python versions.

classmethod ropen(filepath, **kwargs)
Parameters

filepath – Path to file to open to read data

classmethod wopen(filepath, **kwargs)
Parameters

filepath – Path to file to open to write data to

class anyconfig.backend.base.BinaryFilesMixin

Bases: anyconfig.backend.base.TextFilesMixin

Mixin class to open binary (byte string) configuration files.

class anyconfig.backend.base.LoaderMixin

Bases: object

Mixin class to load data.

Inherited classes must implement the following methods.

Member variables:

  • _load_opts: Backend specific options on load

  • _ordered: True if the parser keep the order of items by default

  • _allow_primitives: True if the parser.load* may return objects of primitive data types other than mapping types such like JSON parser

  • _dict_opts: Backend options to customize dict class to make results

classmethod ordered()
Returns

True if parser can keep the order of keys else False.

classmethod allow_primitives()
Returns

True if the parser.load* may return objects of primitive data

types other than mapping types such like JSON parser

classmethod dict_options()
Returns

List of dict factory options

load_from_string(content, container, **kwargs)

Load config from given string content.

Parameters
  • content – Config content string

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path filepath.

Parameters
  • filepath – Config file path

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

load_from_stream(stream, container, **kwargs)

Load config from given file like object stream.

Parameters
  • stream – Config file or file like object

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

loads(content, **options)

Load config from given string content after some checks.

Parameters
  • content – Config file content

  • options – options will be passed to backend specific loading functions. please note that options have to be sanitized w/ filter_options() later to filter out options not in _load_opts.

Returns

dict or dict-like object holding configurations

load(ioi, ac_ignore_missing=False, **options)

Load config from a file path or a file / file-like object which ioi refering after some checks.

Parameters
  • ioi~anyconfig.globals.IOInfo namedtuple object provides various info of input object to load data from

  • ac_ignore_missing – Ignore and just return empty result if given input_ does not exist in actual.

  • options – options will be passed to backend specific loading functions. please note that options have to be sanitized w/ filter_options() later to filter out options not in _load_opts.

Returns

dict or dict-like object holding configurations

class anyconfig.backend.base.DumperMixin

Bases: object

Mixin class to dump data.

Inherited classes must implement the following methods.

Member variables:

  • _dump_opts: Backend specific options on dump

dump_to_string(cnf, **kwargs)

Dump config cnf to a string.

Parameters
  • cnf – Configuration data to dump

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

string represents the configuration

dump_to_path(cnf, filepath, **kwargs)

Dump config cnf to a file filepath.

Parameters
  • cnf – Configuration data to dump

  • filepath – Config file path

  • kwargs – optional keyword parameters to be sanitized :: dict

dump_to_stream(cnf, stream, **kwargs)

Dump config cnf to a file-like object stream.

TODO: How to process socket objects same as file objects ?

Parameters
  • cnf – Configuration data to dump

  • stream – Config file or file like object

  • kwargs – optional keyword parameters to be sanitized :: dict

dumps(cnf, **kwargs)

Dump config cnf to a string.

Parameters
  • cnf – Configuration data to dump

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

string represents the configuration

dump(cnf, ioi, **kwargs)

Dump config cnf to output object of which ioi refering.

Parameters
  • cnf – Configuration data to dump

  • ioi~anyconfig.globals.IOInfo namedtuple object provides various info of input object to load data from

  • kwargs – optional keyword parameters to be sanitized :: dict

Raises

IOError, OSError, AttributeError – When dump failed.

class anyconfig.backend.base.Parser

Bases: anyconfig.backend.base.TextFilesMixin, anyconfig.backend.base.LoaderMixin, anyconfig.backend.base.DumperMixin, anyconfig.processors.Processor

Abstract parser to provide basic implementation of some methods, interfaces and members.

  • _type: Parser type indicate which format it supports

  • _priority: Priority to select it if there are other parsers of same type

  • _extensions: File extensions of formats it supports

  • _open_flags: Opening flags to read and write files

See also

the doc of Processor

class anyconfig.backend.base.FromStringLoaderMixin

Bases: anyconfig.backend.base.LoaderMixin

Abstract config parser provides a method to load configuration from string content to help implement parser of which backend lacks of such function.

Parser classes inherit this class have to override the method load_from_string() at least.

load_from_stream(stream, container, **kwargs)

Load config from given stream stream.

Parameters
  • stream – Config file or file-like object

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path filepath.

Parameters
  • filepath – Config file path

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

class anyconfig.backend.base.FromStreamLoaderMixin

Bases: anyconfig.backend.base.LoaderMixin

Abstract config parser provides a method to load configuration from string content to help implement parser of which backend lacks of such function.

Parser classes inherit this class have to override the method load_from_stream() at least.

load_from_string(content, container, **kwargs)

Load config from given string cnf_content.

Parameters
  • content – Config content string

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path filepath.

Parameters
  • filepath – Config file path

  • container – callble to make a container object later

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

class anyconfig.backend.base.ToStringDumperMixin

Bases: anyconfig.backend.base.DumperMixin

Abstract config parser provides a method to dump configuration to a file or file-like object (stream) and a file of given path to help implement parser of which backend lacks of such functions.

Parser classes inherit this class have to override the method dump_to_string() at least.

dump_to_path(cnf, filepath, **kwargs)

Dump config cnf to a file filepath.

Parameters
  • cnf – Configuration data to dump

  • filepath – Config file path

  • kwargs – optional keyword parameters to be sanitized :: dict

dump_to_stream(cnf, stream, **kwargs)

Dump config cnf to a file-like object stream.

TODO: How to process socket objects same as file objects ?

Parameters
  • cnf – Configuration data to dump

  • stream – Config file or file like object

  • kwargs – optional keyword parameters to be sanitized :: dict

class anyconfig.backend.base.ToStreamDumperMixin

Bases: anyconfig.backend.base.DumperMixin

Abstract config parser provides methods to dump configuration to a string content or a file of given path to help implement parser of which backend lacks of such functions.

Parser classes inherit this class have to override the method dump_to_stream() at least.

dump_to_string(cnf, **kwargs)

Dump config cnf to a string.

Parameters
  • cnf – Configuration data to dump

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

Dict-like object holding config parameters

dump_to_path(cnf, filepath, **kwargs)

Dump config cnf to a file filepath.

Parameters
  • cnf – Configuration data to dump

  • filepath – Config file path

  • kwargs – optional keyword parameters to be sanitized :: dict

class anyconfig.backend.base.StringParser

Bases: anyconfig.backend.base.Parser, anyconfig.backend.base.FromStringLoaderMixin, anyconfig.backend.base.ToStringDumperMixin

Abstract parser based on load_from_string() and dump_to_string().

Parser classes inherit this class must define these methods.

class anyconfig.backend.base.StreamParser

Bases: anyconfig.backend.base.Parser, anyconfig.backend.base.FromStreamLoaderMixin, anyconfig.backend.base.ToStreamDumperMixin

Abstract parser based on load_from_stream() and dump_to_stream().

Parser classes inherit this class must define these methods.

anyconfig.backend.base.load_with_fn(load_fn, content_or_strm, container, allow_primitives=False, **options)

Load data from given string or stream content_or_strm.

Parameters
  • load_fn – Callable to load data

  • content_or_strm – data content or stream provides it

  • container – callble to make a container object

  • allow_primitives – True if the parser.load* may return objects of primitive data types other than mapping types such like JSON parser

  • options – keyword options passed to load_fn

Returns

container object holding data

anyconfig.backend.base.dump_with_fn(dump_fn, data, stream, **options)

Dump data to a string if stream is None, or dump data to a file or file-like object stream.

Parameters
  • dump_fn – Callable to dump data

  • data – Data to dump

  • stream – File or file like object or None

  • options – optional keyword parameters

Returns

String represents data if stream is None or None

class anyconfig.backend.base.StringStreamFnParser

Bases: anyconfig.backend.base.Parser, anyconfig.backend.base.FromStreamLoaderMixin, anyconfig.backend.base.ToStreamDumperMixin

Abstract parser utilizes load and dump functions each backend module provides such like json.load{,s} and json.dump{,s} in JSON backend.

Parser classes inherit this class must define the followings.

  • _load_from_string_fn: Callable to load data from string

  • _load_from_stream_fn: Callable to load data from stream (file object)

  • _dump_to_string_fn: Callable to dump data to string

  • _dump_to_stream_fn: Callable to dump data to stream (file object)

Note

Callables have to be wrapped with to_method() to make self passed to the methods created from them ignoring it.

Seealso

anyconfig.backend.json.Parser

load_from_string(content, container, **options)

Load configuration data from given string content.

Parameters
  • content – Configuration string

  • container – callble to make a container object

  • options – keyword options passed to _load_from_string_fn

Returns

container object holding the configuration data

load_from_stream(stream, container, **options)

Load data from given stream stream.

Parameters
  • stream – Stream provides configuration data

  • container – callble to make a container object

  • options – keyword options passed to _load_from_stream_fn

Returns

container object holding the configuration data

dump_to_string(cnf, **kwargs)

Dump config cnf to a string.

Parameters
  • cnf – Configuration data to dump

  • kwargs – optional keyword parameters to be sanitized :: dict

Returns

string represents the configuration

dump_to_stream(cnf, stream, **kwargs)

Dump config cnf to a file-like object stream.

TODO: How to process socket objects same as file objects ?

Parameters
  • cnf – Configuration data to dump

  • stream – Config file or file like object

  • kwargs – optional keyword parameters to be sanitized :: dict