anyconfig.backends

A module to aggregate config parser (loader/dumper) backends.

exception anyconfig.backends.UnknownParserTypeError(forced_type)

Bases: exceptions.RuntimeError

Raise if no parsers were found for given type.

exception anyconfig.backends.UnknownFileTypeError(path)

Bases: exceptions.RuntimeError

Raise if not parsers were found for given file path.

anyconfig.backends.fst(tpl)
>>> fst((0, 1))
0
anyconfig.backends.snd(tpl)
>>> snd((0, 1))
1
anyconfig.backends.groupby_key(itr, keyfunc)

An wrapper function around itertools.groupby

Parameters:
  • itr – Iterable object, a list/tuple/genrator, etc.
  • keyfunc – Key function to sort itr.
>>> itr = [("a", 1), ("b", -1), ("c", 1)]
>>> res = groupby_key(itr, operator.itemgetter(1))
>>> [(key, tuple(grp)) for key, grp in res]
[(-1, (('b', -1),)), (1, (('a', 1), ('c', 1)))]
anyconfig.backends.is_parser(obj)
Returns:True if given obj is an instance of parser.
>>> is_parser("ini")
False
>>> is_parser(anyconfig.backend.base.Parser)
False
>>> is_parser(anyconfig.backend.base.Parser())
True
anyconfig.backends.find_by_file(path_or_stream, cps=(('bsn', [<class 'anyconfig.backend.bson.Parser'>]), ('bson', [<class 'anyconfig.backend.bson.Parser'>]), ('cbor', [<class 'anyconfig.backend.cbor.Parser'>]), ('ini', [<class 'anyconfig.backend.ini.Parser'>]), ('js', [<class 'anyconfig.backend.json.Parser'>]), ('jsn', [<class 'anyconfig.backend.json.Parser'>]), ('json', [<class 'anyconfig.backend.json.Parser'>]), ('pickle', [<class 'anyconfig.backend.pickle.Parser'>]), ('pkl', [<class 'anyconfig.backend.pickle.Parser'>]), ('properties', [<class 'anyconfig.backend.properties.Parser'>]), ('toml', [<class 'anyconfig.backend.toml.Parser'>]), ('xml', [<class 'anyconfig.backend.xml.Parser'>]), ('yaml', [<class 'anyconfig.backend.yaml.Parser'>]), ('yml', [<class 'anyconfig.backend.yaml.Parser'>])), is_path_=False)

Find config parser by the extension of file path_or_stream, file path or stream (a file or file-like objects).

Parameters:
  • path_or_stream – Config file path or file/file-like object
  • cps – A tuple of pairs of (type, parser_class) or None if you want to compute this value dynamically.
  • is_path – True if given path_or_stream is a file path
Returns:

Config Parser class found

>>> find_by_file("a.missing_cnf_ext") is None
True
>>> strm = anyconfig.compat.StringIO()
>>> find_by_file(strm) is None
True
>>> find_by_file("a.json")
<class 'anyconfig.backend.json.Parser'>
>>> find_by_file("a.json", is_path_=True)
<class 'anyconfig.backend.json.Parser'>
anyconfig.backends.find_by_type(cptype, cps=(('bson', [<class 'anyconfig.backend.bson.Parser'>]), ('cbor', [<class 'anyconfig.backend.cbor.Parser'>]), ('configobj', [<class 'anyconfig.backend.configobj.Parser'>]), ('ini', [<class 'anyconfig.backend.ini.Parser'>]), ('json', [<class 'anyconfig.backend.json.Parser'>]), ('msgpack', [<class 'anyconfig.backend.msgpack.Parser'>]), ('pickle', [<class 'anyconfig.backend.pickle.Parser'>]), ('properties', [<class 'anyconfig.backend.properties.Parser'>]), ('shellvars', [<class 'anyconfig.backend.shellvars.Parser'>]), ('toml', [<class 'anyconfig.backend.toml.Parser'>]), ('xml', [<class 'anyconfig.backend.xml.Parser'>]), ('yaml', [<class 'anyconfig.backend.yaml.Parser'>])))

Find config parser by file’s extension.

Parameters:
  • cptype – Config file’s type
  • cps – A list of pairs (type, parser_class) or None if you want to compute this value dynamically.
Returns:

Config Parser class found

>>> find_by_type("missing_type") is None
True
anyconfig.backends.find_parser(path_or_stream, forced_type=None, is_path_=False)

Find out config parser object appropriate to load from a file of given path or file/file-like object.

Parameters:
  • path_or_stream – Configuration file path or file / file-like object
  • forced_type – Forced configuration parser type
  • is_path – True if given path_or_stream is a file path
Returns:

A tuple of (Parser class or None, “” or error message)

>>> find_parser(None)  
Traceback (most recent call last):
ValueError: path_or_stream or forced_type must be some value
>>> find_parser(None, "type_not_exist"
...             )  
Traceback (most recent call last):
UnknownParserTypeError: No parser found for type 'type_not_exist'
>>> find_parser("cnf.ext_not_found"
...             )  
Traceback (most recent call last):
UnknownFileTypeError: No parser found for file 'cnf.ext_not_found'
>>> find_parser(None, "ini")
<class 'anyconfig.backend.ini.Parser'>
>>> find_parser("cnf.json")
<class 'anyconfig.backend.json.Parser'>
>>> find_parser("cnf.json", is_path_=True)
<class 'anyconfig.backend.json.Parser'>
anyconfig.backends.list_types(cps=(('bson', [<class 'anyconfig.backend.bson.Parser'>]), ('cbor', [<class 'anyconfig.backend.cbor.Parser'>]), ('configobj', [<class 'anyconfig.backend.configobj.Parser'>]), ('ini', [<class 'anyconfig.backend.ini.Parser'>]), ('json', [<class 'anyconfig.backend.json.Parser'>]), ('msgpack', [<class 'anyconfig.backend.msgpack.Parser'>]), ('pickle', [<class 'anyconfig.backend.pickle.Parser'>]), ('properties', [<class 'anyconfig.backend.properties.Parser'>]), ('shellvars', [<class 'anyconfig.backend.shellvars.Parser'>]), ('toml', [<class 'anyconfig.backend.toml.Parser'>]), ('xml', [<class 'anyconfig.backend.xml.Parser'>]), ('yaml', [<class 'anyconfig.backend.yaml.Parser'>])))

List available config types.