Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 1 | """Configuration file parser. |
| 2 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 3 | A configuration file consists of sections, lead by a "[section]" header, |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 27 | __init__(defaults=None, dict_type=_default_dict, |
| 28 | delimiters=('=', ':'), comment_prefixes=('#', ';'), |
| 29 | empty_lines_in_values=True, allow_no_value=False): |
| 30 | Create the parser. When `defaults' is given, it is initialized into the |
| 31 | dictionary or intrinsic defaults. The keys must be strings, the values |
| 32 | must be appropriate for %()s string interpolation. Note that `__name__' |
| 33 | is always an intrinsic default; its value is the section's name. |
| 34 | |
| 35 | When `dict_type' is given, it will be used to create the dictionary |
| 36 | objects for the list of sections, for the options within a section, and |
| 37 | for the default values. |
| 38 | |
| 39 | When `delimiters' is given, it will be used as the set of substrings |
| 40 | that divide keys from values. |
| 41 | |
| 42 | When `comment_prefixes' is given, it will be used as the set of |
| 43 | substrings that prefix comments in a line. |
| 44 | |
| 45 | When `empty_lines_in_values' is False (default: True), each empty line |
| 46 | marks the end of an option. Otherwise, internal empty lines of |
| 47 | a multiline option are kept as part of the value. |
| 48 | |
| 49 | When `allow_no_value' is True (default: False), options without |
| 50 | values are accepted; the value presented for these is None. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 51 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 52 | sections() |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 53 | Return all the configuration section names, sans DEFAULT. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 55 | has_section(section) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 56 | Return whether the given section exists. |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 57 | |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 58 | has_option(section, option) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 59 | Return whether the given option exists in the given section. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 60 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 61 | options(section) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 62 | Return list of configuration options for the named section. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 63 | |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 64 | read(filenames, encoding=None) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 65 | Read and parse the list of named configuration files, given by |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 66 | name. A single filename is also allowed. Non-existing files |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 67 | are ignored. Return list of successfully read files. |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 68 | |
| 69 | readfp(fp, filename=None) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 70 | Read and parse one configuration file, given as a file object. |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 71 | The filename defaults to fp.name; it is only used in error |
Barry Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 72 | messages (if fp has no `name' attribute, the string `<???>' is used). |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 73 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 74 | get(section, option, raw=False, vars=None) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 75 | Return a string value for the named option. All % interpolations are |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 76 | expanded in the return values, based on the defaults passed into the |
| 77 | constructor and the DEFAULT section. Additional substitutions may be |
| 78 | provided using the `vars' argument, which must be a dictionary whose |
| 79 | contents override any pre-existing defaults. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 81 | getint(section, options) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 82 | Like get(), but convert value to an integer. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 83 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 84 | getfloat(section, options) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 85 | Like get(), but convert value to a float. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | f09f6a5 | 1999-01-26 22:01:37 +0000 | [diff] [blame] | 87 | getboolean(section, options) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 88 | Like get(), but convert value to a boolean (currently case |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 89 | insensitively defined as 0, false, no, off for False, and 1, true, |
| 90 | yes, on for True). Returns False or True. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 91 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 92 | items(section, raw=False, vars=None) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 93 | Return a list of tuples with (name, value) for each option |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 94 | in the section. |
| 95 | |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 96 | remove_section(section) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 97 | Remove the given file section and all its options. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | remove_option(section, option) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 100 | Remove the given option from the given section. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 101 | |
| 102 | set(section, option, value) |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 103 | Set the given option. |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 105 | write(fp, space_around_delimiters=True) |
| 106 | Write the configuration state in .ini format. If |
| 107 | `space_around_delimiters' is True (the default), delimiters |
| 108 | between keys and values are surrounded by spaces. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 109 | """ |
| 110 | |
Raymond Hettinger | ff23e8c | 2009-03-03 01:32:48 +0000 | [diff] [blame] | 111 | try: |
| 112 | from collections import OrderedDict as _default_dict |
| 113 | except ImportError: |
| 114 | # fallback for setup.py which hasn't yet built _collections |
| 115 | _default_dict = dict |
| 116 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 117 | import re |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 118 | import sys |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 119 | |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 120 | __all__ = ["NoSectionError", "DuplicateSectionError", "NoOptionError", |
| 121 | "InterpolationError", "InterpolationDepthError", |
| 122 | "InterpolationSyntaxError", "ParsingError", |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 123 | "MissingSectionHeaderError", |
| 124 | "ConfigParser", "SafeConfigParser", "RawConfigParser", |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 125 | "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 127 | DEFAULTSECT = "DEFAULT" |
| 128 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 129 | MAX_INTERPOLATION_DEPTH = 10 |
| 130 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 131 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 133 | # exception classes |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 134 | class Error(Exception): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 135 | """Base class for ConfigParser exceptions.""" |
| 136 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 137 | def _get_message(self): |
| 138 | """Getter for 'message'; needed only to override deprecation in |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 139 | BaseException. |
| 140 | """ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 141 | return self.__message |
| 142 | |
| 143 | def _set_message(self, value): |
| 144 | """Setter for 'message'; needed only to override deprecation in |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 145 | BaseException. |
| 146 | """ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 147 | self.__message = value |
| 148 | |
| 149 | # BaseException.message has been deprecated since Python 2.6. To prevent |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 150 | # DeprecationWarning from popping up over this pre-existing attribute, use a |
| 151 | # new property that takes lookup precedence. |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 152 | message = property(_get_message, _set_message) |
| 153 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 154 | def __init__(self, msg=''): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 155 | self.message = msg |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 156 | Exception.__init__(self, msg) |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 158 | def __repr__(self): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 159 | return self.message |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 160 | |
Fred Drake | 7c1e5ad | 2000-12-11 18:13:19 +0000 | [diff] [blame] | 161 | __str__ = __repr__ |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 162 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 164 | class NoSectionError(Error): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 165 | """Raised when no section matches a requested option.""" |
| 166 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 167 | def __init__(self, section): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 168 | Error.__init__(self, 'No section: %r' % (section,)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | self.section = section |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 170 | self.args = (section, ) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 173 | class DuplicateSectionError(Error): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 174 | """Raised when a section is multiply-created.""" |
| 175 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 176 | def __init__(self, section): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 177 | Error.__init__(self, "Section %r already exists" % section) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 178 | self.section = section |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 179 | self.args = (section, ) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 182 | class NoOptionError(Error): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 183 | """A requested option was not found.""" |
| 184 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 185 | def __init__(self, option, section): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 186 | Error.__init__(self, "No option %r in section: %r" % |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | (option, section)) |
| 188 | self.option = option |
| 189 | self.section = section |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 190 | self.args = (option, section) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 191 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 193 | class InterpolationError(Error): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 194 | """Base class for interpolation-related exceptions.""" |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 195 | |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 196 | def __init__(self, option, section, msg): |
| 197 | Error.__init__(self, msg) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 198 | self.option = option |
| 199 | self.section = section |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 200 | self.args = (option, section, msg) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 201 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 202 | |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 203 | class InterpolationMissingOptionError(InterpolationError): |
| 204 | """A string substitution required a setting which was not available.""" |
| 205 | |
| 206 | def __init__(self, option, section, rawval, reference): |
| 207 | msg = ("Bad value substitution:\n" |
| 208 | "\tsection: [%s]\n" |
| 209 | "\toption : %s\n" |
| 210 | "\tkey : %s\n" |
| 211 | "\trawval : %s\n" |
| 212 | % (section, option, reference, rawval)) |
| 213 | InterpolationError.__init__(self, option, section, msg) |
| 214 | self.reference = reference |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 215 | self.args = (option, section, rawval, reference) |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 216 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 217 | |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 218 | class InterpolationSyntaxError(InterpolationError): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 219 | """Raised when the source text into which substitutions are made |
| 220 | does not conform to the required syntax.""" |
Neal Norwitz | ce1d944 | 2002-12-30 23:38:47 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 222 | |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 223 | class InterpolationDepthError(InterpolationError): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 224 | """Raised when substitutions are nested too deeply.""" |
| 225 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 226 | def __init__(self, option, section, rawval): |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 227 | msg = ("Value interpolation too deeply recursive:\n" |
| 228 | "\tsection: [%s]\n" |
| 229 | "\toption : %s\n" |
| 230 | "\trawval : %s\n" |
| 231 | % (section, option, rawval)) |
| 232 | InterpolationError.__init__(self, option, section, msg) |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 233 | self.args = (option, section, rawval) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 234 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 235 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 236 | class ParsingError(Error): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 237 | """Raised when a configuration file does not follow legal syntax.""" |
| 238 | |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 239 | def __init__(self, filename): |
| 240 | Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 241 | self.filename = filename |
| 242 | self.errors = [] |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 243 | self.args = (filename, ) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 244 | |
| 245 | def append(self, lineno, line): |
| 246 | self.errors.append((lineno, line)) |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 247 | self.message += '\n\t[line %2d]: %s' % (lineno, line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 248 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 249 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 250 | class MissingSectionHeaderError(ParsingError): |
Fred Drake | 8d5dd98 | 2002-12-30 23:51:45 +0000 | [diff] [blame] | 251 | """Raised when a key-value pair is found before any section header.""" |
| 252 | |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 253 | def __init__(self, filename, lineno, line): |
| 254 | Error.__init__( |
| 255 | self, |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 256 | 'File contains no section headers.\nfile: %s, line: %d\n%r' % |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 257 | (filename, lineno, line)) |
| 258 | self.filename = filename |
| 259 | self.lineno = lineno |
| 260 | self.line = line |
Michael Foord | bd6c079 | 2010-07-25 23:09:25 +0000 | [diff] [blame] | 261 | self.args = (filename, lineno, line) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 262 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 263 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 264 | class RawConfigParser: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 265 | """ConfigParser that does not do interpolation.""" |
| 266 | |
| 267 | # Regular expressions for parsing section headers and options |
| 268 | _SECT_TMPL = r""" |
| 269 | \[ # [ |
| 270 | (?P<header>[^]]+) # very permissive! |
| 271 | \] # ] |
| 272 | """ |
| 273 | _OPT_TMPL = r""" |
| 274 | (?P<option>.*?) # very permissive! |
| 275 | \s*(?P<vi>{delim})\s* # any number of space/tab, |
| 276 | # followed by any of the |
| 277 | # allowed delimiters, |
| 278 | # followed by any space/tab |
| 279 | (?P<value>.*)$ # everything up to eol |
| 280 | """ |
| 281 | _OPT_NV_TMPL = r""" |
| 282 | (?P<option>.*?) # very permissive! |
| 283 | \s*(?: # any number of space/tab, |
| 284 | (?P<vi>{delim})\s* # optionally followed by |
| 285 | # any of the allowed |
| 286 | # delimiters, followed by any |
| 287 | # space/tab |
| 288 | (?P<value>.*))?$ # everything up to eol |
| 289 | """ |
| 290 | |
| 291 | # Compiled regular expression for matching sections |
| 292 | SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE) |
| 293 | # Compiled regular expression for matching options with typical separators |
| 294 | OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE) |
| 295 | # Compiled regular expression for matching options with optional values |
| 296 | # delimited using typical separators |
| 297 | OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE) |
| 298 | # Compiled regular expression for matching leading whitespace in a line |
| 299 | NONSPACECRE = re.compile(r"\S") |
| 300 | # Select backwards-compatible inline comment character behavior |
| 301 | # (; and # are comments at the start of a line, but ; only inline) |
| 302 | _COMPATIBLE = object() |
| 303 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 304 | def __init__(self, defaults=None, dict_type=_default_dict, |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 305 | delimiters=('=', ':'), comment_prefixes=_COMPATIBLE, |
| 306 | empty_lines_in_values=True, allow_no_value=False): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 307 | self._dict = dict_type |
| 308 | self._sections = self._dict() |
| 309 | self._defaults = self._dict() |
David Goodger | 68a1abd | 2004-10-03 15:40:25 +0000 | [diff] [blame] | 310 | if defaults: |
| 311 | for key, value in defaults.items(): |
| 312 | self._defaults[self.optionxform(key)] = value |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 313 | self._delimiters = tuple(delimiters) |
| 314 | if delimiters == ('=', ':'): |
| 315 | self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE |
| 316 | else: |
| 317 | delim = "|".join(re.escape(d) for d in delimiters) |
| 318 | if allow_no_value: |
| 319 | self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=delim), |
| 320 | re.VERBOSE) |
| 321 | else: |
| 322 | self._optcre = re.compile(self._OPT_TMPL.format(delim=delim), |
| 323 | re.VERBOSE) |
| 324 | if comment_prefixes is self._COMPATIBLE: |
| 325 | self._startonly_comment_prefixes = ('#',) |
| 326 | self._comment_prefixes = (';',) |
| 327 | else: |
| 328 | self._startonly_comment_prefixes = () |
| 329 | self._comment_prefixes = tuple(comment_prefixes or ()) |
| 330 | self._empty_lines_in_values = empty_lines_in_values |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 331 | |
| 332 | def defaults(self): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 333 | return self._defaults |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 334 | |
| 335 | def sections(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 336 | """Return a list of section names, excluding [DEFAULT]""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 337 | # self._sections will never have [DEFAULT] in it |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 338 | return list(self._sections.keys()) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 339 | |
| 340 | def add_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 341 | """Create a new section in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 343 | Raise DuplicateSectionError if a section by the specified name |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 344 | already exists. Raise ValueError if name is DEFAULT or any of it's |
| 345 | case-insensitive variants. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 346 | """ |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 347 | if section.lower() == "default": |
| 348 | raise ValueError('Invalid section name: %s' % section) |
| 349 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 350 | if section in self._sections: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 351 | raise DuplicateSectionError(section) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 352 | self._sections[section] = self._dict() |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 353 | |
| 354 | def has_section(self, section): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 355 | """Indicate whether the named section is present in the configuration. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 357 | The DEFAULT section is not acknowledged. |
| 358 | """ |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 359 | return section in self._sections |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 360 | |
| 361 | def options(self, section): |
Guido van Rossum | a5a24b7 | 1999-10-04 19:58:22 +0000 | [diff] [blame] | 362 | """Return a list of option names for the given section name.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 363 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 364 | opts = self._sections[section].copy() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 365 | except KeyError: |
| 366 | raise NoSectionError(section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 367 | opts.update(self._defaults) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 368 | if '__name__' in opts: |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 369 | del opts['__name__'] |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 370 | return list(opts.keys()) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 371 | |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 372 | def read(self, filenames, encoding=None): |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 373 | """Read and parse a filename or a list of filenames. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 375 | Files that cannot be opened are silently ignored; this is |
Barry Warsaw | 2539451 | 1999-10-12 16:12:48 +0000 | [diff] [blame] | 376 | designed so that you can specify a list of potential |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 377 | configuration file locations (e.g. current directory, user's |
| 378 | home directory, systemwide directory), and all existing |
| 379 | configuration files in the list will be read. A single |
| 380 | filename may also be given. |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 381 | |
| 382 | Return list of successfully read files. |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 383 | """ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 384 | if isinstance(filenames, str): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 385 | filenames = [filenames] |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 386 | read_ok = [] |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 387 | for filename in filenames: |
| 388 | try: |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 389 | fp = open(filename, encoding=encoding) |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 390 | except IOError: |
| 391 | continue |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 392 | self._read(fp, filename) |
Fred Drake | 2438a48 | 1999-10-04 18:11:56 +0000 | [diff] [blame] | 393 | fp.close() |
Fred Drake | 8290314 | 2004-05-18 04:24:02 +0000 | [diff] [blame] | 394 | read_ok.append(filename) |
| 395 | return read_ok |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 397 | def readfp(self, fp, filename=None): |
| 398 | """Like read() but the argument must be a file-like object. |
| 399 | |
| 400 | The `fp' argument must have a `readline' method. Optional |
| 401 | second argument is the `filename', which if not given, is |
| 402 | taken from fp.name. If fp has no `name' attribute, `<???>' is |
| 403 | used. |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 404 | """ |
| 405 | if filename is None: |
| 406 | try: |
| 407 | filename = fp.name |
| 408 | except AttributeError: |
| 409 | filename = '<???>' |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 410 | self._read(fp, filename) |
Guido van Rossum | 6a8d84b | 1999-10-04 18:57:27 +0000 | [diff] [blame] | 411 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 412 | def get(self, section, option): |
| 413 | opt = self.optionxform(option) |
| 414 | if section not in self._sections: |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 415 | if section != DEFAULTSECT: |
| 416 | raise NoSectionError(section) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 417 | if opt in self._defaults: |
| 418 | return self._defaults[opt] |
| 419 | else: |
| 420 | raise NoOptionError(option, section) |
| 421 | elif opt in self._sections[section]: |
| 422 | return self._sections[section][opt] |
| 423 | elif opt in self._defaults: |
| 424 | return self._defaults[opt] |
| 425 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 426 | raise NoOptionError(option, section) |
Fred Drake | 2a37f9f | 2000-09-27 22:43:54 +0000 | [diff] [blame] | 427 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 428 | def items(self, section): |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 429 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 430 | d2 = self._sections[section] |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 431 | except KeyError: |
| 432 | if section != DEFAULTSECT: |
| 433 | raise NoSectionError(section) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 434 | d2 = self._dict() |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 435 | d = self._defaults.copy() |
| 436 | d.update(d2) |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 437 | if "__name__" in d: |
| 438 | del d["__name__"] |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 439 | return d.items() |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 440 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 441 | def _get(self, section, conv, option): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 442 | return conv(self.get(section, option)) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 443 | |
| 444 | def getint(self, section, option): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 445 | return self._get(section, int, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 446 | |
| 447 | def getfloat(self, section, option): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 448 | return self._get(section, float, option) |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 449 | |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 450 | _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, |
| 451 | '0': False, 'no': False, 'false': False, 'off': False} |
| 452 | |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 453 | def getboolean(self, section, option): |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 454 | v = self.get(section, option) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 455 | if v.lower() not in self._boolean_states: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 456 | raise ValueError('Not a boolean: %s' % v) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 457 | return self._boolean_states[v.lower()] |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 459 | def optionxform(self, optionstr): |
Eric S. Raymond | 9eb54d9 | 2001-02-09 05:19:09 +0000 | [diff] [blame] | 460 | return optionstr.lower() |
Guido van Rossum | 9e480ad | 1999-06-17 18:41:42 +0000 | [diff] [blame] | 461 | |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 462 | def has_option(self, section, option): |
| 463 | """Check for the existence of a given option in a given section.""" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 464 | |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 465 | if not section or section == DEFAULTSECT: |
| 466 | option = self.optionxform(option) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 467 | return option in self._defaults |
| 468 | elif section not in self._sections: |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 469 | return False |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 470 | else: |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 471 | option = self.optionxform(option) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 472 | return (option in self._sections[section] |
| 473 | or option in self._defaults) |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 474 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 475 | def set(self, section, option, value=None): |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 476 | """Set an option.""" |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 477 | |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 478 | if not section or section == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 479 | sectdict = self._defaults |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 480 | else: |
| 481 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 482 | sectdict = self._sections[section] |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 483 | except KeyError: |
| 484 | raise NoSectionError(section) |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 485 | sectdict[self.optionxform(option)] = value |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 486 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 487 | def write(self, fp, space_around_delimiters=True): |
| 488 | """Write an .ini-format representation of the configuration state. |
| 489 | |
| 490 | If `space_around_delimiters' is True (the default), delimiters |
| 491 | between keys and values are surrounded by spaces. |
| 492 | """ |
| 493 | if space_around_delimiters: |
| 494 | d = " {} ".format(self._delimiters[0]) |
| 495 | else: |
| 496 | d = self._delimiters[0] |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 497 | if self._defaults: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 498 | self._write_section(fp, DEFAULTSECT, self._defaults.items(), d) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 499 | for section in self._sections: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 500 | self._write_section(fp, section, |
| 501 | self._sections[section].items(), d) |
| 502 | |
| 503 | def _write_section(self, fp, section_name, section_items, delimiter): |
| 504 | """Write a single section to the specified `fp'.""" |
| 505 | fp.write("[{}]\n".format(section_name)) |
| 506 | for key, value in section_items: |
| 507 | if key == "__name__": |
| 508 | continue |
| 509 | if value is not None: |
| 510 | value = delimiter + str(value).replace('\n', '\n\t') |
| 511 | else: |
| 512 | value = "" |
| 513 | fp.write("{}{}\n".format(key, value)) |
| 514 | fp.write("\n") |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 515 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 516 | def remove_option(self, section, option): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 517 | """Remove an option.""" |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 518 | if not section or section == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 519 | sectdict = self._defaults |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 520 | else: |
| 521 | try: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 522 | sectdict = self._sections[section] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 523 | except KeyError: |
| 524 | raise NoSectionError(section) |
Fred Drake | 3c823aa | 2001-02-26 21:55:34 +0000 | [diff] [blame] | 525 | option = self.optionxform(option) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 526 | existed = option in sectdict |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 527 | if existed: |
Fred Drake | ff4a23b | 2000-12-04 16:29:13 +0000 | [diff] [blame] | 528 | del sectdict[option] |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 529 | return existed |
| 530 | |
Thomas Wouters | ff4df6d | 2000-07-21 05:19:59 +0000 | [diff] [blame] | 531 | def remove_section(self, section): |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 532 | """Remove a file section.""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 533 | existed = section in self._sections |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 534 | if existed: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 535 | del self._sections[section] |
Fred Drake | c2ff905 | 2002-09-27 15:33:11 +0000 | [diff] [blame] | 536 | return existed |
Eric S. Raymond | 649685a | 2000-07-14 14:28:22 +0000 | [diff] [blame] | 537 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 538 | def _read(self, fp, fpname): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 539 | """Parse a sectioned configuration file. |
Guido van Rossum | 3d20986 | 1997-12-09 16:10:31 +0000 | [diff] [blame] | 540 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 541 | Each section in a configuration file contains a header, indicated by a |
| 542 | name in square brackets (`[]'), plus key/value options, indicated by |
| 543 | `name' and `value' delimited with a specific substring (`=' or `:' by |
| 544 | default). |
| 545 | |
| 546 | Values can span multiple lines, as long as they are indented deeper than |
| 547 | the first line of the value. Depending on the parser's mode, blank lines |
| 548 | may be treated as parts of multiline values or ignored. |
| 549 | |
| 550 | Configuration files may include comments, prefixed by specific |
| 551 | characters (`#' and `;' by default). Comments may appear on their own in |
| 552 | an otherwise empty line or may be entered in lines holding values or |
| 553 | section names. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 554 | """ |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 555 | cursect = None # None, or a dictionary |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 556 | optname = None |
| 557 | lineno = 0 |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 558 | indent_level = 0 |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 559 | e = None # None, or an exception |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 560 | for lineno, line in enumerate(fp, start=1): |
Georg Brandl | f206d0e | 2010-07-29 11:56:20 +0000 | [diff] [blame] | 561 | # strip full line comments |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 562 | comment_start = None |
| 563 | for prefix in self._startonly_comment_prefixes: |
| 564 | if line.strip().startswith(prefix): |
| 565 | comment_start = 0 |
| 566 | break |
| 567 | # strip inline comments |
| 568 | for prefix in self._comment_prefixes: |
| 569 | index = line.find(prefix) |
| 570 | if index == 0 or (index > 0 and line[index-1].isspace()): |
| 571 | comment_start = index |
| 572 | break |
| 573 | value = line[:comment_start].strip() |
| 574 | if not value: |
Georg Brandl | f206d0e | 2010-07-29 11:56:20 +0000 | [diff] [blame] | 575 | if self._empty_lines_in_values: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 576 | # add empty line to the value, but only if there was no |
| 577 | # comment on the line |
Georg Brandl | f206d0e | 2010-07-29 11:56:20 +0000 | [diff] [blame] | 578 | if (comment_start is None and |
| 579 | cursect is not None and |
| 580 | optname and |
| 581 | cursect[optname] is not None): |
| 582 | cursect[optname].append('') # newlines added at join |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 583 | else: |
| 584 | # empty line marks end of value |
| 585 | indent_level = sys.maxsize |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 586 | continue |
| 587 | # continuation line? |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 588 | first_nonspace = self.NONSPACECRE.search(line) |
| 589 | cur_indent_level = first_nonspace.start() if first_nonspace else 0 |
| 590 | if (cursect is not None and optname and |
| 591 | cur_indent_level > indent_level): |
| 592 | cursect[optname].append(value) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 593 | # a section header or option header? |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 594 | else: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 595 | indent_level = cur_indent_level |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 596 | # is it a section header? |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 597 | mo = self.SECTCRE.match(value) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 598 | if mo: |
| 599 | sectname = mo.group('header') |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 600 | if sectname in self._sections: |
| 601 | cursect = self._sections[sectname] |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 602 | elif sectname == DEFAULTSECT: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 603 | cursect = self._defaults |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 604 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 605 | cursect = self._dict() |
| 606 | cursect['__name__'] = sectname |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 607 | self._sections[sectname] = cursect |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 608 | # So sections can't start with a continuation line |
| 609 | optname = None |
| 610 | # no section header in the file? |
| 611 | elif cursect is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 612 | raise MissingSectionHeaderError(fpname, lineno, line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 613 | # an option line? |
| 614 | else: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 615 | mo = self._optcre.match(value) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 616 | if mo: |
Fred Drake | c517b9b | 2000-02-28 20:59:03 +0000 | [diff] [blame] | 617 | optname, vi, optval = mo.group('option', 'vi', 'value') |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 618 | if not optname: |
| 619 | e = self._handle_error(e, fpname, lineno, line) |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 620 | optname = self.optionxform(optname.rstrip()) |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 621 | # This check is fine because the OPTCRE cannot |
| 622 | # match if it would set optval to None |
| 623 | if optval is not None: |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 624 | optval = optval.strip() |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 625 | # allow empty values |
| 626 | if optval == '""': |
| 627 | optval = '' |
| 628 | cursect[optname] = [optval] |
| 629 | else: |
| 630 | # valueless option handling |
| 631 | cursect[optname] = optval |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 632 | else: |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 633 | # a non-fatal parsing error occurred. set up the |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 634 | # exception but keep going. the exception will be |
| 635 | # raised at the end of the file and will contain a |
| 636 | # list of all bogus lines |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 637 | e = self._handle_error(e, fpname, lineno, line) |
Barry Warsaw | bfa3f6b | 1998-07-01 20:41:12 +0000 | [diff] [blame] | 638 | # if any parsing errors occurred, raise an exception |
| 639 | if e: |
| 640 | raise e |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 641 | self._join_multiline_values() |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 642 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 643 | def _join_multiline_values(self): |
Brian Curtin | 9a27b0c | 2010-07-26 00:27:10 +0000 | [diff] [blame] | 644 | all_sections = [self._defaults] |
| 645 | all_sections.extend(self._sections.values()) |
| 646 | for options in all_sections: |
| 647 | for name, val in options.items(): |
| 648 | if isinstance(val, list): |
Georg Brandl | f206d0e | 2010-07-29 11:56:20 +0000 | [diff] [blame] | 649 | options[name] = '\n'.join(val).rstrip() |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 650 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 651 | def _handle_error(self, exc, fpname, lineno, line): |
| 652 | if not exc: |
| 653 | exc = ParsingError(fpname) |
| 654 | exc.append(lineno, repr(line)) |
| 655 | return exc |
| 656 | |
| 657 | |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 658 | class ConfigParser(RawConfigParser): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 659 | """ConfigParser implementing interpolation.""" |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 660 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 661 | def get(self, section, option, raw=False, vars=None): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 662 | """Get an option value for a given section. |
| 663 | |
Georg Brandl | 470a123 | 2010-07-29 14:17:12 +0000 | [diff] [blame] | 664 | If `vars' is provided, it must be a dictionary. The option is looked up |
| 665 | in `vars' (if provided), `section', and in `defaults' in that order. |
| 666 | |
| 667 | All % interpolations are expanded in the return values, unless the |
| 668 | optional argument `raw' is true. Values for interpolation keys are |
| 669 | looked up in the same manner as the option. |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 670 | |
| 671 | The section DEFAULT is special. |
| 672 | """ |
| 673 | d = self._defaults.copy() |
| 674 | try: |
| 675 | d.update(self._sections[section]) |
| 676 | except KeyError: |
| 677 | if section != DEFAULTSECT: |
| 678 | raise NoSectionError(section) |
| 679 | # Update with the entry specific variables |
David Goodger | 68a1abd | 2004-10-03 15:40:25 +0000 | [diff] [blame] | 680 | if vars: |
| 681 | for key, value in vars.items(): |
| 682 | d[self.optionxform(key)] = value |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 683 | option = self.optionxform(option) |
| 684 | try: |
| 685 | value = d[option] |
| 686 | except KeyError: |
| 687 | raise NoOptionError(option, section) |
| 688 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 689 | if raw or value is None: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 690 | return value |
| 691 | else: |
| 692 | return self._interpolate(section, option, value, d) |
| 693 | |
Neal Norwitz | f680cc4 | 2002-12-17 01:56:47 +0000 | [diff] [blame] | 694 | def items(self, section, raw=False, vars=None): |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 695 | """Return a list of tuples with (name, value) for each option |
| 696 | in the section. |
| 697 | |
| 698 | All % interpolations are expanded in the return values, based on the |
| 699 | defaults passed into the constructor, unless the optional argument |
| 700 | `raw' is true. Additional substitutions may be provided using the |
| 701 | `vars' argument, which must be a dictionary whose contents overrides |
| 702 | any pre-existing defaults. |
| 703 | |
| 704 | The section DEFAULT is special. |
| 705 | """ |
| 706 | d = self._defaults.copy() |
| 707 | try: |
| 708 | d.update(self._sections[section]) |
| 709 | except KeyError: |
| 710 | if section != DEFAULTSECT: |
| 711 | raise NoSectionError(section) |
| 712 | # Update with the entry specific variables |
| 713 | if vars: |
David Goodger | 68a1abd | 2004-10-03 15:40:25 +0000 | [diff] [blame] | 714 | for key, value in vars.items(): |
| 715 | d[self.optionxform(key)] = value |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 716 | options = list(d.keys()) |
Fred Drake | df393bd | 2002-10-25 20:41:30 +0000 | [diff] [blame] | 717 | if "__name__" in options: |
| 718 | options.remove("__name__") |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 719 | if raw: |
Fred Drake | 8c4da53 | 2003-10-21 16:45:00 +0000 | [diff] [blame] | 720 | return [(option, d[option]) |
| 721 | for option in options] |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 722 | else: |
Fred Drake | 8c4da53 | 2003-10-21 16:45:00 +0000 | [diff] [blame] | 723 | return [(option, self._interpolate(section, option, d[option], d)) |
| 724 | for option in options] |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 725 | |
| 726 | def _interpolate(self, section, option, rawval, vars): |
| 727 | # do the string interpolation |
| 728 | value = rawval |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 729 | depth = MAX_INTERPOLATION_DEPTH |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 730 | while depth: # Loop through this until it's done |
| 731 | depth -= 1 |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 732 | if value and "%(" in value: |
Fred Drake | bc12b01 | 2004-05-18 02:25:51 +0000 | [diff] [blame] | 733 | value = self._KEYCRE.sub(self._interpolation_replace, value) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 734 | try: |
| 735 | value = value % vars |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 736 | except KeyError as e: |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 737 | raise InterpolationMissingOptionError( |
Brett Cannon | ca477b2 | 2007-03-21 22:26:20 +0000 | [diff] [blame] | 738 | option, section, rawval, e.args[0]) |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 739 | else: |
| 740 | break |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 741 | if value and "%(" in value: |
Fred Drake | fce6557 | 2002-10-25 18:08:18 +0000 | [diff] [blame] | 742 | raise InterpolationDepthError(option, section, rawval) |
| 743 | return value |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 744 | |
Fred Drake | bc12b01 | 2004-05-18 02:25:51 +0000 | [diff] [blame] | 745 | _KEYCRE = re.compile(r"%\(([^)]*)\)s|.") |
| 746 | |
| 747 | def _interpolation_replace(self, match): |
| 748 | s = match.group(1) |
| 749 | if s is None: |
| 750 | return match.group() |
| 751 | else: |
| 752 | return "%%(%s)s" % self.optionxform(s) |
| 753 | |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 754 | |
| 755 | class SafeConfigParser(ConfigParser): |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 756 | """ConfigParser implementing sane interpolation.""" |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 757 | |
| 758 | def _interpolate(self, section, option, rawval, vars): |
| 759 | # do the string interpolation |
| 760 | L = [] |
| 761 | self._interpolate_some(option, L, rawval, section, vars, 1) |
| 762 | return ''.join(L) |
| 763 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 764 | _interpvar_re = re.compile(r"%\(([^)]+)\)s") |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 765 | |
| 766 | def _interpolate_some(self, option, accum, rest, section, map, depth): |
| 767 | if depth > MAX_INTERPOLATION_DEPTH: |
| 768 | raise InterpolationDepthError(option, section, rest) |
| 769 | while rest: |
| 770 | p = rest.find("%") |
| 771 | if p < 0: |
| 772 | accum.append(rest) |
| 773 | return |
| 774 | if p > 0: |
| 775 | accum.append(rest[:p]) |
| 776 | rest = rest[p:] |
| 777 | # p is no longer used |
| 778 | c = rest[1:2] |
| 779 | if c == "%": |
| 780 | accum.append("%") |
| 781 | rest = rest[2:] |
| 782 | elif c == "(": |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 783 | m = self._interpvar_re.match(rest) |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 784 | if m is None: |
Neal Norwitz | 10f3018 | 2003-06-29 04:23:35 +0000 | [diff] [blame] | 785 | raise InterpolationSyntaxError(option, section, |
| 786 | "bad interpolation variable reference %r" % rest) |
Fred Drake | bc12b01 | 2004-05-18 02:25:51 +0000 | [diff] [blame] | 787 | var = self.optionxform(m.group(1)) |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 788 | rest = rest[m.end():] |
| 789 | try: |
| 790 | v = map[var] |
| 791 | except KeyError: |
Fred Drake | e2c6491 | 2002-12-31 17:23:27 +0000 | [diff] [blame] | 792 | raise InterpolationMissingOptionError( |
| 793 | option, section, rest, var) |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame] | 794 | if "%" in v: |
| 795 | self._interpolate_some(option, accum, v, |
| 796 | section, map, depth + 1) |
| 797 | else: |
| 798 | accum.append(v) |
| 799 | else: |
| 800 | raise InterpolationSyntaxError( |
Neal Norwitz | 10f3018 | 2003-06-29 04:23:35 +0000 | [diff] [blame] | 801 | option, section, |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 802 | "'%%' must be followed by '%%' or '(', found: %r" % (rest,)) |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 803 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 804 | def set(self, section, option, value=None): |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 805 | """Set an option. Extend ConfigParser.set: check for string values.""" |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 806 | # The only legal non-string value if we allow valueless |
| 807 | # options is None, so we need to check if the value is a |
| 808 | # string if: |
| 809 | # - we do not allow valueless options, or |
| 810 | # - we allow valueless options but the value is not None |
| 811 | if self._optcre is self.OPTCRE or value: |
| 812 | if not isinstance(value, str): |
| 813 | raise TypeError("option values must be strings") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 814 | # check for bad percent signs: |
| 815 | # first, replace all "good" interpolations |
Georg Brandl | 68998bf | 2009-04-27 16:43:36 +0000 | [diff] [blame] | 816 | tmp_value = value.replace('%%', '') |
| 817 | tmp_value = self._interpvar_re.sub('', tmp_value) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 818 | # then, check if there's a lone percent sign left |
Georg Brandl | 1f9fa31 | 2009-04-27 16:42:58 +0000 | [diff] [blame] | 819 | percent_index = tmp_value.find('%') |
| 820 | if percent_index != -1: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 821 | raise ValueError("invalid interpolation syntax in %r at " |
Georg Brandl | 1f9fa31 | 2009-04-27 16:42:58 +0000 | [diff] [blame] | 822 | "position %d" % (value, percent_index)) |
David Goodger | 1cbf206 | 2004-10-03 15:55:09 +0000 | [diff] [blame] | 823 | ConfigParser.set(self, section, option, value) |