blob: eb29b02b19721a5d90c64c3e191c39ecf950c89c [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
Georg Brandl96a60ae2010-07-28 13:13:46 +000027 __init__(defaults=None, dict_type=_default_dict,
28 delimiters=('=', ':'), comment_prefixes=('#', ';'),
Fred Drakea4923622010-08-09 12:52:45 +000029 strict=False, empty_lines_in_values=True, allow_no_value=False):
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
85 and their keys will be added in order.
Guido van Rossum3d209861997-12-09 16:10:31 +000086
Neal Norwitzf680cc42002-12-17 01:56:47 +000087 get(section, option, raw=False, vars=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +000088 Return a string value for the named option. All % interpolations are
Barry Warsawf09f6a51999-01-26 22:01:37 +000089 expanded in the return values, based on the defaults passed into the
90 constructor and the DEFAULT section. Additional substitutions may be
91 provided using the `vars' argument, which must be a dictionary whose
92 contents override any pre-existing defaults.
Guido van Rossum3d209861997-12-09 16:10:31 +000093
Barry Warsawf09f6a51999-01-26 22:01:37 +000094 getint(section, options)
Georg Brandl96a60ae2010-07-28 13:13:46 +000095 Like get(), but convert value to an integer.
Guido van Rossum3d209861997-12-09 16:10:31 +000096
Barry Warsawf09f6a51999-01-26 22:01:37 +000097 getfloat(section, options)
Georg Brandl96a60ae2010-07-28 13:13:46 +000098 Like get(), but convert value to a float.
Guido van Rossum3d209861997-12-09 16:10:31 +000099
Barry Warsawf09f6a51999-01-26 22:01:37 +0000100 getboolean(section, options)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000101 Like get(), but convert value to a boolean (currently case
Neal Norwitzf680cc42002-12-17 01:56:47 +0000102 insensitively defined as 0, false, no, off for False, and 1, true,
103 yes, on for True). Returns False or True.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000104
Neal Norwitzf680cc42002-12-17 01:56:47 +0000105 items(section, raw=False, vars=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000106 Return a list of tuples with (name, value) for each option
Fred Drake2ca041f2002-09-27 15:49:56 +0000107 in the section.
108
Eric S. Raymond649685a2000-07-14 14:28:22 +0000109 remove_section(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000110 Remove the given file section and all its options.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000111
112 remove_option(section, option)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000113 Remove the given option from the given section.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000114
115 set(section, option, value)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000116 Set the given option.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000117
Georg Brandl96a60ae2010-07-28 13:13:46 +0000118 write(fp, space_around_delimiters=True)
119 Write the configuration state in .ini format. If
120 `space_around_delimiters' is True (the default), delimiters
121 between keys and values are surrounded by spaces.
Guido van Rossum3d209861997-12-09 16:10:31 +0000122"""
123
Raymond Hettingerff23e8c2009-03-03 01:32:48 +0000124try:
125 from collections import OrderedDict as _default_dict
126except ImportError:
127 # fallback for setup.py which hasn't yet built _collections
128 _default_dict = dict
129
Fred Drakea4923622010-08-09 12:52:45 +0000130import io
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000131import re
Georg Brandl96a60ae2010-07-28 13:13:46 +0000132import sys
Fred Drakea4923622010-08-09 12:52:45 +0000133import warnings
Guido van Rossum3d209861997-12-09 16:10:31 +0000134
Fred Drakea4923622010-08-09 12:52:45 +0000135__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
136 "NoOptionError", "InterpolationError", "InterpolationDepthError",
Fred Drake8d5dd982002-12-30 23:51:45 +0000137 "InterpolationSyntaxError", "ParsingError",
David Goodger1cbf2062004-10-03 15:55:09 +0000138 "MissingSectionHeaderError",
139 "ConfigParser", "SafeConfigParser", "RawConfigParser",
Fred Drakec2ff9052002-09-27 15:33:11 +0000140 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +0000141
Guido van Rossum3d209861997-12-09 16:10:31 +0000142DEFAULTSECT = "DEFAULT"
143
Fred Drake2a37f9f2000-09-27 22:43:54 +0000144MAX_INTERPOLATION_DEPTH = 10
145
Guido van Rossum3d209861997-12-09 16:10:31 +0000146
Tim Peters88869f92001-01-14 23:36:06 +0000147
Guido van Rossum3d209861997-12-09 16:10:31 +0000148# exception classes
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000149class Error(Exception):
Fred Drake8d5dd982002-12-30 23:51:45 +0000150 """Base class for ConfigParser exceptions."""
151
Guido van Rossum360e4b82007-05-14 22:51:27 +0000152 def _get_message(self):
153 """Getter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000154 BaseException.
155 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000156 return self.__message
157
158 def _set_message(self, value):
159 """Setter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000160 BaseException.
161 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000162 self.__message = value
163
164 # BaseException.message has been deprecated since Python 2.6. To prevent
Fred Drakea4923622010-08-09 12:52:45 +0000165 # DeprecationWarning from popping up over this pre-existing attribute, use
166 # a new property that takes lookup precedence.
Guido van Rossum360e4b82007-05-14 22:51:27 +0000167 message = property(_get_message, _set_message)
168
Guido van Rossum3d209861997-12-09 16:10:31 +0000169 def __init__(self, msg=''):
Fred Drakee2c64912002-12-31 17:23:27 +0000170 self.message = msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000171 Exception.__init__(self, msg)
Fred Drake8d5dd982002-12-30 23:51:45 +0000172
Guido van Rossum3d209861997-12-09 16:10:31 +0000173 def __repr__(self):
Fred Drakee2c64912002-12-31 17:23:27 +0000174 return self.message
Fred Drake8d5dd982002-12-30 23:51:45 +0000175
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000176 __str__ = __repr__
Guido van Rossum3d209861997-12-09 16:10:31 +0000177
Georg Brandl96a60ae2010-07-28 13:13:46 +0000178
Guido van Rossum3d209861997-12-09 16:10:31 +0000179class NoSectionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000180 """Raised when no section matches a requested option."""
181
Guido van Rossum3d209861997-12-09 16:10:31 +0000182 def __init__(self, section):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000183 Error.__init__(self, 'No section: %r' % (section,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000184 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000185 self.args = (section, )
Guido van Rossum3d209861997-12-09 16:10:31 +0000186
Georg Brandl96a60ae2010-07-28 13:13:46 +0000187
Guido van Rossum3d209861997-12-09 16:10:31 +0000188class DuplicateSectionError(Error):
Fred Drakea4923622010-08-09 12:52:45 +0000189 """Raised when a section is repeated in an input source.
Fred Drake8d5dd982002-12-30 23:51:45 +0000190
Fred Drakea4923622010-08-09 12:52:45 +0000191 Possible repetitions that raise this exception are: multiple creation
192 using the API or in strict parsers when a section is found more than once
193 in a single input file, string or dictionary.
194 """
195
196 def __init__(self, section, source=None, lineno=None):
197 msg = [repr(section), " already exists"]
198 if source is not None:
199 message = ["While reading from ", source]
200 if lineno is not None:
201 message.append(" [line {0:2d}]".format(lineno))
202 message.append(": section ")
203 message.extend(msg)
204 msg = message
205 else:
206 msg.insert(0, "Section ")
207 Error.__init__(self, "".join(msg))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000208 self.section = section
Fred Drakea4923622010-08-09 12:52:45 +0000209 self.source = source
210 self.lineno = lineno
211 self.args = (section, source, lineno)
212
213
214class DuplicateOptionError(Error):
215 """Raised by strict parsers when an option is repeated in an input source.
216
217 Current implementation raises this exception only when an option is found
218 more than once in a single file, string or dictionary.
219 """
220
221 def __init__(self, section, option, source=None, lineno=None):
222 msg = [repr(option), " in section ", repr(section),
223 " already exists"]
224 if source is not None:
225 message = ["While reading from ", source]
226 if lineno is not None:
227 message.append(" [line {0:2d}]".format(lineno))
228 message.append(": option ")
229 message.extend(msg)
230 msg = message
231 else:
232 msg.insert(0, "Option ")
233 Error.__init__(self, "".join(msg))
234 self.section = section
235 self.option = option
236 self.source = source
237 self.lineno = lineno
238 self.args = (section, option, source, lineno)
Guido van Rossum3d209861997-12-09 16:10:31 +0000239
Georg Brandl96a60ae2010-07-28 13:13:46 +0000240
Guido van Rossum3d209861997-12-09 16:10:31 +0000241class NoOptionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000242 """A requested option was not found."""
243
Guido van Rossum3d209861997-12-09 16:10:31 +0000244 def __init__(self, option, section):
Fred Drakee2c64912002-12-31 17:23:27 +0000245 Error.__init__(self, "No option %r in section: %r" %
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000246 (option, section))
247 self.option = option
248 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000249 self.args = (option, section)
Guido van Rossum3d209861997-12-09 16:10:31 +0000250
Georg Brandl96a60ae2010-07-28 13:13:46 +0000251
Guido van Rossum3d209861997-12-09 16:10:31 +0000252class InterpolationError(Error):
Fred Drakee2c64912002-12-31 17:23:27 +0000253 """Base class for interpolation-related exceptions."""
Fred Drake8d5dd982002-12-30 23:51:45 +0000254
Fred Drakee2c64912002-12-31 17:23:27 +0000255 def __init__(self, option, section, msg):
256 Error.__init__(self, msg)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000257 self.option = option
258 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000259 self.args = (option, section, msg)
Guido van Rossum3d209861997-12-09 16:10:31 +0000260
Georg Brandl96a60ae2010-07-28 13:13:46 +0000261
Fred Drakee2c64912002-12-31 17:23:27 +0000262class InterpolationMissingOptionError(InterpolationError):
263 """A string substitution required a setting which was not available."""
264
265 def __init__(self, option, section, rawval, reference):
266 msg = ("Bad value substitution:\n"
267 "\tsection: [%s]\n"
268 "\toption : %s\n"
269 "\tkey : %s\n"
270 "\trawval : %s\n"
271 % (section, option, reference, rawval))
272 InterpolationError.__init__(self, option, section, msg)
273 self.reference = reference
Michael Foordbd6c0792010-07-25 23:09:25 +0000274 self.args = (option, section, rawval, reference)
Fred Drakee2c64912002-12-31 17:23:27 +0000275
Georg Brandl96a60ae2010-07-28 13:13:46 +0000276
Fred Drakee2c64912002-12-31 17:23:27 +0000277class InterpolationSyntaxError(InterpolationError):
Fred Drakea4923622010-08-09 12:52:45 +0000278 """Raised when the source text contains invalid syntax.
279
280 Current implementation raises this exception only for SafeConfigParser
281 instances when the source text into which substitutions are made
282 does not conform to the required syntax.
283 """
Neal Norwitzce1d9442002-12-30 23:38:47 +0000284
Georg Brandl96a60ae2010-07-28 13:13:46 +0000285
Fred Drakee2c64912002-12-31 17:23:27 +0000286class InterpolationDepthError(InterpolationError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000287 """Raised when substitutions are nested too deeply."""
288
Fred Drake2a37f9f2000-09-27 22:43:54 +0000289 def __init__(self, option, section, rawval):
Fred Drakee2c64912002-12-31 17:23:27 +0000290 msg = ("Value interpolation too deeply recursive:\n"
291 "\tsection: [%s]\n"
292 "\toption : %s\n"
293 "\trawval : %s\n"
294 % (section, option, rawval))
295 InterpolationError.__init__(self, option, section, msg)
Michael Foordbd6c0792010-07-25 23:09:25 +0000296 self.args = (option, section, rawval)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000297
Georg Brandl96a60ae2010-07-28 13:13:46 +0000298
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000299class ParsingError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000300 """Raised when a configuration file does not follow legal syntax."""
301
Fred Drakea4923622010-08-09 12:52:45 +0000302 def __init__(self, source=None, filename=None):
303 # Exactly one of `source'/`filename' arguments has to be given.
304 # `filename' kept for compatibility.
305 if filename and source:
306 raise ValueError("Cannot specify both `filename' and `source'. "
307 "Use `source'.")
308 elif not filename and not source:
309 raise ValueError("Required argument `source' not given.")
310 elif filename:
311 source = filename
312 Error.__init__(self, 'Source contains parsing errors: %s' % source)
313 self.source = source
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000314 self.errors = []
Fred Drakea4923622010-08-09 12:52:45 +0000315 self.args = (source, )
316
317 @property
318 def filename(self):
319 """Deprecated, use `source'."""
320 warnings.warn(
321 "This 'filename' attribute will be removed in future versions. "
322 "Use 'source' instead.",
323 PendingDeprecationWarning, stacklevel=2
324 )
325 return self.source
326
327 @filename.setter
328 def filename(self, value):
329 """Deprecated, user `source'."""
330 warnings.warn(
331 "The 'filename' attribute will be removed in future versions. "
332 "Use 'source' instead.",
333 PendingDeprecationWarning, stacklevel=2
334 )
335 self.source = value
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000336
337 def append(self, lineno, line):
338 self.errors.append((lineno, line))
Fred Drakee2c64912002-12-31 17:23:27 +0000339 self.message += '\n\t[line %2d]: %s' % (lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000340
Georg Brandl96a60ae2010-07-28 13:13:46 +0000341
Fred Drake2a37f9f2000-09-27 22:43:54 +0000342class MissingSectionHeaderError(ParsingError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000343 """Raised when a key-value pair is found before any section header."""
344
Fred Drake2a37f9f2000-09-27 22:43:54 +0000345 def __init__(self, filename, lineno, line):
346 Error.__init__(
347 self,
Walter Dörwald70a6b492004-02-12 17:35:32 +0000348 'File contains no section headers.\nfile: %s, line: %d\n%r' %
Fred Drake2a37f9f2000-09-27 22:43:54 +0000349 (filename, lineno, line))
Fred Drakea4923622010-08-09 12:52:45 +0000350 self.source = filename
Fred Drake2a37f9f2000-09-27 22:43:54 +0000351 self.lineno = lineno
352 self.line = line
Michael Foordbd6c0792010-07-25 23:09:25 +0000353 self.args = (filename, lineno, line)
Guido van Rossum3d209861997-12-09 16:10:31 +0000354
Georg Brandl96a60ae2010-07-28 13:13:46 +0000355
Fred Drakefce65572002-10-25 18:08:18 +0000356class RawConfigParser:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000357 """ConfigParser that does not do interpolation."""
358
359 # Regular expressions for parsing section headers and options
360 _SECT_TMPL = r"""
361 \[ # [
362 (?P<header>[^]]+) # very permissive!
363 \] # ]
364 """
365 _OPT_TMPL = r"""
366 (?P<option>.*?) # very permissive!
367 \s*(?P<vi>{delim})\s* # any number of space/tab,
368 # followed by any of the
369 # allowed delimiters,
370 # followed by any space/tab
371 (?P<value>.*)$ # everything up to eol
372 """
373 _OPT_NV_TMPL = r"""
374 (?P<option>.*?) # very permissive!
375 \s*(?: # any number of space/tab,
376 (?P<vi>{delim})\s* # optionally followed by
377 # any of the allowed
378 # delimiters, followed by any
379 # space/tab
380 (?P<value>.*))?$ # everything up to eol
381 """
382
383 # Compiled regular expression for matching sections
384 SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
385 # Compiled regular expression for matching options with typical separators
386 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
387 # Compiled regular expression for matching options with optional values
388 # delimited using typical separators
389 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
390 # Compiled regular expression for matching leading whitespace in a line
391 NONSPACECRE = re.compile(r"\S")
392 # Select backwards-compatible inline comment character behavior
393 # (; and # are comments at the start of a line, but ; only inline)
394 _COMPATIBLE = object()
395
Fred Drake03c44a32010-02-19 06:08:41 +0000396 def __init__(self, defaults=None, dict_type=_default_dict,
Fred Drakea4923622010-08-09 12:52:45 +0000397 allow_no_value=False, *, delimiters=('=', ':'),
398 comment_prefixes=_COMPATIBLE, strict=False,
399 empty_lines_in_values=True):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000400 self._dict = dict_type
401 self._sections = self._dict()
402 self._defaults = self._dict()
David Goodger68a1abd2004-10-03 15:40:25 +0000403 if defaults:
404 for key, value in defaults.items():
405 self._defaults[self.optionxform(key)] = value
Georg Brandl96a60ae2010-07-28 13:13:46 +0000406 self._delimiters = tuple(delimiters)
407 if delimiters == ('=', ':'):
408 self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
409 else:
Fred Drakea4923622010-08-09 12:52:45 +0000410 d = "|".join(re.escape(d) for d in delimiters)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000411 if allow_no_value:
Fred Drakea4923622010-08-09 12:52:45 +0000412 self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000413 re.VERBOSE)
414 else:
Fred Drakea4923622010-08-09 12:52:45 +0000415 self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000416 re.VERBOSE)
417 if comment_prefixes is self._COMPATIBLE:
418 self._startonly_comment_prefixes = ('#',)
419 self._comment_prefixes = (';',)
420 else:
421 self._startonly_comment_prefixes = ()
422 self._comment_prefixes = tuple(comment_prefixes or ())
Fred Drakea4923622010-08-09 12:52:45 +0000423 self._strict = strict
Georg Brandl96a60ae2010-07-28 13:13:46 +0000424 self._empty_lines_in_values = empty_lines_in_values
Guido van Rossum3d209861997-12-09 16:10:31 +0000425
426 def defaults(self):
Fred Drakefce65572002-10-25 18:08:18 +0000427 return self._defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000428
429 def sections(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000430 """Return a list of section names, excluding [DEFAULT]"""
Fred Drakefce65572002-10-25 18:08:18 +0000431 # self._sections will never have [DEFAULT] in it
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000432 return list(self._sections.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000433
434 def add_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000435 """Create a new section in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000436
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000437 Raise DuplicateSectionError if a section by the specified name
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000438 already exists. Raise ValueError if name is DEFAULT or any of it's
439 case-insensitive variants.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000440 """
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000441 if section.lower() == "default":
442 raise ValueError('Invalid section name: %s' % section)
443
Fred Drakefce65572002-10-25 18:08:18 +0000444 if section in self._sections:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000445 raise DuplicateSectionError(section)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000446 self._sections[section] = self._dict()
Guido van Rossum3d209861997-12-09 16:10:31 +0000447
448 def has_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000449 """Indicate whether the named section is present in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000450
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000451 The DEFAULT section is not acknowledged.
452 """
Fred Drakefce65572002-10-25 18:08:18 +0000453 return section in self._sections
Guido van Rossum3d209861997-12-09 16:10:31 +0000454
455 def options(self, section):
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000456 """Return a list of option names for the given section name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000457 try:
Fred Drakefce65572002-10-25 18:08:18 +0000458 opts = self._sections[section].copy()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000459 except KeyError:
460 raise NoSectionError(section)
Fred Drakefce65572002-10-25 18:08:18 +0000461 opts.update(self._defaults)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000462 if '__name__' in opts:
Fred Drake2a37f9f2000-09-27 22:43:54 +0000463 del opts['__name__']
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000464 return list(opts.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000465
Georg Brandl8dcaa732010-07-29 12:17:40 +0000466 def read(self, filenames, encoding=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000467 """Read and parse a filename or a list of filenames.
Tim Peters88869f92001-01-14 23:36:06 +0000468
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000469 Files that cannot be opened are silently ignored; this is
Barry Warsaw25394511999-10-12 16:12:48 +0000470 designed so that you can specify a list of potential
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000471 configuration file locations (e.g. current directory, user's
472 home directory, systemwide directory), and all existing
473 configuration files in the list will be read. A single
474 filename may also be given.
Fred Drake82903142004-05-18 04:24:02 +0000475
476 Return list of successfully read files.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000477 """
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000478 if isinstance(filenames, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000479 filenames = [filenames]
Fred Drake82903142004-05-18 04:24:02 +0000480 read_ok = []
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000481 for filename in filenames:
482 try:
Georg Brandl8dcaa732010-07-29 12:17:40 +0000483 fp = open(filename, encoding=encoding)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000484 except IOError:
485 continue
Fred Drakefce65572002-10-25 18:08:18 +0000486 self._read(fp, filename)
Fred Drake2438a481999-10-04 18:11:56 +0000487 fp.close()
Fred Drake82903142004-05-18 04:24:02 +0000488 read_ok.append(filename)
489 return read_ok
Guido van Rossum3d209861997-12-09 16:10:31 +0000490
Fred Drakea4923622010-08-09 12:52:45 +0000491 def read_file(self, f, source=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000492 """Like read() but the argument must be a file-like object.
493
Fred Drakea4923622010-08-09 12:52:45 +0000494 The `f' argument must have a `readline' method. Optional second
495 argument is the `source' specifying the name of the file being read. If
496 not given, it is taken from f.name. If `f' has no `name' attribute,
497 `<???>' is used.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000498 """
Fred Drakea4923622010-08-09 12:52:45 +0000499 if source is None:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000500 try:
Fred Drakea4923622010-08-09 12:52:45 +0000501 srouce = f.name
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000502 except AttributeError:
Fred Drakea4923622010-08-09 12:52:45 +0000503 source = '<???>'
504 self._read(f, source)
505
506 def read_string(self, string, source='<string>'):
507 """Read configuration from a given string."""
508 sfile = io.StringIO(string)
509 self.read_file(sfile, source)
510
511 def read_dict(self, dictionary, source='<dict>'):
512 """Read configuration from a dictionary.
513
514 Keys are section names, values are dictionaries with keys and values
515 that should be present in the section. If the used dictionary type
516 preserves order, sections and their keys will be added in order.
517
518 Optional second argument is the `source' specifying the name of the
519 dictionary being read.
520 """
521 elements_added = set()
522 for section, keys in dictionary.items():
523 try:
524 self.add_section(section)
525 except DuplicateSectionError:
526 if self._strict and section in elements_added:
527 raise
528 elements_added.add(section)
529 for key, value in keys.items():
530 key = self.optionxform(key)
531 if self._strict and (section, key) in elements_added:
532 raise DuplicateOptionError(section, key, source)
533 elements_added.add((section, key))
534 self.set(section, key, value)
535
536 def readfp(self, fp, filename=None):
537 """Deprecated, use read_file instead."""
538 warnings.warn(
539 "This method will be removed in future versions. "
540 "Use 'parser.read_file()' instead.",
541 PendingDeprecationWarning, stacklevel=2
542 )
543 self.read_file(fp, source=filename)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000544
Fred Drakefce65572002-10-25 18:08:18 +0000545 def get(self, section, option):
546 opt = self.optionxform(option)
547 if section not in self._sections:
Fred Drakec2ff9052002-09-27 15:33:11 +0000548 if section != DEFAULTSECT:
549 raise NoSectionError(section)
Fred Drakefce65572002-10-25 18:08:18 +0000550 if opt in self._defaults:
551 return self._defaults[opt]
552 else:
553 raise NoOptionError(option, section)
554 elif opt in self._sections[section]:
555 return self._sections[section][opt]
556 elif opt in self._defaults:
557 return self._defaults[opt]
558 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000559 raise NoOptionError(option, section)
Fred Drake2a37f9f2000-09-27 22:43:54 +0000560
Fred Drakefce65572002-10-25 18:08:18 +0000561 def items(self, section):
Fred Drake2ca041f2002-09-27 15:49:56 +0000562 try:
Fred Drakefce65572002-10-25 18:08:18 +0000563 d2 = self._sections[section]
Fred Drake2ca041f2002-09-27 15:49:56 +0000564 except KeyError:
565 if section != DEFAULTSECT:
566 raise NoSectionError(section)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000567 d2 = self._dict()
Fred Drakefce65572002-10-25 18:08:18 +0000568 d = self._defaults.copy()
569 d.update(d2)
Fred Drakedf393bd2002-10-25 20:41:30 +0000570 if "__name__" in d:
571 del d["__name__"]
Fred Drakefce65572002-10-25 18:08:18 +0000572 return d.items()
Fred Drake2ca041f2002-09-27 15:49:56 +0000573
Fred Drakefce65572002-10-25 18:08:18 +0000574 def _get(self, section, conv, option):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000575 return conv(self.get(section, option))
Guido van Rossum3d209861997-12-09 16:10:31 +0000576
577 def getint(self, section, option):
Fred Drakefce65572002-10-25 18:08:18 +0000578 return self._get(section, int, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000579
580 def getfloat(self, section, option):
Fred Drakefce65572002-10-25 18:08:18 +0000581 return self._get(section, float, option)
Guido van Rossum3d209861997-12-09 16:10:31 +0000582
Fred Drakec2ff9052002-09-27 15:33:11 +0000583 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
584 '0': False, 'no': False, 'false': False, 'off': False}
585
Guido van Rossum3d209861997-12-09 16:10:31 +0000586 def getboolean(self, section, option):
Tim Peterse0c446b2001-10-18 21:57:37 +0000587 v = self.get(section, option)
Fred Drakec2ff9052002-09-27 15:33:11 +0000588 if v.lower() not in self._boolean_states:
Collin Winterce36ad82007-08-30 01:19:48 +0000589 raise ValueError('Not a boolean: %s' % v)
Fred Drakec2ff9052002-09-27 15:33:11 +0000590 return self._boolean_states[v.lower()]
Guido van Rossum3d209861997-12-09 16:10:31 +0000591
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000592 def optionxform(self, optionstr):
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000593 return optionstr.lower()
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000594
Eric S. Raymond417c4892000-07-10 18:11:00 +0000595 def has_option(self, section, option):
596 """Check for the existence of a given option in a given section."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000597 if not section or section == DEFAULTSECT:
598 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000599 return option in self._defaults
600 elif section not in self._sections:
Neal Norwitzf680cc42002-12-17 01:56:47 +0000601 return False
Eric S. Raymond417c4892000-07-10 18:11:00 +0000602 else:
Fred Drake3c823aa2001-02-26 21:55:34 +0000603 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000604 return (option in self._sections[section]
605 or option in self._defaults)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000606
Fred Drake03c44a32010-02-19 06:08:41 +0000607 def set(self, section, option, value=None):
Eric S. Raymond417c4892000-07-10 18:11:00 +0000608 """Set an option."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000609 if not section or section == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000610 sectdict = self._defaults
Eric S. Raymond417c4892000-07-10 18:11:00 +0000611 else:
612 try:
Fred Drakefce65572002-10-25 18:08:18 +0000613 sectdict = self._sections[section]
Eric S. Raymond417c4892000-07-10 18:11:00 +0000614 except KeyError:
615 raise NoSectionError(section)
Fred Drakec2ff9052002-09-27 15:33:11 +0000616 sectdict[self.optionxform(option)] = value
Eric S. Raymond417c4892000-07-10 18:11:00 +0000617
Georg Brandl96a60ae2010-07-28 13:13:46 +0000618 def write(self, fp, space_around_delimiters=True):
619 """Write an .ini-format representation of the configuration state.
620
621 If `space_around_delimiters' is True (the default), delimiters
622 between keys and values are surrounded by spaces.
623 """
624 if space_around_delimiters:
625 d = " {} ".format(self._delimiters[0])
626 else:
627 d = self._delimiters[0]
Fred Drakefce65572002-10-25 18:08:18 +0000628 if self._defaults:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000629 self._write_section(fp, DEFAULTSECT, self._defaults.items(), d)
Fred Drakefce65572002-10-25 18:08:18 +0000630 for section in self._sections:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000631 self._write_section(fp, section,
632 self._sections[section].items(), d)
633
634 def _write_section(self, fp, section_name, section_items, delimiter):
635 """Write a single section to the specified `fp'."""
636 fp.write("[{}]\n".format(section_name))
637 for key, value in section_items:
638 if key == "__name__":
639 continue
640 if value is not None:
641 value = delimiter + str(value).replace('\n', '\n\t')
642 else:
643 value = ""
644 fp.write("{}{}\n".format(key, value))
645 fp.write("\n")
Eric S. Raymond417c4892000-07-10 18:11:00 +0000646
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000647 def remove_option(self, section, option):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000648 """Remove an option."""
Fred Drakec2ff9052002-09-27 15:33:11 +0000649 if not section or section == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000650 sectdict = self._defaults
Eric S. Raymond649685a2000-07-14 14:28:22 +0000651 else:
652 try:
Fred Drakefce65572002-10-25 18:08:18 +0000653 sectdict = self._sections[section]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000654 except KeyError:
655 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000656 option = self.optionxform(option)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000657 existed = option in sectdict
Eric S. Raymond649685a2000-07-14 14:28:22 +0000658 if existed:
Fred Drakeff4a23b2000-12-04 16:29:13 +0000659 del sectdict[option]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000660 return existed
661
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000662 def remove_section(self, section):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000663 """Remove a file section."""
Fred Drakefce65572002-10-25 18:08:18 +0000664 existed = section in self._sections
Fred Drakec2ff9052002-09-27 15:33:11 +0000665 if existed:
Fred Drakefce65572002-10-25 18:08:18 +0000666 del self._sections[section]
Fred Drakec2ff9052002-09-27 15:33:11 +0000667 return existed
Eric S. Raymond649685a2000-07-14 14:28:22 +0000668
Fred Drakefce65572002-10-25 18:08:18 +0000669 def _read(self, fp, fpname):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000670 """Parse a sectioned configuration file.
Guido van Rossum3d209861997-12-09 16:10:31 +0000671
Fred Drakea4923622010-08-09 12:52:45 +0000672 Each section in a configuration file contains a header, indicated by
673 a name in square brackets (`[]'), plus key/value options, indicated by
Georg Brandl96a60ae2010-07-28 13:13:46 +0000674 `name' and `value' delimited with a specific substring (`=' or `:' by
675 default).
676
Fred Drakea4923622010-08-09 12:52:45 +0000677 Values can span multiple lines, as long as they are indented deeper
678 than the first line of the value. Depending on the parser's mode, blank
679 lines may be treated as parts of multiline values or ignored.
Georg Brandl96a60ae2010-07-28 13:13:46 +0000680
681 Configuration files may include comments, prefixed by specific
Fred Drakea4923622010-08-09 12:52:45 +0000682 characters (`#' and `;' by default). Comments may appear on their own
683 in an otherwise empty line or may be entered in lines holding values or
Georg Brandl96a60ae2010-07-28 13:13:46 +0000684 section names.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000685 """
Fred Drakea4923622010-08-09 12:52:45 +0000686 elements_added = set()
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000687 cursect = None # None, or a dictionary
Fred Drakea4923622010-08-09 12:52:45 +0000688 sectname = None
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000689 optname = None
690 lineno = 0
Georg Brandl96a60ae2010-07-28 13:13:46 +0000691 indent_level = 0
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000692 e = None # None, or an exception
Georg Brandl96a60ae2010-07-28 13:13:46 +0000693 for lineno, line in enumerate(fp, start=1):
Georg Brandlf206d0e2010-07-29 11:56:20 +0000694 # strip full line comments
Georg Brandl96a60ae2010-07-28 13:13:46 +0000695 comment_start = None
696 for prefix in self._startonly_comment_prefixes:
697 if line.strip().startswith(prefix):
698 comment_start = 0
699 break
700 # strip inline comments
701 for prefix in self._comment_prefixes:
702 index = line.find(prefix)
703 if index == 0 or (index > 0 and line[index-1].isspace()):
704 comment_start = index
705 break
706 value = line[:comment_start].strip()
707 if not value:
Georg Brandlf206d0e2010-07-29 11:56:20 +0000708 if self._empty_lines_in_values:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000709 # add empty line to the value, but only if there was no
710 # comment on the line
Georg Brandlf206d0e2010-07-29 11:56:20 +0000711 if (comment_start is None and
712 cursect is not None and
713 optname and
714 cursect[optname] is not None):
715 cursect[optname].append('') # newlines added at join
Georg Brandl96a60ae2010-07-28 13:13:46 +0000716 else:
717 # empty line marks end of value
718 indent_level = sys.maxsize
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000719 continue
720 # continuation line?
Georg Brandl96a60ae2010-07-28 13:13:46 +0000721 first_nonspace = self.NONSPACECRE.search(line)
722 cur_indent_level = first_nonspace.start() if first_nonspace else 0
723 if (cursect is not None and optname and
724 cur_indent_level > indent_level):
725 cursect[optname].append(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000726 # a section header or option header?
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000727 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000728 indent_level = cur_indent_level
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000729 # is it a section header?
Georg Brandl96a60ae2010-07-28 13:13:46 +0000730 mo = self.SECTCRE.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000731 if mo:
732 sectname = mo.group('header')
Fred Drakefce65572002-10-25 18:08:18 +0000733 if sectname in self._sections:
Fred Drakea4923622010-08-09 12:52:45 +0000734 if self._strict and sectname in elements_added:
735 raise DuplicateSectionError(sectname, fpname,
736 lineno)
Fred Drakefce65572002-10-25 18:08:18 +0000737 cursect = self._sections[sectname]
Fred Drakea4923622010-08-09 12:52:45 +0000738 elements_added.add(sectname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000739 elif sectname == DEFAULTSECT:
Fred Drakefce65572002-10-25 18:08:18 +0000740 cursect = self._defaults
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000741 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000742 cursect = self._dict()
743 cursect['__name__'] = sectname
Fred Drakefce65572002-10-25 18:08:18 +0000744 self._sections[sectname] = cursect
Fred Drakea4923622010-08-09 12:52:45 +0000745 elements_added.add(sectname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000746 # So sections can't start with a continuation line
747 optname = None
748 # no section header in the file?
749 elif cursect is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000750 raise MissingSectionHeaderError(fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000751 # an option line?
752 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000753 mo = self._optcre.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000754 if mo:
Fred Drakec517b9b2000-02-28 20:59:03 +0000755 optname, vi, optval = mo.group('option', 'vi', 'value')
Georg Brandl96a60ae2010-07-28 13:13:46 +0000756 if not optname:
757 e = self._handle_error(e, fpname, lineno, line)
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000758 optname = self.optionxform(optname.rstrip())
Fred Drakea4923622010-08-09 12:52:45 +0000759 if (self._strict and
760 (sectname, optname) in elements_added):
761 raise DuplicateOptionError(sectname, optname,
762 fpname, lineno)
763 elements_added.add((sectname, optname))
Fred Drake03c44a32010-02-19 06:08:41 +0000764 # This check is fine because the OPTCRE cannot
765 # match if it would set optval to None
766 if optval is not None:
Fred Drake03c44a32010-02-19 06:08:41 +0000767 optval = optval.strip()
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000768 # allow empty values
769 if optval == '""':
770 optval = ''
771 cursect[optname] = [optval]
772 else:
773 # valueless option handling
774 cursect[optname] = optval
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000775 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000776 # a non-fatal parsing error occurred. set up the
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000777 # exception but keep going. the exception will be
778 # raised at the end of the file and will contain a
779 # list of all bogus lines
Georg Brandl96a60ae2010-07-28 13:13:46 +0000780 e = self._handle_error(e, fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000781 # if any parsing errors occurred, raise an exception
782 if e:
783 raise e
Georg Brandl96a60ae2010-07-28 13:13:46 +0000784 self._join_multiline_values()
Fred Drakefce65572002-10-25 18:08:18 +0000785
Georg Brandl96a60ae2010-07-28 13:13:46 +0000786 def _join_multiline_values(self):
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000787 all_sections = [self._defaults]
788 all_sections.extend(self._sections.values())
789 for options in all_sections:
790 for name, val in options.items():
791 if isinstance(val, list):
Georg Brandlf206d0e2010-07-29 11:56:20 +0000792 options[name] = '\n'.join(val).rstrip()
Fred Drakefce65572002-10-25 18:08:18 +0000793
Georg Brandl96a60ae2010-07-28 13:13:46 +0000794 def _handle_error(self, exc, fpname, lineno, line):
795 if not exc:
796 exc = ParsingError(fpname)
797 exc.append(lineno, repr(line))
798 return exc
799
800
Fred Drakefce65572002-10-25 18:08:18 +0000801class ConfigParser(RawConfigParser):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000802 """ConfigParser implementing interpolation."""
Fred Drakefce65572002-10-25 18:08:18 +0000803
Neal Norwitzf680cc42002-12-17 01:56:47 +0000804 def get(self, section, option, raw=False, vars=None):
Fred Drakefce65572002-10-25 18:08:18 +0000805 """Get an option value for a given section.
806
Georg Brandl470a1232010-07-29 14:17:12 +0000807 If `vars' is provided, it must be a dictionary. The option is looked up
808 in `vars' (if provided), `section', and in `defaults' in that order.
809
810 All % interpolations are expanded in the return values, unless the
811 optional argument `raw' is true. Values for interpolation keys are
812 looked up in the same manner as the option.
Fred Drakefce65572002-10-25 18:08:18 +0000813
814 The section DEFAULT is special.
815 """
816 d = self._defaults.copy()
817 try:
818 d.update(self._sections[section])
819 except KeyError:
820 if section != DEFAULTSECT:
821 raise NoSectionError(section)
822 # Update with the entry specific variables
David Goodger68a1abd2004-10-03 15:40:25 +0000823 if vars:
824 for key, value in vars.items():
825 d[self.optionxform(key)] = value
Fred Drakefce65572002-10-25 18:08:18 +0000826 option = self.optionxform(option)
827 try:
828 value = d[option]
829 except KeyError:
830 raise NoOptionError(option, section)
831
Fred Drake03c44a32010-02-19 06:08:41 +0000832 if raw or value is None:
Fred Drakefce65572002-10-25 18:08:18 +0000833 return value
834 else:
835 return self._interpolate(section, option, value, d)
836
Neal Norwitzf680cc42002-12-17 01:56:47 +0000837 def items(self, section, raw=False, vars=None):
Fred Drakea4923622010-08-09 12:52:45 +0000838 """Return a list of (name, value) tuples for each option in a section.
Fred Drakefce65572002-10-25 18:08:18 +0000839
840 All % interpolations are expanded in the return values, based on the
841 defaults passed into the constructor, unless the optional argument
842 `raw' is true. Additional substitutions may be provided using the
843 `vars' argument, which must be a dictionary whose contents overrides
844 any pre-existing defaults.
845
846 The section DEFAULT is special.
847 """
848 d = self._defaults.copy()
849 try:
850 d.update(self._sections[section])
851 except KeyError:
852 if section != DEFAULTSECT:
853 raise NoSectionError(section)
854 # Update with the entry specific variables
855 if vars:
David Goodger68a1abd2004-10-03 15:40:25 +0000856 for key, value in vars.items():
857 d[self.optionxform(key)] = value
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000858 options = list(d.keys())
Fred Drakedf393bd2002-10-25 20:41:30 +0000859 if "__name__" in options:
860 options.remove("__name__")
Fred Drakefce65572002-10-25 18:08:18 +0000861 if raw:
Fred Drake8c4da532003-10-21 16:45:00 +0000862 return [(option, d[option])
863 for option in options]
Fred Drakefce65572002-10-25 18:08:18 +0000864 else:
Fred Drake8c4da532003-10-21 16:45:00 +0000865 return [(option, self._interpolate(section, option, d[option], d))
866 for option in options]
Fred Drakefce65572002-10-25 18:08:18 +0000867
868 def _interpolate(self, section, option, rawval, vars):
869 # do the string interpolation
870 value = rawval
Tim Peters230a60c2002-11-09 05:08:07 +0000871 depth = MAX_INTERPOLATION_DEPTH
Fred Drakefce65572002-10-25 18:08:18 +0000872 while depth: # Loop through this until it's done
873 depth -= 1
Fred Drake03c44a32010-02-19 06:08:41 +0000874 if value and "%(" in value:
Fred Drakebc12b012004-05-18 02:25:51 +0000875 value = self._KEYCRE.sub(self._interpolation_replace, value)
Fred Drakefce65572002-10-25 18:08:18 +0000876 try:
877 value = value % vars
Guido van Rossumb940e112007-01-10 16:19:56 +0000878 except KeyError as e:
Fred Drakee2c64912002-12-31 17:23:27 +0000879 raise InterpolationMissingOptionError(
Brett Cannonca477b22007-03-21 22:26:20 +0000880 option, section, rawval, e.args[0])
Fred Drakefce65572002-10-25 18:08:18 +0000881 else:
882 break
Fred Drake03c44a32010-02-19 06:08:41 +0000883 if value and "%(" in value:
Fred Drakefce65572002-10-25 18:08:18 +0000884 raise InterpolationDepthError(option, section, rawval)
885 return value
Fred Drake0eebd5c2002-10-25 21:52:00 +0000886
Fred Drakebc12b012004-05-18 02:25:51 +0000887 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
888
889 def _interpolation_replace(self, match):
890 s = match.group(1)
891 if s is None:
892 return match.group()
893 else:
894 return "%%(%s)s" % self.optionxform(s)
895
Fred Drake0eebd5c2002-10-25 21:52:00 +0000896
897class SafeConfigParser(ConfigParser):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000898 """ConfigParser implementing sane interpolation."""
Fred Drake0eebd5c2002-10-25 21:52:00 +0000899
900 def _interpolate(self, section, option, rawval, vars):
901 # do the string interpolation
902 L = []
903 self._interpolate_some(option, L, rawval, section, vars, 1)
904 return ''.join(L)
905
Guido van Rossumd8faa362007-04-27 19:54:29 +0000906 _interpvar_re = re.compile(r"%\(([^)]+)\)s")
Fred Drake0eebd5c2002-10-25 21:52:00 +0000907
908 def _interpolate_some(self, option, accum, rest, section, map, depth):
909 if depth > MAX_INTERPOLATION_DEPTH:
910 raise InterpolationDepthError(option, section, rest)
911 while rest:
912 p = rest.find("%")
913 if p < 0:
914 accum.append(rest)
915 return
916 if p > 0:
917 accum.append(rest[:p])
918 rest = rest[p:]
919 # p is no longer used
920 c = rest[1:2]
921 if c == "%":
922 accum.append("%")
923 rest = rest[2:]
924 elif c == "(":
Guido van Rossumd8faa362007-04-27 19:54:29 +0000925 m = self._interpvar_re.match(rest)
Fred Drake0eebd5c2002-10-25 21:52:00 +0000926 if m is None:
Neal Norwitz10f30182003-06-29 04:23:35 +0000927 raise InterpolationSyntaxError(option, section,
928 "bad interpolation variable reference %r" % rest)
Fred Drakebc12b012004-05-18 02:25:51 +0000929 var = self.optionxform(m.group(1))
Fred Drake0eebd5c2002-10-25 21:52:00 +0000930 rest = rest[m.end():]
931 try:
932 v = map[var]
933 except KeyError:
Fred Drakee2c64912002-12-31 17:23:27 +0000934 raise InterpolationMissingOptionError(
935 option, section, rest, var)
Fred Drake0eebd5c2002-10-25 21:52:00 +0000936 if "%" in v:
937 self._interpolate_some(option, accum, v,
938 section, map, depth + 1)
939 else:
940 accum.append(v)
941 else:
942 raise InterpolationSyntaxError(
Neal Norwitz10f30182003-06-29 04:23:35 +0000943 option, section,
Fred Drakea4923622010-08-09 12:52:45 +0000944 "'%%' must be followed by '%%' or '(', "
945 "found: %r" % (rest,))
David Goodger1cbf2062004-10-03 15:55:09 +0000946
Fred Drake03c44a32010-02-19 06:08:41 +0000947 def set(self, section, option, value=None):
David Goodger1cbf2062004-10-03 15:55:09 +0000948 """Set an option. Extend ConfigParser.set: check for string values."""
Fred Drake03c44a32010-02-19 06:08:41 +0000949 # The only legal non-string value if we allow valueless
950 # options is None, so we need to check if the value is a
951 # string if:
952 # - we do not allow valueless options, or
953 # - we allow valueless options but the value is not None
954 if self._optcre is self.OPTCRE or value:
955 if not isinstance(value, str):
956 raise TypeError("option values must be strings")
Fred Drakea4923622010-08-09 12:52:45 +0000957 # check for bad percent signs
958 if value:
959 tmp_value = value.replace('%%', '') # escaped percent signs
960 tmp_value = self._interpvar_re.sub('', tmp_value) # valid syntax
961 if '%' in tmp_value:
962 raise ValueError("invalid interpolation syntax in %r at "
963 "position %d" % (value, tmp_value.find('%')))
David Goodger1cbf2062004-10-03 15:55:09 +0000964 ConfigParser.set(self, section, option, value)