formats¶
The following formats are included within cincoconfig. There should not be a reason to directly reference these classes. Instead, to save or load a configuration in the JSON format, for example, use:
config.save('config.json', format='json')
This will automatically create the cincoconfig.formats.json.JsonConfigFormat
.
- class cincoconfig.formats.BsonConfigFormat¶
BSON configuration file format. This format is only available when the
bson
package is installed.This class should not be directly referenced. Instead, use the config
load()
andsave()
methods, passing format=’bson’.config.save('filename.bson', format='bson') # or config.load('filename.bson', format='bson')
- class cincoconfig.formats.JsonConfigFormat(pretty=True)¶
JSON configuration file format.
This class should not be directly referenced. Instead, use the config
load()
andsave()
methods, passing format=’json’.config.save('filename.json', format='json') # or config.load('filename.json', format='json')
- Parameters
pretty (
bool
) – pretty-print the JSON document in the call tojson.dumps()
- class cincoconfig.formats.PickleConfigFormat¶
Python pickle configuration file format. This format uses the
pickle
module to serialize and deserialize the configuration basic value tree.This class should not be directly referenced. Instead, use the config
load()
andsave()
methods, passing format=’pickle’.config.save('filename.cfg', format='pickle') # or config.load('filename.cfg', format='pickle')
- class cincoconfig.formats.XmlConfigFormat(root_tag='config')¶
XML configuration file format.
This class should not be directly referenced. Instead, use the config
load()
andsave()
methods, passing format=’xml’.config.save('filename.xml', format='xml') # or config.load('filename.xml', format='xml')
To handle dynamic configurations, the Python type for each XML element is stored in the
type
attribute.<x type="int">1024</x>
When the configuration file is loaded, the formatter will attempt to parse the original Python type. If the parsing fails then the original string value is stored in the basic value tree.
- Parameters
root_tag (
str
) – root configuration tag name
- _to_element(key, value)¶
Convert the key/value pair to an XML element.
- _from_element(ele, py_type=None)¶
Parse the XML element to the original Python type. This method will attempt to convert any basic types to their original Python type and, if conversion fails, will use the original string value. For example:
<x type="int">blah</x>
This method will attempt to parse the value, blah, as a
int
, which will fail. Then, the method will store the original string value in the basic value tree:tree = { 'x': 'blah' }
- _prettify(ele)¶
Pretty print the XML element.
- dumps(config, tree)¶
Serialize the basic value
tree
to an XMLbytes
document. The returned XML document will contain a single top-level tag named root_key that all other values are stored under.
- class cincoconfig.formats.YamlConfigFormat(root_key=None)¶
YAML configuration file format. This format is only available when the
PyYAML
package is installed.This class should not be directly referenced. Instead, use the config
load()
andsave()
methods, passing format=’yaml’.config.save('filename.yml', format='yaml') # or config.load('filename.yml', format='yaml')
By default, the basic value tree is serialized to YAML document as-is, where top-level configuration values are placed at the top level of the config file. For example:
>>> # assume tree = {'x': 1, 'y': 2} >>> print(config.dumps(format='yaml')) x: 1 y: 2
The root_key argument can be specified to store all configuration values under a single top-level key:
>>> # assume tree = {'x': 1, 'y': 2} >>> print(config.dumps(format='yaml', root_key='CONFIG')) CONFIG: x: 1 y: 2
The root_key argument affects both how
loads()
anddumps()
behave.- Parameters
root_key (
Optional
[str
]) – the root config key that the configuration values should be stored under
- dumps(config, tree)¶
Serialize the basic value
tree
to YAMLbytes
document. If root_key was specified, the returned YAML document will contain a single top-level field named root_key that all other values are stored under.
- loads(config, content)¶
Deserialize the
content
(abytes
instance containing a YAML document) to a Python basic value tree. If root_key was specified, the returned basic value tree will be scoped to root_key, if it exists in the deserializeddict
. This is equivalent to:tree = yaml.load(content) return tree[self.root_key]
Base Classes¶
All configuration file formats must inherit from and implement all abstract methods in the
cincoconfig.abc.ConfigFormat
class.
- class cincoconfig.ConfigFormat¶
The base class for all configuration file formats.
- dumps(config, tree)¶
Convert the configuration value tree to a bytes object. This method is called to serialize the configuration to a buffer and eventually write to a file.
- classmethod get(name, **kwargs)¶
Get a registered configuration format.
- Parameters
name (
str
) – config format namekwargs – keyword arguments to pass into the config format
__init__()
method
- Return type
- Returns
the config format instance
- classmethod initialize_registry()¶
Initialize the format registry for built-in formats.
- Return type
- loads(config, content)¶
Parse the serialized configuration to a basic value tree that can be parsed by the Config
load_tree()
method.
- classmethod register(name, format_cls)¶
Register a new configuration format.
- Parameters
name (
str
) – format nameformat_cls (
Type
[ConfigFormat
]) –ConfigFormat
subclass to register
- Return type