anyconfig.backend.base.parsers¶
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
- class anyconfig.backend.base.parsers.Parser¶
Bases:
LoaderMixin,DumperMixin,ProcessorAbstract parser to provide basic implementation.
The following members will be expected to be overridden.
_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
anyconfig.models.processor.Processor
- class anyconfig.backend.base.parsers.StringParser¶
Bases:
Parser,FromStringLoaderMixin,ToStringDumperMixinAbstract 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.parsers.StreamParser¶
Bases:
Parser,FromStreamLoaderMixin,ToStreamDumperMixinAbstract parser based on the following methods.
load_from_stream()dump_to_stream().
Parser classes inherit this class must define these methods.
- anyconfig.backend.base.parsers.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[...,Union[None,int,float,bool,str,dict[str,Any]]] |None) – Callable to load datacontent_or_strm (
str|bytes|IO) – data content or stream provides itcontainer (
Callable[...,dict[str,Any]]) – callble to make a container objectallow_primitives (
bool) – True if the parser.load* may return objects of primitive data types other than mapping types such like JSON parseroptions (
dict[str,Any]) – keyword options passed to ‘load_fn’
- Return type:
Union[None,int,float,bool,str,dict[str,Any]]- Returns:
container object holding data
- anyconfig.backend.base.parsers.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[...,str] |None) – Callable to dump datadata (
Union[None,int,float,bool,str,dict[str,Any]]) – Data to dumpstream (
IO|None) – File or file like object or Noneoptions (
dict[str,Any]) – optional keyword parameters
- Return type:
str- Returns:
String represents data if stream is None or None
- class anyconfig.backend.base.parsers.StringStreamFnParser¶
Bases:
Parser,FromStreamLoaderMixin,ToStreamDumperMixinAbstract 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|bytes) – Configuration stringcontainer (
Callable[...,dict[str,Any]]) – callble to make a container objectoptions (
dict[str,Any]) – keyword options passed to ‘_load_from_string_fn’
- Return type:
Union[None,int,float,bool,str,dict[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 datacontainer (
Callable[...,dict[str,Any]]) – callble to make a container objectoptions (
dict[str,Any]) – keyword options passed to ‘_load_from_stream_fn’
- Return type:
Union[None,int,float,bool,str,dict[str,Any]]- Returns:
container object holding the configuration data
- dump_to_string(cnf, **options)¶
Dump config ‘cnf’ to a string.
- Parameters:
cnf (
Union[None,int,float,bool,str,dict[str,Any]]) – Configuration data to dumpoptions (
dict[str,Any]) – optional keyword parameters to be sanitized :: dict
- Return type:
str- Returns:
string represents the configuration
- dump_to_stream(cnf, stream, **options)¶
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,dict[str,Any]]) – Configuration data to dumpstream (
IO) – Config file or file like objectoptions (
dict[str,Any]) – optional keyword parameters to be sanitized :: dict
- Return type:
None