ยปCore Development>Code coverage>Lib/ConfigParser.py

Python code coverage for Lib/ConfigParser.py

#countcontent
1n/a"""Configuration file parser.
2n/a
3n/aA setup file consists of sections, lead by a "[section]" header,
4n/aand followed by "name: value" entries, with continuations and such in
5n/athe style of RFC 822.
6n/a
7n/aThe option values can contain format strings which refer to other values in
8n/athe same section, or values in a special [DEFAULT] section.
9n/a
10n/aFor example:
11n/a
12n/a something: %(dir)s/whatever
13n/a
14n/awould resolve the "%(dir)s" to the value of dir. All reference
15n/aexpansions are done late, on demand.
16n/a
17n/aIntrinsic defaults can be specified by passing them into the
18n/aConfigParser constructor as a dictionary.
19n/a
20n/aclass:
21n/a
22n/aConfigParser -- responsible for parsing a list of
23n/a configuration files, and managing the parsed database.
24n/a
25n/a methods:
26n/a
27n/a __init__(defaults=None)
28n/a create the parser and specify a dictionary of intrinsic defaults. The
29n/a keys must be strings, the values must be appropriate for %()s string
30n/a interpolation. Note that `__name__' is always an intrinsic default;
31n/a its value is the section's name.
32n/a
33n/a sections()
34n/a return all the configuration section names, sans DEFAULT
35n/a
36n/a has_section(section)
37n/a return whether the given section exists
38n/a
39n/a has_option(section, option)
40n/a return whether the given option exists in the given section
41n/a
42n/a options(section)
43n/a return list of configuration options for the named section
44n/a
45n/a read(filenames)
46n/a read and parse the list of named configuration files, given by
47n/a name. A single filename is also allowed. Non-existing files
48n/a are ignored. Return list of successfully read files.
49n/a
50n/a readfp(fp, filename=None)
51n/a read and parse one configuration file, given as a file object.
52n/a The filename defaults to fp.name; it is only used in error
53n/a messages (if fp has no `name' attribute, the string `<???>' is used).
54n/a
55n/a get(section, option, raw=False, vars=None)
56n/a return a string value for the named option. All % interpolations are
57n/a expanded in the return values, based on the defaults passed into the
58n/a constructor and the DEFAULT section. Additional substitutions may be
59n/a provided using the `vars' argument, which must be a dictionary whose
60n/a contents override any pre-existing defaults.
61n/a
62n/a getint(section, options)
63n/a like get(), but convert value to an integer
64n/a
65n/a getfloat(section, options)
66n/a like get(), but convert value to a float
67n/a
68n/a getboolean(section, options)
69n/a like get(), but convert value to a boolean (currently case
70n/a insensitively defined as 0, false, no, off for False, and 1, true,
71n/a yes, on for True). Returns False or True.
72n/a
73n/a items(section, raw=False, vars=None)
74n/a return a list of tuples with (name, value) for each option
75n/a in the section.
76n/a
77n/a remove_section(section)
78n/a remove the given file section and all its options
79n/a
80n/a remove_option(section, option)
81n/a remove the given option from the given section
82n/a
83n/a set(section, option, value)
84n/a set the given option
85n/a
86n/a write(fp)
87n/a write the configuration state in .ini format
881"""
89n/a
901try:
911 from collections import OrderedDict as _default_dict
920except ImportError:
93n/a # fallback for setup.py which hasn't yet built _collections
940 _default_dict = dict
95n/a
961import re
97n/a
981__all__ = ["NoSectionError", "DuplicateSectionError", "NoOptionError",
991 "InterpolationError", "InterpolationDepthError",
1001 "InterpolationSyntaxError", "ParsingError",
1011 "MissingSectionHeaderError",
1021 "ConfigParser", "SafeConfigParser", "RawConfigParser",
1031 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
104n/a
1051DEFAULTSECT = "DEFAULT"
106n/a
1071MAX_INTERPOLATION_DEPTH = 10
108n/a
109n/a
110n/a
111n/a# exception classes
1122class Error(Exception):
1131 """Base class for ConfigParser exceptions."""
114n/a
1151 def _get_message(self):
116n/a """Getter for 'message'; needed only to override deprecation in
117n/a BaseException."""
11820 return self.__message
119n/a
1201 def _set_message(self, value):
121n/a """Setter for 'message'; needed only to override deprecation in
122n/a BaseException."""
12381 self.__message = value
124n/a
125n/a # BaseException.message has been deprecated since Python 2.6. To prevent
126n/a # DeprecationWarning from popping up over this pre-existing attribute, use
127n/a # a new property that takes lookup precedence.
1281 message = property(_get_message, _set_message)
129n/a
1301 def __init__(self, msg=''):
13161 self.message = msg
13261 Exception.__init__(self, msg)
133n/a
1341 def __repr__(self):
1350 return self.message
136n/a
1371 __str__ = __repr__
138n/a
1392class NoSectionError(Error):
1401 """Raised when no section matches a requested option."""
141n/a
1421 def __init__(self, section):
14320 Error.__init__(self, 'No section: %r' % (section,))
14420 self.section = section
145n/a
1462class DuplicateSectionError(Error):
1471 """Raised when a section is multiply-created."""
148n/a
1491 def __init__(self, section):
1505 Error.__init__(self, "Section %r already exists" % section)
1515 self.section = section
152n/a
1532class NoOptionError(Error):
1541 """A requested option was not found."""
155n/a
1561 def __init__(self, option, section):
1575 Error.__init__(self, "No option %r in section: %r" %
1585 (option, section))
1595 self.option = option
1605 self.section = section
161n/a
1622class InterpolationError(Error):
1631 """Base class for interpolation-related exceptions."""
164n/a
1651 def __init__(self, option, section, msg):
1666 Error.__init__(self, msg)
1676 self.option = option
1686 self.section = section
169n/a
1702class InterpolationMissingOptionError(InterpolationError):
1711 """A string substitution required a setting which was not available."""
172n/a
1731 def __init__(self, option, section, rawval, reference):
1743 msg = ("Bad value substitution:\n"
175n/a "\tsection: [%s]\n"
176n/a "\toption : %s\n"
177n/a "\tkey : %s\n"
178n/a "\trawval : %s\n"
1793 % (section, option, reference, rawval))
1803 InterpolationError.__init__(self, option, section, msg)
1813 self.reference = reference
182n/a
1832class InterpolationSyntaxError(InterpolationError):
184n/a """Raised when the source text into which substitutions are made
1851 does not conform to the required syntax."""
186n/a
1872class InterpolationDepthError(InterpolationError):
1881 """Raised when substitutions are nested too deeply."""
189n/a
1901 def __init__(self, option, section, rawval):
1913 msg = ("Value interpolation too deeply recursive:\n"
192n/a "\tsection: [%s]\n"
193n/a "\toption : %s\n"
194n/a "\trawval : %s\n"
1953 % (section, option, rawval))
1963 InterpolationError.__init__(self, option, section, msg)
197n/a
1982class ParsingError(Error):
1991 """Raised when a configuration file does not follow legal syntax."""
200n/a
2011 def __init__(self, filename):
20220 Error.__init__(self, 'File contains parsing errors: %s' % filename)
20320 self.filename = filename
20420 self.errors = []
205n/a
2061 def append(self, lineno, line):
20720 self.errors.append((lineno, line))
20820 self.message += '\n\t[line %2d]: %s' % (lineno, line)
209n/a
2102class MissingSectionHeaderError(ParsingError):
2111 """Raised when a key-value pair is found before any section header."""
212n/a
2131 def __init__(self, filename, lineno, line):
2145 Error.__init__(
2155 self,
2165 'File contains no section headers.\nfile: %s, line: %d\n%r' %
2175 (filename, lineno, line))
2185 self.filename = filename
2195 self.lineno = lineno
2205 self.line = line
221n/a
222n/a
2232class RawConfigParser:
2241 def __init__(self, defaults=None, dict_type=_default_dict,
2251 allow_no_value=False):
226149 self._dict = dict_type
227149 self._sections = self._dict()
228149 self._defaults = self._dict()
229149 if allow_no_value:
23024 self._optcre = self.OPTCRE_NV
231n/a else:
232125 self._optcre = self.OPTCRE
233149 if defaults:
23456 for key, value in defaults.items():
23528 self._defaults[self.optionxform(key)] = value
236n/a
2371 def defaults(self):
2380 return self._defaults
239n/a
2401 def sections(self):
241n/a """Return a list of section names, excluding [DEFAULT]"""
242n/a # self._sections will never have [DEFAULT] in it
24338 return self._sections.keys()
244n/a
2451 def add_section(self, section):
246n/a """Create a new section in the configuration.
247n/a
248n/a Raise DuplicateSectionError if a section by the specified name
249n/a already exists. Raise ValueError if name is DEFAULT or any of it's
250n/a case-insensitive variants.
251n/a """
25232 if section.lower() == "default":
2534 raise ValueError, 'Invalid section name: %s' % section
254n/a
25528 if section in self._sections:
2565 raise DuplicateSectionError(section)
25723 self._sections[section] = self._dict()
258n/a
2591 def has_section(self, section):
260n/a """Indicate whether the named section is present in the configuration.
261n/a
262n/a The DEFAULT section is not acknowledged.
263n/a """
2645 return section in self._sections
265n/a
2661 def options(self, section):
267n/a """Return a list of option names for the given section name."""
26876 try:
26976 opts = self._sections[section].copy()
2705 except KeyError:
2715 raise NoSectionError(section)
27271 opts.update(self._defaults)
27371 if '__name__' in opts:
27451 del opts['__name__']
27571 return opts.keys()
276n/a
2771 def read(self, filenames):
278n/a """Read and parse a filename or a list of filenames.
279n/a
280n/a Files that cannot be opened are silently ignored; this is
281n/a designed so that you can specify a list of potential
282n/a configuration file locations (e.g. current directory, user's
283n/a home directory, systemwide directory), and all existing
284n/a configuration files in the list will be read. A single
285n/a filename may also be given.
286n/a
287n/a Return list of successfully read files.
288n/a """
28943 if isinstance(filenames, basestring):
29028 filenames = [filenames]
29143 read_ok = []
29286 for filename in filenames:
29343 try:
29443 fp = open(filename)
29510 except IOError:
29610 continue
29733 self._read(fp, filename)
29833 fp.close()
29933 read_ok.append(filename)
30043 return read_ok
301n/a
3021 def readfp(self, fp, filename=None):
303n/a """Like read() but the argument must be a file-like object.
304n/a
305n/a The `fp' argument must have a `readline' method. Optional
306n/a second argument is the `filename', which if not given, is
307n/a taken from fp.name. If fp has no `name' attribute, `<???>' is
308n/a used.
309n/a
310n/a """
31183 if filename is None:
31283 try:
31383 filename = fp.name
31483 except AttributeError:
31583 filename = '<???>'
31683 self._read(fp, filename)
317n/a
3181 def get(self, section, option):
31968 opt = self.optionxform(option)
32068 if section not in self._sections:
3216 if section != DEFAULTSECT:
3222 raise NoSectionError(section)
3234 if opt in self._defaults:
3244 return self._defaults[opt]
325n/a else:
3260 raise NoOptionError(option, section)
32762 elif opt in self._sections[section]:
32858 return self._sections[section][opt]
3294 elif opt in self._defaults:
3302 return self._defaults[opt]
331n/a else:
3322 raise NoOptionError(option, section)
333n/a
3341 def items(self, section):
3352 try:
3362 d2 = self._sections[section]
3370 except KeyError:
3380 if section != DEFAULTSECT:
3390 raise NoSectionError(section)
3400 d2 = self._dict()
3412 d = self._defaults.copy()
3422 d.update(d2)
3432 if "__name__" in d:
3442 del d["__name__"]
3452 return d.items()
346n/a
3471 def _get(self, section, conv, option):
3484 return conv(self.get(section, option))
349n/a
3501 def getint(self, section, option):
3514 return self._get(section, int, option)
352n/a
3531 def getfloat(self, section, option):
3540 return self._get(section, float, option)
355n/a
3561 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
3571 '0': False, 'no': False, 'false': False, 'off': False}
358n/a
3591 def getboolean(self, section, option):
36060 v = self.get(section, option)
36160 if v.lower() not in self._boolean_states:
36220 raise ValueError, 'Not a boolean: %s' % v
36340 return self._boolean_states[v.lower()]
364n/a
3651 def optionxform(self, optionstr):
3661146 return optionstr.lower()
367n/a
3681 def has_option(self, section, option):
369n/a """Check for the existence of a given option in a given section."""
37069 if not section or section == DEFAULTSECT:
3710 option = self.optionxform(option)
3720 return option in self._defaults
37369 elif section not in self._sections:
3740 return False
375n/a else:
37669 option = self.optionxform(option)
37769 return (option in self._sections[section]
37837 or option in self._defaults)
379n/a
3801 def set(self, section, option, value=None):
381n/a """Set an option."""
38257 if not section or section == DEFAULTSECT:
3830 sectdict = self._defaults
384n/a else:
38557 try:
38657 sectdict = self._sections[section]
3875 except KeyError:
3885 raise NoSectionError(section)
38952 sectdict[self.optionxform(option)] = value
390n/a
3911 def write(self, fp):
392n/a """Write an .ini-format representation of the configuration state."""
3936 if self._defaults:
3945 fp.write("[%s]\n" % DEFAULTSECT)
39510 for (key, value) in self._defaults.items():
3965 fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
3975 fp.write("\n")
39814 for section in self._sections:
3998 fp.write("[%s]\n" % section)
40027 for (key, value) in self._sections[section].items():
40119 if key != "__name__":
40211 if value is None:
4031 fp.write("%s\n" % (key))
404n/a else:
40510 fp.write("%s = %s\n" %
40610 (key, str(value).replace('\n', '\n\t')))
4078 fp.write("\n")
408n/a
4091 def remove_option(self, section, option):
410n/a """Remove an option."""
41120 if not section or section == DEFAULTSECT:
4120 sectdict = self._defaults
413n/a else:
41420 try:
41520 sectdict = self._sections[section]
4165 except KeyError:
4175 raise NoSectionError(section)
41815 option = self.optionxform(option)
41915 existed = option in sectdict
42015 if existed:
42110 del sectdict[option]
42215 return existed
423n/a
4241 def remove_section(self, section):
425n/a """Remove a file section."""
4260 existed = section in self._sections
4270 if existed:
4280 del self._sections[section]
4290 return existed
430n/a
431n/a #
432n/a # Regular expressions for parsing section headers and options.
433n/a #
4341 SECTCRE = re.compile(
4351 r'\[' # [
436n/a r'(?P<header>[^]]+)' # very permissive!
437n/a r'\]' # ]
438n/a )
4391 OPTCRE = re.compile(
4401 r'(?P<option>[^:=\s][^:=]*)' # very permissive!
441n/a r'\s*(?P<vi>[:=])\s*' # any number of space/tab,
442n/a # followed by separator
443n/a # (either : or =), followed
444n/a # by any # space/tab
445n/a r'(?P<value>.*)$' # everything up to eol
446n/a )
4471 OPTCRE_NV = re.compile(
4481 r'(?P<option>[^:=\s][^:=]*)' # very permissive!
449n/a r'\s*(?:' # any number of space/tab,
450n/a r'(?P<vi>[:=])\s*' # optionally followed by
451n/a # separator (either : or
452n/a # =), followed by any #
453n/a # space/tab
454n/a r'(?P<value>.*))?$' # everything up to eol
455n/a )
456n/a
4571 def _read(self, fp, fpname):
458n/a """Parse a sectioned setup file.
459n/a
460n/a The sections in setup file contains a title line at the top,
461n/a indicated by a name in square brackets (`[]'), plus key/value
462n/a options lines, indicated by `name: value' format lines.
463n/a Continuations are represented by an embedded newline then
464n/a leading whitespace. Blank lines, lines beginning with a '#',
465n/a and just about everything else are ignored.
466n/a """
467116 cursect = None # None, or a dictionary
468116 optname = None
469116 lineno = 0
470116 e = None # None, or an exception
4711042 while True:
4721042 line = fp.readline()
4731042 if not line:
474111 break
475931 lineno = lineno + 1
476n/a # comment or blank line?
477931 if line.strip() == '' or line[0] in '#;':
4780 continue
479830 if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
480n/a # no leading whitespace
4810 continue
482n/a # continuation line?
483830 if line[0].isspace() and cursect is not None and optname:
48436 value = line.strip()
48536 if value:
48636 cursect[optname] = "%s\n%s" % (cursect[optname], value)
487n/a # a section header or option header?
488n/a else:
489n/a # is it a section header?
490794 mo = self.SECTCRE.match(line)
491794 if mo:
492233 sectname = mo.group('header')
493233 if sectname in self._sections:
49415 cursect = self._sections[sectname]
495218 elif sectname == DEFAULTSECT:
4965 cursect = self._defaults
497n/a else:
498213 cursect = self._dict()
499213 cursect['__name__'] = sectname
500213 self._sections[sectname] = cursect
501n/a # So sections can't start with a continuation line
502233 optname = None
503n/a # no section header in the file?
504561 elif cursect is None:
5055 raise MissingSectionHeaderError(fpname, lineno, line)
506n/a # an option line?
507n/a else:
508556 mo = self._optcre.match(line)
509556 if mo:
510536 optname, vi, optval = mo.group('option', 'vi', 'value')
511n/a # This check is fine because the OPTCRE cannot
512n/a # match if it would set optval to None
513536 if optval is not None:
514534 if vi in ('=', ':') and ';' in optval:
515n/a # ';' is a comment delimiter only if it follows
516n/a # a spacing character
5175 pos = optval.find(';')
5185 if pos != -1 and optval[pos-1].isspace():
5195 optval = optval[:pos]
520534 optval = optval.strip()
521n/a # allow empty values
522536 if optval == '""':
5230 optval = ''
524536 optname = self.optionxform(optname.rstrip())
525536 cursect[optname] = optval
526n/a else:
527n/a # a non-fatal parsing error occurred. set up the
528n/a # exception but keep going. the exception will be
529n/a # raised at the end of the file and will contain a
530n/a # list of all bogus lines
53120 if not e:
53220 e = ParsingError(fpname)
53320 e.append(lineno, repr(line))
534n/a # if any parsing errors occurred, raise an exception
535111 if e:
53620 raise e
537n/a
538n/a
5392class ConfigParser(RawConfigParser):
540n/a
5411 def get(self, section, option, raw=False, vars=None):
542n/a """Get an option value for a given section.
543n/a
544n/a All % interpolations are expanded in the return values, based on the
545n/a defaults passed into the constructor, unless the optional argument
546n/a `raw' is true. Additional substitutions may be provided using the
547n/a `vars' argument, which must be a dictionary whose contents overrides
548n/a any pre-existing defaults.
549n/a
550n/a The section DEFAULT is special.
551n/a """
552269 d = self._defaults.copy()
553269 try:
554269 d.update(self._sections[section])
5559 except KeyError:
5569 if section != DEFAULTSECT:
5573 raise NoSectionError(section)
558n/a # Update with the entry specific variables
559266 if vars:
5600 for key, value in vars.items():
5610 d[self.optionxform(key)] = value
562266 option = self.optionxform(option)
563266 try:
564266 value = d[option]
5653 except KeyError:
5663 raise NoOptionError(option, section)
567n/a
568263 if raw or value is None:
56923 return value
570n/a else:
571240 return self._interpolate(section, option, value, d)
572n/a
5731 def items(self, section, raw=False, vars=None):
574n/a """Return a list of tuples with (name, value) for each option
575n/a in the section.
576n/a
577n/a All % interpolations are expanded in the return values, based on the
578n/a defaults passed into the constructor, unless the optional argument
579n/a `raw' is true. Additional substitutions may be provided using the
580n/a `vars' argument, which must be a dictionary whose contents overrides
581n/a any pre-existing defaults.
582n/a
583n/a The section DEFAULT is special.
584n/a """
5853 d = self._defaults.copy()
5863 try:
5873 d.update(self._sections[section])
5880 except KeyError:
5890 if section != DEFAULTSECT:
5900 raise NoSectionError(section)
591n/a # Update with the entry specific variables
5923 if vars:
5930 for key, value in vars.items():
5940 d[self.optionxform(key)] = value
5953 options = d.keys()
5963 if "__name__" in options:
5973 options.remove("__name__")
5983 if raw:
5990 return [(option, d[option])
6000 for option in options]
601n/a else:
6023 return [(option, self._interpolate(section, option, d[option], d))
60318 for option in options]
604n/a
6051 def _interpolate(self, section, option, rawval, vars):
606n/a # do the string interpolation
607175 value = rawval
608175 depth = MAX_INTERPOLATION_DEPTH
609210 while depth: # Loop through this until it's done
610208 depth -= 1
611208 if value and "%(" in value:
61239 value = self._KEYCRE.sub(self._interpolation_replace, value)
61337 try:
61437 value = value % vars
6152 except KeyError, e:
6161 raise InterpolationMissingOptionError(
6171 option, section, rawval, e.args[0])
618n/a else:
619168 break
620170 if value and "%(" in value:
6211 raise InterpolationDepthError(option, section, rawval)
622169 return value
623n/a
6241 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
625n/a
6261 def _interpolation_replace(self, match):
6271344 s = match.group(1)
6281344 if s is None:
6291308 return match.group()
630n/a else:
63136 return "%%(%s)s" % self.optionxform(s)
632n/a
633n/a
6342class SafeConfigParser(ConfigParser):
635n/a
6361 def _interpolate(self, section, option, rawval, vars):
637n/a # do the string interpolation
63880 L = []
63980 self._interpolate_some(option, L, rawval, section, vars, 1)
64076 return ''.join(L)
641n/a
6421 _interpvar_re = re.compile(r"%\(([^)]+)\)s")
643n/a
6441 def _interpolate_some(self, option, accum, rest, section, map, depth):
645136 if depth > MAX_INTERPOLATION_DEPTH:
6462 raise InterpolationDepthError(option, section, rest)
647194 while rest:
648158 p = rest.find("%")
649158 if p < 0:
65076 accum.append(rest)
65176 return
65282 if p > 0:
65320 accum.append(rest[:p])
65420 rest = rest[p:]
655n/a # p is no longer used
65682 c = rest[1:2]
65782 if c == "%":
6586 accum.append("%")
6596 rest = rest[2:]
66076 elif c == "(":
66176 m = self._interpvar_re.match(rest)
66276 if m is None:
6630 raise InterpolationSyntaxError(option, section,
6640 "bad interpolation variable reference %r" % rest)
66576 var = self.optionxform(m.group(1))
66676 rest = rest[m.end():]
66776 try:
66876 v = map[var]
6692 except KeyError:
6702 raise InterpolationMissingOptionError(
6712 option, section, rest, var)
67274 if "%" in v:
67356 self._interpolate_some(option, accum, v,
67456 section, map, depth + 1)
675n/a else:
67618 accum.append(v)
677n/a else:
6780 raise InterpolationSyntaxError(
6790 option, section,
6800 "'%%' must be followed by '%%' or '(', found: %r" % (rest,))
681n/a
6821 def set(self, section, option, value=None):
683n/a """Set an option. Extend ConfigParser.set: check for string values."""
684n/a # The only legal non-string value if we allow valueless
685n/a # options is None, so we need to check if the value is a
686n/a # string if:
687n/a # - we do not allow valueless options, or
688n/a # - we allow valueless options but the value is not None
68938 if self._optcre is self.OPTCRE or value:
69038 if not isinstance(value, basestring):
69112 raise TypeError("option values must be strings")
692n/a # check for bad percent signs:
693n/a # first, replace all "good" interpolations
69426 tmp_value = value.replace('%%', '')
69526 tmp_value = self._interpvar_re.sub('', tmp_value)
696n/a # then, check if there's a lone percent sign left
69726 percent_index = tmp_value.find('%')
69826 if percent_index != -1:
6996 raise ValueError("invalid interpolation syntax in %r at "
7006 "position %d" % (value, percent_index))
70120 ConfigParser.set(self, section, option, value)