F
Fuzzyman
There have been a couple of config file 'systems' announced recently,
that focus on building more powerful and complex configuration files.
ConfigObj is a module to enable you to much more *simply* access
config files.
This is version 3, which is a big overhaul. It extends ConfigObj to
reading config files with sections and various other simplifications.
I find ConfigObj extremely easy to use and use it for reading config
files and data persistence......
http://www.voidspace.org.uk/atlantibots/pythonutils.html#configobj
(also the home of caseless and listparse modules)
INTRODUCTION and EXTRACT from docs
ConfigObj
ConfigObj allows you to read, modify and create config files in python
with basically single line commands.
If you give it a filename it will read the config automatically and
you can access or change the values by treating it like a dictionary.
ConfigObj 3 is a major upgrade to ConfigObj - it will now read and
write config files with sections (like windows INI files) as well as
various other improvements and simplifications.
ConfigObj has the following advantages over other configuration
systems :
Ease of use
Multiple (list) values for keywords
Easy to create, modify *and* write files
Easy to hand edit/create the config files
comments are preserved
quoting is optional
will understand various different keyword/value dividers
Because the programmers interface is the same as the Python
dictionary, ConfigObj is also extremely useful for data persistence.
The fact that it can store lists and all the files it creates are
easily 'human readable' is a big plus for this. Most functionality can
be achieved with dictionary like syntax.
So it implements a system that is easy for your users to use and easy
for you to examine the files that it creates.
Feedback on this module, including the documentation, is very much
welcomed. Questions, criticism, comments, bug reports and suggestions
all welcomed.
ConfigObj needs python 2.2+
######################################################################
THE BASICS
USAGE :
from configobj import ConfigObj, pref_dict, exceptionlist
config = ConfigObj(infile=[], indict = {}, **keywargs)
You create a basic configobj by giving it a filename and any options
you want to set. The options can either be in the form of keyword
arguments or as a dictionary. A useful default dictionary is available
to modify - but we'll look more at the options later.
config = ConfigObj(filename)
ConfigObj then reads the file and parses the values from it.
Values are always read in as strings - or lists of strings.
The file can either be a straightforward config file with just
keywords and values, or it can be divided into sections. Each section
then has it's own set of keywords. See the paragraph on config files
to see the difference.
You can then access the keywords in the same way as you access a
dictionary :
value1 = config['keyword1']
value2 = config['keyword2']
..
..
You can even use the same method to change the values, and write it
out using the write method :
config['keyword1'] = value1
config['keyword2'] = value2
..
..
config.write()
ConfigObj inherits most of it's methods from the built in type
dictionary - so most of the things you can do with dictionaries you
can do with a configobj.
Keywords in ConfigObj are case insensitive. This is done using a class
called caseless. If you ever need a case insensitive dictionary or
list you can use these !
This means that :
print config['FISH']
is the same as :
print config['fish']
It also means that the case of the keywords in your config files
doesn't matter. ConfigObj will try and preserve the case you used when
it writes files out though.
If the config file has sections in it, then each section will be a
dictionary of keywords and values.
print config['section1']
{'keyword1': 'value 1', 'keyword2': 'value 2', 'keyword3': 'value 3'}
You can create an empty new section with :
config['section 2'] = None
or you can just pass in a dictionary :
config['section 2'] = {'keyword1': 'value 1', 'keyword2': 'value 2',
'keyword3': 'value 3'}
You access values in a section with :
value1 = config['section 1']['keyword1']
value2 = config['section 1']['keyword2']
You can also use ConfigObj to just read in *some* values from a file,
and then just update those values in the file.
This is done using a 'configspec' when you read a file and the
'writein' method to write it out again. But we'll see more about those
later.
You can even create a completely empty configobj from scratch :
config = ConfigObj()
config.filename = filename
config['keyword1'] = value
config['keyword2'] = value
..
..
configobj.write()
It can then be read back in with :
config = ConfigObj(filename)
Easy hey !!
A blank configobj created with
config = ConfigObj()
will be a flatfile by default. The same applies if you specify a
filename which doesn't yet exist. To make it a configobj with sections
you need to specify :
config = ConfigObj(flatfile=False)
The last thing I'll mention when covering the basics is list values in
config files.
Values are always strings - if you want integers, or anything else,
you can do the conversion yourself !
If a line in a config file is a list -
'keyword' = [value1, value2, value3]
Then it will be read in and turned into a list (using the listparse
module).
If you pass in a list to a configobj then it will be written out as a
list. This includes nested lists !! (lists of lists !! - unless you
turn recursivelist off - see the options section).
This means :
config['keyword'] = [ value1, [value2, value3], value4]
is perfectly valid.
Regards,
Fuzzy
http://www.voidspace.org.uk
that focus on building more powerful and complex configuration files.
ConfigObj is a module to enable you to much more *simply* access
config files.
This is version 3, which is a big overhaul. It extends ConfigObj to
reading config files with sections and various other simplifications.
I find ConfigObj extremely easy to use and use it for reading config
files and data persistence......
http://www.voidspace.org.uk/atlantibots/pythonutils.html#configobj
(also the home of caseless and listparse modules)
INTRODUCTION and EXTRACT from docs
ConfigObj
ConfigObj allows you to read, modify and create config files in python
with basically single line commands.
If you give it a filename it will read the config automatically and
you can access or change the values by treating it like a dictionary.
ConfigObj 3 is a major upgrade to ConfigObj - it will now read and
write config files with sections (like windows INI files) as well as
various other improvements and simplifications.
ConfigObj has the following advantages over other configuration
systems :
Ease of use
Multiple (list) values for keywords
Easy to create, modify *and* write files
Easy to hand edit/create the config files
comments are preserved
quoting is optional
will understand various different keyword/value dividers
Because the programmers interface is the same as the Python
dictionary, ConfigObj is also extremely useful for data persistence.
The fact that it can store lists and all the files it creates are
easily 'human readable' is a big plus for this. Most functionality can
be achieved with dictionary like syntax.
So it implements a system that is easy for your users to use and easy
for you to examine the files that it creates.
Feedback on this module, including the documentation, is very much
welcomed. Questions, criticism, comments, bug reports and suggestions
all welcomed.
ConfigObj needs python 2.2+
######################################################################
THE BASICS
USAGE :
from configobj import ConfigObj, pref_dict, exceptionlist
config = ConfigObj(infile=[], indict = {}, **keywargs)
You create a basic configobj by giving it a filename and any options
you want to set. The options can either be in the form of keyword
arguments or as a dictionary. A useful default dictionary is available
to modify - but we'll look more at the options later.
config = ConfigObj(filename)
ConfigObj then reads the file and parses the values from it.
Values are always read in as strings - or lists of strings.
The file can either be a straightforward config file with just
keywords and values, or it can be divided into sections. Each section
then has it's own set of keywords. See the paragraph on config files
to see the difference.
You can then access the keywords in the same way as you access a
dictionary :
value1 = config['keyword1']
value2 = config['keyword2']
..
..
You can even use the same method to change the values, and write it
out using the write method :
config['keyword1'] = value1
config['keyword2'] = value2
..
..
config.write()
ConfigObj inherits most of it's methods from the built in type
dictionary - so most of the things you can do with dictionaries you
can do with a configobj.
Keywords in ConfigObj are case insensitive. This is done using a class
called caseless. If you ever need a case insensitive dictionary or
list you can use these !
This means that :
print config['FISH']
is the same as :
print config['fish']
It also means that the case of the keywords in your config files
doesn't matter. ConfigObj will try and preserve the case you used when
it writes files out though.
If the config file has sections in it, then each section will be a
dictionary of keywords and values.
print config['section1']
{'keyword1': 'value 1', 'keyword2': 'value 2', 'keyword3': 'value 3'}
You can create an empty new section with :
config['section 2'] = None
or you can just pass in a dictionary :
config['section 2'] = {'keyword1': 'value 1', 'keyword2': 'value 2',
'keyword3': 'value 3'}
You access values in a section with :
value1 = config['section 1']['keyword1']
value2 = config['section 1']['keyword2']
You can also use ConfigObj to just read in *some* values from a file,
and then just update those values in the file.
This is done using a 'configspec' when you read a file and the
'writein' method to write it out again. But we'll see more about those
later.
You can even create a completely empty configobj from scratch :
config = ConfigObj()
config.filename = filename
config['keyword1'] = value
config['keyword2'] = value
..
..
configobj.write()
It can then be read back in with :
config = ConfigObj(filename)
Easy hey !!
A blank configobj created with
config = ConfigObj()
will be a flatfile by default. The same applies if you specify a
filename which doesn't yet exist. To make it a configobj with sections
you need to specify :
config = ConfigObj(flatfile=False)
The last thing I'll mention when covering the basics is list values in
config files.
Values are always strings - if you want integers, or anything else,
you can do the conversion yourself !
If a line in a config file is a list -
'keyword' = [value1, value2, value3]
Then it will be read in and turned into a list (using the listparse
module).
If you pass in a list to a configobj then it will be written out as a
list. This includes nested lists !! (lists of lists !! - unless you
turn recursivelist off - see the options section).
This means :
config['keyword'] = [ value1, [value2, value3], value4]
is perfectly valid.
Regards,
Fuzzy
http://www.voidspace.org.uk