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 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 36 | options(section) |
| 37 | return list of configuration options for the named section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | c0780ac | 1999-01-30 04:35:47 +0000 | [diff] [blame] | 39 | read(filenames) |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 40 | read and parse the list of named configuration files |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 41 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 48 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 49 | getint(section, options) |
| 50 | like get(), but convert value to an integer |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 51 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 52 | getfloat(section, options) |
| 53 | like get(), but convert value to a float |
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 | getboolean(section, options) |
| 56 | like get(), but convert value to a boolean (currently defined as 0 or |
| 57 | 1, only) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 58 | """ |
| 59 | |
| 60 | import sys |
| 61 | import string |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 62 | import re |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 63 | |
| 64 | DEFAULTSECT = "DEFAULT" |
| 65 | |
| 66 | |
| 67 | |
| 68 | # exception classes |
| 69 | class Error: |
| 70 | def __init__(self, msg=''): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 71 | self._msg = msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 72 | def __repr__(self): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 73 | return self._msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 74 | |
| 75 | class NoSectionError(Error): |
| 76 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | Error.__init__(self, 'No section: %s' % section) |
| 78 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 79 | |
| 80 | class DuplicateSectionError(Error): |
| 81 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 82 | Error.__init__(self, "Section %s already exists" % section) |
| 83 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 84 | |
| 85 | class NoOptionError(Error): |
| 86 | def __init__(self, option, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | Error.__init__(self, "No option `%s' in section: %s" % |
| 88 | (option, section)) |
| 89 | self.option = option |
| 90 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 91 | |
| 92 | class InterpolationError(Error): |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 93 | def __init__(self, reference, option, section, rawval): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | Error.__init__(self, |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 95 | "Bad value substitution:\n" |
| 96 | "\tsection: [%s]\n" |
| 97 | "\toption : %s\n" |
| 98 | "\tkey : %s\n" |
| 99 | "\trawval : %s\n" |
| 100 | % (section, option, reference, rawval)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | self.reference = reference |
| 102 | self.option = option |
| 103 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 104 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 105 | class MissingSectionHeaderError(Error): |
| 106 | def __init__(self, filename, lineno, line): |
| 107 | Error.__init__( |
| 108 | self, |
| 109 | 'File contains no section headers.\nfile: %s, line: %d\n%s' % |
| 110 | (filename, lineno, line)) |
| 111 | self.filename = filename |
| 112 | self.lineno = lineno |
| 113 | self.line = line |
| 114 | |
| 115 | class ParsingError(Error): |
| 116 | def __init__(self, filename): |
| 117 | Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 118 | self.filename = filename |
| 119 | self.errors = [] |
| 120 | |
| 121 | def append(self, lineno, line): |
| 122 | self.errors.append((lineno, line)) |
| 123 | self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) |
| 124 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | class ConfigParser: |
| 128 | def __init__(self, defaults=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | self.__sections = {} |
| 130 | if defaults is None: |
| 131 | self.__defaults = {} |
| 132 | else: |
| 133 | self.__defaults = defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 134 | |
| 135 | def defaults(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 136 | return self.__defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 137 | |
| 138 | def sections(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 139 | """Return a list of section names, excluding [DEFAULT]""" |
| 140 | # self.__sections will never have [DEFAULT] in it |
| 141 | return self.__sections.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 142 | |
| 143 | def add_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 144 | """Create a new section in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 146 | Raise DuplicateSectionError if a section by the specified name |
| 147 | already exists. |
| 148 | """ |
| 149 | if self.__sections.has_key(section): |
| 150 | raise DuplicateSectionError(section) |
| 151 | self.__sections[section] = {} |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 152 | |
| 153 | def has_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 154 | """Indicate whether the named section is present in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 156 | The DEFAULT section is not acknowledged. |
| 157 | """ |
| 158 | return self.__sections.has_key(section) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 159 | |
| 160 | def options(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | try: |
| 162 | opts = self.__sections[section].copy() |
| 163 | except KeyError: |
| 164 | raise NoSectionError(section) |
| 165 | opts.update(self.__defaults) |
| 166 | return opts.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 167 | |
| 168 | def read(self, filenames): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | """Read and parse a list of filenames.""" |
| 170 | if type(filenames) is type(''): |
| 171 | filenames = [filenames] |
| 172 | for file in filenames: |
| 173 | try: |
| 174 | fp = open(file, 'r') |
| 175 | self.__read(fp) |
| 176 | except IOError: |
| 177 | pass |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 179 | def get(self, section, option, raw=0, vars=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | """Get an option value for a given section. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 181 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 188 | The section DEFAULT is special. |
| 189 | """ |
| 190 | try: |
| 191 | sectdict = self.__sections[section].copy() |
| 192 | except KeyError: |
| 193 | if section == DEFAULTSECT: |
| 194 | sectdict = {} |
| 195 | else: |
| 196 | raise NoSectionError(section) |
| 197 | d = self.__defaults.copy() |
| 198 | d.update(sectdict) |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 199 | # Update with the entry specific variables |
| 200 | if vars: |
| 201 | d.update(vars) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 202 | option = string.lower(option) |
| 203 | try: |
| 204 | rawval = d[option] |
| 205 | except KeyError: |
| 206 | raise NoOptionError(option, section) |
| 207 | # do the string interpolation |
| 208 | if raw: |
| 209 | return rawval |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 211 | value = rawval # Make it a pretty variable name |
| 212 | while 1: # Loop through this until it's done |
| 213 | if not string.find(value, "%("): |
| 214 | try: |
| 215 | value = value % d |
| 216 | except KeyError, key: |
| 217 | raise InterpolationError(key, option, section, rawval) |
| 218 | else: |
| 219 | return value |
| 220 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 221 | def __get(self, section, conv, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 222 | return conv(self.get(section, option)) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 223 | |
| 224 | def getint(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 225 | return self.__get(section, string.atoi, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 226 | |
| 227 | def getfloat(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 228 | return self.__get(section, string.atof, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 229 | |
| 230 | def getboolean(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 231 | v = self.get(section, option) |
| 232 | val = string.atoi(v) |
| 233 | if val not in (0, 1): |
| 234 | raise ValueError, 'Not a boolean: %s' % v |
| 235 | return val |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 236 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 237 | # |
| 238 | # Regular expressions for parsing section headers and options. Note a |
| 239 | # slight semantic change from the previous version, because of the use |
| 240 | # of \w, _ is allowed in section header names. |
| 241 | __SECTCRE = re.compile( |
| 242 | r'\[' # [ |
| 243 | r'(?P<header>[-\w]+)' # `-', `_' or any alphanum |
| 244 | r'\]' # ] |
| 245 | ) |
| 246 | __OPTCRE = re.compile( |
| 247 | r'(?P<option>[-.\w]+)' # - . _ alphanum |
| 248 | r'[ \t]*[:=][ \t]*' # any number of space/tab, |
| 249 | # followed by separator |
| 250 | # (either : or =), followed |
| 251 | # by any # space/tab |
| 252 | r'(?P<value>.*)$' # everything up to eol |
| 253 | ) |
| 254 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 255 | def __read(self, fp): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 256 | """Parse a sectioned setup file. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 258 | The sections in setup file contains a title line at the top, |
| 259 | indicated by a name in square brackets (`[]'), plus key/value |
| 260 | options lines, indicated by `name: value' format lines. |
| 261 | Continuation are represented by an embedded newline then |
| 262 | leading whitespace. Blank lines, lines beginning with a '#', |
| 263 | and just about everything else is ignored. |
| 264 | """ |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 265 | cursect = None # None, or a dictionary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 266 | optname = None |
| 267 | lineno = 0 |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 268 | e = None # None, or an exception |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 269 | while 1: |
| 270 | line = fp.readline() |
| 271 | if not line: |
| 272 | break |
| 273 | lineno = lineno + 1 |
| 274 | # comment or blank line? |
| 275 | if string.strip(line) == '' or line[0] in '#;': |
| 276 | continue |
| 277 | if string.lower(string.split(line)[0]) == 'rem' \ |
| 278 | and line[0] == "r": # no leading whitespace |
| 279 | continue |
| 280 | # continuation line? |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 281 | 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] | 282 | value = string.strip(line) |
| 283 | if value: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 284 | cursect[optname] = cursect[optname] + '\n ' + value |
| 285 | # a section header or option header? |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 286 | else: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 287 | # is it a section header? |
| 288 | mo = self.__SECTCRE.match(line) |
| 289 | if mo: |
| 290 | sectname = mo.group('header') |
| 291 | if self.__sections.has_key(sectname): |
| 292 | cursect = self.__sections[sectname] |
| 293 | elif sectname == DEFAULTSECT: |
| 294 | cursect = self.__defaults |
| 295 | else: |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 296 | cursect = {'__name__': sectname} |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 297 | self.__sections[sectname] = cursect |
| 298 | # So sections can't start with a continuation line |
| 299 | optname = None |
| 300 | # no section header in the file? |
| 301 | elif cursect is None: |
| 302 | raise MissingSectionHeaderError(fp.name, lineno, `line`) |
| 303 | # an option line? |
| 304 | else: |
| 305 | mo = self.__OPTCRE.match(line) |
| 306 | if mo: |
| 307 | optname, optval = mo.group('option', 'value') |
| 308 | optname = string.lower(optname) |
| 309 | optval = string.strip(optval) |
| 310 | # allow empty values |
| 311 | if optval == '""': |
| 312 | optval = '' |
| 313 | cursect[optname] = optval |
| 314 | else: |
| 315 | # a non-fatal parsing error occurred. set up the |
| 316 | # exception but keep going. the exception will be |
| 317 | # raised at the end of the file and will contain a |
| 318 | # list of all bogus lines |
| 319 | if not e: |
| 320 | e = ParsingError(fp.name) |
| 321 | e.append(lineno, `line`) |
| 322 | # if any parsing errors occurred, raise an exception |
| 323 | if e: |
| 324 | raise e |