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 | |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 39 | has_option(section, option) |
| 40 | return whether the given option exists in the given section |
| 41 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 42 | options(section) |
| 43 | return list of configuration options for the named section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 45 | has_option(section, option) |
| 46 | return whether the given section has the given option |
| 47 | |
Guido van Rossum | c0780ac | 1999-01-30 04:35:47 +0000 | [diff] [blame] | 48 | read(filenames) |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 49 | 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 Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 56 | messages (if fp has no `name' attribute, the string `<???>' is used). |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 57 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 58 | 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 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 | getint(section, options) |
| 66 | like get(), but convert value to an integer |
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 | getfloat(section, options) |
| 69 | like get(), but convert value to a float |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 70 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 71 | getboolean(section, options) |
| 72 | like get(), but convert value to a boolean (currently defined as 0 or |
| 73 | 1, only) |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | remove_section(section) |
| 76 | remove the given file section and all its options |
| 77 | |
| 78 | remove_option(section, option) |
| 79 | remove the given option from the given section |
| 80 | |
| 81 | set(section, option, value) |
| 82 | set the given option |
| 83 | |
| 84 | write(fp) |
| 85 | write the configuration state in .ini format |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 86 | """ |
| 87 | |
| 88 | import sys |
| 89 | import string |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 90 | import re |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 91 | |
| 92 | DEFAULTSECT = "DEFAULT" |
| 93 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 94 | MAX_INTERPOLATION_DEPTH = 10 |
| 95 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | # exception classes |
| 99 | class Error: |
| 100 | def __init__(self, msg=''): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 101 | self._msg = msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 102 | def __repr__(self): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 103 | return self._msg |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 104 | |
| 105 | class NoSectionError(Error): |
| 106 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | Error.__init__(self, 'No section: %s' % section) |
| 108 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 109 | |
| 110 | class DuplicateSectionError(Error): |
| 111 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | Error.__init__(self, "Section %s already exists" % section) |
| 113 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 114 | |
| 115 | class NoOptionError(Error): |
| 116 | def __init__(self, option, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | Error.__init__(self, "No option `%s' in section: %s" % |
| 118 | (option, section)) |
| 119 | self.option = option |
| 120 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 121 | |
| 122 | class InterpolationError(Error): |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 123 | def __init__(self, reference, option, section, rawval): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | Error.__init__(self, |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 125 | "Bad value substitution:\n" |
| 126 | "\tsection: [%s]\n" |
| 127 | "\toption : %s\n" |
| 128 | "\tkey : %s\n" |
| 129 | "\trawval : %s\n" |
| 130 | % (section, option, reference, rawval)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 131 | self.reference = reference |
| 132 | self.option = option |
| 133 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 134 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 135 | class InterpolationDepthError(Error): |
| 136 | def __init__(self, option, section, rawval): |
| 137 | Error.__init__(self, |
| 138 | "Value interpolation too deeply recursive:\n" |
| 139 | "\tsection: [%s]\n" |
| 140 | "\toption : %s\n" |
| 141 | "\trawval : %s\n" |
| 142 | % (section, option, rawval)) |
| 143 | self.option = option |
| 144 | self.section = section |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 145 | |
| 146 | class ParsingError(Error): |
| 147 | def __init__(self, filename): |
| 148 | Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 149 | self.filename = filename |
| 150 | self.errors = [] |
| 151 | |
| 152 | def append(self, lineno, line): |
| 153 | self.errors.append((lineno, line)) |
| 154 | self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) |
| 155 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 156 | class MissingSectionHeaderError(ParsingError): |
| 157 | def __init__(self, filename, lineno, line): |
| 158 | Error.__init__( |
| 159 | self, |
| 160 | 'File contains no section headers.\nfile: %s, line: %d\n%s' % |
| 161 | (filename, lineno, line)) |
| 162 | self.filename = filename |
| 163 | self.lineno = lineno |
| 164 | self.line = line |
| 165 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | class ConfigParser: |
| 169 | def __init__(self, defaults=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | self.__sections = {} |
| 171 | if defaults is None: |
| 172 | self.__defaults = {} |
| 173 | else: |
| 174 | self.__defaults = defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 175 | |
| 176 | def defaults(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 177 | return self.__defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 178 | |
| 179 | def sections(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | """Return a list of section names, excluding [DEFAULT]""" |
| 181 | # self.__sections will never have [DEFAULT] in it |
| 182 | return self.__sections.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 183 | |
| 184 | def add_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 185 | """Create a new section in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | Raise DuplicateSectionError if a section by the specified name |
| 188 | already exists. |
| 189 | """ |
| 190 | if self.__sections.has_key(section): |
| 191 | raise DuplicateSectionError(section) |
| 192 | self.__sections[section] = {} |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 193 | |
| 194 | def has_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 195 | """Indicate whether the named section is present in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 197 | The DEFAULT section is not acknowledged. |
| 198 | """ |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 199 | return section in self.sections() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 200 | |
| 201 | def options(self, section): |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 202 | """Return a list of option names for the given section name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 203 | try: |
| 204 | opts = self.__sections[section].copy() |
| 205 | except KeyError: |
| 206 | raise NoSectionError(section) |
| 207 | opts.update(self.__defaults) |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 208 | if opts.has_key('__name__'): |
| 209 | del opts['__name__'] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 210 | return opts.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 212 | def has_option(self, section, option): |
| 213 | """Return whether the given section has the given option.""" |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 214 | return option in self.options(section) |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 216 | def read(self, filenames): |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 217 | """Read and parse a filename or a list of filenames. |
| 218 | |
| 219 | Files that cannot be opened are silently ignored; this is |
Barry Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 220 | designed so that you can specify a list of potential |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 221 | configuration file locations (e.g. current directory, user's |
| 222 | home directory, systemwide directory), and all existing |
| 223 | configuration files in the list will be read. A single |
| 224 | filename may also be given. |
| 225 | """ |
Fred Drake | fd4114e | 2000-05-09 14:46:40 +0000 | [diff] [blame] | 226 | if type(filenames) in [type(''), type(u'')]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 227 | filenames = [filenames] |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 228 | for filename in filenames: |
| 229 | try: |
| 230 | fp = open(filename) |
| 231 | except IOError: |
| 232 | continue |
| 233 | self.__read(fp, filename) |
Fred Drake | 2438a48 | 1999-10-04 18:11:56 +0000 | [diff] [blame] | 234 | fp.close() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 236 | def readfp(self, fp, filename=None): |
| 237 | """Like read() but the argument must be a file-like object. |
| 238 | |
| 239 | The `fp' argument must have a `readline' method. Optional |
| 240 | second argument is the `filename', which if not given, is |
| 241 | taken from fp.name. If fp has no `name' attribute, `<???>' is |
| 242 | used. |
| 243 | |
| 244 | """ |
| 245 | if filename is None: |
| 246 | try: |
| 247 | filename = fp.name |
| 248 | except AttributeError: |
| 249 | filename = '<???>' |
| 250 | self.__read(fp, filename) |
| 251 | |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 252 | def get(self, section, option, raw=0, vars=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 253 | """Get an option value for a given section. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 254 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 255 | All % interpolations are expanded in the return values, based on the |
| 256 | defaults passed into the constructor, unless the optional argument |
| 257 | `raw' is true. Additional substitutions may be provided using the |
| 258 | `vars' argument, which must be a dictionary whose contents overrides |
| 259 | any pre-existing defaults. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 261 | The section DEFAULT is special. |
| 262 | """ |
| 263 | try: |
| 264 | sectdict = self.__sections[section].copy() |
| 265 | except KeyError: |
| 266 | if section == DEFAULTSECT: |
| 267 | sectdict = {} |
| 268 | else: |
| 269 | raise NoSectionError(section) |
| 270 | d = self.__defaults.copy() |
| 271 | d.update(sectdict) |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 272 | # Update with the entry specific variables |
| 273 | if vars: |
| 274 | d.update(vars) |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 275 | option = self.optionxform(option) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 276 | try: |
| 277 | rawval = d[option] |
| 278 | except KeyError: |
| 279 | raise NoOptionError(option, section) |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 281 | if raw: |
| 282 | return rawval |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 283 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 284 | # do the string interpolation |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 285 | value = rawval # Make it a pretty variable name |
Guido van Rossum | 72ce858 | 1999-02-12 14:13:10 +0000 | [diff] [blame] | 286 | depth = 0 |
| 287 | while depth < 10: # Loop through this until it's done |
| 288 | depth = depth + 1 |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 289 | if string.find(value, "%(") >= 0: |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 290 | try: |
| 291 | value = value % d |
| 292 | except KeyError, key: |
| 293 | raise InterpolationError(key, option, section, rawval) |
| 294 | else: |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 295 | break |
| 296 | if value.find("%(") >= 0: |
| 297 | raise InterpolationDepthError(option, section, rawval) |
| 298 | return value |
Guido van Rossum | e6506e7 | 1999-01-26 19:29:25 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 300 | def __get(self, section, conv, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 301 | return conv(self.get(section, option)) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 302 | |
| 303 | def getint(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 304 | return self.__get(section, string.atoi, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 305 | |
| 306 | def getfloat(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 307 | return self.__get(section, string.atof, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 308 | |
| 309 | def getboolean(self, section, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 310 | v = self.get(section, option) |
| 311 | val = string.atoi(v) |
| 312 | if val not in (0, 1): |
| 313 | raise ValueError, 'Not a boolean: %s' % v |
| 314 | return val |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 316 | def optionxform(self, optionstr): |
| 317 | return string.lower(optionstr) |
| 318 | |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 319 | def has_option(self, section, option): |
| 320 | """Check for the existence of a given option in a given section.""" |
| 321 | if not section or section == "DEFAULT": |
| 322 | return self.__defaults.has_key(option) |
| 323 | elif not self.has_section(section): |
| 324 | return 0 |
| 325 | else: |
| 326 | 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) |
| 337 | sectdict[option] = value |
| 338 | |
| 339 | def write(self, fp): |
| 340 | """Write an .ini-format representation of the configuration state.""" |
| 341 | if self.__defaults: |
| 342 | fp.write("[DEFAULT]\n") |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 343 | for (key, value) in self.__defaults.items(): |
| 344 | fp.write("%s = %s\n" % (key, value)) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 345 | fp.write("\n") |
| 346 | for section in self.sections(): |
| 347 | fp.write("[" + section + "]\n") |
| 348 | sectdict = self.__sections[section] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 349 | for (key, value) in sectdict.items(): |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 350 | if key == "__name__": |
| 351 | continue |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 352 | fp.write("%s = %s\n" % (key, value)) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 353 | fp.write("\n") |
| 354 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 355 | def remove_option(self, section, option): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 356 | """Remove an option.""" |
| 357 | if not section or section == "DEFAULT": |
| 358 | sectdict = self.__defaults |
| 359 | else: |
| 360 | try: |
| 361 | sectdict = self.__sections[section] |
| 362 | except KeyError: |
| 363 | raise NoSectionError(section) |
Fred Drake | ff4a23b | 2000-12-04 16:29:13 +0000 | [diff] [blame] | 364 | existed = sectdict.has_key(option) |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 365 | if existed: |
Fred Drake | ff4a23b | 2000-12-04 16:29:13 +0000 | [diff] [blame] | 366 | del sectdict[option] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 367 | return existed |
| 368 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 369 | def remove_section(self, section): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 370 | """Remove a file section.""" |
| 371 | if self.__sections.has_key(section): |
| 372 | del self.__sections[section] |
| 373 | return 1 |
| 374 | else: |
| 375 | return 0 |
| 376 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 377 | # |
| 378 | # Regular expressions for parsing section headers and options. Note a |
| 379 | # slight semantic change from the previous version, because of the use |
| 380 | # of \w, _ is allowed in section header names. |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 381 | SECTCRE = re.compile( |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 382 | r'\[' # [ |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 383 | r'(?P<header>[-\w_.*,(){} ]+)' # a lot of stuff found by IvL |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 384 | r'\]' # ] |
| 385 | ) |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 386 | OPTCRE = re.compile( |
Fred Drake | 1ab41fc | 2000-02-28 23:23:55 +0000 | [diff] [blame] | 387 | r'(?P<option>[-\w_.*,(){}]+)' # a lot of stuff found by IvL |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 388 | r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab, |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 389 | # followed by separator |
| 390 | # (either : or =), followed |
| 391 | # by any # space/tab |
| 392 | r'(?P<value>.*)$' # everything up to eol |
| 393 | ) |
| 394 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 395 | def __read(self, fp, fpname): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 396 | """Parse a sectioned setup file. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 398 | The sections in setup file contains a title line at the top, |
| 399 | indicated by a name in square brackets (`[]'), plus key/value |
| 400 | options lines, indicated by `name: value' format lines. |
| 401 | Continuation are represented by an embedded newline then |
| 402 | leading whitespace. Blank lines, lines beginning with a '#', |
| 403 | and just about everything else is ignored. |
| 404 | """ |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 405 | cursect = None # None, or a dictionary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 406 | optname = None |
| 407 | lineno = 0 |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 408 | e = None # None, or an exception |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 409 | while 1: |
| 410 | line = fp.readline() |
| 411 | if not line: |
| 412 | break |
| 413 | lineno = lineno + 1 |
| 414 | # comment or blank line? |
| 415 | if string.strip(line) == '' or line[0] in '#;': |
| 416 | continue |
| 417 | if string.lower(string.split(line)[0]) == 'rem' \ |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 418 | and line[0] in "rR": # no leading whitespace |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 419 | continue |
| 420 | # continuation line? |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 421 | 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] | 422 | value = string.strip(line) |
| 423 | if value: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 424 | cursect[optname] = cursect[optname] + '\n ' + value |
| 425 | # a section header or option header? |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 426 | else: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 427 | # is it a section header? |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 428 | mo = self.SECTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 429 | if mo: |
| 430 | sectname = mo.group('header') |
| 431 | if self.__sections.has_key(sectname): |
| 432 | cursect = self.__sections[sectname] |
| 433 | elif sectname == DEFAULTSECT: |
| 434 | cursect = self.__defaults |
| 435 | else: |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 436 | cursect = {'__name__': sectname} |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 437 | self.__sections[sectname] = cursect |
| 438 | # So sections can't start with a continuation line |
| 439 | optname = None |
| 440 | # no section header in the file? |
| 441 | elif cursect is None: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 442 | raise MissingSectionHeaderError(fpname, lineno, `line`) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 443 | # an option line? |
| 444 | else: |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 445 | mo = self.OPTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 446 | if mo: |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 447 | optname, vi, optval = mo.group('option', 'vi', 'value') |
Jeremy Hylton | 820314e | 2000-03-03 20:43:57 +0000 | [diff] [blame] | 448 | if vi in ('=', ':') and ';' in optval: |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 449 | # ';' is a comment delimiter only if it follows |
| 450 | # a spacing character |
| 451 | pos = string.find(optval, ';') |
| 452 | if pos and optval[pos-1] in string.whitespace: |
| 453 | optval = optval[:pos] |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 454 | optval = string.strip(optval) |
| 455 | # allow empty values |
| 456 | if optval == '""': |
| 457 | optval = '' |
Guido van Rossum | 4126736 | 2000-09-25 14:42:33 +0000 | [diff] [blame] | 458 | cursect[self.optionxform(optname)] = optval |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 459 | else: |
| 460 | # a non-fatal parsing error occurred. set up the |
| 461 | # exception but keep going. the exception will be |
| 462 | # raised at the end of the file and will contain a |
| 463 | # list of all bogus lines |
| 464 | if not e: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 465 | e = ParsingError(fpname) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 466 | e.append(lineno, `line`) |
| 467 | # if any parsing errors occurred, raise an exception |
| 468 | if e: |
| 469 | raise e |