blob: 0e41d2f1b5f35878e433f566909d3fc160c161d7 [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
Guido van Rossum3d209861997-12-09 16:10:31 +00007Intrinsic defaults can be specified by passing them into the
Łukasz Langa7f64c8a2010-12-16 01:16:22 +00008ConfigParser constructor as a dictionary.
Guido van Rossum3d209861997-12-09 16:10:31 +00009
10class:
11
Łukasz Langa7f64c8a2010-12-16 01:16:22 +000012ConfigParser -- responsible for parsing a list of
Łukasz Langab6a6f5f2010-12-03 16:28:00 +000013 configuration files, and managing the parsed database.
Guido van Rossum3d209861997-12-09 16:10:31 +000014
15 methods:
16
Fred Drakecc645b92010-09-04 04:35:34 +000017 __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
Łukasz Langab25a7912010-12-17 01:32:29 +000018 delimiters=('=', ':'), comment_prefixes=('#', ';'),
19 inline_comment_prefixes=None, strict=True,
20 empty_lines_in_values=True):
Georg Brandl96a60ae2010-07-28 13:13:46 +000021 Create the parser. When `defaults' is given, it is initialized into the
22 dictionary or intrinsic defaults. The keys must be strings, the values
Łukasz Langa5c863392010-11-21 13:41:35 +000023 must be appropriate for %()s string interpolation.
Georg Brandl96a60ae2010-07-28 13:13:46 +000024
25 When `dict_type' is given, it will be used to create the dictionary
26 objects for the list of sections, for the options within a section, and
27 for the default values.
28
29 When `delimiters' is given, it will be used as the set of substrings
30 that divide keys from values.
31
32 When `comment_prefixes' is given, it will be used as the set of
Łukasz Langab25a7912010-12-17 01:32:29 +000033 substrings that prefix comments in empty lines. Comments can be
34 indented.
35
36 When `inline_comment_prefixes' is given, it will be used as the set of
37 substrings that prefix comments in non-empty lines.
Georg Brandl96a60ae2010-07-28 13:13:46 +000038
Fred Drakea4923622010-08-09 12:52:45 +000039 When `strict` is True, the parser won't allow for any section or option
40 duplicates while reading from a single source (file, string or
Łukasz Langab25a7912010-12-17 01:32:29 +000041 dictionary). Default is True.
Fred Drakea4923622010-08-09 12:52:45 +000042
Georg Brandl96a60ae2010-07-28 13:13:46 +000043 When `empty_lines_in_values' is False (default: True), each empty line
44 marks the end of an option. Otherwise, internal empty lines of
45 a multiline option are kept as part of the value.
46
47 When `allow_no_value' is True (default: False), options without
48 values are accepted; the value presented for these is None.
Guido van Rossum3d209861997-12-09 16:10:31 +000049
Barry Warsawf09f6a51999-01-26 22:01:37 +000050 sections()
Georg Brandl96a60ae2010-07-28 13:13:46 +000051 Return all the configuration section names, sans DEFAULT.
Guido van Rossum3d209861997-12-09 16:10:31 +000052
Guido van Rossuma5a24b71999-10-04 19:58:22 +000053 has_section(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +000054 Return whether the given section exists.
Guido van Rossuma5a24b71999-10-04 19:58:22 +000055
Eric S. Raymond649685a2000-07-14 14:28:22 +000056 has_option(section, option)
Georg Brandl96a60ae2010-07-28 13:13:46 +000057 Return whether the given option exists in the given section.
Eric S. Raymond649685a2000-07-14 14:28:22 +000058
Barry Warsawf09f6a51999-01-26 22:01:37 +000059 options(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +000060 Return list of configuration options for the named section.
Guido van Rossum3d209861997-12-09 16:10:31 +000061
Georg Brandl8dcaa732010-07-29 12:17:40 +000062 read(filenames, encoding=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +000063 Read and parse the list of named configuration files, given by
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000064 name. A single filename is also allowed. Non-existing files
Fred Drake82903142004-05-18 04:24:02 +000065 are ignored. Return list of successfully read files.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +000066
Fred Drakea4923622010-08-09 12:52:45 +000067 read_file(f, filename=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +000068 Read and parse one configuration file, given as a file object.
Fred Drakea4923622010-08-09 12:52:45 +000069 The filename defaults to f.name; it is only used in error
70 messages (if f has no `name' attribute, the string `<???>' is used).
71
72 read_string(string)
73 Read configuration from a given string.
74
75 read_dict(dictionary)
76 Read configuration from a dictionary. Keys are section names,
77 values are dictionaries with keys and values that should be present
78 in the section. If the used dictionary type preserves order, sections
Fred Drakecc645b92010-09-04 04:35:34 +000079 and their keys will be added in order. Values are automatically
80 converted to strings.
Guido van Rossum3d209861997-12-09 16:10:31 +000081
Łukasz Langa26d513c2010-11-10 18:57:39 +000082 get(section, option, raw=False, vars=None, fallback=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000083 Return a string value for the named option. All % interpolations are
Barry Warsawf09f6a51999-01-26 22:01:37 +000084 expanded in the return values, based on the defaults passed into the
85 constructor and the DEFAULT section. Additional substitutions may be
86 provided using the `vars' argument, which must be a dictionary whose
Fred Drakecc645b92010-09-04 04:35:34 +000087 contents override any pre-existing defaults. If `option' is a key in
88 `vars', the value from `vars' is used.
Guido van Rossum3d209861997-12-09 16:10:31 +000089
Łukasz Langa26d513c2010-11-10 18:57:39 +000090 getint(section, options, raw=False, vars=None, fallback=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000091 Like get(), but convert value to an integer.
Guido van Rossum3d209861997-12-09 16:10:31 +000092
Łukasz Langa26d513c2010-11-10 18:57:39 +000093 getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000094 Like get(), but convert value to a float.
Guido van Rossum3d209861997-12-09 16:10:31 +000095
Łukasz Langa26d513c2010-11-10 18:57:39 +000096 getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
Georg Brandl96a60ae2010-07-28 13:13:46 +000097 Like get(), but convert value to a boolean (currently case
Neal Norwitzf680cc42002-12-17 01:56:47 +000098 insensitively defined as 0, false, no, off for False, and 1, true,
99 yes, on for True). Returns False or True.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000100
Neal Norwitzf680cc42002-12-17 01:56:47 +0000101 items(section, raw=False, vars=None)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000102 Return a list of tuples with (name, value) for each option
Fred Drake2ca041f2002-09-27 15:49:56 +0000103 in the section.
104
Eric S. Raymond649685a2000-07-14 14:28:22 +0000105 remove_section(section)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000106 Remove the given file section and all its options.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000107
108 remove_option(section, option)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000109 Remove the given option from the given section.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000110
111 set(section, option, value)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000112 Set the given option.
Eric S. Raymond649685a2000-07-14 14:28:22 +0000113
Georg Brandl96a60ae2010-07-28 13:13:46 +0000114 write(fp, space_around_delimiters=True)
115 Write the configuration state in .ini format. If
116 `space_around_delimiters' is True (the default), delimiters
117 between keys and values are surrounded by spaces.
Guido van Rossum3d209861997-12-09 16:10:31 +0000118"""
119
Łukasz Langa26d513c2010-11-10 18:57:39 +0000120from collections import MutableMapping, OrderedDict as _default_dict
121import functools
Fred Drakea4923622010-08-09 12:52:45 +0000122import io
Łukasz Langa26d513c2010-11-10 18:57:39 +0000123import itertools
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000124import re
Georg Brandl96a60ae2010-07-28 13:13:46 +0000125import sys
Fred Drakea4923622010-08-09 12:52:45 +0000126import warnings
Guido van Rossum3d209861997-12-09 16:10:31 +0000127
Fred Drakea4923622010-08-09 12:52:45 +0000128__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
129 "NoOptionError", "InterpolationError", "InterpolationDepthError",
Fred Drake8d5dd982002-12-30 23:51:45 +0000130 "InterpolationSyntaxError", "ParsingError",
David Goodger1cbf2062004-10-03 15:55:09 +0000131 "MissingSectionHeaderError",
132 "ConfigParser", "SafeConfigParser", "RawConfigParser",
Fred Drakec2ff9052002-09-27 15:33:11 +0000133 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +0000134
Guido van Rossum3d209861997-12-09 16:10:31 +0000135DEFAULTSECT = "DEFAULT"
136
Fred Drake2a37f9f2000-09-27 22:43:54 +0000137MAX_INTERPOLATION_DEPTH = 10
138
Guido van Rossum3d209861997-12-09 16:10:31 +0000139
Tim Peters88869f92001-01-14 23:36:06 +0000140
Guido van Rossum3d209861997-12-09 16:10:31 +0000141# exception classes
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000142class Error(Exception):
Fred Drake8d5dd982002-12-30 23:51:45 +0000143 """Base class for ConfigParser exceptions."""
144
Guido van Rossum360e4b82007-05-14 22:51:27 +0000145 def _get_message(self):
146 """Getter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000147 BaseException.
148 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000149 return self.__message
150
151 def _set_message(self, value):
152 """Setter for 'message'; needed only to override deprecation in
Georg Brandl96a60ae2010-07-28 13:13:46 +0000153 BaseException.
154 """
Guido van Rossum360e4b82007-05-14 22:51:27 +0000155 self.__message = value
156
157 # BaseException.message has been deprecated since Python 2.6. To prevent
Fred Drakea4923622010-08-09 12:52:45 +0000158 # DeprecationWarning from popping up over this pre-existing attribute, use
159 # a new property that takes lookup precedence.
Guido van Rossum360e4b82007-05-14 22:51:27 +0000160 message = property(_get_message, _set_message)
161
Guido van Rossum3d209861997-12-09 16:10:31 +0000162 def __init__(self, msg=''):
Fred Drakee2c64912002-12-31 17:23:27 +0000163 self.message = msg
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000164 Exception.__init__(self, msg)
Fred Drake8d5dd982002-12-30 23:51:45 +0000165
Guido van Rossum3d209861997-12-09 16:10:31 +0000166 def __repr__(self):
Fred Drakee2c64912002-12-31 17:23:27 +0000167 return self.message
Fred Drake8d5dd982002-12-30 23:51:45 +0000168
Fred Drake7c1e5ad2000-12-11 18:13:19 +0000169 __str__ = __repr__
Guido van Rossum3d209861997-12-09 16:10:31 +0000170
Georg Brandl96a60ae2010-07-28 13:13:46 +0000171
Guido van Rossum3d209861997-12-09 16:10:31 +0000172class NoSectionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000173 """Raised when no section matches a requested option."""
174
Guido van Rossum3d209861997-12-09 16:10:31 +0000175 def __init__(self, section):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000176 Error.__init__(self, 'No section: %r' % (section,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000177 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000178 self.args = (section, )
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 DuplicateSectionError(Error):
Fred Drakea4923622010-08-09 12:52:45 +0000182 """Raised when a section is repeated in an input source.
Fred Drake8d5dd982002-12-30 23:51:45 +0000183
Fred Drakea4923622010-08-09 12:52:45 +0000184 Possible repetitions that raise this exception are: multiple creation
185 using the API or in strict parsers when a section is found more than once
186 in a single input file, string or dictionary.
187 """
188
189 def __init__(self, section, source=None, lineno=None):
190 msg = [repr(section), " already exists"]
191 if source is not None:
192 message = ["While reading from ", source]
193 if lineno is not None:
194 message.append(" [line {0:2d}]".format(lineno))
195 message.append(": section ")
196 message.extend(msg)
197 msg = message
198 else:
199 msg.insert(0, "Section ")
200 Error.__init__(self, "".join(msg))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000201 self.section = section
Fred Drakea4923622010-08-09 12:52:45 +0000202 self.source = source
203 self.lineno = lineno
204 self.args = (section, source, lineno)
205
206
207class DuplicateOptionError(Error):
208 """Raised by strict parsers when an option is repeated in an input source.
209
210 Current implementation raises this exception only when an option is found
211 more than once in a single file, string or dictionary.
212 """
213
214 def __init__(self, section, option, source=None, lineno=None):
215 msg = [repr(option), " in section ", repr(section),
216 " already exists"]
217 if source is not None:
218 message = ["While reading from ", source]
219 if lineno is not None:
220 message.append(" [line {0:2d}]".format(lineno))
221 message.append(": option ")
222 message.extend(msg)
223 msg = message
224 else:
225 msg.insert(0, "Option ")
226 Error.__init__(self, "".join(msg))
227 self.section = section
228 self.option = option
229 self.source = source
230 self.lineno = lineno
231 self.args = (section, option, source, lineno)
Guido van Rossum3d209861997-12-09 16:10:31 +0000232
Georg Brandl96a60ae2010-07-28 13:13:46 +0000233
Guido van Rossum3d209861997-12-09 16:10:31 +0000234class NoOptionError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000235 """A requested option was not found."""
236
Guido van Rossum3d209861997-12-09 16:10:31 +0000237 def __init__(self, option, section):
Fred Drakee2c64912002-12-31 17:23:27 +0000238 Error.__init__(self, "No option %r in section: %r" %
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000239 (option, section))
240 self.option = option
241 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000242 self.args = (option, section)
Guido van Rossum3d209861997-12-09 16:10:31 +0000243
Georg Brandl96a60ae2010-07-28 13:13:46 +0000244
Guido van Rossum3d209861997-12-09 16:10:31 +0000245class InterpolationError(Error):
Fred Drakee2c64912002-12-31 17:23:27 +0000246 """Base class for interpolation-related exceptions."""
Fred Drake8d5dd982002-12-30 23:51:45 +0000247
Fred Drakee2c64912002-12-31 17:23:27 +0000248 def __init__(self, option, section, msg):
249 Error.__init__(self, msg)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000250 self.option = option
251 self.section = section
Michael Foordbd6c0792010-07-25 23:09:25 +0000252 self.args = (option, section, msg)
Guido van Rossum3d209861997-12-09 16:10:31 +0000253
Georg Brandl96a60ae2010-07-28 13:13:46 +0000254
Fred Drakee2c64912002-12-31 17:23:27 +0000255class InterpolationMissingOptionError(InterpolationError):
256 """A string substitution required a setting which was not available."""
257
258 def __init__(self, option, section, rawval, reference):
259 msg = ("Bad value substitution:\n"
260 "\tsection: [%s]\n"
261 "\toption : %s\n"
262 "\tkey : %s\n"
263 "\trawval : %s\n"
264 % (section, option, reference, rawval))
265 InterpolationError.__init__(self, option, section, msg)
266 self.reference = reference
Michael Foordbd6c0792010-07-25 23:09:25 +0000267 self.args = (option, section, rawval, reference)
Fred Drakee2c64912002-12-31 17:23:27 +0000268
Georg Brandl96a60ae2010-07-28 13:13:46 +0000269
Fred Drakee2c64912002-12-31 17:23:27 +0000270class InterpolationSyntaxError(InterpolationError):
Fred Drakea4923622010-08-09 12:52:45 +0000271 """Raised when the source text contains invalid syntax.
272
Łukasz Langa7f64c8a2010-12-16 01:16:22 +0000273 Current implementation raises this exception when the source text into
274 which substitutions are made does not conform to the required syntax.
Fred Drakea4923622010-08-09 12:52:45 +0000275 """
Neal Norwitzce1d9442002-12-30 23:38:47 +0000276
Georg Brandl96a60ae2010-07-28 13:13:46 +0000277
Fred Drakee2c64912002-12-31 17:23:27 +0000278class InterpolationDepthError(InterpolationError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000279 """Raised when substitutions are nested too deeply."""
280
Fred Drake2a37f9f2000-09-27 22:43:54 +0000281 def __init__(self, option, section, rawval):
Fred Drakee2c64912002-12-31 17:23:27 +0000282 msg = ("Value interpolation too deeply recursive:\n"
283 "\tsection: [%s]\n"
284 "\toption : %s\n"
285 "\trawval : %s\n"
286 % (section, option, rawval))
287 InterpolationError.__init__(self, option, section, msg)
Michael Foordbd6c0792010-07-25 23:09:25 +0000288 self.args = (option, section, rawval)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000289
Georg Brandl96a60ae2010-07-28 13:13:46 +0000290
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000291class ParsingError(Error):
Fred Drake8d5dd982002-12-30 23:51:45 +0000292 """Raised when a configuration file does not follow legal syntax."""
293
Fred Drakea4923622010-08-09 12:52:45 +0000294 def __init__(self, source=None, filename=None):
295 # Exactly one of `source'/`filename' arguments has to be given.
296 # `filename' kept for compatibility.
297 if filename and source:
298 raise ValueError("Cannot specify both `filename' and `source'. "
299 "Use `source'.")
300 elif not filename and not source:
301 raise ValueError("Required argument `source' not given.")
302 elif filename:
303 source = filename
304 Error.__init__(self, 'Source contains parsing errors: %s' % source)
305 self.source = source
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000306 self.errors = []
Fred Drakea4923622010-08-09 12:52:45 +0000307 self.args = (source, )
308
309 @property
310 def filename(self):
311 """Deprecated, use `source'."""
312 warnings.warn(
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000313 "The 'filename' attribute will be removed in future versions. "
Fred Drakea4923622010-08-09 12:52:45 +0000314 "Use 'source' instead.",
Łukasz Langa49afa382010-11-11 19:53:23 +0000315 DeprecationWarning, stacklevel=2
Fred Drakea4923622010-08-09 12:52:45 +0000316 )
317 return self.source
318
319 @filename.setter
320 def filename(self, value):
321 """Deprecated, user `source'."""
322 warnings.warn(
323 "The 'filename' attribute will be removed in future versions. "
324 "Use 'source' instead.",
Łukasz Langa49afa382010-11-11 19:53:23 +0000325 DeprecationWarning, stacklevel=2
Fred Drakea4923622010-08-09 12:52:45 +0000326 )
327 self.source = value
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000328
329 def append(self, lineno, line):
330 self.errors.append((lineno, line))
Fred Drakee2c64912002-12-31 17:23:27 +0000331 self.message += '\n\t[line %2d]: %s' % (lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +0000332
Georg Brandl96a60ae2010-07-28 13:13:46 +0000333
Fred Drake2a37f9f2000-09-27 22:43:54 +0000334class MissingSectionHeaderError(ParsingError):
Fred Drake8d5dd982002-12-30 23:51:45 +0000335 """Raised when a key-value pair is found before any section header."""
336
Fred Drake2a37f9f2000-09-27 22:43:54 +0000337 def __init__(self, filename, lineno, line):
338 Error.__init__(
339 self,
Walter Dörwald70a6b492004-02-12 17:35:32 +0000340 'File contains no section headers.\nfile: %s, line: %d\n%r' %
Fred Drake2a37f9f2000-09-27 22:43:54 +0000341 (filename, lineno, line))
Fred Drakea4923622010-08-09 12:52:45 +0000342 self.source = filename
Fred Drake2a37f9f2000-09-27 22:43:54 +0000343 self.lineno = lineno
344 self.line = line
Michael Foordbd6c0792010-07-25 23:09:25 +0000345 self.args = (filename, lineno, line)
Guido van Rossum3d209861997-12-09 16:10:31 +0000346
Georg Brandl96a60ae2010-07-28 13:13:46 +0000347
Fred Drakecc645b92010-09-04 04:35:34 +0000348# Used in parser getters to indicate the default behaviour when a specific
349# option is not found it to raise an exception. Created to enable `None' as
350# a valid fallback value.
351_UNSET = object()
352
353
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000354class Interpolation:
355 """Dummy interpolation that passes the value through with no changes."""
356
357 def before_get(self, parser, section, option, value, defaults):
358 return value
359
360 def before_set(self, parser, section, option, value):
361 return value
362
363 def before_read(self, parser, section, option, value):
364 return value
365
366 def before_write(self, parser, section, option, value):
367 return value
368
369
370class BasicInterpolation(Interpolation):
Łukasz Langa7f64c8a2010-12-16 01:16:22 +0000371 """Interpolation as implemented in the classic ConfigParser.
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000372
373 The option values can contain format strings which refer to other values in
374 the same section, or values in the special default section.
375
376 For example:
377
378 something: %(dir)s/whatever
379
380 would resolve the "%(dir)s" to the value of dir. All reference
381 expansions are done late, on demand. If a user needs to use a bare % in
382 a configuration file, she can escape it by writing %%. Other other % usage
383 is considered a user error and raises `InterpolationSyntaxError'."""
384
385 _KEYCRE = re.compile(r"%\(([^)]+)\)s")
386
387 def before_get(self, parser, section, option, value, defaults):
388 L = []
389 self._interpolate_some(parser, option, L, value, section, defaults, 1)
390 return ''.join(L)
391
392 def before_set(self, parser, section, option, value):
393 tmp_value = value.replace('%%', '') # escaped percent signs
394 tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
395 if '%' in tmp_value:
396 raise ValueError("invalid interpolation syntax in %r at "
397 "position %d" % (value, tmp_value.find('%')))
398 return value
399
400 def _interpolate_some(self, parser, option, accum, rest, section, map,
401 depth):
402 if depth > MAX_INTERPOLATION_DEPTH:
403 raise InterpolationDepthError(option, section, rest)
404 while rest:
405 p = rest.find("%")
406 if p < 0:
407 accum.append(rest)
408 return
409 if p > 0:
410 accum.append(rest[:p])
411 rest = rest[p:]
412 # p is no longer used
413 c = rest[1:2]
414 if c == "%":
415 accum.append("%")
416 rest = rest[2:]
417 elif c == "(":
418 m = self._KEYCRE.match(rest)
419 if m is None:
420 raise InterpolationSyntaxError(option, section,
421 "bad interpolation variable reference %r" % rest)
422 var = parser.optionxform(m.group(1))
423 rest = rest[m.end():]
424 try:
425 v = map[var]
426 except KeyError:
427 raise InterpolationMissingOptionError(
428 option, section, rest, var)
429 if "%" in v:
430 self._interpolate_some(parser, option, accum, v,
431 section, map, depth + 1)
432 else:
433 accum.append(v)
434 else:
435 raise InterpolationSyntaxError(
436 option, section,
437 "'%%' must be followed by '%%' or '(', "
438 "found: %r" % (rest,))
439
440
441class ExtendedInterpolation(Interpolation):
442 """Advanced variant of interpolation, supports the syntax used by
443 `zc.buildout'. Enables interpolation between sections."""
444
445 _KEYCRE = re.compile(r"\$\{([^}]+)\}")
446
447 def before_get(self, parser, section, option, value, defaults):
448 L = []
449 self._interpolate_some(parser, option, L, value, section, defaults, 1)
450 return ''.join(L)
451
452 def before_set(self, parser, section, option, value):
453 tmp_value = value.replace('$$', '') # escaped dollar signs
454 tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
455 if '$' in tmp_value:
456 raise ValueError("invalid interpolation syntax in %r at "
457 "position %d" % (value, tmp_value.find('%')))
458 return value
459
460 def _interpolate_some(self, parser, option, accum, rest, section, map,
461 depth):
462 if depth > MAX_INTERPOLATION_DEPTH:
463 raise InterpolationDepthError(option, section, rest)
464 while rest:
465 p = rest.find("$")
466 if p < 0:
467 accum.append(rest)
468 return
469 if p > 0:
470 accum.append(rest[:p])
471 rest = rest[p:]
472 # p is no longer used
473 c = rest[1:2]
474 if c == "$":
475 accum.append("$")
476 rest = rest[2:]
477 elif c == "{":
478 m = self._KEYCRE.match(rest)
479 if m is None:
480 raise InterpolationSyntaxError(option, section,
481 "bad interpolation variable reference %r" % rest)
482 path = parser.optionxform(m.group(1)).split(':')
483 rest = rest[m.end():]
484 sect = section
485 opt = option
486 try:
487 if len(path) == 1:
488 opt = path[0]
489 v = map[opt]
490 elif len(path) == 2:
491 sect = path[0]
492 opt = path[1]
493 v = parser.get(sect, opt, raw=True)
494 else:
495 raise InterpolationSyntaxError(
496 option, section,
497 "More than one ':' found: %r" % (rest,))
498 except KeyError:
499 raise InterpolationMissingOptionError(
500 option, section, rest, var)
501 if "$" in v:
502 self._interpolate_some(parser, opt, accum, v, sect,
503 dict(parser.items(sect, raw=True)),
504 depth + 1)
505 else:
506 accum.append(v)
507 else:
508 raise InterpolationSyntaxError(
509 option, section,
510 "'$' must be followed by '$' or '{', "
511 "found: %r" % (rest,))
512
513
Łukasz Langa7f64c8a2010-12-16 01:16:22 +0000514class LegacyInterpolation(Interpolation):
515 """Deprecated interpolation used in old versions of ConfigParser.
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000516 Use BasicInterpolation or ExtendedInterpolation instead."""
517
518 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
519
520 def before_get(self, parser, section, option, value, vars):
521 rawval = value
522 depth = MAX_INTERPOLATION_DEPTH
523 while depth: # Loop through this until it's done
524 depth -= 1
525 if value and "%(" in value:
526 replace = functools.partial(self._interpolation_replace,
527 parser=parser)
528 value = self._KEYCRE.sub(replace, value)
529 try:
530 value = value % vars
531 except KeyError as e:
532 raise InterpolationMissingOptionError(
533 option, section, rawval, e.args[0])
534 else:
535 break
536 if value and "%(" in value:
537 raise InterpolationDepthError(option, section, rawval)
538 return value
539
540 def before_set(self, parser, section, option, value):
541 return value
542
543 @staticmethod
544 def _interpolation_replace(match, parser):
545 s = match.group(1)
546 if s is None:
547 return match.group()
548 else:
549 return "%%(%s)s" % parser.optionxform(s)
550
551
Łukasz Langa26d513c2010-11-10 18:57:39 +0000552class RawConfigParser(MutableMapping):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000553 """ConfigParser that does not do interpolation."""
554
555 # Regular expressions for parsing section headers and options
556 _SECT_TMPL = r"""
557 \[ # [
558 (?P<header>[^]]+) # very permissive!
559 \] # ]
560 """
561 _OPT_TMPL = r"""
562 (?P<option>.*?) # very permissive!
563 \s*(?P<vi>{delim})\s* # any number of space/tab,
564 # followed by any of the
565 # allowed delimiters,
566 # followed by any space/tab
567 (?P<value>.*)$ # everything up to eol
568 """
569 _OPT_NV_TMPL = r"""
570 (?P<option>.*?) # very permissive!
571 \s*(?: # any number of space/tab,
572 (?P<vi>{delim})\s* # optionally followed by
573 # any of the allowed
574 # delimiters, followed by any
575 # space/tab
576 (?P<value>.*))?$ # everything up to eol
577 """
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000578 # Interpolation algorithm to be used if the user does not specify another
579 _DEFAULT_INTERPOLATION = Interpolation()
Georg Brandl96a60ae2010-07-28 13:13:46 +0000580 # Compiled regular expression for matching sections
581 SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
582 # Compiled regular expression for matching options with typical separators
583 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
584 # Compiled regular expression for matching options with optional values
585 # delimited using typical separators
586 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
587 # Compiled regular expression for matching leading whitespace in a line
588 NONSPACECRE = re.compile(r"\S")
Fred Drakecc645b92010-09-04 04:35:34 +0000589 # Possible boolean values in the configuration.
590 BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
591 '0': False, 'no': False, 'false': False, 'off': False}
Georg Brandl96a60ae2010-07-28 13:13:46 +0000592
Fred Drake03c44a32010-02-19 06:08:41 +0000593 def __init__(self, defaults=None, dict_type=_default_dict,
Fred Drakea4923622010-08-09 12:52:45 +0000594 allow_no_value=False, *, delimiters=('=', ':'),
Łukasz Langab25a7912010-12-17 01:32:29 +0000595 comment_prefixes=('#', ';'), inline_comment_prefixes=None,
596 strict=True, empty_lines_in_values=True,
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000597 default_section=DEFAULTSECT,
598 interpolation=_UNSET):
599
Thomas Wouters89f507f2006-12-13 04:49:30 +0000600 self._dict = dict_type
601 self._sections = self._dict()
602 self._defaults = self._dict()
Łukasz Langa49afa382010-11-11 19:53:23 +0000603 self._proxies = self._dict()
Łukasz Langac264c092010-11-20 16:15:37 +0000604 self._proxies[default_section] = SectionProxy(self, default_section)
David Goodger68a1abd2004-10-03 15:40:25 +0000605 if defaults:
606 for key, value in defaults.items():
607 self._defaults[self.optionxform(key)] = value
Georg Brandl96a60ae2010-07-28 13:13:46 +0000608 self._delimiters = tuple(delimiters)
609 if delimiters == ('=', ':'):
610 self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
611 else:
Fred Drakea4923622010-08-09 12:52:45 +0000612 d = "|".join(re.escape(d) for d in delimiters)
Georg Brandl96a60ae2010-07-28 13:13:46 +0000613 if allow_no_value:
Fred Drakea4923622010-08-09 12:52:45 +0000614 self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000615 re.VERBOSE)
616 else:
Fred Drakea4923622010-08-09 12:52:45 +0000617 self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
Georg Brandl96a60ae2010-07-28 13:13:46 +0000618 re.VERBOSE)
Łukasz Langab25a7912010-12-17 01:32:29 +0000619 self._comment_prefixes = tuple(comment_prefixes or ())
620 self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
Fred Drakea4923622010-08-09 12:52:45 +0000621 self._strict = strict
Łukasz Langa26d513c2010-11-10 18:57:39 +0000622 self._allow_no_value = allow_no_value
Georg Brandl96a60ae2010-07-28 13:13:46 +0000623 self._empty_lines_in_values = empty_lines_in_values
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000624 if interpolation is _UNSET:
625 self._interpolation = self._DEFAULT_INTERPOLATION
626 else:
627 self._interpolation = interpolation
628 self.default_section=default_section
Guido van Rossum3d209861997-12-09 16:10:31 +0000629
630 def defaults(self):
Fred Drakefce65572002-10-25 18:08:18 +0000631 return self._defaults
Guido van Rossum3d209861997-12-09 16:10:31 +0000632
633 def sections(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000634 """Return a list of section names, excluding [DEFAULT]"""
Fred Drakefce65572002-10-25 18:08:18 +0000635 # self._sections will never have [DEFAULT] in it
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000636 return list(self._sections.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000637
638 def add_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000639 """Create a new section in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000640
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000641 Raise DuplicateSectionError if a section by the specified name
Łukasz Langac264c092010-11-20 16:15:37 +0000642 already exists. Raise ValueError if name is DEFAULT.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000643 """
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000644 if section == self.default_section:
Łukasz Langa3a11e712010-12-03 22:15:19 +0000645 raise ValueError('Invalid section name: %r' % section)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000646
Fred Drakefce65572002-10-25 18:08:18 +0000647 if section in self._sections:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000648 raise DuplicateSectionError(section)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000649 self._sections[section] = self._dict()
Łukasz Langa49afa382010-11-11 19:53:23 +0000650 self._proxies[section] = SectionProxy(self, section)
Guido van Rossum3d209861997-12-09 16:10:31 +0000651
652 def has_section(self, section):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000653 """Indicate whether the named section is present in the configuration.
Guido van Rossum3d209861997-12-09 16:10:31 +0000654
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000655 The DEFAULT section is not acknowledged.
656 """
Fred Drakefce65572002-10-25 18:08:18 +0000657 return section in self._sections
Guido van Rossum3d209861997-12-09 16:10:31 +0000658
659 def options(self, section):
Guido van Rossuma5a24b71999-10-04 19:58:22 +0000660 """Return a list of option names for the given section name."""
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000661 try:
Fred Drakefce65572002-10-25 18:08:18 +0000662 opts = self._sections[section].copy()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000663 except KeyError:
664 raise NoSectionError(section)
Fred Drakefce65572002-10-25 18:08:18 +0000665 opts.update(self._defaults)
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000666 return list(opts.keys())
Guido van Rossum3d209861997-12-09 16:10:31 +0000667
Georg Brandl8dcaa732010-07-29 12:17:40 +0000668 def read(self, filenames, encoding=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000669 """Read and parse a filename or a list of filenames.
Tim Peters88869f92001-01-14 23:36:06 +0000670
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000671 Files that cannot be opened are silently ignored; this is
Barry Warsaw25394511999-10-12 16:12:48 +0000672 designed so that you can specify a list of potential
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000673 configuration file locations (e.g. current directory, user's
674 home directory, systemwide directory), and all existing
675 configuration files in the list will be read. A single
676 filename may also be given.
Fred Drake82903142004-05-18 04:24:02 +0000677
678 Return list of successfully read files.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000679 """
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000680 if isinstance(filenames, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000681 filenames = [filenames]
Fred Drake82903142004-05-18 04:24:02 +0000682 read_ok = []
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000683 for filename in filenames:
684 try:
Florent Xicluna42d54452010-09-22 22:35:38 +0000685 with open(filename, encoding=encoding) as fp:
686 self._read(fp, filename)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000687 except IOError:
688 continue
Fred Drake82903142004-05-18 04:24:02 +0000689 read_ok.append(filename)
690 return read_ok
Guido van Rossum3d209861997-12-09 16:10:31 +0000691
Fred Drakea4923622010-08-09 12:52:45 +0000692 def read_file(self, f, source=None):
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000693 """Like read() but the argument must be a file-like object.
694
Fred Drakea4923622010-08-09 12:52:45 +0000695 The `f' argument must have a `readline' method. Optional second
696 argument is the `source' specifying the name of the file being read. If
697 not given, it is taken from f.name. If `f' has no `name' attribute,
698 `<???>' is used.
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000699 """
Fred Drakea4923622010-08-09 12:52:45 +0000700 if source is None:
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000701 try:
Florent Xicluna42d54452010-09-22 22:35:38 +0000702 source = f.name
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000703 except AttributeError:
Fred Drakea4923622010-08-09 12:52:45 +0000704 source = '<???>'
705 self._read(f, source)
706
707 def read_string(self, string, source='<string>'):
708 """Read configuration from a given string."""
709 sfile = io.StringIO(string)
710 self.read_file(sfile, source)
711
712 def read_dict(self, dictionary, source='<dict>'):
713 """Read configuration from a dictionary.
714
715 Keys are section names, values are dictionaries with keys and values
716 that should be present in the section. If the used dictionary type
717 preserves order, sections and their keys will be added in order.
718
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +0000719 All types held in the dictionary are converted to strings during
720 reading, including section names, option names and keys.
721
Fred Drakea4923622010-08-09 12:52:45 +0000722 Optional second argument is the `source' specifying the name of the
723 dictionary being read.
724 """
725 elements_added = set()
726 for section, keys in dictionary.items():
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +0000727 section = str(section)
Fred Drakea4923622010-08-09 12:52:45 +0000728 try:
729 self.add_section(section)
Łukasz Langa26d513c2010-11-10 18:57:39 +0000730 except (DuplicateSectionError, ValueError):
Fred Drakea4923622010-08-09 12:52:45 +0000731 if self._strict and section in elements_added:
732 raise
733 elements_added.add(section)
734 for key, value in keys.items():
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +0000735 key = self.optionxform(str(key))
Fred Drakecc645b92010-09-04 04:35:34 +0000736 if value is not None:
737 value = str(value)
Fred Drakea4923622010-08-09 12:52:45 +0000738 if self._strict and (section, key) in elements_added:
739 raise DuplicateOptionError(section, key, source)
740 elements_added.add((section, key))
741 self.set(section, key, value)
742
743 def readfp(self, fp, filename=None):
744 """Deprecated, use read_file instead."""
745 warnings.warn(
746 "This method will be removed in future versions. "
747 "Use 'parser.read_file()' instead.",
Łukasz Langa49afa382010-11-11 19:53:23 +0000748 DeprecationWarning, stacklevel=2
Fred Drakea4923622010-08-09 12:52:45 +0000749 )
750 self.read_file(fp, source=filename)
Guido van Rossum6a8d84b1999-10-04 18:57:27 +0000751
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000752 def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
Fred Drakecc645b92010-09-04 04:35:34 +0000753 """Get an option value for a given section.
754
755 If `vars' is provided, it must be a dictionary. The option is looked up
756 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
Łukasz Langa26d513c2010-11-10 18:57:39 +0000757 If the key is not found and `fallback' is provided, it is used as
758 a fallback value. `None' can be provided as a `fallback' value.
759
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000760 If interpolation is enabled and the optional argument `raw' is False,
761 all interpolations are expanded in the return values.
762
763 Arguments `raw', `vars', and `fallback' are keyword only.
764
765 The section DEFAULT is special.
Fred Drakecc645b92010-09-04 04:35:34 +0000766 """
767 try:
768 d = self._unify_values(section, vars)
769 except NoSectionError:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000770 if fallback is _UNSET:
Fred Drakecc645b92010-09-04 04:35:34 +0000771 raise
Fred Drakefce65572002-10-25 18:08:18 +0000772 else:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000773 return fallback
Fred Drakecc645b92010-09-04 04:35:34 +0000774 option = self.optionxform(option)
775 try:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000776 value = d[option]
Fred Drakecc645b92010-09-04 04:35:34 +0000777 except KeyError:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000778 if fallback is _UNSET:
Fred Drakefce65572002-10-25 18:08:18 +0000779 raise NoOptionError(option, section)
Fred Drakecc645b92010-09-04 04:35:34 +0000780 else:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000781 return fallback
Fred Drake2a37f9f2000-09-27 22:43:54 +0000782
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000783 if raw or value is None:
784 return value
785 else:
786 return self._interpolation.before_get(self, section, option, value,
787 d)
Fred Drake2ca041f2002-09-27 15:49:56 +0000788
Łukasz Langa26d513c2010-11-10 18:57:39 +0000789 def _get(self, section, conv, option, **kwargs):
790 return conv(self.get(section, option, **kwargs))
Guido van Rossum3d209861997-12-09 16:10:31 +0000791
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000792 def getint(self, section, option, *, raw=False, vars=None,
793 fallback=_UNSET):
Fred Drakecc645b92010-09-04 04:35:34 +0000794 try:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000795 return self._get(section, int, option, raw=raw, vars=vars)
Fred Drakecc645b92010-09-04 04:35:34 +0000796 except (NoSectionError, NoOptionError):
Łukasz Langa26d513c2010-11-10 18:57:39 +0000797 if fallback is _UNSET:
Fred Drakecc645b92010-09-04 04:35:34 +0000798 raise
799 else:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000800 return fallback
Guido van Rossum3d209861997-12-09 16:10:31 +0000801
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000802 def getfloat(self, section, option, *, raw=False, vars=None,
803 fallback=_UNSET):
Fred Drakecc645b92010-09-04 04:35:34 +0000804 try:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000805 return self._get(section, float, option, raw=raw, vars=vars)
Fred Drakecc645b92010-09-04 04:35:34 +0000806 except (NoSectionError, NoOptionError):
Łukasz Langa26d513c2010-11-10 18:57:39 +0000807 if fallback is _UNSET:
Fred Drakecc645b92010-09-04 04:35:34 +0000808 raise
809 else:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000810 return fallback
Guido van Rossum3d209861997-12-09 16:10:31 +0000811
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000812 def getboolean(self, section, option, *, raw=False, vars=None,
813 fallback=_UNSET):
Fred Drakecc645b92010-09-04 04:35:34 +0000814 try:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000815 return self._get(section, self._convert_to_boolean, option,
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000816 raw=raw, vars=vars)
Fred Drakecc645b92010-09-04 04:35:34 +0000817 except (NoSectionError, NoOptionError):
Łukasz Langa26d513c2010-11-10 18:57:39 +0000818 if fallback is _UNSET:
Fred Drakecc645b92010-09-04 04:35:34 +0000819 raise
820 else:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000821 return fallback
Guido van Rossum3d209861997-12-09 16:10:31 +0000822
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000823 def items(self, section, raw=False, vars=None):
824 """Return a list of (name, value) tuples for each option in a section.
825
826 All % interpolations are expanded in the return values, based on the
827 defaults passed into the constructor, unless the optional argument
828 `raw' is true. Additional substitutions may be provided using the
829 `vars' argument, which must be a dictionary whose contents overrides
830 any pre-existing defaults.
831
832 The section DEFAULT is special.
833 """
834 d = self._defaults.copy()
835 try:
836 d.update(self._sections[section])
837 except KeyError:
838 if section != self.default_section:
839 raise NoSectionError(section)
840 # Update with the entry specific variables
841 if vars:
842 for key, value in vars.items():
843 d[self.optionxform(key)] = value
Łukasz Langa24bcc612010-12-04 11:48:11 +0000844 value_getter = lambda option: self._interpolation.before_get(self,
845 section, option, d[option], d)
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000846 if raw:
Łukasz Langa24bcc612010-12-04 11:48:11 +0000847 value_getter = lambda option: d[option]
848 return [(option, value_getter(option)) for option in d.keys()]
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000849
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000850 def optionxform(self, optionstr):
Eric S. Raymond9eb54d92001-02-09 05:19:09 +0000851 return optionstr.lower()
Guido van Rossum9e480ad1999-06-17 18:41:42 +0000852
Eric S. Raymond417c4892000-07-10 18:11:00 +0000853 def has_option(self, section, option):
854 """Check for the existence of a given option in a given section."""
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000855 if not section or section == self.default_section:
Fred Drakec2ff9052002-09-27 15:33:11 +0000856 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000857 return option in self._defaults
858 elif section not in self._sections:
Neal Norwitzf680cc42002-12-17 01:56:47 +0000859 return False
Eric S. Raymond417c4892000-07-10 18:11:00 +0000860 else:
Fred Drake3c823aa2001-02-26 21:55:34 +0000861 option = self.optionxform(option)
Fred Drakefce65572002-10-25 18:08:18 +0000862 return (option in self._sections[section]
863 or option in self._defaults)
Eric S. Raymond417c4892000-07-10 18:11:00 +0000864
Fred Drake03c44a32010-02-19 06:08:41 +0000865 def set(self, section, option, value=None):
Eric S. Raymond417c4892000-07-10 18:11:00 +0000866 """Set an option."""
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000867 if value:
868 value = self._interpolation.before_set(self, section, option,
869 value)
870 if not section or section == self.default_section:
Fred Drakefce65572002-10-25 18:08:18 +0000871 sectdict = self._defaults
Eric S. Raymond417c4892000-07-10 18:11:00 +0000872 else:
873 try:
Fred Drakefce65572002-10-25 18:08:18 +0000874 sectdict = self._sections[section]
Eric S. Raymond417c4892000-07-10 18:11:00 +0000875 except KeyError:
876 raise NoSectionError(section)
Fred Drakec2ff9052002-09-27 15:33:11 +0000877 sectdict[self.optionxform(option)] = value
Eric S. Raymond417c4892000-07-10 18:11:00 +0000878
Georg Brandl96a60ae2010-07-28 13:13:46 +0000879 def write(self, fp, space_around_delimiters=True):
880 """Write an .ini-format representation of the configuration state.
881
882 If `space_around_delimiters' is True (the default), delimiters
883 between keys and values are surrounded by spaces.
884 """
885 if space_around_delimiters:
886 d = " {} ".format(self._delimiters[0])
887 else:
888 d = self._delimiters[0]
Fred Drakefce65572002-10-25 18:08:18 +0000889 if self._defaults:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000890 self._write_section(fp, self.default_section,
Łukasz Langac264c092010-11-20 16:15:37 +0000891 self._defaults.items(), d)
Fred Drakefce65572002-10-25 18:08:18 +0000892 for section in self._sections:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000893 self._write_section(fp, section,
894 self._sections[section].items(), d)
895
896 def _write_section(self, fp, section_name, section_items, delimiter):
897 """Write a single section to the specified `fp'."""
898 fp.write("[{}]\n".format(section_name))
899 for key, value in section_items:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000900 value = self._interpolation.before_write(self, section_name, key,
901 value)
Łukasz Langa26d513c2010-11-10 18:57:39 +0000902 if value is not None or not self._allow_no_value:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000903 value = delimiter + str(value).replace('\n', '\n\t')
904 else:
905 value = ""
906 fp.write("{}{}\n".format(key, value))
907 fp.write("\n")
Eric S. Raymond417c4892000-07-10 18:11:00 +0000908
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000909 def remove_option(self, section, option):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000910 """Remove an option."""
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000911 if not section or section == self.default_section:
Fred Drakefce65572002-10-25 18:08:18 +0000912 sectdict = self._defaults
Eric S. Raymond649685a2000-07-14 14:28:22 +0000913 else:
914 try:
Fred Drakefce65572002-10-25 18:08:18 +0000915 sectdict = self._sections[section]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000916 except KeyError:
917 raise NoSectionError(section)
Fred Drake3c823aa2001-02-26 21:55:34 +0000918 option = self.optionxform(option)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000919 existed = option in sectdict
Eric S. Raymond649685a2000-07-14 14:28:22 +0000920 if existed:
Fred Drakeff4a23b2000-12-04 16:29:13 +0000921 del sectdict[option]
Eric S. Raymond649685a2000-07-14 14:28:22 +0000922 return existed
923
Thomas Woutersff4df6d2000-07-21 05:19:59 +0000924 def remove_section(self, section):
Eric S. Raymond649685a2000-07-14 14:28:22 +0000925 """Remove a file section."""
Fred Drakefce65572002-10-25 18:08:18 +0000926 existed = section in self._sections
Fred Drakec2ff9052002-09-27 15:33:11 +0000927 if existed:
Fred Drakefce65572002-10-25 18:08:18 +0000928 del self._sections[section]
Łukasz Langa49afa382010-11-11 19:53:23 +0000929 del self._proxies[section]
Fred Drakec2ff9052002-09-27 15:33:11 +0000930 return existed
Eric S. Raymond649685a2000-07-14 14:28:22 +0000931
Łukasz Langa26d513c2010-11-10 18:57:39 +0000932 def __getitem__(self, key):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000933 if key != self.default_section and not self.has_section(key):
Łukasz Langa26d513c2010-11-10 18:57:39 +0000934 raise KeyError(key)
Łukasz Langa49afa382010-11-11 19:53:23 +0000935 return self._proxies[key]
Łukasz Langa26d513c2010-11-10 18:57:39 +0000936
937 def __setitem__(self, key, value):
938 # To conform with the mapping protocol, overwrites existing values in
939 # the section.
940
941 # XXX this is not atomic if read_dict fails at any point. Then again,
942 # no update method in configparser is atomic in this implementation.
943 self.remove_section(key)
944 self.read_dict({key: value})
945
946 def __delitem__(self, key):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000947 if key == self.default_section:
Łukasz Langa26d513c2010-11-10 18:57:39 +0000948 raise ValueError("Cannot remove the default section.")
949 if not self.has_section(key):
950 raise KeyError(key)
951 self.remove_section(key)
952
953 def __contains__(self, key):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000954 return key == self.default_section or self.has_section(key)
Łukasz Langa26d513c2010-11-10 18:57:39 +0000955
956 def __len__(self):
957 return len(self._sections) + 1 # the default section
958
959 def __iter__(self):
960 # XXX does it break when underlying container state changed?
Łukasz Langab6a6f5f2010-12-03 16:28:00 +0000961 return itertools.chain((self.default_section,), self._sections.keys())
Łukasz Langa26d513c2010-11-10 18:57:39 +0000962
Fred Drakefce65572002-10-25 18:08:18 +0000963 def _read(self, fp, fpname):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000964 """Parse a sectioned configuration file.
Guido van Rossum3d209861997-12-09 16:10:31 +0000965
Fred Drakea4923622010-08-09 12:52:45 +0000966 Each section in a configuration file contains a header, indicated by
967 a name in square brackets (`[]'), plus key/value options, indicated by
Georg Brandl96a60ae2010-07-28 13:13:46 +0000968 `name' and `value' delimited with a specific substring (`=' or `:' by
969 default).
970
Fred Drakea4923622010-08-09 12:52:45 +0000971 Values can span multiple lines, as long as they are indented deeper
972 than the first line of the value. Depending on the parser's mode, blank
973 lines may be treated as parts of multiline values or ignored.
Georg Brandl96a60ae2010-07-28 13:13:46 +0000974
975 Configuration files may include comments, prefixed by specific
Fred Drakea4923622010-08-09 12:52:45 +0000976 characters (`#' and `;' by default). Comments may appear on their own
977 in an otherwise empty line or may be entered in lines holding values or
Georg Brandl96a60ae2010-07-28 13:13:46 +0000978 section names.
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000979 """
Fred Drakea4923622010-08-09 12:52:45 +0000980 elements_added = set()
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000981 cursect = None # None, or a dictionary
Fred Drakea4923622010-08-09 12:52:45 +0000982 sectname = None
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000983 optname = None
984 lineno = 0
Georg Brandl96a60ae2010-07-28 13:13:46 +0000985 indent_level = 0
Brian Curtin9a27b0c2010-07-26 00:27:10 +0000986 e = None # None, or an exception
Georg Brandl96a60ae2010-07-28 13:13:46 +0000987 for lineno, line in enumerate(fp, start=1):
Georg Brandl96a60ae2010-07-28 13:13:46 +0000988 comment_start = None
Georg Brandl96a60ae2010-07-28 13:13:46 +0000989 # strip inline comments
Łukasz Langab25a7912010-12-17 01:32:29 +0000990 for prefix in self._inline_comment_prefixes:
Georg Brandl96a60ae2010-07-28 13:13:46 +0000991 index = line.find(prefix)
992 if index == 0 or (index > 0 and line[index-1].isspace()):
993 comment_start = index
994 break
Łukasz Langab25a7912010-12-17 01:32:29 +0000995 # strip full line comments
996 for prefix in self._comment_prefixes:
997 if line.strip().startswith(prefix):
998 comment_start = 0
999 break
Georg Brandl96a60ae2010-07-28 13:13:46 +00001000 value = line[:comment_start].strip()
1001 if not value:
Georg Brandlf206d0e2010-07-29 11:56:20 +00001002 if self._empty_lines_in_values:
Georg Brandl96a60ae2010-07-28 13:13:46 +00001003 # add empty line to the value, but only if there was no
1004 # comment on the line
Georg Brandlf206d0e2010-07-29 11:56:20 +00001005 if (comment_start is None and
1006 cursect is not None and
1007 optname and
1008 cursect[optname] is not None):
1009 cursect[optname].append('') # newlines added at join
Georg Brandl96a60ae2010-07-28 13:13:46 +00001010 else:
1011 # empty line marks end of value
1012 indent_level = sys.maxsize
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001013 continue
1014 # continuation line?
Georg Brandl96a60ae2010-07-28 13:13:46 +00001015 first_nonspace = self.NONSPACECRE.search(line)
1016 cur_indent_level = first_nonspace.start() if first_nonspace else 0
1017 if (cursect is not None and optname and
1018 cur_indent_level > indent_level):
1019 cursect[optname].append(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001020 # a section header or option header?
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001021 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +00001022 indent_level = cur_indent_level
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001023 # is it a section header?
Georg Brandl96a60ae2010-07-28 13:13:46 +00001024 mo = self.SECTCRE.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001025 if mo:
1026 sectname = mo.group('header')
Fred Drakefce65572002-10-25 18:08:18 +00001027 if sectname in self._sections:
Fred Drakea4923622010-08-09 12:52:45 +00001028 if self._strict and sectname in elements_added:
1029 raise DuplicateSectionError(sectname, fpname,
1030 lineno)
Fred Drakefce65572002-10-25 18:08:18 +00001031 cursect = self._sections[sectname]
Fred Drakea4923622010-08-09 12:52:45 +00001032 elements_added.add(sectname)
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001033 elif sectname == self.default_section:
Fred Drakefce65572002-10-25 18:08:18 +00001034 cursect = self._defaults
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001035 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001036 cursect = self._dict()
Fred Drakefce65572002-10-25 18:08:18 +00001037 self._sections[sectname] = cursect
Łukasz Langa49afa382010-11-11 19:53:23 +00001038 self._proxies[sectname] = SectionProxy(self, sectname)
Fred Drakea4923622010-08-09 12:52:45 +00001039 elements_added.add(sectname)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001040 # So sections can't start with a continuation line
1041 optname = None
1042 # no section header in the file?
1043 elif cursect is None:
Walter Dörwald70a6b492004-02-12 17:35:32 +00001044 raise MissingSectionHeaderError(fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001045 # an option line?
1046 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +00001047 mo = self._optcre.match(value)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001048 if mo:
Fred Drakec517b9b2000-02-28 20:59:03 +00001049 optname, vi, optval = mo.group('option', 'vi', 'value')
Georg Brandl96a60ae2010-07-28 13:13:46 +00001050 if not optname:
1051 e = self._handle_error(e, fpname, lineno, line)
Brian Curtin9a27b0c2010-07-26 00:27:10 +00001052 optname = self.optionxform(optname.rstrip())
Fred Drakea4923622010-08-09 12:52:45 +00001053 if (self._strict and
1054 (sectname, optname) in elements_added):
1055 raise DuplicateOptionError(sectname, optname,
1056 fpname, lineno)
1057 elements_added.add((sectname, optname))
Fred Drake03c44a32010-02-19 06:08:41 +00001058 # This check is fine because the OPTCRE cannot
1059 # match if it would set optval to None
1060 if optval is not None:
Fred Drake03c44a32010-02-19 06:08:41 +00001061 optval = optval.strip()
Brian Curtin9a27b0c2010-07-26 00:27:10 +00001062 # allow empty values
1063 if optval == '""':
1064 optval = ''
1065 cursect[optname] = [optval]
1066 else:
1067 # valueless option handling
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001068 cursect[optname] = None
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001069 else:
Georg Brandl96a60ae2010-07-28 13:13:46 +00001070 # a non-fatal parsing error occurred. set up the
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001071 # exception but keep going. the exception will be
1072 # raised at the end of the file and will contain a
1073 # list of all bogus lines
Georg Brandl96a60ae2010-07-28 13:13:46 +00001074 e = self._handle_error(e, fpname, lineno, line)
Barry Warsawbfa3f6b1998-07-01 20:41:12 +00001075 # if any parsing errors occurred, raise an exception
1076 if e:
1077 raise e
Georg Brandl96a60ae2010-07-28 13:13:46 +00001078 self._join_multiline_values()
Fred Drakefce65572002-10-25 18:08:18 +00001079
Georg Brandl96a60ae2010-07-28 13:13:46 +00001080 def _join_multiline_values(self):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001081 defaults = self.default_section, self._defaults
1082 all_sections = itertools.chain((defaults,),
1083 self._sections.items())
1084 for section, options in all_sections:
Brian Curtin9a27b0c2010-07-26 00:27:10 +00001085 for name, val in options.items():
1086 if isinstance(val, list):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001087 val = '\n'.join(val).rstrip()
1088 options[name] = self._interpolation.before_read(self,
1089 section,
1090 name, val)
Fred Drakefce65572002-10-25 18:08:18 +00001091
Georg Brandl96a60ae2010-07-28 13:13:46 +00001092 def _handle_error(self, exc, fpname, lineno, line):
1093 if not exc:
1094 exc = ParsingError(fpname)
1095 exc.append(lineno, repr(line))
1096 return exc
1097
Fred Drakecc645b92010-09-04 04:35:34 +00001098 def _unify_values(self, section, vars):
1099 """Create a copy of the DEFAULTSECT with values from a specific
1100 `section' and the `vars' dictionary. If provided, values in `vars'
1101 take precendence.
Fred Drakefce65572002-10-25 18:08:18 +00001102 """
1103 d = self._defaults.copy()
1104 try:
1105 d.update(self._sections[section])
1106 except KeyError:
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001107 if section != self.default_section:
Fred Drakefce65572002-10-25 18:08:18 +00001108 raise NoSectionError(section)
1109 # Update with the entry specific variables
David Goodger68a1abd2004-10-03 15:40:25 +00001110 if vars:
1111 for key, value in vars.items():
Fred Drakecc645b92010-09-04 04:35:34 +00001112 if value is not None:
1113 value = str(value)
David Goodger68a1abd2004-10-03 15:40:25 +00001114 d[self.optionxform(key)] = value
Fred Drakecc645b92010-09-04 04:35:34 +00001115 return d
1116
1117 def _convert_to_boolean(self, value):
1118 """Return a boolean value translating from other types if necessary.
1119 """
1120 if value.lower() not in self.BOOLEAN_STATES:
1121 raise ValueError('Not a boolean: %s' % value)
1122 return self.BOOLEAN_STATES[value.lower()]
1123
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +00001124 def _validate_value_types(self, *, section="", option="", value=""):
Łukasz Langa26d513c2010-11-10 18:57:39 +00001125 """Raises a TypeError for non-string values.
1126
1127 The only legal non-string value if we allow valueless
1128 options is None, so we need to check if the value is a
1129 string if:
1130 - we do not allow valueless options, or
1131 - we allow valueless options but the value is not None
1132
1133 For compatibility reasons this method is not used in classic set()
Łukasz Langa7f64c8a2010-12-16 01:16:22 +00001134 for RawConfigParsers. It is invoked in every case for mapping protocol
1135 access and in ConfigParser.set().
Łukasz Langa26d513c2010-11-10 18:57:39 +00001136 """
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +00001137 if not isinstance(section, str):
1138 raise TypeError("section names must be strings")
1139 if not isinstance(option, str):
1140 raise TypeError("option keys must be strings")
Łukasz Langa26d513c2010-11-10 18:57:39 +00001141 if not self._allow_no_value or value:
1142 if not isinstance(value, str):
1143 raise TypeError("option values must be strings")
1144
1145
Fred Drakecc645b92010-09-04 04:35:34 +00001146class ConfigParser(RawConfigParser):
1147 """ConfigParser implementing interpolation."""
1148
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001149 _DEFAULT_INTERPOLATION = BasicInterpolation()
David Goodger1cbf2062004-10-03 15:55:09 +00001150
Fred Drake03c44a32010-02-19 06:08:41 +00001151 def set(self, section, option, value=None):
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001152 """Set an option. Extends RawConfigParser.set by validating type and
1153 interpolation syntax on the value."""
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +00001154 self._validate_value_types(option=option, value=value)
Łukasz Langab6a6f5f2010-12-03 16:28:00 +00001155 super().set(section, option, value)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001156
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +00001157 def add_section(self, section):
1158 """Create a new section in the configuration. Extends
1159 RawConfigParser.add_section by validating if the section name is
1160 a string."""
1161 self._validate_value_types(section=section)
1162 super().add_section(section)
1163
Łukasz Langa26d513c2010-11-10 18:57:39 +00001164
Łukasz Langa7f64c8a2010-12-16 01:16:22 +00001165class SafeConfigParser(ConfigParser):
1166 """ConfigParser alias for backwards compatibility purposes."""
1167
1168 def __init__(self, *args, **kwargs):
1169 super().__init__(*args, **kwargs)
1170 warnings.warn(
1171 "The SafeConfigParser class has been renamed to ConfigParser "
1172 "in Python 3.2. This alias will be removed in future versions."
1173 " Use ConfigParser directly instead.",
1174 DeprecationWarning, stacklevel=2
1175 )
1176
1177
Łukasz Langa26d513c2010-11-10 18:57:39 +00001178class SectionProxy(MutableMapping):
1179 """A proxy for a single section from a parser."""
1180
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001181 def __init__(self, parser, name):
1182 """Creates a view on a section of the specified `name` in `parser`."""
Łukasz Langa26d513c2010-11-10 18:57:39 +00001183 self._parser = parser
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001184 self._name = name
Łukasz Langa26d513c2010-11-10 18:57:39 +00001185
1186 def __repr__(self):
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001187 return '<Section: {}>'.format(self._name)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001188
1189 def __getitem__(self, key):
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001190 if not self._parser.has_option(self._name, key):
Łukasz Langa26d513c2010-11-10 18:57:39 +00001191 raise KeyError(key)
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001192 return self._parser.get(self._name, key)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001193
1194 def __setitem__(self, key, value):
Łukasz Langa2cf9ddb2010-12-04 12:46:01 +00001195 self._parser._validate_value_types(option=key, value=value)
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001196 return self._parser.set(self._name, key, value)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001197
1198 def __delitem__(self, key):
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001199 if not self._parser.has_option(self._name, key):
Łukasz Langa26d513c2010-11-10 18:57:39 +00001200 raise KeyError(key)
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001201 return self._parser.remove_option(self._name, key)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001202
1203 def __contains__(self, key):
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001204 return self._parser.has_option(self._name, key)
Łukasz Langa26d513c2010-11-10 18:57:39 +00001205
1206 def __len__(self):
Łukasz Langa26d513c2010-11-10 18:57:39 +00001207 # XXX weak performance
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001208 return len(self._parser.options(self._name))
Łukasz Langa26d513c2010-11-10 18:57:39 +00001209
1210 def __iter__(self):
Łukasz Langa26d513c2010-11-10 18:57:39 +00001211 # XXX weak performance
1212 # XXX does not break when underlying container state changed
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001213 return self._parser.options(self._name).__iter__()
1214
Łukasz Langa2f0fd0f2010-12-04 17:48:18 +00001215 def get(self, option, fallback=None, *, raw=False, vars=None):
1216 return self._parser.get(self._name, option, raw=raw, vars=vars,
1217 fallback=fallback)
1218
1219 def getint(self, option, fallback=None, *, raw=False, vars=None):
1220 return self._parser.getint(self._name, option, raw=raw, vars=vars,
1221 fallback=fallback)
1222
1223 def getfloat(self, option, fallback=None, *, raw=False, vars=None):
1224 return self._parser.getfloat(self._name, option, raw=raw, vars=vars,
1225 fallback=fallback)
1226
1227 def getboolean(self, option, fallback=None, *, raw=False, vars=None):
1228 return self._parser.getboolean(self._name, option, raw=raw, vars=vars,
1229 fallback=fallback)
1230
Łukasz Langaa73dc9d2010-11-21 13:56:42 +00001231 @property
1232 def parser(self):
1233 # The parser object of the proxy is read-only.
1234 return self._parser
1235
1236 @property
1237 def name(self):
1238 # The name of the section on a proxy is read-only.
1239 return self._name