anyconfig.backend.base

Abstract implementation of backend modules.

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.

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
anyconfig.backend.base.mk_opt_args(keys, kwargs)

Make optional kwargs valid and optimized for each backend.

Parameters:
  • keys – optional argument names
  • kwargs – keyword arguements to process
>>> mk_opt_args(("aaa", ), dict(aaa=1, bbb=2))
{'aaa': 1}
>>> mk_opt_args(("aaa", ), dict(bbb=2))
{}
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.Parser

Bases: object

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

container

alias of MergeableDict

classmethod to_container(obj)

Create an instance from any object.

classmethod type()

Parser’s type

classmethod priority()

Parser’s priority

classmethod extensions()

File extensions which this parser can process

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
load_from_string(content, **kwargs)

Load config from given string content.

Parameters:
  • content – Config content string
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load_from_path(filepath, **kwargs)

Load config from given file path filepath.

Parameters:
  • filepath – Config file path
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load_from_stream(stream, **kwargs)

Load config from given file like object stream.

Parameters:
  • stream – Config file or file like object
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

loads(content, **kwargs)

Load config from given string content after some checks.

Parameters:
  • content – Config file content
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load(path_or_stream, ignore_missing=False, **kwargs)

Load config from a file path or a file / file-like object path_or_stream after some checks.

Parameters:
  • path_or_stream – Config file path or file{,-like} object
  • ignore_missing – Ignore and just return None if given path_or_stream is not a file / file-like object (thus, it should be a file path) and does not exist in actual
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

dump_to_string(cnf, **kwargs)

Dump config cnf to a string.

Parameters:
  • cnf – Configuration data to dump :: self.container
  • 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 :: self.container
  • 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 :: self.container
  • 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 :: self.container
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

string represents the configuration

dump(cnf, path_or_stream, **kwargs)

Dump config cnf to a filepath or file-like object path_or_stream.

Parameters:
  • cnf – Configuration data to dump :: self.container
  • path_or_stream – Config file path or file{,-like} object
  • kwargs – optional keyword parameters to be sanitized :: dict
Raises IOError, OSError, AttributeError:
 

When dump failed.

class anyconfig.backend.base.FromStringLoader

Bases: anyconfig.backend.base.Parser

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, **kwargs)

Load config from given stream stream.

Parameters:
  • stream – Config file or file-like object
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load_from_path(filepath, **kwargs)

Load config from given file path filepath.

Parameters:
  • filepath – Config file path
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

class anyconfig.backend.base.FromStreamLoader

Bases: anyconfig.backend.base.Parser

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, **kwargs)

Load config from given string cnf_content.

Parameters:
  • content – Config content string
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load_from_path(filepath, **kwargs)

Load config from given file path filepath.

Parameters:
  • filepath – Config file path
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

class anyconfig.backend.base.ToStringDumper

Bases: anyconfig.backend.base.Parser

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 :: self.container
  • 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 :: self.container
  • stream – Config file or file like object
  • kwargs – optional keyword parameters to be sanitized :: dict
class anyconfig.backend.base.ToStreamDumper

Bases: anyconfig.backend.base.Parser

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.

to_stream(*args, **kwargs)

StringIO([s]) – Return a StringIO-like stream for reading or writing

dump_to_string(cnf, **kwargs)

Dump config cnf to a string.

Parameters:
  • cnf – Configuration data to dump :: self.container
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

dump_to_path(cnf, filepath, **kwargs)

Dump config cnf to a file filepath.

Parameters:
  • cnf – Configuration data to dump :: self.container
  • filepath – Config file path
  • kwargs – optional keyword parameters to be sanitized :: dict

Previous topic

anyconfig.backend

Next topic

anyconfig.backend.bson

This Page