AnyConf is a configuration library for python that wraps several other configuration libraries under a common API. Some uses of AnyConf include:
- Support more than one file format for configuration data
- Maintain agility to switch formats later
- Defer a decision about which format(s) to support
AnyConf makes use of existing configuration libraries. It does not re-implement the functionality itself. Obviously, not all libraries have the same set of capabilities. While AnyConf tries to be as flexible as possible there will naturally be some features that cannot be supported under a generic API.
The behaviours and capabilities of the libraries that AnyConf depends on may change between releases. Applications written for specific versions of python or configuration libraries should be aware of how these changes may affect their configuration data. AnyConf makes no attempt to compensate for these behavioural changes.
Create a new ConfigLoader and load your configuration data from a file-like object.
import anyconf
config = anyconf.ConfigLoader()
config.load(myOpenFile, anyconf.FORMAT_INI)
The exact methods that the file-like object needs to support are dependant on what the underlying configuration library
requires. For instance, .ini
files use the configparser
module, which only requires the readline
method.
The returned Config
object can be traversed in a hierarchical manner, referencing child sections and options as
properties or dict items. Use the dict notation when a configuration item might conflict with a python keyword or
library method, or contain characters that are illegal in property names (such as spaces).
configValue config.someSection.someOption
anotherValue = config["another section"]["another option"]
Format | Underlying Library | Notes |
.ini | configparser | Uses ConfigParser in python 2 |
.xml | minidom |
- 2.5
- 2.6
- 2.7
- 3.1
- 3.2
This section demonstrates how AnyConf maps each file format to python objects.
XML Source | Python Representation | Comments |
<e> my text goes here </e> |
config.e == 'my text\n goes here' | Accessing an element that contains only CDATA text returns that text as the value of the option with leading and trailing whitespace removed. |
<e> <a>true</a> <b>false</b> <c /> </e> |
config.e.a == True config.e.b == False config.e.c == True |
Case-insensitive "true" and "false" become the boolean True and False respectively. Blank text also become the boolean True. |
<e option="value" /> | config.e.option == 'value' | Element attributes can also be read as configuration options. |
<e> <f>something</f> </e> |
config.e.f == 'something' | Nested elements form a hierarchy of python objects. |
<e> <f attr="first" /> <f attr="second" /> </e> |
config.e.f[0].attr == 'first' config.e.f[1].attr == 'second' |
If an element has more than one child with the same name then they are represented as a list of options. |
- Elements with attributes or child elements ignore their CDATA text. It is best to encapsulate all text within an element and avoid attributes altogether.
- Use the
getAsList
method for options that may be lists of less than 2 elements to guarantee that the API will always return a list.