473,574 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(infil e=[], 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(filen ame)

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(filen ame)
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(flatf ile=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 3814
mi*****@foord.n et (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({'rub 1': ['one', 'two'], 'rub2': ['three', 'four']})
---
rub1:
- one
- two
rub2:
- three
- four

--
Wilk - http://flibuste.net
Jul 18 '05 #2
Wilk <wi******@OUTfl ibuste.net> wrote in message news:<87******* *****@blakie.ri ol>...
mi*****@foord.n et (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({'rub 1': ['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(filen ame)
config['keyword1] = value1
config['list keyword] = [value2, value3, value4]
..
..

config.write()

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

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
2265
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 : http://www.voidspace.org.uk/atlantibots/pythonutils.html They're my new config object - ConfigObj - and my output object StandOut....... ConfigObj parses and updates...
4
3508
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, position = x,y,z}. Things that I have in my mind now are: 1) simply reading in strings, analysing the strings myself, 2) writing a lexer/parser, 3)...
9
23589
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 time) b) fgets (read in blocks of whatever size) c) fread (get the size of the file and fread the entire thing into memory) and when would it be...
3
1947
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 in the project and under VSS source control. I'd like to rename the file to something more appropriate. I can't find any reference to that...
10
2536
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 the code itself. How can I have the dll read a app.config or web.config file for data? The information I'm looking to store in the file is...
16
1976
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 do I mock up what ASP does to read the Web.Config file? I've tried messing around in the System.Configuration library and haven't been able to find...
0
1107
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 attribute on the ConfigObj instance that is also used for writing (and can be changed). You can find it in the SVN repository at :
3
4584
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'" is reported. .... <pages> <namespaces> <clear></clear> <add namespace="System"/>
3
2921
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 brings me to the dir: C:\Visual Studio 2005\WebSites\WebSite1, it creates default.aspx and default.aspx.vb and I pasted the following code into...
0
7837
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7755
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8133
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5647
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5333
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3791
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2268
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.