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 | 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 |
Barry Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 53 | messages (if fp has no `name' attribute, the string `<???>' is used). |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 54 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 55 | get(section, option, raw=False, vars=None) |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 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) |
Guido van Rossum | fb06f75 | 2001-10-04 19:58:46 +0000 | [diff] [blame] | 69 | like get(), but convert value to a boolean (currently case |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 70 | insensitively defined as 0, false, no, off for False, and 1, true, |
| 71 | yes, on for True). Returns False or True. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 72 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 73 | items(section, raw=False, vars=None) |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 74 | return a list of tuples with (name, value) for each option |
| 75 | in the section. |
| 76 | |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 77 | remove_section(section) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 78 | remove the given file section and all its options |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | remove_option(section, option) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 81 | remove the given option from the given section |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | set(section, option, value) |
| 84 | set the given option |
| 85 | |
| 86 | write(fp) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 87 | write the configuration state in .ini format |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 88 | """ |
| 89 | |
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 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 92 | __all__ = ["NoSectionError","DuplicateSectionError","NoOptionError", |
| 93 | "InterpolationError","InterpolationDepthError","ParsingError", |
| 94 | "MissingSectionHeaderError","ConfigParser", |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 95 | "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 97 | DEFAULTSECT = "DEFAULT" |
| 98 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 99 | MAX_INTERPOLATION_DEPTH = 10 |
| 100 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 101 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 103 | # exception classes |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 104 | class Error(Exception): |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 105 | def __init__(self, msg=''): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 106 | self._msg = msg |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 107 | Exception.__init__(self, msg) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 108 | def __repr__(self): |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 109 | return self._msg |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 110 | __str__ = __repr__ |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 111 | |
| 112 | class NoSectionError(Error): |
| 113 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | Error.__init__(self, 'No section: %s' % section) |
| 115 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 116 | |
| 117 | class DuplicateSectionError(Error): |
| 118 | def __init__(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | Error.__init__(self, "Section %s already exists" % section) |
| 120 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 121 | |
| 122 | class NoOptionError(Error): |
| 123 | def __init__(self, option, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | Error.__init__(self, "No option `%s' in section: %s" % |
| 125 | (option, section)) |
| 126 | self.option = option |
| 127 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 128 | |
| 129 | class InterpolationError(Error): |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 130 | def __init__(self, reference, option, section, rawval): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 131 | Error.__init__(self, |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 132 | "Bad value substitution:\n" |
| 133 | "\tsection: [%s]\n" |
| 134 | "\toption : %s\n" |
| 135 | "\tkey : %s\n" |
| 136 | "\trawval : %s\n" |
| 137 | % (section, option, reference, rawval)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | self.reference = reference |
| 139 | self.option = option |
| 140 | self.section = section |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 141 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 142 | class InterpolationDepthError(Error): |
| 143 | def __init__(self, option, section, rawval): |
| 144 | Error.__init__(self, |
| 145 | "Value interpolation too deeply recursive:\n" |
| 146 | "\tsection: [%s]\n" |
| 147 | "\toption : %s\n" |
| 148 | "\trawval : %s\n" |
| 149 | % (section, option, rawval)) |
| 150 | self.option = option |
| 151 | self.section = section |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 152 | |
| 153 | class ParsingError(Error): |
| 154 | def __init__(self, filename): |
| 155 | Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 156 | self.filename = filename |
| 157 | self.errors = [] |
| 158 | |
| 159 | def append(self, lineno, line): |
| 160 | self.errors.append((lineno, line)) |
| 161 | self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) |
| 162 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 163 | class MissingSectionHeaderError(ParsingError): |
| 164 | def __init__(self, filename, lineno, line): |
| 165 | Error.__init__( |
| 166 | self, |
| 167 | 'File contains no section headers.\nfile: %s, line: %d\n%s' % |
| 168 | (filename, lineno, line)) |
| 169 | self.filename = filename |
| 170 | self.lineno = lineno |
| 171 | self.line = line |
| 172 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 173 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 174 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 175 | class RawConfigParser: |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 176 | def __init__(self, defaults=None): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 177 | self._sections = {} |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 178 | if defaults is None: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 179 | self._defaults = {} |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 180 | else: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 181 | self._defaults = defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 182 | |
| 183 | def defaults(self): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 184 | return self._defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 185 | |
| 186 | def sections(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | """Return a list of section names, excluding [DEFAULT]""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 188 | # self._sections will never have [DEFAULT] in it |
| 189 | return self._sections.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 190 | |
| 191 | def add_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 192 | """Create a new section in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 194 | Raise DuplicateSectionError if a section by the specified name |
| 195 | already exists. |
| 196 | """ |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 197 | if section in self._sections: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 198 | raise DuplicateSectionError(section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 199 | self._sections[section] = {} |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 200 | |
| 201 | def has_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 202 | """Indicate whether the named section is present in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 204 | The DEFAULT section is not acknowledged. |
| 205 | """ |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 206 | return section in self._sections |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 207 | |
| 208 | def options(self, section): |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 209 | """Return a list of option names for the given section name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 210 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 211 | opts = self._sections[section].copy() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 212 | except KeyError: |
| 213 | raise NoSectionError(section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 214 | opts.update(self._defaults) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 215 | if '__name__' in opts: |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 216 | del opts['__name__'] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 217 | return opts.keys() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 218 | |
| 219 | def read(self, filenames): |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 220 | """Read and parse a filename or a list of filenames. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 222 | Files that cannot be opened are silently ignored; this is |
Barry Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 223 | designed so that you can specify a list of potential |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 224 | configuration file locations (e.g. current directory, user's |
| 225 | home directory, systemwide directory), and all existing |
| 226 | configuration files in the list will be read. A single |
| 227 | filename may also be given. |
| 228 | """ |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 229 | if isinstance(filenames, basestring): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 230 | filenames = [filenames] |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 231 | for filename in filenames: |
| 232 | try: |
| 233 | fp = open(filename) |
| 234 | except IOError: |
| 235 | continue |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 236 | self._read(fp, filename) |
Fred Drake | 2438a48 | 1999-10-04 18:11:56 +0000 | [diff] [blame] | 237 | fp.close() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 239 | def readfp(self, fp, filename=None): |
| 240 | """Like read() but the argument must be a file-like object. |
| 241 | |
| 242 | The `fp' argument must have a `readline' method. Optional |
| 243 | second argument is the `filename', which if not given, is |
| 244 | taken from fp.name. If fp has no `name' attribute, `<???>' is |
| 245 | used. |
| 246 | |
| 247 | """ |
| 248 | if filename is None: |
| 249 | try: |
| 250 | filename = fp.name |
| 251 | except AttributeError: |
| 252 | filename = '<???>' |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 253 | self._read(fp, filename) |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 254 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 255 | def get(self, section, option): |
| 256 | opt = self.optionxform(option) |
| 257 | if section not in self._sections: |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 258 | if section != DEFAULTSECT: |
| 259 | raise NoSectionError(section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 260 | if opt in self._defaults: |
| 261 | return self._defaults[opt] |
| 262 | else: |
| 263 | raise NoOptionError(option, section) |
| 264 | elif opt in self._sections[section]: |
| 265 | return self._sections[section][opt] |
| 266 | elif opt in self._defaults: |
| 267 | return self._defaults[opt] |
| 268 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 269 | raise NoOptionError(option, section) |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 270 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 271 | def items(self, section): |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 272 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 273 | d2 = self._sections[section] |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 274 | except KeyError: |
| 275 | if section != DEFAULTSECT: |
| 276 | raise NoSectionError(section) |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 277 | d2 = {} |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 278 | d = self._defaults.copy() |
| 279 | d.update(d2) |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 280 | if "__name__" in d: |
| 281 | del d["__name__"] |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 282 | return d.items() |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 283 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 284 | def _get(self, section, conv, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 285 | return conv(self.get(section, option)) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 286 | |
| 287 | def getint(self, section, option): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 288 | return self._get(section, int, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 289 | |
| 290 | def getfloat(self, section, option): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 291 | return self._get(section, float, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 292 | |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 293 | _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, |
| 294 | '0': False, 'no': False, 'false': False, 'off': False} |
| 295 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 296 | def getboolean(self, section, option): |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 297 | v = self.get(section, option) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 298 | if v.lower() not in self._boolean_states: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 299 | raise ValueError, 'Not a boolean: %s' % v |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 300 | return self._boolean_states[v.lower()] |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 302 | def optionxform(self, optionstr): |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 303 | return optionstr.lower() |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 304 | |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 305 | def has_option(self, section, option): |
| 306 | """Check for the existence of a given option in a given section.""" |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 307 | if not section or section == DEFAULTSECT: |
| 308 | option = self.optionxform(option) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 309 | return option in self._defaults |
| 310 | elif section not in self._sections: |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 311 | return False |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 312 | else: |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 313 | option = self.optionxform(option) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 314 | return (option in self._sections[section] |
| 315 | or option in self._defaults) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 316 | |
| 317 | def set(self, section, option, value): |
| 318 | """Set an option.""" |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 319 | if not section or section == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 320 | sectdict = self._defaults |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 321 | else: |
| 322 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 323 | sectdict = self._sections[section] |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 324 | except KeyError: |
| 325 | raise NoSectionError(section) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 326 | sectdict[self.optionxform(option)] = value |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 327 | |
| 328 | def write(self, fp): |
| 329 | """Write an .ini-format representation of the configuration state.""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 330 | if self._defaults: |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 331 | fp.write("[%s]\n" % DEFAULTSECT) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 332 | for (key, value) in self._defaults.items(): |
Andrew M. Kuchling | 00824ed | 2002-03-08 18:08:47 +0000 | [diff] [blame] | 333 | fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 334 | fp.write("\n") |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 335 | for section in self._sections: |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 336 | fp.write("[%s]\n" % section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 337 | for (key, value) in self._sections[section].items(): |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 338 | if key != "__name__": |
| 339 | fp.write("%s = %s\n" % |
| 340 | (key, str(value).replace('\n', '\n\t'))) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 341 | fp.write("\n") |
| 342 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 343 | def remove_option(self, section, option): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 344 | """Remove an option.""" |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 345 | if not section or section == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 346 | sectdict = self._defaults |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 347 | else: |
| 348 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 349 | sectdict = self._sections[section] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 350 | except KeyError: |
| 351 | raise NoSectionError(section) |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 352 | option = self.optionxform(option) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 353 | existed = option in sectdict |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 354 | if existed: |
Fred Drake | ff4a23b | 2000-12-04 16:29:13 +0000 | [diff] [blame] | 355 | del sectdict[option] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 356 | return existed |
| 357 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 358 | def remove_section(self, section): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 359 | """Remove a file section.""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 360 | existed = section in self._sections |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 361 | if existed: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 362 | del self._sections[section] |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 363 | return existed |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 364 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 365 | # |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 366 | # Regular expressions for parsing section headers and options. |
| 367 | # |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 368 | SECTCRE = re.compile( |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 369 | r'\[' # [ |
Fred Drake | d4df94b | 2001-02-14 15:24:17 +0000 | [diff] [blame] | 370 | r'(?P<header>[^]]+)' # very permissive! |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 371 | r'\]' # ] |
| 372 | ) |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 373 | OPTCRE = re.compile( |
Fred Drake | 176916a | 2002-09-27 16:21:18 +0000 | [diff] [blame] | 374 | r'(?P<option>[^:=\s][^:=]*)' # very permissive! |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 375 | r'\s*(?P<vi>[:=])\s*' # any number of space/tab, |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 376 | # followed by separator |
| 377 | # (either : or =), followed |
| 378 | # by any # space/tab |
| 379 | r'(?P<value>.*)$' # everything up to eol |
| 380 | ) |
| 381 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 382 | def _read(self, fp, fpname): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 383 | """Parse a sectioned setup file. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 384 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 385 | The sections in setup file contains a title line at the top, |
| 386 | indicated by a name in square brackets (`[]'), plus key/value |
| 387 | options lines, indicated by `name: value' format lines. |
Andrew M. Kuchling | 9050a51 | 2002-11-06 14:51:20 +0000 | [diff] [blame] | 388 | Continuations are represented by an embedded newline then |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 389 | leading whitespace. Blank lines, lines beginning with a '#', |
Andrew M. Kuchling | 9050a51 | 2002-11-06 14:51:20 +0000 | [diff] [blame] | 390 | and just about everything else are ignored. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 391 | """ |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 392 | cursect = None # None, or a dictionary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 393 | optname = None |
| 394 | lineno = 0 |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 395 | e = None # None, or an exception |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 396 | while True: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 397 | line = fp.readline() |
| 398 | if not line: |
| 399 | break |
| 400 | lineno = lineno + 1 |
| 401 | # comment or blank line? |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 402 | if line.strip() == '' or line[0] in '#;': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 403 | continue |
Fred Drake | 176916a | 2002-09-27 16:21:18 +0000 | [diff] [blame] | 404 | if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR": |
| 405 | # no leading whitespace |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 406 | continue |
| 407 | # continuation line? |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 408 | if line[0].isspace() and cursect is not None and optname: |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 409 | value = line.strip() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 410 | if value: |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 411 | cursect[optname] = "%s\n%s" % (cursect[optname], value) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 412 | # a section header or option header? |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 413 | else: |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 414 | # is it a section header? |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 415 | mo = self.SECTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 416 | if mo: |
| 417 | sectname = mo.group('header') |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 418 | if sectname in self._sections: |
| 419 | cursect = self._sections[sectname] |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 420 | elif sectname == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 421 | cursect = self._defaults |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 422 | else: |
Barry Warsaw | 6446212 | 1998-08-06 18:48:41 +0000 | [diff] [blame] | 423 | cursect = {'__name__': sectname} |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 424 | self._sections[sectname] = cursect |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 425 | # So sections can't start with a continuation line |
| 426 | optname = None |
| 427 | # no section header in the file? |
| 428 | elif cursect is None: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 429 | raise MissingSectionHeaderError(fpname, lineno, `line`) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 430 | # an option line? |
| 431 | else: |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 432 | mo = self.OPTCRE.match(line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 433 | if mo: |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 434 | optname, vi, optval = mo.group('option', 'vi', 'value') |
Jeremy Hylton | 820314e | 2000-03-03 20:43:57 +0000 | [diff] [blame] | 435 | if vi in ('=', ':') and ';' in optval: |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 436 | # ';' is a comment delimiter only if it follows |
| 437 | # a spacing character |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 438 | pos = optval.find(';') |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 439 | if pos != -1 and optval[pos-1].isspace(): |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 440 | optval = optval[:pos] |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 441 | optval = optval.strip() |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 442 | # allow empty values |
| 443 | if optval == '""': |
| 444 | optval = '' |
Fred Drake | 176916a | 2002-09-27 16:21:18 +0000 | [diff] [blame] | 445 | optname = self.optionxform(optname.rstrip()) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 446 | cursect[optname] = optval |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 447 | else: |
| 448 | # a non-fatal parsing error occurred. set up the |
| 449 | # exception but keep going. the exception will be |
| 450 | # raised at the end of the file and will contain a |
| 451 | # list of all bogus lines |
| 452 | if not e: |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 453 | e = ParsingError(fpname) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 454 | e.append(lineno, `line`) |
| 455 | # if any parsing errors occurred, raise an exception |
| 456 | if e: |
| 457 | raise e |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 458 | |
| 459 | |
| 460 | class ConfigParser(RawConfigParser): |
| 461 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 462 | def get(self, section, option, raw=False, vars=None): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 463 | """Get an option value for a given section. |
| 464 | |
| 465 | All % interpolations are expanded in the return values, based on the |
| 466 | defaults passed into the constructor, unless the optional argument |
| 467 | `raw' is true. Additional substitutions may be provided using the |
| 468 | `vars' argument, which must be a dictionary whose contents overrides |
| 469 | any pre-existing defaults. |
| 470 | |
| 471 | The section DEFAULT is special. |
| 472 | """ |
| 473 | d = self._defaults.copy() |
| 474 | try: |
| 475 | d.update(self._sections[section]) |
| 476 | except KeyError: |
| 477 | if section != DEFAULTSECT: |
| 478 | raise NoSectionError(section) |
| 479 | # Update with the entry specific variables |
| 480 | if vars is not None: |
| 481 | d.update(vars) |
| 482 | option = self.optionxform(option) |
| 483 | try: |
| 484 | value = d[option] |
| 485 | except KeyError: |
| 486 | raise NoOptionError(option, section) |
| 487 | |
| 488 | if raw: |
| 489 | return value |
| 490 | else: |
| 491 | return self._interpolate(section, option, value, d) |
| 492 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 493 | def items(self, section, raw=False, vars=None): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 494 | """Return a list of tuples with (name, value) for each option |
| 495 | in the section. |
| 496 | |
| 497 | All % interpolations are expanded in the return values, based on the |
| 498 | defaults passed into the constructor, unless the optional argument |
| 499 | `raw' is true. Additional substitutions may be provided using the |
| 500 | `vars' argument, which must be a dictionary whose contents overrides |
| 501 | any pre-existing defaults. |
| 502 | |
| 503 | The section DEFAULT is special. |
| 504 | """ |
| 505 | d = self._defaults.copy() |
| 506 | try: |
| 507 | d.update(self._sections[section]) |
| 508 | except KeyError: |
| 509 | if section != DEFAULTSECT: |
| 510 | raise NoSectionError(section) |
| 511 | # Update with the entry specific variables |
| 512 | if vars: |
| 513 | d.update(vars) |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 514 | options = d.keys() |
| 515 | if "__name__" in options: |
| 516 | options.remove("__name__") |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 517 | if raw: |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 518 | for option in options: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 519 | yield (option, d[option]) |
| 520 | else: |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 521 | for option in options: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 522 | yield (option, |
| 523 | self._interpolate(section, option, d[option], d)) |
| 524 | |
| 525 | def _interpolate(self, section, option, rawval, vars): |
| 526 | # do the string interpolation |
| 527 | value = rawval |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 528 | depth = MAX_INTERPOLATION_DEPTH |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 529 | while depth: # Loop through this until it's done |
| 530 | depth -= 1 |
| 531 | if value.find("%(") != -1: |
| 532 | try: |
| 533 | value = value % vars |
| 534 | except KeyError, key: |
| 535 | raise InterpolationError(key, option, section, rawval) |
| 536 | else: |
| 537 | break |
| 538 | if value.find("%(") != -1: |
| 539 | raise InterpolationDepthError(option, section, rawval) |
| 540 | return value |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 541 | |
| 542 | |
| 543 | class SafeConfigParser(ConfigParser): |
| 544 | |
| 545 | def _interpolate(self, section, option, rawval, vars): |
| 546 | # do the string interpolation |
| 547 | L = [] |
| 548 | self._interpolate_some(option, L, rawval, section, vars, 1) |
| 549 | return ''.join(L) |
| 550 | |
| 551 | _interpvar_match = re.compile(r"%\(([^)]+)\)s").match |
| 552 | |
| 553 | def _interpolate_some(self, option, accum, rest, section, map, depth): |
| 554 | if depth > MAX_INTERPOLATION_DEPTH: |
| 555 | raise InterpolationDepthError(option, section, rest) |
| 556 | while rest: |
| 557 | p = rest.find("%") |
| 558 | if p < 0: |
| 559 | accum.append(rest) |
| 560 | return |
| 561 | if p > 0: |
| 562 | accum.append(rest[:p]) |
| 563 | rest = rest[p:] |
| 564 | # p is no longer used |
| 565 | c = rest[1:2] |
| 566 | if c == "%": |
| 567 | accum.append("%") |
| 568 | rest = rest[2:] |
| 569 | elif c == "(": |
| 570 | m = self._interpvar_match(rest) |
| 571 | if m is None: |
| 572 | raise InterpolationSyntaxError( |
| 573 | "bad interpolation variable syntax at: %r" % rest) |
| 574 | var = m.group(1) |
| 575 | rest = rest[m.end():] |
| 576 | try: |
| 577 | v = map[var] |
| 578 | except KeyError: |
| 579 | raise InterpolationError( |
| 580 | "no value found for %r" % var) |
| 581 | if "%" in v: |
| 582 | self._interpolate_some(option, accum, v, |
| 583 | section, map, depth + 1) |
| 584 | else: |
| 585 | accum.append(v) |
| 586 | else: |
| 587 | raise InterpolationSyntaxError( |
| 588 | "'%' must be followed by '%' or '('") |