configtree.formatter

The module provides formatters of configtree.tree.Tree objects

configtree.formatter.map

Dictionary that stores map of formatters. It is filled using entry points named configtree.formatter. But can be also modified within loaderconf.py module to add ad hoc formatter. See configtree.loader.

The map is used by script configtree.script.ctdump() to load available formatters and print result.

configtree.formatter.option(name, **kw)

Decorator that adds __options__ list to formatter and puts passed parameters into the list as a tuple (name, kw).

The __options__ list is used by script configtree.script.ctdump() to include options into its argument parser. See argparse.

Parameters:
configtree.formatter.to_json(tree, rare=False, indent=None, sort=False)

Format tree into JSON

Parameters:

Examples:

>>> from configtree import Tree

>>> tree = Tree({'a.x': "Foo", 'a.y': "Bar"})
>>> result = to_json(tree, indent=4, sort=True)
>>> print(result)       
{
    "a.x": "Foo",
    "a.y": "Bar"
}
>>> result = to_json(tree, rare=True, indent=4, sort=True)
>>> print(result)       
{
    "a": {
        "x": "Foo",
        "y": "Bar"
    }
}
configtree.formatter.to_shell(tree, prefix='', seq_sep=' ', sort=False, capitalize=False)

Format tree into shell (Bash) expression format

Parameters:
  • tree (Tree) – Tree object to format
  • prefix (bool) – Key prefix
  • seq_sep (str) – Sequence items separator
  • sort (bool) – Sort keys
  • capitalize (bool) – Capitalize keys

Examples:

>>> from configtree import Tree

>>> tree = Tree({'a.x': "Foo", 'a.y': "Bar"})
>>> result = to_shell(tree, prefix='local ', sort=True)
>>> print(result)       
local a_x='Foo'
local a_y='Bar'
>>> result = to_shell(tree, sort=True, capitalize=True)
>>> print(result)       
A_X='Foo'
A_Y='Bar'

>>> tree = Tree({'list': [1, 2, 3]})
>>> result = to_shell(tree)
>>> print(result)       
list='1 2 3'
>>> result = to_shell(tree, seq_sep=':')
>>> print(result)       
list='1:2:3'