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 withinloaderconf.py
module to add ad hoc formatter. Seeconfigtree.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 scriptconfigtree.script.ctdump()
to include options into its argument parser. Seeargparse
.Parameters: - name (str) – Option name
- kw (dict) – Option parameters that are passed into
argparse.ArgumentParser.add_argument()
-
configtree.formatter.
to_json
(tree, rare=False, indent=None, sort=False)¶ Format
tree
into JSONParameters: - tree (Tree) – Tree object to format
- rare (bool) – Use
configtree.tree.rarefy()
on tree before format - indent (int) – Indent size
- sort (bool) – Sort keys
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 formatParameters: 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'