--- Lib/ConfigParser.py.old 2002-10-25 18:52:00.000000000 -0300 +++ Lib/ConfigParser.py 2002-11-07 20:00:36.000000000 -0200 @@ -24,11 +24,13 @@ methods: - __init__(defaults=None) + __init__(defaults=None, singlesection=None) create the parser and specify a dictionary of intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. Note that `__name__' is always an intrinsic default; - it's value is the section's name. + it's value is the section's name. If singlesection is not None, the + whole file will be considered as a single section, named accordingly + to that parameter value. The file should have no headers in that case. sections() return all the configuration section names, sans DEFAULT @@ -173,12 +175,13 @@ class RawConfigParser: - def __init__(self, defaults=None): + def __init__(self, defaults=None, singlesection=None): self._sections = {} if defaults is None: self._defaults = {} else: self._defaults = defaults + self._singlesection = singlesection def defaults(self): return self._defaults @@ -328,12 +331,14 @@ def write(self, fp): """Write an .ini-format representation of the configuration state.""" if self._defaults: - fp.write("[%s]\n" % DEFAULTSECT) + if self._singlesection is None: + fp.write("[%s]\n" % DEFAULTSECT) for (key, value) in self._defaults.items(): fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) fp.write("\n") for section in self._sections: - fp.write("[%s]\n" % section) + if self._singlesection is None: + fp.write("[%s]\n" % section) for (key, value) in self._sections[section].items(): if key != "__name__": fp.write("%s = %s\n" % @@ -389,7 +394,13 @@ leading whitespace. Blank lines, lines beginning with a '#', and just about everything else is ignored. """ - cursect = None # None, or a dictionary + if self._singlesection is not None: + needheaders = False + cursect = {'__name__': self._singlesection} + self._sections[self._singlesection] = cursect + else: + needheaders = True + cursect = None optname = None lineno = 0 e = None # None, or an exception @@ -412,7 +423,7 @@ # a section header or option header? else: # is it a section header? - mo = self.SECTCRE.match(line) + mo = needheaders and self.SECTCRE.match(line) if mo: sectname = mo.group('header') if sectname in self._sections: --- Doc/lib/libcfgparser.tex.old 2002-11-07 19:51:37.000000000 -0200 +++ Doc/lib/libcfgparser.tex 2002-11-07 19:59:18.000000000 -0200 @@ -43,14 +43,24 @@ may be passed into the \method{get()} method which will override all others. -\begin{classdesc}{RawConfigParser}{\optional{defaults}} +It's also possible to parse a configuration file without headers. To +do that, you specify a section name to the whole file, trough the +\var{singlesection} parameter of the \class{ConfigParser} constructor. + +\begin{classdesc}{RawConfigParser}{\optional{defaults\optional{, + singlesection}}} The basic configuration object. When \var{defaults} is given, it is initialized into the dictionary of intrinsic defaults. This class -does not support the magical interpolation behavior. +does not support the magical interpolation behavior. If \var{singlesection} +is given, the whole file will be considered as a single section, +named accordingly to that parameter value. The file should have no +headers in that case. + \versionadded{2.3} \end{classdesc} -\begin{classdesc}{ConfigParser}{\optional{defaults}} +\begin{classdesc}{ConfigParser}{\optional{defaults\optional{, + singlesection}}} Derived class of \class{RawConfigParser} that implements the magical interpolation feature and adds optional arguments the \method{get()} and \method{items()} methods. The values in \var{defaults} must be @@ -59,7 +69,8 @@ and will override any value provided in \var{defaults}. \end{classdesc} -\begin{classdesc}{SafeConfigParser}{\optional{defaults}} +\begin{classdesc}{SafeConfigParser}{\optional{defaults\optional{, + singlesection}}} Derived class of \class{ConfigParser} that implements a more-sane variant of the magical interpolation feature. This implementation is more predictable as well.