[Python-Dev] Changes to ConfigParser

Peter Funk pf@artcom-gmbh.de
Tue, 11 Jul 2000 09:12:29 +0200 (MEST)


Hi,

Eric S. Raymond:
> Earlier today, I committed a patch to ConfigParser that adds three new
> methods to the class.  Here is the documentation:
[...]
Applying the well known dictionary API to config info can help to
to avoid reinventing a new API to deal with configuration info.

What I would love to see, is a class 'ConfigDict' derived from 
'UserDict.UserDict', which contains the section-names as keys and
section-objects as values, where section-objects are also dictionaries
containing the option-names as keys.
The new class 'ConfigDict' should then provide a 'read()' or 'parse()' method 
(or take an optional filename as constructor argument) and would also 
provide 'write' and/or 'save' method(s).
Hypothecical examples of use:
        rcfilename = os.path.join(os.environ.get("HOME", "."), ".myFoorc")
	cnf = ConfigParser.ConfigDict(rcfilename)
	# or alternatively:
	cnf = ConfigParser.ConfigDict()
	cnf.read(open(rcfilename, "r"))
	# ... accessing the configuration
	if cnf.has_key("FONT_SETTINGS"):
	    try:
		size = int(cnf["FONT_SETTINGS"]["size"])
	    except KeyError:
	        size = 10
	# ... adding a section:
	cnf["new_section"] = {}
	# ... adding an option to the default section:
	cnf["screensize"] = "%dx%d" % (width, height)
	# ...
	cnf["FONT_SETTINGS"]["size"] = 10
	# removing an option:
	del cnf["FONT_SETTINGS"]["fontname"]
	# removing a section or an option from the default section:
	del cnf["old_section"]
	del cnf["foo_bar"]
	# ...
	cnf.save() # only if instantiated using a filename argument
	# or alternatively:
	cnf.write(open(".myrc", "w"))

[...]
> To do these things, forgetool needs to maintain some local state that is
> conveniently represented in the format of a Windows .ini file.  More,
> it needs to be able to *update* .ini files as well as parse them.

This is a very common need.  I agree, that this functionality should be 
provided by the standard library.  

Recently we had some discussion about a 'userpref' module, which should
fill another related gap:  provide a portable way to determine the
rcfilename.  a dot-rc file is the unix way to store this kind of information, 
on Windows a application specific ini file probaly belongs into another
local directory (there is no $HOME) and on the Mac it probably belongs
into a preferences folder ... still a lot of work to do. :-(

Regards, Peter