Package: kon
Table of contentsOverviewInstallationUsageBasic UsageLoad BehaviorJSONFileHandlerOptionsINIFileHandlerOptionsCustomizing SafeConfigParserImplementation DetailsOverviewkonis 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 forINIfiles. 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 onlyINIandJSONfiles.InstallationInstall the extension usingpip:$pipinstallkonoreasy_install:$easy_installkonUsageBasic UsageYou can instantiate aKonfigobject by calling its constructor:fromkonimportKonfigcfg=Konfig('/path/to/folder/or/ini-file')# this can also accept options for loading by handlerscfg.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’sloadmethod. 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.fromkonimportKonfigcfg=Konfig('/path/to/folder/or/ini-file',a=1,b=2.2,c=False)cfg.load()Load BehaviorTheloadmethod does a couple of things.It will walk the directory tree starting from the root directory you provide in the constructor call and create objects per directory as it goes along.Once it encounters a file, it will try to get a file handler for that type of file.If it cannot get a file handler, the file will be skipped.If it can get the file handler, it will instantiate it, pass it in the defaults it got and then callit’sloadmethod passing in any parameters it got.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.iniThe corresponding object tree created byloadbefore 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.iniit will result in a single object kon.a with merged sub-children.JSONFileHandlerThis handler parsesJSONfiles that it finds in the path specified or an individual file if it is so specified. Internally it delegates to the default pythonjsonlibrary.Optionspreserve_caseIf set tofalsenormalizes the key names to be lower case. By default the json loads function preserves case. This is the opposite of what happens withINIFileHandler.encodingPassthrough option forjson.loadsINIFileHandlerThis handler parsesINIfiles that it finds in the path specified or an individual file if it is so specified. Internally it usesSafeConfigParserto load and parse the files. The individual sections, options and values are processed again.Optionspreserve_caseIf set totruepreserves the case of the option names. Sections are stillcase-insensitive. By defaultSafeConfigParsernormalizes all option names to lower case. This will prevent that.dict_typePassthrough option forSafeConfigParser, see belowallow_no_valuePassthrough option forSafeConfigParser, see belowCustomizing SafeConfigParserIf you want to customize the way the internalSafeConfigParserworks you can use the arguments as specified in theRawConfigParserconstructor documentation and pass them to theloadmethod call on aKonfiginstance.WarningDo not pass in the defaults argument as that will beignored. The defaults that are sent to the parser should be provided as keyword arguments to the constructor.For example:cfg=Konfig('/path/to/folder/or/ini-file')cfg.load(dict_type=OrderedDict,allow_no_value=True)Implementation DetailsKonfigusesSafeConfigParserto load the INI file. Consequently you get the built-in parsing and interpolation capabilities of the parser.BecauseSafeConfigParserdoes not automatically coerce the values to an appropriate type,konwill try to do it’s best to do some for you. The following cast attempts are made in order of precedence:intfloatbooleanlist, dict or tuple (usingast.literal_eval)NoteThe behavior deviates fromSafeConfigParser’s treatment of boolean because a type-coercion tointhappens before a type-coercion toboolean. So if you want a boolean set it to one ofyes, no, on, off, true or falseonly.