1 """Configuration file parser.
3 A setup file consists of sections, lead by a "[section]" header,
4 and followed by "name: value" entries, with continuations and such in
7 The option values can contain format strings which refer to other values in
8 the same section, or values in a special [DEFAULT] section.
12 something: %(dir)s/whatever
14 would resolve the "%(dir)s" to the value of dir. All reference
15 expansions are done late, on demand.
17 Intrinsic defaults can be specified by passing them into the
18 ConfigParser constructor as a dictionary.
22 ConfigParser -- responsible for for parsing a list of
23 configuration files, and managing the parsed database.
27 __init__(defaults=None)
28 create the parser and specify a dictionary of intrinsic defaults. The
29 keys must be strings, the values must be appropriate for %()s string
30 interpolation. Note that `__name__' is always an intrinsic default;
31 it's value is the section's name.
34 return all the configuration section names, sans DEFAULT
37 return list of configuration options for the named section
40 read and parse the list of named configuration files
42 get(section, option, raw=0, vars=None)
43 return a string value for the named option. All % interpolations are
44 expanded in the return values, based on the defaults passed into the
45 constructor and the DEFAULT section. Additional substitutions may be
46 provided using the `vars' argument, which must be a dictionary whose
47 contents override any pre-existing defaults.
49 getint(section, options)
50 like get(), but convert value to an integer
52 getfloat(section, options)
53 like get(), but convert value to a float
55 getboolean(section, options)
56 like get(), but convert value to a boolean (currently defined as 0 or
64 DEFAULTSECT
= "DEFAULT"
70 def __init__(self
, msg
=''):
75 class NoSectionError(Error
):
76 def __init__(self
, section
):
77 Error
.__init
__(self
, 'No section: %s' % section
)
78 self
.section
= section
80 class DuplicateSectionError(Error
):
81 def __init__(self
, section
):
82 Error
.__init
__(self
, "Section %s already exists" % section
)
83 self
.section
= section
85 class NoOptionError(Error
):
86 def __init__(self
, option
, section
):
87 Error
.__init
__(self
, "No option `%s' in section: %s" %
90 self
.section
= section
92 class InterpolationError(Error
):
93 def __init__(self
, reference
, option
, section
, rawval
):
95 "Bad value substitution:\n"
100 % (section
, option
, reference
, rawval
))
101 self
.reference
= reference
103 self
.section
= section
105 class MissingSectionHeaderError(Error
):
106 def __init__(self
, filename
, lineno
, line
):
109 'File contains no section headers.\nfile: %s, line: %d\n%s' %
110 (filename
, lineno
, line
))
111 self
.filename
= filename
115 class ParsingError(Error
):
116 def __init__(self
, filename
):
117 Error
.__init
__(self
, 'File contains parsing errors: %s' % filename
)
118 self
.filename
= filename
121 def append(self
, lineno
, line
):
122 self
.errors
.append((lineno
, line
))
123 self
._msg
= self
._msg
+ '\n\t[line %2d]: %s' % (lineno
, line
)
128 def __init__(self
, defaults
=None):
133 self
.__defaults
= defaults
136 return self
.__defaults
139 """Return a list of section names, excluding [DEFAULT]"""
140 # self.__sections will never have [DEFAULT] in it
141 return self
.__sections
.keys()
143 def add_section(self
, section
):
144 """Create a new section in the configuration.
146 Raise DuplicateSectionError if a section by the specified name
149 if self
.__sections
.has_key(section
):
150 raise DuplicateSectionError(section
)
151 self
.__sections
[section
] = {}
153 def has_section(self
, section
):
154 """Indicate whether the named section is present in the configuration.
156 The DEFAULT section is not acknowledged.
158 return self
.__sections
.has_key(section
)
160 def options(self
, section
):
162 opts
= self
.__sections
[section
].copy()
164 raise NoSectionError(section
)
165 opts
.update(self
.__defaults
)
168 def read(self
, filenames
):
169 """Read and parse a list of filenames."""
170 if type(filenames
) is type(''):
171 filenames
= [filenames
]
172 for file in filenames
:
179 def get(self
, section
, option
, raw
=0, vars=None):
180 """Get an option value for a given section.
182 All % interpolations are expanded in the return values, based on the
183 defaults passed into the constructor, unless the optional argument
184 `raw' is true. Additional substitutions may be provided using the
185 `vars' argument, which must be a dictionary whose contents overrides
186 any pre-existing defaults.
188 The section DEFAULT is special.
191 sectdict
= self
.__sections
[section
].copy()
193 if section
== DEFAULTSECT
:
196 raise NoSectionError(section
)
197 d
= self
.__defaults
.copy()
199 # Update with the entry specific variables
202 option
= string
.lower(option
)
206 raise NoOptionError(option
, section
)
207 # do the string interpolation
211 value
= rawval
# Make it a pretty variable name
213 while depth
< 10: # Loop through this until it's done
215 if not string
.find(value
, "%("):
218 except KeyError, key
:
219 raise InterpolationError(key
, option
, section
, rawval
)
223 def __get(self
, section
, conv
, option
):
224 return conv(self
.get(section
, option
))
226 def getint(self
, section
, option
):
227 return self
.__get
(section
, string
.atoi
, option
)
229 def getfloat(self
, section
, option
):
230 return self
.__get
(section
, string
.atof
, option
)
232 def getboolean(self
, section
, option
):
233 v
= self
.get(section
, option
)
235 if val
not in (0, 1):
236 raise ValueError, 'Not a boolean: %s' % v
240 # Regular expressions for parsing section headers and options. Note a
241 # slight semantic change from the previous version, because of the use
242 # of \w, _ is allowed in section header names.
243 __SECTCRE
= re
.compile(
245 r
'(?P<header>[-\w]+)' # `-', `_' or any alphanum
248 __OPTCRE
= re
.compile(
249 r
'(?P<option>[-.\w]+)' # - . _ alphanum
250 r
'[ \t]*[:=][ \t]*' # any number of space/tab,
251 # followed by separator
252 # (either : or =), followed
254 r
'(?P<value>.*)$' # everything up to eol
257 def __read(self
, fp
):
258 """Parse a sectioned setup file.
260 The sections in setup file contains a title line at the top,
261 indicated by a name in square brackets (`[]'), plus key/value
262 options lines, indicated by `name: value' format lines.
263 Continuation are represented by an embedded newline then
264 leading whitespace. Blank lines, lines beginning with a '#',
265 and just about everything else is ignored.
267 cursect
= None # None, or a dictionary
270 e
= None # None, or an exception
276 # comment or blank line?
277 if string
.strip(line
) == '' or line
[0] in '#;':
279 if string
.lower(string
.split(line
)[0]) == 'rem' \
280 and line
[0] == "r": # no leading whitespace
283 if line
[0] in ' \t' and cursect
is not None and optname
:
284 value
= string
.strip(line
)
286 cursect
[optname
] = cursect
[optname
] + '\n ' + value
287 # a section header or option header?
289 # is it a section header?
290 mo
= self
.__SECTCRE
.match(line
)
292 sectname
= mo
.group('header')
293 if self
.__sections
.has_key(sectname
):
294 cursect
= self
.__sections
[sectname
]
295 elif sectname
== DEFAULTSECT
:
296 cursect
= self
.__defaults
298 cursect
= {'__name__': sectname
}
299 self
.__sections
[sectname
] = cursect
300 # So sections can't start with a continuation line
302 # no section header in the file?
303 elif cursect
is None:
304 raise MissingSectionHeaderError(fp
.name
, lineno
, `line`
)
307 mo
= self
.__OPTCRE
.match(line
)
309 optname
, optval
= mo
.group('option', 'value')
310 optname
= string
.lower(optname
)
311 optval
= string
.strip(optval
)
315 cursect
[optname
] = optval
317 # a non-fatal parsing error occurred. set up the
318 # exception but keep going. the exception will be
319 # raised at the end of the file and will contain a
320 # list of all bogus lines
322 e
= ParsingError(fp
.name
)
323 e
.append(lineno
, `line`
)
324 # if any parsing errors occurred, raise an exception