blob: 699803d84a5a117ca825afedda35ed8ce463d89b [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 Rossuma5a24b71999-10-04 19:58:22 +000045 has_option(section, option)
46 return whether the given section has the given option
47
Guido van Rossumc0780ac1999-01-30 04:35:47 +000048 read(filenames)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000049 read and parse the list of named configuration files, given by
50 name. A single filename is also allowed. Non-existing files
51 are ignored.
52
53 readfp(fp, filename=None)
54 read and parse one configuration file, given as a file object.
55 The filename defaults to fp.name; it is only used in error
Barry Warsaw25394511999-10-12 16:12:48 +000056 messages (if fp has no `name' attribute, the string `<???>' is used).
Guido van Rossum3d209861997-12-09 16:10:31 +000057
Barry Warsawf09f6a51999-01-26 22:01:37 +000058 get(section, option, raw=0, vars=None)
59 return a string value for the named option. All % interpolations are
60 expanded in the return values, based on the defaults passed into the
61 constructor and the DEFAULT section. Additional substitutions may be
62 provided using the `vars' argument, which must be a dictionary whose
63 contents override any pre-existing defaults.
Guido van Rossum3d209861997-12-09 16:10:31 +000064
Barry Warsawf09f6a51999-01-26 22:01:37 +000065 getint(section, options)
66 like get(), but convert value to an integer
Guido van Rossum3d209861997-12-09 16:10:31 +000067
Barry Warsawf09f6a51999-01-26 22:01:37 +000068 getfloat(section, options)
69 like get(), but convert value to a float
Guido van Rossum3d209861997-12-09 16:10:31 +000070
Barry Warsawf09f6a51999-01-26 22:01:37 +000071 getboolean(section, options)
72 like get(), but convert value to a boolean (currently defined as 0 or
73 1, only)
Eric S. Raymond649685a2000-07-14 14:28:22 +000074
75 remove_section(section)
Tim Peters88869f92001-01-14 23:36:06 +000076 remove the given file section and all its options
Eric S. Raymond649685a2000-07-14 14:28:22 +000077
78 remove_option(section, option)
Tim Peters88869f92001-01-14 23:36:06 +000079 remove the given option from the given section
Eric S. Raymond649685a2000-07-14 14:28:22 +000080
81 set(section, option, value)
82 set the given option
83
84 write(fp)
Tim Peters88869f92001-01-14 23:36:06 +000085 write the configuration state in .ini format
Guido van Rossum3d209861997-12-09 16:10:31 +000086"""
87
Guido van Rossum3d209861997-12-09 16:10:31 +000088import string
Barry Warsawbfa3f6b1998-07-01 20:41:12 +000089import re
Guido van Rossum3d209861997-12-09 16:10:31 +000090
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000091__all__ = ["NoSectionError","DuplicateSectionError","NoOptionError",
92 "InterpolationError","InterpolationDepthError","ParsingError",
93 "MissingSectionHeaderError","ConfigParser",
94 "MAX_INTERPOLATION_DEPTH"]
95
Guido van Rossum3d209861997-12-09 16:10:31 +000096DEFAULTSECT = "DEFAULT"
97
Fred Drake2a37f9f2000-09-27 22:43:54 +000098MAX_INTERPOLATION_DEPTH = 10
99
Guido van Rossum3d209861997-12-09 16:10:31 +0000100
Tim Peters88869f92001-01-14 23:36:06 +0000101
Guido van Rossum3d209861997-12-09 16:10:31 +0000102# exception classes
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000103class Error(Exception):
Guido van Rossum3d209861997-12-09 16:10:31 +0000104 def __init__(self, msg=''):
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000105 self._msg = msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000106 Exception.__init__(self, msg)
Guido van Rossum3d209861997-12-09 16:10:31 +0000107 def __repr__(self):
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000108 return self._msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000109 __str__ = __repr__
Guido van Rossum3d209861997-12-09 16:10:31 +0000110
111class NoSectionError(Error):
112 def __init__(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000113 Error.__init__(self, 'No section: %s' % section)
114 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000115
116class DuplicateSectionError(Error):
117 def __init__(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000118 Error.__init__(self, "Section %s already exists" % section)
119 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000120
121class NoOptionError(Error):
122 def __init__(self, option, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 Error.__init__(self, "No option `%s' in section: %s" %
124 (option, section))
125 self.option = option
126 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000127
128class InterpolationError(Error):
Barry Warsaw64462121998-08-06 18:48:41 +0000129 def __init__(self, reference, option, section, rawval):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 Error.__init__(self,
Barry Warsaw64462121998-08-06 18:48:41 +0000131 "Bad value substitution:\n"
132 "\tsection: [%s]\n"
133 "\toption : %s\n"
134 "\tkey : %s\n"
135 "\trawval : %s\n"
136 % (section, option, reference, rawval))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 self.reference = reference
138 self.option = option
139 self.section = section
Guido van Rossum3d209861997-12-09 16:10:31 +0000140
Fred Drake2a37f9f2000-09-27 22:43:54 +0000141class InterpolationDepthError(Error):
142 def __init__(self, option, section, rawval):
143 Error.__init__(self,
144 "Value interpolation too deeply recursive:\n"
145 "\tsection: [%s]\n"
146 "\toption : %s\n"
147 "\trawval : %s\n"
148 % (section, option, rawval))
149 self.option = option
150 self.section = section
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000151
152class ParsingError(Error):
153 def __init__(self, filename):
154 Error.__init__(self, 'File contains parsing errors: %s' % filename)
155 self.filename = filename
156 self.errors = []
157
158 def append(self, lineno, line):
159 self.errors.append((lineno, line))
160 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
161
Fred Drake2a37f9f2000-09-27 22:43:54 +0000162class MissingSectionHeaderError(ParsingError):
163 def __init__(self, filename, lineno, line):
164 Error.__init__(
165 self,
166 'File contains no section headers.\nfile: %s, line: %d\n%s' %
167 (filename, lineno, line))
168 self.filename = filename
169 self.lineno = lineno
170 self.line = line
171
Guido van Rossum3d209861997-12-09 16:10:31 +0000172
Tim Peters88869f92001-01-14 23:36:06 +0000173
Guido van Rossum3d209861997-12-09 16:10:31 +0000174class ConfigParser:
175 def __init__(self, defaults=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000176 self.__sections = {}
177 if defaults is None:
178 self.__defaults = {}
179 else:
180 self.__defaults = defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000181
182 def defaults(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000183 return self.__defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000184
185 def sections(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 """Return a list of section names, excluding [DEFAULT]"""
187 # self.__sections will never have [DEFAULT] in it
188 return self.__sections.keys()
Guido van Rossum3d209861997-12-09 16:10:31 +0000189
190 def add_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000191 """Create a new section in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000192
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000193 Raise DuplicateSectionError if a section by the specified name
194 already exists.
195 """
196 if self.__sections.has_key(section):
197 raise DuplicateSectionError(section)
198 self.__sections[section] = {}
Guido van Rossum3d209861997-12-09 16:10:31 +0000199
200 def has_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000201 """Indicate whether the named section is present in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000202
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000203 The DEFAULT section is not acknowledged.
204 """
Fred Drake2a37f9f2000-09-27 22:43:54 +0000205 return section in self.sections()
Guido van Rossum3d209861997-12-09 16:10:31 +0000206
207 def options(self, section):
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000208 """Return a list of option names for the given section name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000209 try:
210 opts = self.__sections[section].copy()
211 except KeyError:
212 raise NoSectionError(section)
213 opts.update(self.__defaults)
Fred Drake2a37f9f2000-09-27 22:43:54 +0000214 if opts.has_key('__name__'):
215 del opts['__name__']
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000216 return opts.keys()
Guido van Rossum3d209861997-12-09 16:10:31 +0000217
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000218 def has_option(self, section, option):
219 """Return whether the given section has the given option."""
Fred Drake2a37f9f2000-09-27 22:43:54 +0000220 return option in self.options(section)
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000221
Guido van Rossum3d209861997-12-09 16:10:31 +0000222 def read(self, filenames):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000223 """Read and parse a filename or a list of filenames.
Tim Peters88869f92001-01-14 23:36:06 +0000224
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000225 Files that cannot be opened are silently ignored; this is
Barry Warsaw25394511999-10-12 16:12:48 +0000226 designed so that you can specify a list of potential
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000227 configuration file locations (e.g. current directory, user's
228 home directory, systemwide directory), and all existing
229 configuration files in the list will be read. A single
230 filename may also be given.
231 """
Fred Drakefd4114e2000-05-09 14:46:40 +0000232 if type(filenames) in [type(''), type(u'')]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000233 filenames = [filenames]
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000234 for filename in filenames:
235 try:
236 fp = open(filename)
237 except IOError:
238 continue
239 self.__read(fp, filename)
Fred Drake2438a481999-10-04 18:11:56 +0000240 fp.close()
Guido van Rossum3d209861997-12-09 16:10:31 +0000241
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000242 def readfp(self, fp, filename=None):
243 """Like read() but the argument must be a file-like object.
244
245 The `fp' argument must have a `readline' method. Optional
246 second argument is the `filename', which if not given, is
247 taken from fp.name. If fp has no `name' attribute, `<???>' is
248 used.
249
250 """
251 if filename is None:
252 try:
253 filename = fp.name
254 except AttributeError:
255 filename = '<???>'
256 self.__read(fp, filename)
257
Guido van Rossume6506e71999-01-26 19:29:25 +0000258 def get(self, section, option, raw=0, vars=None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000259 """Get an option value for a given section.
Guido van Rossum3d209861997-12-09 16:10:31 +0000260
Barry Warsawf09f6a51999-01-26 22:01:37 +0000261 All % interpolations are expanded in the return values, based on the
262 defaults passed into the constructor, unless the optional argument
263 `raw' is true. Additional substitutions may be provided using the
264 `vars' argument, which must be a dictionary whose contents overrides
265 any pre-existing defaults.
Guido van Rossum3d209861997-12-09 16:10:31 +0000266
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000267 The section DEFAULT is special.
268 """
269 try:
270 sectdict = self.__sections[section].copy()
271 except KeyError:
272 if section == DEFAULTSECT:
273 sectdict = {}
274 else:
275 raise NoSectionError(section)
276 d = self.__defaults.copy()
277 d.update(sectdict)
Guido van Rossume6506e71999-01-26 19:29:25 +0000278 # Update with the entry specific variables
279 if vars:
280 d.update(vars)
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000281 option = self.optionxform(option)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000282 try:
283 rawval = d[option]
284 except KeyError:
285 raise NoOptionError(option, section)
Fred Drake2a37f9f2000-09-27 22:43:54 +0000286
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000287 if raw:
288 return rawval
Guido van Rossum3d209861997-12-09 16:10:31 +0000289
Fred Drake2a37f9f2000-09-27 22:43:54 +0000290 # do the string interpolation
Guido van Rossume6506e71999-01-26 19:29:25 +0000291 value = rawval # Make it a pretty variable name
Tim Peters88869f92001-01-14 23:36:06 +0000292 depth = 0
Guido van Rossum72ce8581999-02-12 14:13:10 +0000293 while depth < 10: # Loop through this until it's done
294 depth = depth + 1
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000295 if value.find("%(") >= 0:
Guido van Rossume6506e71999-01-26 19:29:25 +0000296 try:
297 value = value % d
298 except KeyError, key:
299 raise InterpolationError(key, option, section, rawval)
300 else:
Fred Drake2a37f9f2000-09-27 22:43:54 +0000301 break
302 if value.find("%(") >= 0:
303 raise InterpolationDepthError(option, section, rawval)
304 return value
Tim Peters88869f92001-01-14 23:36:06 +0000305
Guido van Rossum3d209861997-12-09 16:10:31 +0000306 def __get(self, section, conv, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000307 return conv(self.get(section, option))
Guido van Rossum3d209861997-12-09 16:10:31 +0000308
309 def getint(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000310 return self.__get(section, string.atoi, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000311
312 def getfloat(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000313 return self.__get(section, string.atof, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000314
315 def getboolean(self, section, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000316 v = self.get(section, option)
Eric S. Raymondf2960192001-02-09 05:37:25 +0000317 val = int(v)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000318 if val not in (0, 1):
319 raise ValueError, 'Not a boolean: %s' % v
320 return val
Guido van Rossum3d209861997-12-09 16:10:31 +0000321
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000322 def optionxform(self, optionstr):
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000323 return optionstr.lower()
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000324
Eric S. Raymond417c4892000-07-10 18:11:00 +0000325 def has_option(self, section, option):
326 """Check for the existence of a given option in a given section."""
327 if not section or section == "DEFAULT":
328 return self.__defaults.has_key(option)
329 elif not self.has_section(section):
330 return 0
331 else:
Fred Drake3c823aa2001-02-26 21:55:34 +0000332 option = self.optionxform(option)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000333 return self.__sections[section].has_key(option)
334
335 def set(self, section, option, value):
336 """Set an option."""
337 if not section or section == "DEFAULT":
338 sectdict = self.__defaults
339 else:
340 try:
341 sectdict = self.__sections[section]
342 except KeyError:
343 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000344 option = self.optionxform(option)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000345 sectdict[option] = value
346
347 def write(self, fp):
348 """Write an .ini-format representation of the configuration state."""
349 if self.__defaults:
350 fp.write("[DEFAULT]\n")
Eric S. Raymond649685a2000-07-14 14:28:22 +0000351 for (key, value) in self.__defaults.items():
352 fp.write("%s = %s\n" % (key, value))
Eric S. Raymond417c4892000-07-10 18:11:00 +0000353 fp.write("\n")
354 for section in self.sections():
355 fp.write("[" + section + "]\n")
356 sectdict = self.__sections[section]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000357 for (key, value) in sectdict.items():
Eric S. Raymond417c4892000-07-10 18:11:00 +0000358 if key == "__name__":
359 continue
Eric S. Raymond649685a2000-07-14 14:28:22 +0000360 fp.write("%s = %s\n" % (key, value))
Eric S. Raymond417c4892000-07-10 18:11:00 +0000361 fp.write("\n")
362
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000363 def remove_option(self, section, option):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000364 """Remove an option."""
365 if not section or section == "DEFAULT":
366 sectdict = self.__defaults
367 else:
368 try:
369 sectdict = self.__sections[section]
370 except KeyError:
371 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000372 option = self.optionxform(option)
Fred Drakeff4a23b2000-12-04 16:29:13 +0000373 existed = sectdict.has_key(option)
Eric S. Raymond649685a2000-07-14 14:28:22 +0000374 if existed:
Fred Drakeff4a23b2000-12-04 16:29:13 +0000375 del sectdict[option]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000376 return existed
377
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000378 def remove_section(self, section):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000379 """Remove a file section."""
380 if self.__sections.has_key(section):
381 del self.__sections[section]
382 return 1
383 else:
384 return 0
385
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000386 #
387 # Regular expressions for parsing section headers and options. Note a
388 # slight semantic change from the previous version, because of the use
389 # of \w, _ is allowed in section header names.
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000390 SECTCRE = re.compile(
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000391 r'\[' # [
Fred Draked4df94b2001-02-14 15:24:17 +0000392 r'(?P<header>[^]]+)' # very permissive!
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000393 r'\]' # ]
394 )
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000395 OPTCRE = re.compile(
Fred Draked83bbbf2001-02-12 17:18:11 +0000396 r'(?P<option>[]\-[\w_.*,(){}]+)' # a lot of stuff found by IvL
Fred Drakec517b9b2000-02-28 20:59:03 +0000397 r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000398 # followed by separator
399 # (either : or =), followed
400 # by any # space/tab
401 r'(?P<value>.*)$' # everything up to eol
402 )
403
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000404 def __read(self, fp, fpname):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000405 """Parse a sectioned setup file.
Guido van Rossum3d209861997-12-09 16:10:31 +0000406
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000407 The sections in setup file contains a title line at the top,
408 indicated by a name in square brackets (`[]'), plus key/value
409 options lines, indicated by `name: value' format lines.
410 Continuation are represented by an embedded newline then
411 leading whitespace. Blank lines, lines beginning with a '#',
412 and just about everything else is ignored.
413 """
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000414 cursect = None # None, or a dictionary
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000415 optname = None
416 lineno = 0
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000417 e = None # None, or an exception
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000418 while 1:
419 line = fp.readline()
420 if not line:
421 break
422 lineno = lineno + 1
423 # comment or blank line?
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000424 if line.strip() == '' or line[0] in '#;':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000425 continue
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000426 if line.split()[0].lower() == 'rem' \
Fred Drakec517b9b2000-02-28 20:59:03 +0000427 and line[0] in "rR": # no leading whitespace
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000428 continue
429 # continuation line?
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000430 if line[0] in ' \t' and cursect is not None and optname:
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000431 value = line.strip()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000432 if value:
Fred Drakebeb67132001-07-06 17:22:48 +0000433 k = self.optionxform(optname)
434 cursect[k] = "%s\n%s" % (cursect[k], value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000435 # a section header or option header?
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000436 else:
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000437 # is it a section header?
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000438 mo = self.SECTCRE.match(line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000439 if mo:
440 sectname = mo.group('header')
441 if self.__sections.has_key(sectname):
442 cursect = self.__sections[sectname]
443 elif sectname == DEFAULTSECT:
444 cursect = self.__defaults
445 else:
Barry Warsaw64462121998-08-06 18:48:41 +0000446 cursect = {'__name__': sectname}
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000447 self.__sections[sectname] = cursect
448 # So sections can't start with a continuation line
449 optname = None
450 # no section header in the file?
451 elif cursect is None:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000452 raise MissingSectionHeaderError(fpname, lineno, `line`)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000453 # an option line?
454 else:
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000455 mo = self.OPTCRE.match(line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000456 if mo:
Fred Drakec517b9b2000-02-28 20:59:03 +0000457 optname, vi, optval = mo.group('option', 'vi', 'value')
Jeremy Hylton820314e2000-03-03 20:43:57 +0000458 if vi in ('=', ':') and ';' in optval:
Fred Drakec517b9b2000-02-28 20:59:03 +0000459 # ';' is a comment delimiter only if it follows
460 # a spacing character
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000461 pos = optval.find(';')
Fred Drakec517b9b2000-02-28 20:59:03 +0000462 if pos and optval[pos-1] in string.whitespace:
463 optval = optval[:pos]
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000464 optval = optval.strip()
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000465 # allow empty values
466 if optval == '""':
467 optval = ''
Guido van Rossum41267362000-09-25 14:42:33 +0000468 cursect[self.optionxform(optname)] = optval
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000469 else:
470 # a non-fatal parsing error occurred. set up the
471 # exception but keep going. the exception will be
472 # raised at the end of the file and will contain a
473 # list of all bogus lines
474 if not e:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000475 e = ParsingError(fpname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000476 e.append(lineno, `line`)
477 # if any parsing errors occurred, raise an exception
478 if e:
479 raise e