anyconfig.backends

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

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.uniq(iterable, **kwopts)

sorted + uniq

Note

sorted(set(iterable), key=iterable.index) cannot be used for any iterables (generator, a list of dicts, etc.), I guess.

Parameters:
  • iterable – Iterable objects, a list, generator, iterator, etc.
  • kwopts – Keyword options passed to sorted()
Returns:

a sorted list of items in iterable

>>> uniq([1, 2, 3, 1, 2])
[1, 2, 3]
>>> uniq((i for i in (2, 10, 3, 2, 5, 1, 7, 3)))
[1, 2, 3, 5, 7, 10]
>>> uniq(({str(i): i} for i in (2, 10, 3, 2, 5, 1, 7, 3)),
...      key=lambda d: int(list(d.keys())[0]))
[{'1': 1}, {'2': 2}, {'3': 3}, {'5': 5}, {'7': 7}, {'10': 10}]
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.list_parsers_by_type(cps=None)
Returns:List (generator) of (config_type, [config_parser])
anyconfig.backends.list_parsers_by_extension(cps=None)
Returns:List (generator) of (config_ext, [config_parser])
anyconfig.backends.find_by_file(path_or_stream, cps=None, 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 list of pairs :: (type, parser_class)
  • 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=None)

Find config parser by file’s extension.

Parameters:
  • cptype – Config file’s type
  • cps – A list of pairs :: (type, parser_class)
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_=None)

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, "ini")
(<class 'anyconfig.backend.ini.Parser'>, '')
>>> find_parser(None, "type_not_exist")
(None, 'No parser found for given type: type_not_exist')
>>> find_parser("cnf.json")
(<class 'anyconfig.backend.json.Parser'>, '')
>>> find_parser("cnf.json", is_path_=True)
(<class 'anyconfig.backend.json.Parser'>, '')
>>> find_parser("cnf.ext_not_found")
(None, 'No parser found for given file: cnf.ext_not_found')
anyconfig.backends.list_types(cps=None)

List available config types.

Previous topic

anyconfig.api

Next topic

anyconfig.backend

This Page