blob: 7f1514f902077cdbb1abe83ed730a05d2527641a [file] [log] [blame]
Guido van Rossum3d209861997-12-09 16:10:31 +00001"""Configuration file parser.
2
Georg Brandl96a60ae2010-07-28 13:13:46 +00003A configuration file consists of sections, lead by a "[section]" header,
Guido van Rossum3d209861997-12-09 16:10:31 +00004and followed by "name: value" entries, with continuations and such in
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00005the style of RFC 822.
Guido van Rossum3d209861997-12-09 16:10:31 +00006
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00007The option values can contain format strings which refer to other values in
8the same section, or values in a special [DEFAULT] section.
9
Guido van Rossum3d209861997-12-09 16:10:31 +000010For example:
11
12 something: %(dir)s/whatever
13
14would resolve the "%(dir)s" to the value of dir. All reference
15expansions are done late, on demand.
16
17Intrinsic defaults can be specified by passing them into the
18ConfigParser constructor as a dictionary.
19
20class:
21
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000022ConfigParser -- responsible for parsing a list of
Guido van Rossum3d209861997-12-09 16:10:31 +000023 configuration files, and managing the parsed database.
24
25 methods:
26
Fred Drakecc645b92010-09-04 04:35:34 +000027 __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
28 delimiters=('=', ':'), comment_prefixes=_COMPATIBLE,
29 strict=False, empty_lines_in_values=True):
Georg Brandl96a60ae2010-07-28 13:13:46 +000030 Create the parser. When `defaults' is given, it is initialized into the
31 dictionary or intrinsic defaults. The keys must be strings, the values
Fred Drakea4923622010-08-09 12:52:45 +000032 must be appropriate for %()s string interpolation. Note that `__name__'
Georg Brandl96a60ae2010-07-28 13:13:46 +000033 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
Fred Drakea4923622010-08-09 12:52:45 +000045 When `strict` is True, the parser won't allow for any section or option
46 duplicates while reading from a single source (file, string or
47 dictionary). Default is False.
48
Georg Brandl96a60ae2010-07-28 13:13:46 +000049 When `empty_lines_in_values' is False (default: True), each empty line
50 marks the end of an option. Otherwise, internal empty lines of
51 a multiline option are kept as part of the value.
52
53 When `allow_no_value' is True (default: False), options without
54 values are accepted; the value presented for these is None.
Guido van Rossum3d209861997-12-09 16:10:31 +000055
Barry Warsawf09f6a51999-01-26 22:01:37 +000056 sections()
Georg Brandl96a60ae2010-07-28 13:13:46 +000057 Return all the configuration section names, sans DEFAULT.
Guido van Rossum3d209861997-12-09 16:10:31 +000058
Guido van Rossuma5a24b71999-10-04 19:58:22 +000059 has_section(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +000060 Return whether the given section exists.
Guido van Rossuma5a24b71999-10-04 19:58:22 +000061
Eric S. Raymond649685a2000-07-14 14:28:22 +000062 has_option(section, option)
Georg Brandl96a60ae2010-07-28 13:13:46 +000063 Return whether the given option exists in the given section.
Eric S. Raymond649685a2000-07-14 14:28:22 +000064
Barry Warsawf09f6a51999-01-26 22:01:37 +000065 options(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +000066 Return list of configuration options for the named section.
Guido van Rossum3d209861997-12-09 16:10:31 +000067
Georg Brandl8dcaa732010-07-29 12:17:40 +000068 read(filenames, encoding=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +000069 Read and parse the list of named configuration files, given by
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000070 name. A single filename is also allowed. Non-existing files
Fred Drake82903142004-05-18 04:24:02 +000071 are ignored. Return list of successfully read files.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000072
Fred Drakea4923622010-08-09 12:52:45 +000073 read_file(f, filename=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +000074 Read and parse one configuration file, given as a file object.
Fred Drakea4923622010-08-09 12:52:45 +000075 The filename defaults to f.name; it is only used in error
76 messages (if f has no `name' attribute, the string `<???>' is used).
77
78 read_string(string)
79 Read configuration from a given string.
80
81 read_dict(dictionary)
82 Read configuration from a dictionary. Keys are section names,
83 values are dictionaries with keys and values that should be present
84 in the section. If the used dictionary type preserves order, sections
Fred Drakecc645b92010-09-04 04:35:34 +000085 and their keys will be added in order. Values are automatically
86 converted to strings.
Guido van Rossum3d209861997-12-09 16:10:31 +000087
Fred Drakecc645b92010-09-04 04:35:34 +000088 get(section, option, raw=False, vars=None, default=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000089 Return a string value for the named option. All % interpolations are
Barry Warsawf09f6a51999-01-26 22:01:37 +000090 expanded in the return values, based on the defaults passed into the
91 constructor and the DEFAULT section. Additional substitutions may be
92 provided using the `vars' argument, which must be a dictionary whose
Fred Drakecc645b92010-09-04 04:35:34 +000093 contents override any pre-existing defaults. If `option' is a key in
94 `vars', the value from `vars' is used.
Guido van Rossum3d209861997-12-09 16:10:31 +000095
Fred Drakecc645b92010-09-04 04:35:34 +000096 getint(section, options, raw=False, vars=None, default=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000097 Like get(), but convert value to an integer.
Guido van Rossum3d209861997-12-09 16:10:31 +000098
Fred Drakecc645b92010-09-04 04:35:34 +000099 getfloat(section, options, raw=False, vars=None, default=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000100 Like get(), but convert value to a float.
Guido van Rossum3d209861997-12-09 16:10:31 +0000101
Fred Drakecc645b92010-09-04 04:35:34 +0000102 getboolean(section, options, raw=False, vars=None, default=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000103 Like get(), but convert value to a boolean (currently case
Neal Norwitzf680cc42002-12-17 01:56:47 +0000104 insensitively defined as 0, false, no, off for False, and 1, true,
105 yes, on for True). Returns False or True.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000106
Neal Norwitzf680cc42002-12-17 01:56:47 +0000107 items(section, raw=False, vars=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000108 Return a list of tuples with (name, value) for each option
Fred Drake2ca041f2002-09-27 15:49:56 +0000109 in the section.
110
Eric S. Raymond649685a2000-07-14 14:28:22 +0000111 remove_section(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000112 Remove the given file section and all its options.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000113
114 remove_option(section, option)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000115 Remove the given option from the given section.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000116
117 set(section, option, value)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000118 Set the given option.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000119
Georg Brandl96a60ae2010-07-28 13:13:46 +0000120 write(fp, space_around_delimiters=True)
121 Write the configuration state in .ini format. If
122 `space_around_delimiters' is True (the default), delimiters
123 between keys and values are surrounded by spaces.
Guido van Rossum3d209861997-12-09 16:10:31 +0000124"""
125
Raymond Hettingerff23e8c2009-03-03 01:32:48 +0000126try:
127 from collections import OrderedDict as _default_dict
128except ImportError:
129 # fallback for setup.py which hasn't yet built _collections
130 _default_dict = dict
131
Fred Drakea4923622010-08-09 12:52:45 +0000132import io
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000133import re
Georg Brandl96a60ae2010-07-28 13:13:46 +0000134import sys
Fred Drakea4923622010-08-09 12:52:45 +0000135import warnings
Guido van Rossum3d209861997-12-09 16:10:31 +0000136
Fred Drakea4923622010-08-09 12:52:45 +0000137__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
138 "NoOptionError", "InterpolationError", "InterpolationDepthError",
Fred Drake8d5dd982002-12-30 23:51:45 +0000139 "InterpolationSyntaxError", "ParsingError",
David Goodger1cbf2062004-10-03 15:55:09 +0000140 "MissingSectionHeaderError",
141 "ConfigParser", "SafeConfigParser", "RawConfigParser",
Fred Drakec2ff9052002-09-27 15:33:11 +0000142 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +0000143
Guido van Rossum3d209861997-12-09 16:10:31 +0000144DEFAULTSECT = "DEFAULT"
145
Fred Drake2a37f9f2000-09-27 22:43:54 +0000146MAX_INTERPOLATION_DEPTH = 10
147
Guido van Rossum3d209861997-12-09 16:10:31 +0000148
Tim Peters88869f92001-01-14 23:36:06 +0000149
Guido van Rossum3d209861997-12-09 16:10:31 +0000150# exception classes
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000151class Error(Exception):
Fred Drake8d5dd982002-12-30 23:51:45 +0000152 """Base class for ConfigParser exceptions."""
153
Guido van Rossum360e4b82007-05-14 22:51:27 +0000154 def _get_message(self):
155 """Getter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000156 BaseException.
157 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000158 return self.__message
159
160 def _set_message(self, value):
161 """Setter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000162 BaseException.
163 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000164 self.__message = value
165
166 # BaseException.message has been deprecated since Python 2.6. To prevent
Fred Drakea4923622010-08-09 12:52:45 +0000167 # DeprecationWarning from popping up over this pre-existing attribute, use
168 # a new property that takes lookup precedence.
Guido van Rossum360e4b82007-05-14 22:51:27 +0000169 message = property(_get_message, _set_message)
170
Guido van Rossum3d209861997-12-09 16:10:31 +0000171 def __init__(self, msg=''):
Fred Drakee2c64912002-12-31 17:23:27 +0000172 self.message = msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000173 Exception.__init__(self, msg)
Fred Drake8d5dd982002-12-30 23:51:45 +0000174
Guido van Rossum3d209861997-12-09 16:10:31 +0000175 def __repr__(self):
Fred Drakee2c64912002-12-31 17:23:27 +0000176 return self.message
Fred Drake8d5dd982002-12-30 23:51:45 +0000177
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000178 __str__ = __repr__
Guido van Rossum3d209861997-12-09 16:10:31 +0000179
Georg Brandl96a60ae2010-07-28 13:13:46 +0000180
Guido van Rossum3d209861997-12-09 16:10:31 +0000181class NoSectionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000182 """Raised when no section matches a requested option."""
183
Guido van Rossum3d209861997-12-09 16:10:31 +0000184 def __init__(self, section):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000185 Error.__init__(self, 'No section: %r' % (section,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000187 self.args = (section, )
Guido van Rossum3d209861997-12-09 16:10:31 +0000188
Georg Brandl96a60ae2010-07-28 13:13:46 +0000189
Guido van Rossum3d209861997-12-09 16:10:31 +0000190class DuplicateSectionError(Error):
Fred Drakea4923622010-08-09 12:52:45 +0000191 """Raised when a section is repeated in an input source.
Fred Drake8d5dd982002-12-30 23:51:45 +0000192
Fred Drakea4923622010-08-09 12:52:45 +0000193 Possible repetitions that raise this exception are: multiple creation
194 using the API or in strict parsers when a section is found more than once
195 in a single input file, string or dictionary.
196 """
197
198 def __init__(self, section, source=None, lineno=None):
199 msg = [repr(section), " already exists"]
200 if source is not None:
201 message = ["While reading from ", source]
202 if lineno is not None:
203 message.append(" [line {0:2d}]".format(lineno))
204 message.append(": section ")
205 message.extend(msg)
206 msg = message
207 else:
208 msg.insert(0, "Section ")
209 Error.__init__(self, "".join(msg))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000210 self.section = section
Fred Drakea4923622010-08-09 12:52:45 +0000211 self.source = source
212 self.lineno = lineno
213 self.args = (section, source, lineno)
214
215
216class DuplicateOptionError(Error):
217 """Raised by strict parsers when an option is repeated in an input source.
218
219 Current implementation raises this exception only when an option is found
220 more than once in a single file, string or dictionary.
221 """
222
223 def __init__(self, section, option, source=None, lineno=None):
224 msg = [repr(option), " in section ", repr(section),
225 " already exists"]
226 if source is not None:
227 message = ["While reading from ", source]
228 if lineno is not None:
229 message.append(" [line {0:2d}]".format(lineno))
230 message.append(": option ")
231 message.extend(msg)
232 msg = message
233 else:
234 msg.insert(0, "Option ")
235 Error.__init__(self, "".join(msg))
236 self.section = section
237 self.option = option
238 self.source = source
239 self.lineno = lineno
240 self.args = (section, option, source, lineno)
Guido van Rossum3d209861997-12-09 16:10:31 +0000241
Georg Brandl96a60ae2010-07-28 13:13:46 +0000242
Guido van Rossum3d209861997-12-09 16:10:31 +0000243class NoOptionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000244 """A requested option was not found."""
245
Guido van Rossum3d209861997-12-09 16:10:31 +0000246 def __init__(self, option, section):
Fred Drakee2c64912002-12-31 17:23:27 +0000247 Error.__init__(self, "No option %r in section: %r" %
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000248 (option, section))
249 self.option = option
250 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000251 self.args = (option, section)
Guido van Rossum3d209861997-12-09 16:10:31 +0000252
Georg Brandl96a60ae2010-07-28 13:13:46 +0000253
Guido van Rossum3d209861997-12-09 16:10:31 +0000254class InterpolationError(Error):
Fred Drakee2c64912002-12-31 17:23:27 +0000255 """Base class for interpolation-related exceptions."""
Fred Drake8d5dd982002-12-30 23:51:45 +0000256
Fred Drakee2c64912002-12-31 17:23:27 +0000257 def __init__(self, option, section, msg):
258 Error.__init__(self, msg)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000259 self.option = option
260 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000261 self.args = (option, section, msg)
Guido van Rossum3d209861997-12-09 16:10:31 +0000262
Georg Brandl96a60ae2010-07-28 13:13:46 +0000263
Fred Drakee2c64912002-12-31 17:23:27 +0000264class InterpolationMissingOptionError(InterpolationError):
265 """A string substitution required a setting which was not available."""
266
267 def __init__(self, option, section, rawval, reference):
268 msg = ("Bad value substitution:\n"
269 "\tsection: [%s]\n"
270 "\toption : %s\n"
271 "\tkey : %s\n"
272 "\trawval : %s\n"
273 % (section, option, reference, rawval))
274 InterpolationError.__init__(self, option, section, msg)
275 self.reference = reference
Michael Foordbd6c0792010-07-25 23:09:25 +0000276 self.args = (option, section, rawval, reference)
Fred Drakee2c64912002-12-31 17:23:27 +0000277
Georg Brandl96a60ae2010-07-28 13:13:46 +0000278
Fred Drakee2c64912002-12-31 17:23:27 +0000279class InterpolationSyntaxError(InterpolationError):
Fred Drakea4923622010-08-09 12:52:45 +0000280 """Raised when the source text contains invalid syntax.
281
282 Current implementation raises this exception only for SafeConfigParser
283 instances when the source text into which substitutions are made
284 does not conform to the required syntax.
285 """
Neal Norwitzce1d9442002-12-30 23:38:47 +0000286
Georg Brandl96a60ae2010-07-28 13:13:46 +0000287
Fred Drakee2c64912002-12-31 17:23:27 +0000288class InterpolationDepthError(InterpolationError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000289 """Raised when substitutions are nested too deeply."""
290
Fred Drake2a37f9f2000-09-27 22:43:54 +0000291 def __init__(self, option, section, rawval):
Fred Drakee2c64912002-12-31 17:23:27 +0000292 msg = ("Value interpolation too deeply recursive:\n"
293 "\tsection: [%s]\n"
294 "\toption : %s\n"
295 "\trawval : %s\n"
296 % (section, option, rawval))
297 InterpolationError.__init__(self, option, section, msg)
Michael Foordbd6c0792010-07-25 23:09:25 +0000298 self.args = (option, section, rawval)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000299
Georg Brandl96a60ae2010-07-28 13:13:46 +0000300
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000301class ParsingError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000302 """Raised when a configuration file does not follow legal syntax."""
303
Fred Drakea4923622010-08-09 12:52:45 +0000304 def __init__(self, source=None, filename=None):
305 # Exactly one of `source'/`filename' arguments has to be given.
306 # `filename' kept for compatibility.
307 if filename and source:
308 raise ValueError("Cannot specify both `filename' and `source'. "
309 "Use `source'.")
310 elif not filename and not source:
311 raise ValueError("Required argument `source' not given.")
312 elif filename:
313 source = filename
314 Error.__init__(self, 'Source contains parsing errors: %s' % source)
315 self.source = source
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000316 self.errors = []
Fred Drakea4923622010-08-09 12:52:45 +0000317 self.args = (source, )
318
319 @property
320 def filename(self):
321 """Deprecated, use `source'."""
322 warnings.warn(
323 "This 'filename' attribute will be removed in future versions. "
324 "Use 'source' instead.",
325 PendingDeprecationWarning, stacklevel=2
326 )
327 return self.source
328
329 @filename.setter
330 def filename(self, value):
331 """Deprecated, user `source'."""
332 warnings.warn(
333 "The 'filename' attribute will be removed in future versions. "
334 "Use 'source' instead.",
335 PendingDeprecationWarning, stacklevel=2
336 )
337 self.source = value
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000338
339 def append(self, lineno, line):
340 self.errors.append((lineno, line))
Fred Drakee2c64912002-12-31 17:23:27 +0000341 self.message += '\n\t[line %2d]: %s' % (lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000342
Georg Brandl96a60ae2010-07-28 13:13:46 +0000343
Fred Drake2a37f9f2000-09-27 22:43:54 +0000344class MissingSectionHeaderError(ParsingError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000345 """Raised when a key-value pair is found before any section header."""
346
Fred Drake2a37f9f2000-09-27 22:43:54 +0000347 def __init__(self, filename, lineno, line):
348 Error.__init__(
349 self,
Walter Dörwald70a6b492004-02-12 17:35:32 +0000350 'File contains no section headers.\nfile: %s, line: %d\n%r' %
Fred Drake2a37f9f2000-09-27 22:43:54 +0000351 (filename, lineno, line))
Fred Drakea4923622010-08-09 12:52:45 +0000352 self.source = filename
Fred Drake2a37f9f2000-09-27 22:43:54 +0000353 self.lineno = lineno
354 self.line = line
Michael Foordbd6c0792010-07-25 23:09:25 +0000355 self.args = (filename, lineno, line)
Guido van Rossum3d209861997-12-09 16:10:31 +0000356
Georg Brandl96a60ae2010-07-28 13:13:46 +0000357
Fred Drakecc645b92010-09-04 04:35:34 +0000358# Used in parsers to denote selecting a backwards-compatible inline comment
359# character behavior (; and # are comments at the start of a line, but ; only
360# inline)
361_COMPATIBLE = object()
362
363# Used in parser getters to indicate the default behaviour when a specific
364# option is not found it to raise an exception. Created to enable `None' as
365# a valid fallback value.
366_UNSET = object()
367
368
Fred Drakefce65572002-10-25 18:08:18 +0000369class RawConfigParser:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000370 """ConfigParser that does not do interpolation."""
371
372 # Regular expressions for parsing section headers and options
373 _SECT_TMPL = r"""
374 \[ # [
375 (?P<header>[^]]+) # very permissive!
376 \] # ]
377 """
378 _OPT_TMPL = r"""
379 (?P<option>.*?) # very permissive!
380 \s*(?P<vi>{delim})\s* # any number of space/tab,
381 # followed by any of the
382 # allowed delimiters,
383 # followed by any space/tab
384 (?P<value>.*)$ # everything up to eol
385 """
386 _OPT_NV_TMPL = r"""
387 (?P<option>.*?) # very permissive!
388 \s*(?: # any number of space/tab,
389 (?P<vi>{delim})\s* # optionally followed by
390 # any of the allowed
391 # delimiters, followed by any
392 # space/tab
393 (?P<value>.*))?$ # everything up to eol
394 """
395
396 # Compiled regular expression for matching sections
397 SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
398 # Compiled regular expression for matching options with typical separators
399 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
400 # Compiled regular expression for matching options with optional values
401 # delimited using typical separators
402 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
403 # Compiled regular expression for matching leading whitespace in a line
404 NONSPACECRE = re.compile(r"\S")
Fred Drakecc645b92010-09-04 04:35:34 +0000405 # Possible boolean values in the configuration.
406 BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
407 '0': False, 'no': False, 'false': False, 'off': False}
Georg Brandl96a60ae2010-07-28 13:13:46 +0000408
Fred Drake03c44a32010-02-19 06:08:41 +0000409 def __init__(self, defaults=None, dict_type=_default_dict,
Fred Drakea4923622010-08-09 12:52:45 +0000410 allow_no_value=False, *, delimiters=('=', ':'),
411 comment_prefixes=_COMPATIBLE, strict=False,
412 empty_lines_in_values=True):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000413 self._dict = dict_type
414 self._sections = self._dict()
415 self._defaults = self._dict()
David Goodger68a1abd2004-10-03 15:40:25 +0000416 if defaults:
417 for key, value in defaults.items():
418 self._defaults[self.optionxform(key)] = value
Georg Brandl96a60ae2010-07-28 13:13:46 +0000419 self._delimiters = tuple(delimiters)
420 if delimiters == ('=', ':'):
421 self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
422 else:
Fred Drakea4923622010-08-09 12:52:45 +0000423 d = "|".join(re.escape(d) for d in delimiters)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000424 if allow_no_value:
Fred Drakea4923622010-08-09 12:52:45 +0000425 self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000426 re.VERBOSE)
427 else:
Fred Drakea4923622010-08-09 12:52:45 +0000428 self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000429 re.VERBOSE)
Fred Drakecc645b92010-09-04 04:35:34 +0000430 if comment_prefixes is _COMPATIBLE:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000431 self._startonly_comment_prefixes = ('#',)
432 self._comment_prefixes = (';',)
433 else:
434 self._startonly_comment_prefixes = ()
435 self._comment_prefixes = tuple(comment_prefixes or ())
Fred Drakea4923622010-08-09 12:52:45 +0000436 self._strict = strict
Georg Brandl96a60ae2010-07-28 13:13:46 +0000437 self._empty_lines_in_values = empty_lines_in_values
Guido van Rossum3d209861997-12-09 16:10:31 +0000438
439 def defaults(self):
Fred Drakefce65572002-10-25 18:08:18 +0000440 return self._defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000441
442 def sections(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000443 """Return a list of section names, excluding [DEFAULT]"""
Fred Drakefce65572002-10-25 18:08:18 +0000444 # self._sections will never have [DEFAULT] in it
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000445 return list(self._sections.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000446
447 def add_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000448 """Create a new section in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000449
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000450 Raise DuplicateSectionError if a section by the specified name
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000451 already exists. Raise ValueError if name is DEFAULT or any of it's
452 case-insensitive variants.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000453 """
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000454 if section.lower() == "default":
455 raise ValueError('Invalid section name: %s' % section)
456
Fred Drakefce65572002-10-25 18:08:18 +0000457 if section in self._sections:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000458 raise DuplicateSectionError(section)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459 self._sections[section] = self._dict()
Guido van Rossum3d209861997-12-09 16:10:31 +0000460
461 def has_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000462 """Indicate whether the named section is present in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000463
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000464 The DEFAULT section is not acknowledged.
465 """
Fred Drakefce65572002-10-25 18:08:18 +0000466 return section in self._sections
Guido van Rossum3d209861997-12-09 16:10:31 +0000467
468 def options(self, section):
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000469 """Return a list of option names for the given section name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000470 try:
Fred Drakefce65572002-10-25 18:08:18 +0000471 opts = self._sections[section].copy()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000472 except KeyError:
473 raise NoSectionError(section)
Fred Drakefce65572002-10-25 18:08:18 +0000474 opts.update(self._defaults)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000475 if '__name__' in opts:
Fred Drake2a37f9f2000-09-27 22:43:54 +0000476 del opts['__name__']
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000477 return list(opts.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000478
Georg Brandl8dcaa732010-07-29 12:17:40 +0000479 def read(self, filenames, encoding=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000480 """Read and parse a filename or a list of filenames.
Tim Peters88869f92001-01-14 23:36:06 +0000481
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000482 Files that cannot be opened are silently ignored; this is
Barry Warsaw25394511999-10-12 16:12:48 +0000483 designed so that you can specify a list of potential
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000484 configuration file locations (e.g. current directory, user's
485 home directory, systemwide directory), and all existing
486 configuration files in the list will be read. A single
487 filename may also be given.
Fred Drake82903142004-05-18 04:24:02 +0000488
489 Return list of successfully read files.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000490 """
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000491 if isinstance(filenames, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000492 filenames = [filenames]
Fred Drake82903142004-05-18 04:24:02 +0000493 read_ok = []
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000494 for filename in filenames:
495 try:
Georg Brandl8dcaa732010-07-29 12:17:40 +0000496 fp = open(filename, encoding=encoding)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000497 except IOError:
498 continue
Fred Drakefce65572002-10-25 18:08:18 +0000499 self._read(fp, filename)
Fred Drake2438a481999-10-04 18:11:56 +0000500 fp.close()
Fred Drake82903142004-05-18 04:24:02 +0000501 read_ok.append(filename)
502 return read_ok
Guido van Rossum3d209861997-12-09 16:10:31 +0000503
Fred Drakea4923622010-08-09 12:52:45 +0000504 def read_file(self, f, source=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000505 """Like read() but the argument must be a file-like object.
506
Fred Drakea4923622010-08-09 12:52:45 +0000507 The `f' argument must have a `readline' method. Optional second
508 argument is the `source' specifying the name of the file being read. If
509 not given, it is taken from f.name. If `f' has no `name' attribute,
510 `<???>' is used.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000511 """
Fred Drakea4923622010-08-09 12:52:45 +0000512 if source is None:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000513 try:
Fred Drakea4923622010-08-09 12:52:45 +0000514 srouce = f.name
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000515 except AttributeError:
Fred Drakea4923622010-08-09 12:52:45 +0000516 source = '<???>'
517 self._read(f, source)
518
519 def read_string(self, string, source='<string>'):
520 """Read configuration from a given string."""
521 sfile = io.StringIO(string)
522 self.read_file(sfile, source)
523
524 def read_dict(self, dictionary, source='<dict>'):
525 """Read configuration from a dictionary.
526
527 Keys are section names, values are dictionaries with keys and values
528 that should be present in the section. If the used dictionary type
529 preserves order, sections and their keys will be added in order.
530
531 Optional second argument is the `source' specifying the name of the
532 dictionary being read.
533 """
534 elements_added = set()
535 for section, keys in dictionary.items():
536 try:
537 self.add_section(section)
538 except DuplicateSectionError:
539 if self._strict and section in elements_added:
540 raise
541 elements_added.add(section)
542 for key, value in keys.items():
543 key = self.optionxform(key)
Fred Drakecc645b92010-09-04 04:35:34 +0000544 if value is not None:
545 value = str(value)
Fred Drakea4923622010-08-09 12:52:45 +0000546 if self._strict and (section, key) in elements_added:
547 raise DuplicateOptionError(section, key, source)
548 elements_added.add((section, key))
549 self.set(section, key, value)
550
551 def readfp(self, fp, filename=None):
552 """Deprecated, use read_file instead."""
553 warnings.warn(
554 "This method will be removed in future versions. "
555 "Use 'parser.read_file()' instead.",
556 PendingDeprecationWarning, stacklevel=2
557 )
558 self.read_file(fp, source=filename)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000559
Fred Drakecc645b92010-09-04 04:35:34 +0000560 def get(self, section, option, vars=None, default=_UNSET):
561 """Get an option value for a given section.
562
563 If `vars' is provided, it must be a dictionary. The option is looked up
564 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
565 If the key is not found and `default' is provided, it is used as
566 a fallback value. `None' can be provided as a `default' value.
567 """
568 try:
569 d = self._unify_values(section, vars)
570 except NoSectionError:
571 if default is _UNSET:
572 raise
Fred Drakefce65572002-10-25 18:08:18 +0000573 else:
Fred Drakecc645b92010-09-04 04:35:34 +0000574 return default
575 option = self.optionxform(option)
576 try:
577 return d[option]
578 except KeyError:
579 if default is _UNSET:
Fred Drakefce65572002-10-25 18:08:18 +0000580 raise NoOptionError(option, section)
Fred Drakecc645b92010-09-04 04:35:34 +0000581 else:
582 return default
Fred Drake2a37f9f2000-09-27 22:43:54 +0000583
Fred Drakefce65572002-10-25 18:08:18 +0000584 def items(self, section):
Fred Drake2ca041f2002-09-27 15:49:56 +0000585 try:
Fred Drakefce65572002-10-25 18:08:18 +0000586 d2 = self._sections[section]
Fred Drake2ca041f2002-09-27 15:49:56 +0000587 except KeyError:
588 if section != DEFAULTSECT:
589 raise NoSectionError(section)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000590 d2 = self._dict()
Fred Drakefce65572002-10-25 18:08:18 +0000591 d = self._defaults.copy()
592 d.update(d2)
Fred Drakedf393bd2002-10-25 20:41:30 +0000593 if "__name__" in d:
594 del d["__name__"]
Fred Drakefce65572002-10-25 18:08:18 +0000595 return d.items()
Fred Drake2ca041f2002-09-27 15:49:56 +0000596
Fred Drakecc645b92010-09-04 04:35:34 +0000597 def _get(self, section, conv, option, *args, **kwargs):
598 return conv(self.get(section, option, *args, **kwargs))
Guido van Rossum3d209861997-12-09 16:10:31 +0000599
Fred Drakecc645b92010-09-04 04:35:34 +0000600 def getint(self, section, option, vars=None, default=_UNSET):
601 try:
602 return self._get(section, int, option, vars)
603 except (NoSectionError, NoOptionError):
604 if default is _UNSET:
605 raise
606 else:
607 return default
Guido van Rossum3d209861997-12-09 16:10:31 +0000608
Fred Drakecc645b92010-09-04 04:35:34 +0000609 def getfloat(self, section, option, vars=None, default=_UNSET):
610 try:
611 return self._get(section, float, option, vars)
612 except (NoSectionError, NoOptionError):
613 if default is _UNSET:
614 raise
615 else:
616 return default
Guido van Rossum3d209861997-12-09 16:10:31 +0000617
Fred Drakecc645b92010-09-04 04:35:34 +0000618 def getboolean(self, section, option, vars=None, default=_UNSET):
619 try:
620 return self._get(section, self._convert_to_boolean, option, vars)
621 except (NoSectionError, NoOptionError):
622 if default is _UNSET:
623 raise
624 else:
625 return default
Guido van Rossum3d209861997-12-09 16:10:31 +0000626
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000627 def optionxform(self, optionstr):
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000628 return optionstr.lower()
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000629
Eric S. Raymond417c4892000-07-10 18:11:00 +0000630 def has_option(self, section, option):
631 """Check for the existence of a given option in a given section."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000632 if not section or section == DEFAULTSECT:
633 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000634 return option in self._defaults
635 elif section not in self._sections:
Neal Norwitzf680cc42002-12-17 01:56:47 +0000636 return False
Eric S. Raymond417c4892000-07-10 18:11:00 +0000637 else:
Fred Drake3c823aa2001-02-26 21:55:34 +0000638 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000639 return (option in self._sections[section]
640 or option in self._defaults)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000641
Fred Drake03c44a32010-02-19 06:08:41 +0000642 def set(self, section, option, value=None):
Eric S. Raymond417c4892000-07-10 18:11:00 +0000643 """Set an option."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000644 if not section or section == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000645 sectdict = self._defaults
Eric S. Raymond417c4892000-07-10 18:11:00 +0000646 else:
647 try:
Fred Drakefce65572002-10-25 18:08:18 +0000648 sectdict = self._sections[section]
Eric S. Raymond417c4892000-07-10 18:11:00 +0000649 except KeyError:
650 raise NoSectionError(section)
Fred Drakec2ff9052002-09-27 15:33:11 +0000651 sectdict[self.optionxform(option)] = value
Eric S. Raymond417c4892000-07-10 18:11:00 +0000652
Georg Brandl96a60ae2010-07-28 13:13:46 +0000653 def write(self, fp, space_around_delimiters=True):
654 """Write an .ini-format representation of the configuration state.
655
656 If `space_around_delimiters' is True (the default), delimiters
657 between keys and values are surrounded by spaces.
658 """
659 if space_around_delimiters:
660 d = " {} ".format(self._delimiters[0])
661 else:
662 d = self._delimiters[0]
Fred Drakefce65572002-10-25 18:08:18 +0000663 if self._defaults:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000664 self._write_section(fp, DEFAULTSECT, self._defaults.items(), d)
Fred Drakefce65572002-10-25 18:08:18 +0000665 for section in self._sections:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000666 self._write_section(fp, section,
667 self._sections[section].items(), d)
668
669 def _write_section(self, fp, section_name, section_items, delimiter):
670 """Write a single section to the specified `fp'."""
671 fp.write("[{}]\n".format(section_name))
672 for key, value in section_items:
673 if key == "__name__":
674 continue
Fred Drake88444412010-09-03 04:22:36 +0000675 if (value is not None) or (self._optcre == self.OPTCRE):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000676 value = delimiter + str(value).replace('\n', '\n\t')
677 else:
678 value = ""
679 fp.write("{}{}\n".format(key, value))
680 fp.write("\n")
Eric S. Raymond417c4892000-07-10 18:11:00 +0000681
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000682 def remove_option(self, section, option):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000683 """Remove an option."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000684 if not section or section == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000685 sectdict = self._defaults
Eric S. Raymond649685a2000-07-14 14:28:22 +0000686 else:
687 try:
Fred Drakefce65572002-10-25 18:08:18 +0000688 sectdict = self._sections[section]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000689 except KeyError:
690 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000691 option = self.optionxform(option)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000692 existed = option in sectdict
Eric S. Raymond649685a2000-07-14 14:28:22 +0000693 if existed:
Fred Drakeff4a23b2000-12-04 16:29:13 +0000694 del sectdict[option]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000695 return existed
696
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000697 def remove_section(self, section):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000698 """Remove a file section."""
Fred Drakefce65572002-10-25 18:08:18 +0000699 existed = section in self._sections
Fred Drakec2ff9052002-09-27 15:33:11 +0000700 if existed:
Fred Drakefce65572002-10-25 18:08:18 +0000701 del self._sections[section]
Fred Drakec2ff9052002-09-27 15:33:11 +0000702 return existed
Eric S. Raymond649685a2000-07-14 14:28:22 +0000703
Fred Drakefce65572002-10-25 18:08:18 +0000704 def _read(self, fp, fpname):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000705 """Parse a sectioned configuration file.
Guido van Rossum3d209861997-12-09 16:10:31 +0000706
Fred Drakea4923622010-08-09 12:52:45 +0000707 Each section in a configuration file contains a header, indicated by
708 a name in square brackets (`[]'), plus key/value options, indicated by
Georg Brandl96a60ae2010-07-28 13:13:46 +0000709 `name' and `value' delimited with a specific substring (`=' or `:' by
710 default).
711
Fred Drakea4923622010-08-09 12:52:45 +0000712 Values can span multiple lines, as long as they are indented deeper
713 than the first line of the value. Depending on the parser's mode, blank
714 lines may be treated as parts of multiline values or ignored.
Georg Brandl96a60ae2010-07-28 13:13:46 +0000715
716 Configuration files may include comments, prefixed by specific
Fred Drakea4923622010-08-09 12:52:45 +0000717 characters (`#' and `;' by default). Comments may appear on their own
718 in an otherwise empty line or may be entered in lines holding values or
Georg Brandl96a60ae2010-07-28 13:13:46 +0000719 section names.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000720 """
Fred Drakea4923622010-08-09 12:52:45 +0000721 elements_added = set()
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000722 cursect = None # None, or a dictionary
Fred Drakea4923622010-08-09 12:52:45 +0000723 sectname = None
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000724 optname = None
725 lineno = 0
Georg Brandl96a60ae2010-07-28 13:13:46 +0000726 indent_level = 0
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000727 e = None # None, or an exception
Georg Brandl96a60ae2010-07-28 13:13:46 +0000728 for lineno, line in enumerate(fp, start=1):
Georg Brandlf206d0e2010-07-29 11:56:20 +0000729 # strip full line comments
Georg Brandl96a60ae2010-07-28 13:13:46 +0000730 comment_start = None
731 for prefix in self._startonly_comment_prefixes:
732 if line.strip().startswith(prefix):
733 comment_start = 0
734 break
735 # strip inline comments
736 for prefix in self._comment_prefixes:
737 index = line.find(prefix)
738 if index == 0 or (index > 0 and line[index-1].isspace()):
739 comment_start = index
740 break
741 value = line[:comment_start].strip()
742 if not value:
Georg Brandlf206d0e2010-07-29 11:56:20 +0000743 if self._empty_lines_in_values:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000744 # add empty line to the value, but only if there was no
745 # comment on the line
Georg Brandlf206d0e2010-07-29 11:56:20 +0000746 if (comment_start is None and
747 cursect is not None and
748 optname and
749 cursect[optname] is not None):
750 cursect[optname].append('') # newlines added at join
Georg Brandl96a60ae2010-07-28 13:13:46 +0000751 else:
752 # empty line marks end of value
753 indent_level = sys.maxsize
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000754 continue
755 # continuation line?
Georg Brandl96a60ae2010-07-28 13:13:46 +0000756 first_nonspace = self.NONSPACECRE.search(line)
757 cur_indent_level = first_nonspace.start() if first_nonspace else 0
758 if (cursect is not None and optname and
759 cur_indent_level > indent_level):
760 cursect[optname].append(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000761 # a section header or option header?
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000762 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000763 indent_level = cur_indent_level
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000764 # is it a section header?
Georg Brandl96a60ae2010-07-28 13:13:46 +0000765 mo = self.SECTCRE.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000766 if mo:
767 sectname = mo.group('header')
Fred Drakefce65572002-10-25 18:08:18 +0000768 if sectname in self._sections:
Fred Drakea4923622010-08-09 12:52:45 +0000769 if self._strict and sectname in elements_added:
770 raise DuplicateSectionError(sectname, fpname,
771 lineno)
Fred Drakefce65572002-10-25 18:08:18 +0000772 cursect = self._sections[sectname]
Fred Drakea4923622010-08-09 12:52:45 +0000773 elements_added.add(sectname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000774 elif sectname == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000775 cursect = self._defaults
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000776 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000777 cursect = self._dict()
778 cursect['__name__'] = sectname
Fred Drakefce65572002-10-25 18:08:18 +0000779 self._sections[sectname] = cursect
Fred Drakea4923622010-08-09 12:52:45 +0000780 elements_added.add(sectname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000781 # So sections can't start with a continuation line
782 optname = None
783 # no section header in the file?
784 elif cursect is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000785 raise MissingSectionHeaderError(fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000786 # an option line?
787 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000788 mo = self._optcre.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000789 if mo:
Fred Drakec517b9b2000-02-28 20:59:03 +0000790 optname, vi, optval = mo.group('option', 'vi', 'value')
Georg Brandl96a60ae2010-07-28 13:13:46 +0000791 if not optname:
792 e = self._handle_error(e, fpname, lineno, line)
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000793 optname = self.optionxform(optname.rstrip())
Fred Drakea4923622010-08-09 12:52:45 +0000794 if (self._strict and
795 (sectname, optname) in elements_added):
796 raise DuplicateOptionError(sectname, optname,
797 fpname, lineno)
798 elements_added.add((sectname, optname))
Fred Drake03c44a32010-02-19 06:08:41 +0000799 # This check is fine because the OPTCRE cannot
800 # match if it would set optval to None
801 if optval is not None:
Fred Drake03c44a32010-02-19 06:08:41 +0000802 optval = optval.strip()
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000803 # allow empty values
804 if optval == '""':
805 optval = ''
806 cursect[optname] = [optval]
807 else:
808 # valueless option handling
809 cursect[optname] = optval
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000810 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000811 # a non-fatal parsing error occurred. set up the
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000812 # exception but keep going. the exception will be
813 # raised at the end of the file and will contain a
814 # list of all bogus lines
Georg Brandl96a60ae2010-07-28 13:13:46 +0000815 e = self._handle_error(e, fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000816 # if any parsing errors occurred, raise an exception
817 if e:
818 raise e
Georg Brandl96a60ae2010-07-28 13:13:46 +0000819 self._join_multiline_values()
Fred Drakefce65572002-10-25 18:08:18 +0000820
Georg Brandl96a60ae2010-07-28 13:13:46 +0000821 def _join_multiline_values(self):
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000822 all_sections = [self._defaults]
823 all_sections.extend(self._sections.values())
824 for options in all_sections:
825 for name, val in options.items():
826 if isinstance(val, list):
Georg Brandlf206d0e2010-07-29 11:56:20 +0000827 options[name] = '\n'.join(val).rstrip()
Fred Drakefce65572002-10-25 18:08:18 +0000828
Georg Brandl96a60ae2010-07-28 13:13:46 +0000829 def _handle_error(self, exc, fpname, lineno, line):
830 if not exc:
831 exc = ParsingError(fpname)
832 exc.append(lineno, repr(line))
833 return exc
834
Fred Drakecc645b92010-09-04 04:35:34 +0000835 def _unify_values(self, section, vars):
836 """Create a copy of the DEFAULTSECT with values from a specific
837 `section' and the `vars' dictionary. If provided, values in `vars'
838 take precendence.
Fred Drakefce65572002-10-25 18:08:18 +0000839 """
840 d = self._defaults.copy()
841 try:
842 d.update(self._sections[section])
843 except KeyError:
844 if section != DEFAULTSECT:
845 raise NoSectionError(section)
846 # Update with the entry specific variables
David Goodger68a1abd2004-10-03 15:40:25 +0000847 if vars:
848 for key, value in vars.items():
Fred Drakecc645b92010-09-04 04:35:34 +0000849 if value is not None:
850 value = str(value)
David Goodger68a1abd2004-10-03 15:40:25 +0000851 d[self.optionxform(key)] = value
Fred Drakecc645b92010-09-04 04:35:34 +0000852 return d
853
854 def _convert_to_boolean(self, value):
855 """Return a boolean value translating from other types if necessary.
856 """
857 if value.lower() not in self.BOOLEAN_STATES:
858 raise ValueError('Not a boolean: %s' % value)
859 return self.BOOLEAN_STATES[value.lower()]
860
861
862class ConfigParser(RawConfigParser):
863 """ConfigParser implementing interpolation."""
864
865 def get(self, section, option, raw=False, vars=None, default=_UNSET):
866 """Get an option value for a given section.
867
868 If `vars' is provided, it must be a dictionary. The option is looked up
869 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
870 If the key is not found and `default' is provided, it is used as
871 a fallback value. `None' can be provided as a `default' value.
872
873 All % interpolations are expanded in the return values, unless the
874 optional argument `raw' is true. Values for interpolation keys are
875 looked up in the same manner as the option.
876
877 The section DEFAULT is special.
878 """
879 try:
880 d = self._unify_values(section, vars)
881 except NoSectionError:
882 if default is _UNSET:
883 raise
884 else:
885 return default
Fred Drakefce65572002-10-25 18:08:18 +0000886 option = self.optionxform(option)
887 try:
888 value = d[option]
889 except KeyError:
Fred Drakecc645b92010-09-04 04:35:34 +0000890 if default is _UNSET:
891 raise NoOptionError(option, section)
892 else:
893 return default
Fred Drakefce65572002-10-25 18:08:18 +0000894
Fred Drake03c44a32010-02-19 06:08:41 +0000895 if raw or value is None:
Fred Drakefce65572002-10-25 18:08:18 +0000896 return value
897 else:
898 return self._interpolate(section, option, value, d)
899
Fred Drakecc645b92010-09-04 04:35:34 +0000900 def getint(self, section, option, raw=False, vars=None, default=_UNSET):
901 try:
902 return self._get(section, int, option, raw, vars)
903 except (NoSectionError, NoOptionError):
904 if default is _UNSET:
905 raise
906 else:
907 return default
908
909 def getfloat(self, section, option, raw=False, vars=None, default=_UNSET):
910 try:
911 return self._get(section, float, option, raw, vars)
912 except (NoSectionError, NoOptionError):
913 if default is _UNSET:
914 raise
915 else:
916 return default
917
918 def getboolean(self, section, option, raw=False, vars=None,
919 default=_UNSET):
920 try:
921 return self._get(section, self._convert_to_boolean, option, raw,
922 vars)
923 except (NoSectionError, NoOptionError):
924 if default is _UNSET:
925 raise
926 else:
927 return default
928
Neal Norwitzf680cc42002-12-17 01:56:47 +0000929 def items(self, section, raw=False, vars=None):
Fred Drakea4923622010-08-09 12:52:45 +0000930 """Return a list of (name, value) tuples for each option in a section.
Fred Drakefce65572002-10-25 18:08:18 +0000931
932 All % interpolations are expanded in the return values, based on the
933 defaults passed into the constructor, unless the optional argument
934 `raw' is true. Additional substitutions may be provided using the
935 `vars' argument, which must be a dictionary whose contents overrides
936 any pre-existing defaults.
937
938 The section DEFAULT is special.
939 """
940 d = self._defaults.copy()
941 try:
942 d.update(self._sections[section])
943 except KeyError:
944 if section != DEFAULTSECT:
945 raise NoSectionError(section)
946 # Update with the entry specific variables
947 if vars:
David Goodger68a1abd2004-10-03 15:40:25 +0000948 for key, value in vars.items():
949 d[self.optionxform(key)] = value
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000950 options = list(d.keys())
Fred Drakedf393bd2002-10-25 20:41:30 +0000951 if "__name__" in options:
952 options.remove("__name__")
Fred Drakefce65572002-10-25 18:08:18 +0000953 if raw:
Fred Drake8c4da532003-10-21 16:45:00 +0000954 return [(option, d[option])
955 for option in options]
Fred Drakefce65572002-10-25 18:08:18 +0000956 else:
Fred Drake8c4da532003-10-21 16:45:00 +0000957 return [(option, self._interpolate(section, option, d[option], d))
958 for option in options]
Fred Drakefce65572002-10-25 18:08:18 +0000959
960 def _interpolate(self, section, option, rawval, vars):
961 # do the string interpolation
962 value = rawval
Tim Peters230a60c2002-11-09 05:08:07 +0000963 depth = MAX_INTERPOLATION_DEPTH
Fred Drakefce65572002-10-25 18:08:18 +0000964 while depth: # Loop through this until it's done
965 depth -= 1
Fred Drake03c44a32010-02-19 06:08:41 +0000966 if value and "%(" in value:
Fred Drakebc12b012004-05-18 02:25:51 +0000967 value = self._KEYCRE.sub(self._interpolation_replace, value)
Fred Drakefce65572002-10-25 18:08:18 +0000968 try:
969 value = value % vars
Guido van Rossumb940e112007-01-10 16:19:56 +0000970 except KeyError as e:
Fred Drakee2c64912002-12-31 17:23:27 +0000971 raise InterpolationMissingOptionError(
Brett Cannonca477b22007-03-21 22:26:20 +0000972 option, section, rawval, e.args[0])
Fred Drakefce65572002-10-25 18:08:18 +0000973 else:
974 break
Fred Drake03c44a32010-02-19 06:08:41 +0000975 if value and "%(" in value:
Fred Drakefce65572002-10-25 18:08:18 +0000976 raise InterpolationDepthError(option, section, rawval)
977 return value
Fred Drake0eebd5c2002-10-25 21:52:00 +0000978
Fred Drakebc12b012004-05-18 02:25:51 +0000979 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
980
981 def _interpolation_replace(self, match):
982 s = match.group(1)
983 if s is None:
984 return match.group()
985 else:
986 return "%%(%s)s" % self.optionxform(s)
987
Fred Drake0eebd5c2002-10-25 21:52:00 +0000988
989class SafeConfigParser(ConfigParser):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000990 """ConfigParser implementing sane interpolation."""
Fred Drake0eebd5c2002-10-25 21:52:00 +0000991
992 def _interpolate(self, section, option, rawval, vars):
993 # do the string interpolation
994 L = []
995 self._interpolate_some(option, L, rawval, section, vars, 1)
996 return ''.join(L)
997
Guido van Rossumd8faa362007-04-27 19:54:29 +0000998 _interpvar_re = re.compile(r"%\(([^)]+)\)s")
Fred Drake0eebd5c2002-10-25 21:52:00 +0000999
1000 def _interpolate_some(self, option, accum, rest, section, map, depth):
1001 if depth > MAX_INTERPOLATION_DEPTH:
1002 raise InterpolationDepthError(option, section, rest)
1003 while rest:
1004 p = rest.find("%")
1005 if p < 0:
1006 accum.append(rest)
1007 return
1008 if p > 0:
1009 accum.append(rest[:p])
1010 rest = rest[p:]
1011 # p is no longer used
1012 c = rest[1:2]
1013 if c == "%":
1014 accum.append("%")
1015 rest = rest[2:]
1016 elif c == "(":
Guido van Rossumd8faa362007-04-27 19:54:29 +00001017 m = self._interpvar_re.match(rest)
Fred Drake0eebd5c2002-10-25 21:52:00 +00001018 if m is None:
Neal Norwitz10f30182003-06-29 04:23:35 +00001019 raise InterpolationSyntaxError(option, section,
1020 "bad interpolation variable reference %r" % rest)
Fred Drakebc12b012004-05-18 02:25:51 +00001021 var = self.optionxform(m.group(1))
Fred Drake0eebd5c2002-10-25 21:52:00 +00001022 rest = rest[m.end():]
1023 try:
1024 v = map[var]
1025 except KeyError:
Fred Drakee2c64912002-12-31 17:23:27 +00001026 raise InterpolationMissingOptionError(
1027 option, section, rest, var)
Fred Drake0eebd5c2002-10-25 21:52:00 +00001028 if "%" in v:
1029 self._interpolate_some(option, accum, v,
1030 section, map, depth + 1)
1031 else:
1032 accum.append(v)
1033 else:
1034 raise InterpolationSyntaxError(
Neal Norwitz10f30182003-06-29 04:23:35 +00001035 option, section,
Fred Drakea4923622010-08-09 12:52:45 +00001036 "'%%' must be followed by '%%' or '(', "
1037 "found: %r" % (rest,))
David Goodger1cbf2062004-10-03 15:55:09 +00001038
Fred Drake03c44a32010-02-19 06:08:41 +00001039 def set(self, section, option, value=None):
David Goodger1cbf2062004-10-03 15:55:09 +00001040 """Set an option. Extend ConfigParser.set: check for string values."""
Fred Drake03c44a32010-02-19 06:08:41 +00001041 # The only legal non-string value if we allow valueless
1042 # options is None, so we need to check if the value is a
1043 # string if:
1044 # - we do not allow valueless options, or
1045 # - we allow valueless options but the value is not None
1046 if self._optcre is self.OPTCRE or value:
1047 if not isinstance(value, str):
1048 raise TypeError("option values must be strings")
Fred Drakea4923622010-08-09 12:52:45 +00001049 # check for bad percent signs
1050 if value:
1051 tmp_value = value.replace('%%', '') # escaped percent signs
1052 tmp_value = self._interpvar_re.sub('', tmp_value) # valid syntax
1053 if '%' in tmp_value:
1054 raise ValueError("invalid interpolation syntax in %r at "
1055 "position %d" % (value, tmp_value.find('%')))
David Goodger1cbf2062004-10-03 15:55:09 +00001056 ConfigParser.set(self, section, option, value)