anyconfig.backend.base

Backend basic classes, functions and constants.

class anyconfig.backend.base.BinaryFilesMixin

Bases: object

Mixin class to open configuration files as a binary data.

classmethod ropen(filepath, **kwargs)

Open filepath with read only mode.

Parameters:

filepath – Path to file to open to read data

classmethod wopen(filepath, **kwargs)

Open filepath with write mode.

Parameters:

filepath – Path to file to open to write data to

anyconfig.backend.base.IoiT

alias of IOInfo

class anyconfig.backend.base.ToStringDumperMixin

Bases: DumperMixin

Abstract config parser provides the followings.

  • 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 (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

  • filepath (str) – Config file path

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

Return type:

None

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 (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

  • stream (IO) – Config file or file like object

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

Return type:

None

class anyconfig.backend.base.ToStreamDumperMixin

Bases: DumperMixin

Abstract config parser provides the following 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 (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

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

Return type:

str

Returns:

Dict-like object holding config parameters

dump_to_path(cnf, filepath, **kwargs)

Dump config ‘cnf’ to a file ‘filepath`.

Parameters:
  • cnf (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

  • filepath (str) – Config file path

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

Return type:

None

class anyconfig.backend.base.BinaryDumperMixin

Bases: DumperMixin

Mixin class to dump binary (byte string) configuration data.

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

  • _open_read_mode: Backend option to specify read mode passed to open()

classmethod ordered()

Test if the parser keeps the order of the data.

Return type:

bool

classmethod allow_primitives()

Test if the paresr allows to hold primitive data.

Return type:

bool

Returns:

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

classmethod dict_options()

Get the list of dict factory options.

Return type:

List[str]

ropen(filepath, **kwargs)

Open files with read only mode.

load_from_string(content, container, **kwargs)

Load config from given string ‘content’.

Parameters:
  • content (str) – Config content string

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path ‘filepath`.

Parameters:
  • filepath (str) – Config file path

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

load_from_stream(stream, container, **kwargs)

Load config from given file like object ‘stream`.

Parameters:
  • stream (IO) – Config file or file like object

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

loads(content, **options)

Load config from given string ‘content’ after some checks.

Parameters:
  • content (str) – Config file content

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

dict or dict-like object holding configurations

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

Load config from ioi.

Parameters:
  • ioi (IOInfo) – ‘anyconfig.ioinfo.IOInfo’ namedtuple object provides various info of input object to load data from

  • ac_ignore_missing (bool) – Ignore and just return empty result if given ioi object does not exist in actual.

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

dict or dict-like object holding configurations

class anyconfig.backend.base.FromStringLoaderMixin

Bases: LoaderMixin

Abstract parser provides a method below.

  • amethod 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 (IO) – Config file or file-like object

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path ‘filepath’.

Parameters:
  • filepath (str) – Config file path

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

class anyconfig.backend.base.FromStreamLoaderMixin

Bases: LoaderMixin

Abstract parser provides a method below.

  • 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 (str) – Config content string

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

load_from_path(filepath, container, **kwargs)

Load config from given file path ‘filepath’.

Parameters:
  • filepath (str) – Config file path

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object later

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

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

Dict-like object holding config parameters

class anyconfig.backend.base.BinaryLoaderMixin

Bases: LoaderMixin

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

anyconfig.backend.base.ensure_outdir_exists(filepath)

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

Parameters:

filepath (Union[str, Path]) – path of file to dump

Return type:

None

anyconfig.backend.base.to_method(func)

Lift func() to a method.

It will be called with the first argument ‘self’ ignored.

Parameters:

func (Callable[..., Any]) – Any callable object

Return type:

Callable[..., Any]

class anyconfig.backend.base.Parser

Bases: LoaderMixin, DumperMixin, Processor

Abstract parser to provide basic implementation of some methods as below.

  • _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

class anyconfig.backend.base.StringParser

Bases: Parser, FromStringLoaderMixin, ToStringDumperMixin

Abstract parser based on the following methods.

  • load_from_string()

  • dump_to_string().

Parser classes inherit this class must define these methods.

class anyconfig.backend.base.StreamParser

Bases: Parser, FromStreamLoaderMixin, ToStreamDumperMixin

Abstract parser based on the following methods.

  • load_from_stream()

  • dump_to_stream().

Parser classes inherit this class must define these methods.

class anyconfig.backend.base.StringStreamFnParser

Bases: Parser, FromStreamLoaderMixin, ToStreamDumperMixin

Abstract parser utilizes load and dump functions.

Each backend module should provide functions 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 (str) – Configuration string

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object

  • options – keyword options passed to ‘_load_from_string_fn’

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

container object holding the configuration data

load_from_stream(stream, container, **options)

Load data from given stream ‘stream’.

Parameters:
  • stream (IO) – Stream provides configuration data

  • container (Callable[..., Mapping[str, Any]]) – callble to make a container object

  • options – keyword options passed to ‘_load_from_stream_fn’

Return type:

Union[None, int, float, bool, str, Mapping[str, Any]]

Returns:

container object holding the configuration data

dump_to_string(cnf, **kwargs)

Dump config ‘cnf’ to a string.

Parameters:
  • cnf (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

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

Return type:

str

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 (Union[None, int, float, bool, str, Mapping[str, Any]]) – Configuration data to dump

  • stream (IO) – Config file or file like object

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

Return type:

None