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