Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 1 | """Configuration file parser. |
| 2 | |
| 3 | A setup file consists of sections, lead by a "[section]" header, |
| 4 | and followed by "name: value" entries, with continuations and such in |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 5 | the style of RFC 822. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 6 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 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. |
| 9 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 10 | For example: |
| 11 | |
| 12 | something: %(dir)s/whatever |
| 13 | |
| 14 | would resolve the "%(dir)s" to the value of dir. All reference |
| 15 | expansions are done late, on demand. |
| 16 | |
| 17 | Intrinsic defaults can be specified by passing them into the |
| 18 | ConfigParser constructor as a dictionary. |
| 19 | |
| 20 | class: |
| 21 | |
| 22 | ConfigParser -- responsible for for parsing a list of |
| 23 | configuration files, and managing the parsed database. |
| 24 | |
| 25 | methods: |
| 26 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 32 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 33 | sections() |
| 34 | return all the configuration section names, sans DEFAULT |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 36 | has_section(section) |
| 37 | return whether the given section exists |
| 38 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 39 | options(section) |
| 40 | return list of configuration options for the named section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 42 | has_option(section, option) |
| 43 | return whether the given section has the given option |
| 44 | |
Guido van Rossum | c0780ac | 1999-01-30 04:35:47 +0000 | [diff] [blame] | 45 | read(filenames) |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 46 | 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 |
| 53 | messages. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 55 | 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 Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 62 | getint(section, options) |
| 63 | like get(), but convert value to an integer |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 65 | getfloat(section, options) |
| 66 | like get(), but convert value to a float |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 67 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 68 | getboolean(section, options) |
| 69 | like get(), but convert value to a boolean (currently defined as 0 or |
| 70 | 1, only) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 71 | """ |
| 72 | |
| 73 | import sys |
| 74 | import string |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 75 | import re |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 76 | |
| 77 | DEFAULTSECT = "DEFAULT" |
| 78 | |
| 79 | |
| 80 | |
| 81 | # exception classes |
| 82 | class Error: |
| 83 | def __init__(self, msg=''): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 84 | self._msg = msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 85 | def __repr__(self): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 86 | return self._msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 87 | |
| 88 | class NoSectionError(Error): |
| 89 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 90 | Error.__init__(self, 'No section: %s' % section) |
| 91 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 92 | |
| 93 | class DuplicateSectionError(Error): |
| 94 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | Error.__init__(self, "Section %s already exists" % section) |
| 96 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 97 | |
| 98 | class NoOptionError(Error): |
| 99 | def __init__(self, option, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 100 | Error.__init__(self, "No option `%s' in section: %s" % |
| 101 | (option, section)) |
| 102 | self.option = option |
| 103 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 104 | |
| 105 | class InterpolationError(Error): |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 106 | def __init__(self, reference, option, section, rawval): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | Error.__init__(self, |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 108 | "Bad value substitution:\n" |
| 109 | "\tsection: [%s]\n" |
| 110 | "\toption : %s\n" |
| 111 | "\tkey : %s\n" |
| 112 | "\trawval : %s\n" |
| 113 | % (section, option, reference, rawval)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | self.reference = reference |
| 115 | self.option = option |
| 116 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 118 | class MissingSectionHeaderError(Error): |
| 119 | def __init__(self, filename, lineno, line): |
| 120 | Error.__init__( |
| 121 | self, |
| 122 | 'File contains no section headers.\nfile: %s, line: %d\n%s' % |
| 123 | (filename, lineno, line)) |
| 124 | self.filename = filename |
| 125 | self.lineno = lineno |
| 126 | self.line = line |
| 127 | |
| 128 | class ParsingError(Error): |
| 129 | def __init__(self, filename): |
| 130 | Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 131 | self.filename = filename |
| 132 | self.errors = [] |
| 133 | |
| 134 | def append(self, lineno, line): |
| 135 | self.errors.append((lineno, line)) |
| 136 | self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) |
| 137 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | class ConfigParser: |
| 141 | def __init__(self, defaults=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 142 | self.__sections = {} |
| 143 | if defaults is None: |
| 144 | self.__defaults = {} |
| 145 | else: |
| 146 | self.__defaults = defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 147 | |
| 148 | def defaults(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | return self.__defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 150 | |
| 151 | def sections(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 152 | """Return a list of section names, excluding [DEFAULT]""" |
| 153 | # self.__sections will never have [DEFAULT] in it |
| 154 | return self.__sections.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 155 | |
| 156 | def add_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 157 | """Create a new section in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 159 | Raise DuplicateSectionError if a section by the specified name |
| 160 | already exists. |
| 161 | """ |
| 162 | if self.__sections.has_key(section): |
| 163 | raise DuplicateSectionError(section) |
| 164 | self.__sections[section] = {} |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 165 | |
| 166 | def has_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | """Indicate whether the named section is present in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | The DEFAULT section is not acknowledged. |
| 170 | """ |
| 171 | return self.__sections.has_key(section) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 172 | |
| 173 | def options(self, section): |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 174 | """Return a list of option names for the given section name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 175 | try: |
| 176 | opts = self.__sections[section].copy() |
| 177 | except KeyError: |
| 178 | raise NoSectionError(section) |
| 179 | opts.update(self.__defaults) |
| 180 | return opts.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 182 | def has_option(self, section, option): |
| 183 | """Return whether the given section has the given option.""" |
| 184 | try: |
| 185 | opts = self.__sections[section] |
| 186 | except KeyError: |
| 187 | raise NoSectionError(section) |
| 188 | return opts.has_key(option) |
| 189 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 190 | def read(self, filenames): |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 191 | |
| 192 | """Read and parse a filename or a list of filenames. |
| 193 | |
| 194 | Files that cannot be opened are silently ignored; this is |
| 195 | designed so that you can specifiy a list of potential |
| 196 | configuration file locations (e.g. current directory, user's |
| 197 | home directory, systemwide directory), and all existing |
| 198 | configuration files in the list will be read. A single |
| 199 | filename may also be given. |
| 200 | """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | if type(filenames) is type(''): |
| 202 | filenames = [filenames] |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 203 | for filename in filenames: |
| 204 | try: |
| 205 | fp = open(filename) |
| 206 | except IOError: |
| 207 | continue |
| 208 | self.__read(fp, filename) |
Fred Drake | 2438a48 | 1999-10-04 18:11:56 +0000 | [diff] [blame] | 209 | fp.close() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 211 | def readfp(self, fp, filename=None): |
| 212 | """Like read() but the argument must be a file-like object. |
| 213 | |
| 214 | The `fp' argument must have a `readline' method. Optional |
| 215 | second argument is the `filename', which if not given, is |
| 216 | taken from fp.name. If fp has no `name' attribute, `<???>' is |
| 217 | used. |
| 218 | |
| 219 | """ |
| 220 | if filename is None: |
| 221 | try: |
| 222 | filename = fp.name |
| 223 | except AttributeError: |
| 224 | filename = '<???>' |
| 225 | self.__read(fp, filename) |
| 226 | |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 227 | def get(self, section, option, raw=0, vars=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 228 | """Get an option value for a given section. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 229 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 230 | All % interpolations are expanded in the return values, based on the |
| 231 | defaults passed into the constructor, unless the optional argument |
| 232 | `raw' is true. Additional substitutions may be provided using the |
| 233 | `vars' argument, which must be a dictionary whose contents overrides |
| 234 | any pre-existing defaults. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 236 | The section DEFAULT is special. |
| 237 | """ |
| 238 | try: |
| 239 | sectdict = self.__sections[section].copy() |
| 240 | except KeyError: |
| 241 | if section == DEFAULTSECT: |
| 242 | sectdict = {} |
| 243 | else: |
| 244 | raise NoSectionError(section) |
| 245 | d = self.__defaults.copy() |
| 246 | d.update(sectdict) |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 247 | # Update with the entry specific variables |
| 248 | if vars: |
| 249 | d.update(vars) |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 250 | option = self.optionxform(option) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 251 | try: |
| 252 | rawval = d[option] |
| 253 | except KeyError: |
| 254 | raise NoOptionError(option, section) |
| 255 | # do the string interpolation |
| 256 | if raw: |
| 257 | return rawval |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 259 | value = rawval # Make it a pretty variable name |
Guido van Rossum | 72ce858 | 1999-02-12 14:13:10 +0000 | [diff] [blame] | 260 | depth = 0 |
| 261 | while depth < 10: # Loop through this until it's done |
| 262 | depth = depth + 1 |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 263 | if string.find(value, "%(") >= 0: |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 264 | try: |
| 265 | value = value % d |
| 266 | except KeyError, key: |
| 267 | raise InterpolationError(key, option, section, rawval) |
| 268 | else: |
| 269 | return value |
| 270 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 271 | def __get(self, section, conv, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 272 | return conv(self.get(section, option)) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 273 | |
| 274 | def getint(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 275 | return self.__get(section, string.atoi, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 276 | |
| 277 | def getfloat(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 278 | return self.__get(section, string.atof, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 279 | |
| 280 | def getboolean(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 281 | v = self.get(section, option) |
| 282 | val = string.atoi(v) |
| 283 | if val not in (0, 1): |
| 284 | raise ValueError, 'Not a boolean: %s' % v |
| 285 | return val |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 287 | def optionxform(self, optionstr): |
| 288 | return string.lower(optionstr) |
| 289 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 290 | # |
| 291 | # Regular expressions for parsing section headers and options. Note a |
| 292 | # slight semantic change from the previous version, because of the use |
| 293 | # of \w, _ is allowed in section header names. |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 294 | SECTCRE = re.compile( |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 295 | r'\[' # [ |
| 296 | r'(?P<header>[-\w]+)' # `-', `_' or any alphanum |
| 297 | r'\]' # ] |
| 298 | ) |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 299 | OPTCRE = re.compile( |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 300 | r'(?P<option>[-.\w]+)' # - . _ alphanum |
| 301 | r'[ \t]*[:=][ \t]*' # any number of space/tab, |
| 302 | # followed by separator |
| 303 | # (either : or =), followed |
| 304 | # by any # space/tab |
| 305 | r'(?P<value>.*)$' # everything up to eol |
| 306 | ) |
| 307 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 308 | def __read(self, fp, fpname): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 309 | """Parse a sectioned setup file. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 311 | The sections in setup file contains a title line at the top, |
| 312 | indicated by a name in square brackets (`[]'), plus key/value |
| 313 | options lines, indicated by `name: value' format lines. |
| 314 | Continuation are represented by an embedded newline then |
| 315 | leading whitespace. Blank lines, lines beginning with a '#', |
| 316 | and just about everything else is ignored. |
| 317 | """ |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 318 | cursect = None # None, or a dictionary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 319 | optname = None |
| 320 | lineno = 0 |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 321 | e = None # None, or an exception |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 322 | while 1: |
| 323 | line = fp.readline() |
| 324 | if not line: |
| 325 | break |
| 326 | lineno = lineno + 1 |
| 327 | # comment or blank line? |
| 328 | if string.strip(line) == '' or line[0] in '#;': |
| 329 | continue |
| 330 | if string.lower(string.split(line)[0]) == 'rem' \ |
| 331 | and line[0] == "r": # no leading whitespace |
| 332 | continue |
| 333 | # continuation line? |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 334 | if line[0] in ' \t' and cursect is not None and optname: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 335 | value = string.strip(line) |
| 336 | if value: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 337 | cursect[optname] = cursect[optname] + '\n ' + value |
| 338 | # a section header or option header? |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 339 | else: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 340 | # is it a section header? |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 341 | mo = self.SECTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 342 | if mo: |
| 343 | sectname = mo.group('header') |
| 344 | if self.__sections.has_key(sectname): |
| 345 | cursect = self.__sections[sectname] |
| 346 | elif sectname == DEFAULTSECT: |
| 347 | cursect = self.__defaults |
| 348 | else: |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 349 | cursect = {'__name__': sectname} |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 350 | self.__sections[sectname] = cursect |
| 351 | # So sections can't start with a continuation line |
| 352 | optname = None |
| 353 | # no section header in the file? |
| 354 | elif cursect is None: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 355 | raise MissingSectionHeaderError(fpname, lineno, `line`) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 356 | # an option line? |
| 357 | else: |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 358 | mo = self.OPTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 359 | if mo: |
| 360 | optname, optval = mo.group('option', 'value') |
| 361 | optname = string.lower(optname) |
| 362 | optval = string.strip(optval) |
| 363 | # allow empty values |
| 364 | if optval == '""': |
| 365 | optval = '' |
| 366 | cursect[optname] = optval |
| 367 | else: |
| 368 | # a non-fatal parsing error occurred. set up the |
| 369 | # exception but keep going. the exception will be |
| 370 | # raised at the end of the file and will contain a |
| 371 | # list of all bogus lines |
| 372 | if not e: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 373 | e = ParsingError(fpname) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 374 | e.append(lineno, `line`) |
| 375 | # if any parsing errors occurred, raise an exception |
| 376 | if e: |
| 377 | raise e |