473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ANN : ConfigObj 3.0.0 - Simple config file parsing

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/atlantib...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
Jul 18 '05 #1
4 3803
mi*****@foord.net (Fuzzyman) writes:
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.


What do you thing about yaml for config file ? it's very pythonic with
indentation :

import yaml
print yaml.load("""
rub1:
- one
- two
rub2:
- three
- four
""").next()

{'rub1': ['one', 'two'], 'rub2': ['three', 'four']}

it can also work on the other side :

print yaml.dump({'rub1': ['one', 'two'], 'rub2': ['three', 'four']})
---
rub1:
- one
- two
rub2:
- three
- four

--
Wilk - http://flibuste.net
Jul 18 '05 #2
Wilk <wi******@OUTflibuste.net> wrote in message news:<87************@blakie.riol>...
mi*****@foord.net (Fuzzyman) writes:
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.


What do you thing about yaml for config file ? it's very pythonic with
indentation :

import yaml
print yaml.load("""
rub1:
- one
- two
rub2:
- three
- four
""").next()

{'rub1': ['one', 'two'], 'rub2': ['three', 'four']}

it can also work on the other side :

print yaml.dump({'rub1': ['one', 'two'], 'rub2': ['three', 'four']})
---
rub1:
- one
- two
rub2:
- three
- four


I'm sure YAML is fine - I think ConfigObj is still probably 'simpler'.
A basic config file can be created with :
config = ConfigObj(filename)
config['keyword1] = value1
config['list keyword] = [value2, value3, value4]
..
..

config.write()

You can then read it back with :
config = ConfigObj(filename)

Lot's of extra options of course. The created config files look like
config files that most people will be used to creating :

"keyword1" = "value1"
"keyword2" = "value2"
..
..
..

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #3
Fuzzyman wrote:
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/atlantib...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.


Just as an aside, we've just written a config system for our app that I
think is quite interesting and am planning to release when I get time.

Some of the goals are the same as ConfigObject: easy for either a human
or a computer to read / edit the files.
Some are different. Particularly it handles hierarchical config tries in
a nice pythonic syntax. For example you can specify things like this:

app1.title = "Title of app"
app1.font.face = "Arial"

Or like this:

app1:
title = "Title of app"
font.face = "Arial"

You can also copy bits of the tree if you like:

app1:
title = "Title of app"
subtitle = "Red"
font:
face = "Arial"
size = 16
style = "normal"

app2:
title = "Other title"
subtitle = app1.subtitle
font = app1.font:
size = 18

The module will automatically remember where an element is defined and
save changed values to the same location. It will also save new values
to an appropriate location (e.g. app2.newvalue would appear in the app2
section).

I think the hierarchical aspect of configuration is important ; it lets
you do things like Mozilla's generic prefs interface etc. It also gives
plugins / customizations to an app an easy way to add their own preferences.

If anyone is interested in this module, post a reply...

David
Jul 18 '05 #4
David Fraser <da****@sjsoft.com> writes:
Just as an aside, we've just written a config system for our app that
I think is quite interesting and am planning to release when I get
time.

Some of the goals are the same as ConfigObject: easy for either a
human or a computer to read / edit the files.
Some are different. Particularly it handles hierarchical config tries
in a nice pythonic syntax. For example you can specify things like
this:

app1.title = "Title of app"
app1.font.face = "Arial"

Or like this:

app1:
title = "Title of app"
font.face = "Arial" [...]
The module will automatically remember where an element is defined and
save changed values to the same location. It will also save new values
to an appropriate location (e.g. app2.newvalue would appear in the
app2 section).

I think the hierarchical aspect of configuration is important ; it
lets you do things like Mozilla's generic prefs interface etc. It also
gives plugins / customizations to an app an easy way to add their own
preferences.

If anyone is interested in this module, post a reply...


Looks interesting, if you ask me.

Thomas
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fuzzyman | last post by:
A couple of little scripts I've written for my python project (AAAcontroller - see www.atlantibots.org.uk) might be useful to others - so I've stuck them up on the web : ...
4
by: Gert Van den Eynde | last post by:
Hi all, Could you give me some pointers on how to parse a text input file in C++? Most will be config-file style input (keyword = data), but some maybe 'structures' like material{ name = n,...
9
by: Mantorok Redgormor | last post by:
If I am parsing a config file that uses '#' for comments and the config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is it recommended to use a) fgetc (parse a character at a...
3
by: Dave Cullen | last post by:
I'm a C# noob trying to maintain and modify someone else's code. The previous programmer used a config file to load parameters for a database connect string. The file is named app.config and it's...
10
by: NuB | last post by:
I'm creating a C# class file(DLL) that will be used by an asp.net application. In the DLL I want to read a web.config, or app.config file so some information can change without having to go into...
16
by: Timm | last post by:
I'm trying to use ASP objects (basically formed and populated based on Web.Config settings) and I want to use them in a different non-asp program with minimal reprogramming. So, my question is how...
0
by: Fuzzyman | last post by:
Hello All, I've added (optional) unicode support for ConfigObj. This is now available from SVN. You can specify an encoding to decode the config file on reading. This maps to an encoding...
3
by: =?Utf-8?B?RGFuYQ==?= | last post by:
I am re-posting this message after registering my posting alias. When I specify an end tag for the clear element of namespaces in my web.config file, the parser error "Unrecognized element 'add'"...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.