A late-to-the-party, lightweight, stupid simple configuration manager.
python setup.py install
should do it.
or
pip install conflate
-
Create a dictionary full of the properties you'll want to be able to load from a file.
config = { height : 600, width : 960, database_location : '/var/databas/foo', pages : ['index.html', 'upload.html', 'template.html'], preferences : None }
Yes, you may put anything you want as a value. These will probably be your defaults. Put
None
if you don't want the property to have a default. -
You can create a confmgr object with a configuration specified, or just give it the filename(required) and wait until later. Configuration needs to be in the working directory and will be created (by a read or write) if not present.
from conflate import confmgr c = confmgr('test.conf', CONF=config) # from above
-
And now you can write to disk with
writeconf
, read from disk withreadconf
, and check your configuration withprintconf
.c.writeconf() # writes out initial configuration c.readconf() # sync contents of CONF with disk c.printconf() # is everything okay?
-
Changing properties is as easy as editing a dictionary.
c.CONF['width'] = 700 # update an exisiting one c.CONF['comment'] = 'This is a test' # and make a new one c.writeconf() # ...and write everything to disk.
- confmgr can take named attributes:
- assign_op - change the assignment operator to whatever you please.
Defaults to '='.
e.g. '=' for
test = 1
or ':' fortest : 1
- comment_op - change comment symbol to whatever you please.
Defaults to '#'
e.g. '#' for
# is a comment
or ';' for; is a comment
- silent - a boolean that will silence any prompts
- CONF - the property that contains the configuration dict. Can be assigned at object creation as an attribute, or directly later.
Ship a configuration file with your product, or write a script to set it up the first time. This way you can include comments and defaults that can be changed later.
An example configuration file can be found in the source. It is named 'test.conf'.
If you want to stick to DRY and keep your config entirely on disk, you can
generate your in-memory version of the configuration from the disk version by
using the "read_uservalues" keyword argument of readconf
.