[Patches] [ python-Patches-549037 ] ConfigParser: optional section header

noreply@sourceforge.net noreply@sourceforge.net
Thu, 07 Nov 2002 13:15:52 -0800

Patches item #549037, was opened at 2002-04-26 12:00
You can respond by visiting: 

Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Detlef Lannert (lannert)
Assigned to: Nobody/Anonymous (nobody)
Summary: ConfigParser: optional section header

Initial Comment:
Each configuration file parsed by ConfigParser.py must
with a section header line (a section name enclosed in
In many cases where I just want to parse a file with
variable settings for a program this is IMO a nuisance:
user must know the expected section name and insert a
redundant header line, even if there are no other

The "surprise factor" is even higher when RFC[2]822
is used; the config file then looks more like a
standard mail
header which wouldn't start with a section title

Since the config file is read and the case of missing
titles handled in the __read() method of the
there is no easy way to modify the parser's behaviour
by subclassing and overwriting a method.

The patch lets the caller specify a default section
name; if
the config file doesn't start with a [section] line but
option lines, a suitable section is automatically
created and
holds the option entries. In any other case, i.e., when
header line is present in the config file and/or when
default startsection name is specified, the parser's
is unchanged. Thus there shouldn't be a compatibility

In case the upload of the patch file fails (which has
to me and my browser before), please have a look at
diffs also add some lines to test_cfgparser.py.


>Comment By: Gustavo Niemeyer (niemeyer)
Date: 2002-11-07 21:15

Logged In: YES 

Thanks for purposing that Detlef. Having a configuration file without 
headers could indeed be interesting in some situations. 
I have a few comments about the implementation: 
The patch includes a new parameter in read functions, stating 
what's the first section name. It means that we could have other 
sections after the first unheaded section. IMO, that situation should 
still be considered an error. 
One possible way to implement it is to include a "noheaders" boolean 
parameter for the constructor. Then, the user would have to know what's 
the standard single section name, to pass it to functions like get(). 
Another way would be to include something like a "singlesection" parameter 
in the constructor. This parameter would accept a string option, which 
would name the single section. 
As an argument against the whole issue, I'm not sure how unconfortable 
it is to simply include a header in the file to satisfy the parser. 
As an argument favorable, this could allow ConfigParser to parse simple 
(no escapes or variables) shell configuration files and other simple 
configurations using NAME=VALUE style. 
I'm attaching an alternative implementation of the singlesection algorithm, 
described above. Would it be enough for your needs? 


You can respond by visiting: