blob: fec847c5cee6d2e09bca966f817b825496b31d1a [file] [log] [blame]
Guido van Rossum3d209861997-12-09 16:10:31 +00001"""Configuration file parser.
2
3A setup file consists of sections, lead by a "[section]" header,
4and followed by "name: value" entries, with continuations and such in
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00005the style of RFC 822.
Guido van Rossum3d209861997-12-09 16:10:31 +00006
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00007The option values can contain format strings which refer to other values in
8the same section, or values in a special [DEFAULT] section.
9
Guido van Rossum3d209861997-12-09 16:10:31 +000010For example:
11
12 something: %(dir)s/whatever
13
14would resolve the "%(dir)s" to the value of dir. All reference
15expansions are done late, on demand.
16
17Intrinsic defaults can be specified by passing them into the
18ConfigParser constructor as a dictionary.
19
20class:
21
22ConfigParser -- responsible for for parsing a list of
23 configuration files, and managing the parsed database.
24
25 methods:
26
Barry Warsawf09f6a51999-01-26 22:01:37 +000027 __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.
Guido van Rossum3d209861997-12-09 16:10:31 +000032
Barry Warsawf09f6a51999-01-26 22:01:37 +000033 sections()
34 return all the configuration section names, sans DEFAULT
Guido van Rossum3d209861997-12-09 16:10:31 +000035
Guido van Rossuma5a24b71999-10-04 19:58:22 +000036 has_section(section)
37 return whether the given section exists
38
Eric S. Raymond649685a2000-07-14 14:28:22 +000039 has_option(section, option)
40 return whether the given option exists in the given section
41
Barry Warsawf09f6a51999-01-26 22:01:37 +000042 options(section)
43 return list of configuration options for the named section
Guido van Rossum3d209861997-12-09 16:10:31 +000044
Guido van Rossumc0780ac1999-01-30 04:35:47 +000045 read(filenames)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000046 read and parse the list of named configuration files, given by
47 name. A single filename is also allowed. Non-existing files
48 are ignored.
49
50 readfp(fp, filename=None)
51 read and parse one configuration file, given as a file object.
52 The filename defaults to fp.name; it is only used in error
Barry Warsaw25394511999-10-12 16:12:48 +000053 messages (if fp has no `name' attribute, the string `<???>' is used).
Guido van Rossum3d209861997-12-09 16:10:31 +000054
Barry Warsawf09f6a51999-01-26 22:01:37 +000055 get(section, option, raw=0, vars=None)
56 return a string value for the named option. All % interpolations are
57 expanded in the return values, based on the defaults passed into the
58 constructor and the DEFAULT section. Additional substitutions may be
59 provided using the `vars' argument, which must be a dictionary whose
60 contents override any pre-existing defaults.
Guido van Rossum3d209861997-12-09 16:10:31 +000061
Barry Warsawf09f6a51999-01-26 22:01:37 +000062 getint(section, options)
63 like get(), but convert value to an integer
Guido van Rossum3d209861997-12-09 16:10:31 +000064
Barry Warsawf09f6a51999-01-26 22:01:37 +000065 getfloat(section, options)
66 like get(), but convert value to a float
Guido van Rossum3d209861997-12-09 16:10:31 +000067
Barry Warsawf09f6a51999-01-26 22:01:37 +000068 getboolean(section, options)
69 like get(), but convert value to a boolean (currently defined as 0 or
70 1, only)
Eric S. Raymond649685a2000-07-14 14:28:22 +000071
72 remove_section(section)
Tim Peters88869f92001-01-14 23:36:06 +000073 remove the given file section and all its options
Eric S. Raymond649685a2000-07-14 14:28:22 +000074
75 remove_option(section, option)
Tim Peters88869f92001-01-14 23:36:06 +000076 remove the given option from the given section
Eric S. Raymond649685a2000-07-14 14:28:22 +000077
78 set(section, option, value)
79 set the given option
80
81 write(fp)
Tim Peters88869f92001-01-14 23:36:06 +000082 write the configuration state in .ini format
Guido van Rossum3d209861997-12-09 16:10:31 +000083"""
84
Martin v. Löwis339d0f72001-08-17 18:39:25 +000085import string, types
Barry Warsawbfa3f6b1998-07-01 20:41:12 +000086import re
Guido van Rossum3d209861997-12-09 16:10:31 +000087
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000088__all__ = ["NoSectionError","DuplicateSectionError","NoOptionError",
89 "InterpolationError","InterpolationDepthError","ParsingError",
90 "MissingSectionHeaderError","ConfigParser",
91 "MAX_INTERPOLATION_DEPTH"]
92
Guido van Rossum3d209861997-12-09 16:10:31 +000093DEFAULTSECT = "DEFAULT"
94
Fred Drake2a37f9f2000-09-27 22:43:54 +000095MAX_INTERPOLATION_DEPTH = 10
96
Guido van Rossum3d209861997-12-09 16:10:31 +000097
Tim Peters88869f92001-01-14 23:36:06 +000098
Guido van Rossum3d209861997-12-09 16:10:31 +000099# exception classes
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000100class Error(Exception):
Guido van Rossum3d209861997-12-09 16:10:31 +0000101 def __init__(self, msg=''):
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000102 self._msg = msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000103 Exception.__init__(self, msg)
Guido van Rossum3d209861997-12-09 16:10:31 +0000104 def __repr__(self):
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000105 return self._msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000106 __str__ = __repr__
Guido van Rossum3d209861997-12-09 16:10:31 +0000107
108class NoSectionError(Error):
109 def __init__(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000110 Error.__init__(self, 'No section: %s' % section)
111 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000112
113class DuplicateSectionError(Error):
114 def __init__(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000115 Error.__init__(self, "Section %s already exists" % section)
116 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000117
118class NoOptionError(Error):
119 def __init__(self, option, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 Error.__init__(self, "No option `%s' in section: %s" %
121 (option, section))
122 self.option = option
123 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000124
125class InterpolationError(Error):
Barry Warsaw64462121998-08-06 18:48:41 +0000126 def __init__(self, reference, option, section, rawval):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000127 Error.__init__(self,
Barry Warsaw64462121998-08-06 18:48:41 +0000128 "Bad value substitution:\n"
129 "\tsection: [%s]\n"
130 "\toption : %s\n"
131 "\tkey : %s\n"
132 "\trawval : %s\n"
133 % (section, option, reference, rawval))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000134 self.reference = reference
135 self.option = option
136 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000137
Fred Drake2a37f9f2000-09-27 22:43:54 +0000138class InterpolationDepthError(Error):
139 def __init__(self, option, section, rawval):
140 Error.__init__(self,
141 "Value interpolation too deeply recursive:\n"
142 "\tsection: [%s]\n"
143 "\toption : %s\n"
144 "\trawval : %s\n"
145 % (section, option, rawval))
146 self.option = option
147 self.section = section
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000148
149class ParsingError(Error):
150 def __init__(self, filename):
151 Error.__init__(self, 'File contains parsing errors: %s' % filename)
152 self.filename = filename
153 self.errors = []
154
155 def append(self, lineno, line):
156 self.errors.append((lineno, line))
157 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
158
Fred Drake2a37f9f2000-09-27 22:43:54 +0000159class MissingSectionHeaderError(ParsingError):
160 def __init__(self, filename, lineno, line):
161 Error.__init__(
162 self,
163 'File contains no section headers.\nfile: %s, line: %d\n%s' %
164 (filename, lineno, line))
165 self.filename = filename
166 self.lineno = lineno
167 self.line = line
168
Guido van Rossum3d209861997-12-09 16:10:31 +0000169
Tim Peters88869f92001-01-14 23:36:06 +0000170
Guido van Rossum3d209861997-12-09 16:10:31 +0000171class ConfigParser:
172 def __init__(self, defaults=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000173 self.__sections = {}
174 if defaults is None:
175 self.__defaults = {}
176 else:
177 self.__defaults = defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000178
179 def defaults(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000180 return self.__defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000181
182 def sections(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000183 """Return a list of section names, excluding [DEFAULT]"""
184 # self.__sections will never have [DEFAULT] in it
185 return self.__sections.keys()
Guido van Rossum3d209861997-12-09 16:10:31 +0000186
187 def add_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000188 """Create a new section in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000189
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000190 Raise DuplicateSectionError if a section by the specified name
191 already exists.
192 """
193 if self.__sections.has_key(section):
194 raise DuplicateSectionError(section)
195 self.__sections[section] = {}
Guido van Rossum3d209861997-12-09 16:10:31 +0000196
197 def has_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000198 """Indicate whether the named section is present in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000199
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000200 The DEFAULT section is not acknowledged.
201 """
Fred Drake2a37f9f2000-09-27 22:43:54 +0000202 return section in self.sections()
Guido van Rossum3d209861997-12-09 16:10:31 +0000203
204 def options(self, section):
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000205 """Return a list of option names for the given section name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000206 try:
207 opts = self.__sections[section].copy()
208 except KeyError:
209 raise NoSectionError(section)
210 opts.update(self.__defaults)
Fred Drake2a37f9f2000-09-27 22:43:54 +0000211 if opts.has_key('__name__'):
212 del opts['__name__']
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000213 return opts.keys()
Guido van Rossum3d209861997-12-09 16:10:31 +0000214
215 def read(self, filenames):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000216 """Read and parse a filename or a list of filenames.
Tim Peters88869f92001-01-14 23:36:06 +0000217
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000218 Files that cannot be opened are silently ignored; this is
Barry Warsaw25394511999-10-12 16:12:48 +0000219 designed so that you can specify a list of potential
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000220 configuration file locations (e.g. current directory, user's
221 home directory, systemwide directory), and all existing
222 configuration files in the list will be read. A single
223 filename may also be given.
224 """
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000225 if type(filenames) in types.StringTypes:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000226 filenames = [filenames]
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000227 for filename in filenames:
228 try:
229 fp = open(filename)
230 except IOError:
231 continue
232 self.__read(fp, filename)
Fred Drake2438a481999-10-04 18:11:56 +0000233 fp.close()
Guido van Rossum3d209861997-12-09 16:10:31 +0000234
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000235 def readfp(self, fp, filename=None):
236 """Like read() but the argument must be a file-like object.
237
238 The `fp' argument must have a `readline' method. Optional
239 second argument is the `filename', which if not given, is
240 taken from fp.name. If fp has no `name' attribute, `<???>' is
241 used.
242
243 """
244 if filename is None:
245 try:
246 filename = fp.name
247 except AttributeError:
248 filename = '<???>'
249 self.__read(fp, filename)
250
Guido van Rossume6506e71999-01-26 19:29:25 +0000251 def get(self, section, option, raw=0, vars=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000252 """Get an option value for a given section.
Guido van Rossum3d209861997-12-09 16:10:31 +0000253
Barry Warsawf09f6a51999-01-26 22:01:37 +0000254 All % interpolations are expanded in the return values, based on the
255 defaults passed into the constructor, unless the optional argument
256 `raw' is true. Additional substitutions may be provided using the
257 `vars' argument, which must be a dictionary whose contents overrides
258 any pre-existing defaults.
Guido van Rossum3d209861997-12-09 16:10:31 +0000259
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000260 The section DEFAULT is special.
261 """
262 try:
263 sectdict = self.__sections[section].copy()
264 except KeyError:
265 if section == DEFAULTSECT:
266 sectdict = {}
267 else:
268 raise NoSectionError(section)
269 d = self.__defaults.copy()
270 d.update(sectdict)
Guido van Rossume6506e71999-01-26 19:29:25 +0000271 # Update with the entry specific variables
272 if vars:
273 d.update(vars)
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000274 option = self.optionxform(option)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000275 try:
276 rawval = d[option]
277 except KeyError:
278 raise NoOptionError(option, section)
Fred Drake2a37f9f2000-09-27 22:43:54 +0000279
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000280 if raw:
281 return rawval
Guido van Rossum3d209861997-12-09 16:10:31 +0000282
Fred Drake2a37f9f2000-09-27 22:43:54 +0000283 # do the string interpolation
Guido van Rossume6506e71999-01-26 19:29:25 +0000284 value = rawval # Make it a pretty variable name
Tim Peters88869f92001-01-14 23:36:06 +0000285 depth = 0
Guido van Rossum72ce8581999-02-12 14:13:10 +0000286 while depth < 10: # Loop through this until it's done
287 depth = depth + 1
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000288 if value.find("%(") >= 0:
Guido van Rossume6506e71999-01-26 19:29:25 +0000289 try:
290 value = value % d
291 except KeyError, key:
292 raise InterpolationError(key, option, section, rawval)
293 else:
Fred Drake2a37f9f2000-09-27 22:43:54 +0000294 break
295 if value.find("%(") >= 0:
296 raise InterpolationDepthError(option, section, rawval)
297 return value
Tim Peters88869f92001-01-14 23:36:06 +0000298
Guido van Rossum3d209861997-12-09 16:10:31 +0000299 def __get(self, section, conv, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000300 return conv(self.get(section, option))
Guido van Rossum3d209861997-12-09 16:10:31 +0000301
302 def getint(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000303 return self.__get(section, string.atoi, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000304
305 def getfloat(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000306 return self.__get(section, string.atof, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000307
308 def getboolean(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000309 v = self.get(section, option)
Eric S. Raymondf2960192001-02-09 05:37:25 +0000310 val = int(v)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000311 if val not in (0, 1):
312 raise ValueError, 'Not a boolean: %s' % v
313 return val
Guido van Rossum3d209861997-12-09 16:10:31 +0000314
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000315 def optionxform(self, optionstr):
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000316 return optionstr.lower()
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000317
Eric S. Raymond417c4892000-07-10 18:11:00 +0000318 def has_option(self, section, option):
319 """Check for the existence of a given option in a given section."""
320 if not section or section == "DEFAULT":
321 return self.__defaults.has_key(option)
322 elif not self.has_section(section):
323 return 0
324 else:
Fred Drake3c823aa2001-02-26 21:55:34 +0000325 option = self.optionxform(option)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000326 return self.__sections[section].has_key(option)
327
328 def set(self, section, option, value):
329 """Set an option."""
330 if not section or section == "DEFAULT":
331 sectdict = self.__defaults
332 else:
333 try:
334 sectdict = self.__sections[section]
335 except KeyError:
336 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000337 option = self.optionxform(option)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000338 sectdict[option] = value
339
340 def write(self, fp):
341 """Write an .ini-format representation of the configuration state."""
342 if self.__defaults:
343 fp.write("[DEFAULT]\n")
Eric S. Raymond649685a2000-07-14 14:28:22 +0000344 for (key, value) in self.__defaults.items():
345 fp.write("%s = %s\n" % (key, value))
Eric S. Raymond417c4892000-07-10 18:11:00 +0000346 fp.write("\n")
347 for section in self.sections():
348 fp.write("[" + section + "]\n")
349 sectdict = self.__sections[section]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000350 for (key, value) in sectdict.items():
Eric S. Raymond417c4892000-07-10 18:11:00 +0000351 if key == "__name__":
352 continue
Eric S. Raymond649685a2000-07-14 14:28:22 +0000353 fp.write("%s = %s\n" % (key, value))
Eric S. Raymond417c4892000-07-10 18:11:00 +0000354 fp.write("\n")
355
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000356 def remove_option(self, section, option):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000357 """Remove an option."""
358 if not section or section == "DEFAULT":
359 sectdict = self.__defaults
360 else:
361 try:
362 sectdict = self.__sections[section]
363 except KeyError:
364 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000365 option = self.optionxform(option)
Fred Drakeff4a23b2000-12-04 16:29:13 +0000366 existed = sectdict.has_key(option)
Eric S. Raymond649685a2000-07-14 14:28:22 +0000367 if existed:
Fred Drakeff4a23b2000-12-04 16:29:13 +0000368 del sectdict[option]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000369 return existed
370
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000371 def remove_section(self, section):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000372 """Remove a file section."""
373 if self.__sections.has_key(section):
374 del self.__sections[section]
375 return 1
376 else:
377 return 0
378
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000379 #
380 # Regular expressions for parsing section headers and options. Note a
381 # slight semantic change from the previous version, because of the use
382 # of \w, _ is allowed in section header names.
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000383 SECTCRE = re.compile(
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000384 r'\[' # [
Fred Draked4df94b2001-02-14 15:24:17 +0000385 r'(?P<header>[^]]+)' # very permissive!
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000386 r'\]' # ]
387 )
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000388 OPTCRE = re.compile(
Fred Draked83bbbf2001-02-12 17:18:11 +0000389 r'(?P<option>[]\-[\w_.*,(){}]+)' # a lot of stuff found by IvL
Fred Drakec517b9b2000-02-28 20:59:03 +0000390 r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000391 # followed by separator
392 # (either : or =), followed
393 # by any # space/tab
394 r'(?P<value>.*)$' # everything up to eol
395 )
396
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000397 def __read(self, fp, fpname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000398 """Parse a sectioned setup file.
Guido van Rossum3d209861997-12-09 16:10:31 +0000399
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000400 The sections in setup file contains a title line at the top,
401 indicated by a name in square brackets (`[]'), plus key/value
402 options lines, indicated by `name: value' format lines.
403 Continuation are represented by an embedded newline then
404 leading whitespace. Blank lines, lines beginning with a '#',
405 and just about everything else is ignored.
406 """
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000407 cursect = None # None, or a dictionary
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000408 optname = None
409 lineno = 0
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000410 e = None # None, or an exception
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000411 while 1:
412 line = fp.readline()
413 if not line:
414 break
415 lineno = lineno + 1
416 # comment or blank line?
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000417 if line.strip() == '' or line[0] in '#;':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000418 continue
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000419 if line.split()[0].lower() == 'rem' \
Fred Drakec517b9b2000-02-28 20:59:03 +0000420 and line[0] in "rR": # no leading whitespace
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000421 continue
422 # continuation line?
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000423 if line[0] in ' \t' and cursect is not None and optname:
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000424 value = line.strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000425 if value:
Fred Drakebeb67132001-07-06 17:22:48 +0000426 k = self.optionxform(optname)
427 cursect[k] = "%s\n%s" % (cursect[k], value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000428 # a section header or option header?
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000429 else:
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000430 # is it a section header?
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000431 mo = self.SECTCRE.match(line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000432 if mo:
433 sectname = mo.group('header')
434 if self.__sections.has_key(sectname):
435 cursect = self.__sections[sectname]
436 elif sectname == DEFAULTSECT:
437 cursect = self.__defaults
438 else:
Barry Warsaw64462121998-08-06 18:48:41 +0000439 cursect = {'__name__': sectname}
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000440 self.__sections[sectname] = cursect
441 # So sections can't start with a continuation line
442 optname = None
443 # no section header in the file?
444 elif cursect is None:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000445 raise MissingSectionHeaderError(fpname, lineno, `line`)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000446 # an option line?
447 else:
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000448 mo = self.OPTCRE.match(line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000449 if mo:
Fred Drakec517b9b2000-02-28 20:59:03 +0000450 optname, vi, optval = mo.group('option', 'vi', 'value')
Jeremy Hylton820314e2000-03-03 20:43:57 +0000451 if vi in ('=', ':') and ';' in optval:
Fred Drakec517b9b2000-02-28 20:59:03 +0000452 # ';' is a comment delimiter only if it follows
453 # a spacing character
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000454 pos = optval.find(';')
Fred Drakec517b9b2000-02-28 20:59:03 +0000455 if pos and optval[pos-1] in string.whitespace:
456 optval = optval[:pos]
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000457 optval = optval.strip()
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000458 # allow empty values
459 if optval == '""':
460 optval = ''
Guido van Rossum41267362000-09-25 14:42:33 +0000461 cursect[self.optionxform(optname)] = optval
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000462 else:
463 # a non-fatal parsing error occurred. set up the
464 # exception but keep going. the exception will be
465 # raised at the end of the file and will contain a
466 # list of all bogus lines
467 if not e:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000468 e = ParsingError(fpname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000469 e.append(lineno, `line`)
470 # if any parsing errors occurred, raise an exception
471 if e:
472 raise e