A configuration file(s) management library
Table of contents
kon is a very simple configuration file(s) management system that parses one or more configuration files in a path and provides an object tree that can be accessed directly. It also attempts to parse the data as concrete types so that you don’t have to explicitly get specific types, only for INI files. This does introduce some constraints and deviations from default implementations in python, but the convenience is generally worth it. Moreover this behavior can be overridden. kon currently handles only INI and JSON files.
Install the extension using pip:
$ pip install kon
$ easy_install kon
You can instantiate a Konfig object by calling its constructor:
from kon import Konfig cfg = Konfig('/path/to/folder/or/ini-file') # this can also accept options for loading by handlers cfg.load()
The load method will try to get an appropriate file handler based on the filename extension. If none exists, it will skip that file. The file handlers are instantiated with any default values you may want to provide the configuration. For each file handler it then calls the instance’s load method. This method can accept handler specific loading options. Currently supported handlers and their behaviors are documented below.
If you want to specify default arguments, you can pass them in as keywords arguments to the constructor and they are passed through to the file handlers used to load the files.
from kon import Konfig cfg = Konfig('/path/to/folder/or/ini-file', a=1, b=2.2, c=False) cfg.load()
The load method does a couple of things.
For e.g. if you have a directory tree like the following:
kon ├── a.ini ├── b │ ├── c.ini │ ├── d.json │ ├── e │ │ └── e.ini │ ├── g.json │ └── h.ini ├── i.ini ├── j.ini └── k.ini
The corresponding object tree created by load before calling the handlers load will be exactly like the tree above sans the filename extension.
In the event that you have a folder and a file by the same prefix, for e.g.:
kon ├── a.ini └── a.ini └── c.ini
it will result in a single object kon.a with merged sub-children.
This handler parses JSON files that it finds in the path specified or an individual file if it is so specified. Internally it delegates to the default python json library.
This handler parses INI files that it finds in the path specified or an individual file if it is so specified. Internally it uses SafeConfigParser to load and parse the files. The individual sections, options and values are processed again.
If you want to customize the way the internal SafeConfigParser works you can use the arguments as specified in the RawConfigParser constructor documentation and pass them to the load method call on a Konfig instance.
Do not pass in the defaults argument as that will be ignored. The defaults that are sent to the parser should be provided as keyword arguments to the constructor.
cfg = Konfig('/path/to/folder/or/ini-file') cfg.load(dict_type=OrderedDict, allow_no_value=True)
Konfig uses SafeConfigParser to load the INI file. Consequently you get the built-in parsing and interpolation capabilities of the parser.
Because SafeConfigParser does not automatically coerce the values to an appropriate type, kon will try to do it’s best to do some for you. The following cast attempts are made in order of precedence: