1997-12-09 12:10:31 -04:00
|
|
|
"""Configuration file parser.
|
|
|
|
|
|
|
|
A setup file consists of sections, lead by a "[section]" header,
|
|
|
|
and followed by "name: value" entries, with continuations and such in
|
1998-07-01 17:41:12 -03:00
|
|
|
the style of RFC 822.
|
|
|
|
|
|
|
|
The option values can contain format strings which refer to other values in
|
|
|
|
the same section, or values in a special [DEFAULT] section.
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
something: %(dir)s/whatever
|
|
|
|
|
|
|
|
would resolve the "%(dir)s" to the value of dir. All reference
|
|
|
|
expansions are done late, on demand.
|
|
|
|
|
|
|
|
Intrinsic defaults can be specified by passing them into the
|
|
|
|
ConfigParser constructor as a dictionary.
|
|
|
|
|
|
|
|
class:
|
|
|
|
|
2003-10-20 11:01:56 -03:00
|
|
|
ConfigParser -- responsible for parsing a list of
|
1997-12-09 12:10:31 -04:00
|
|
|
configuration files, and managing the parsed database.
|
|
|
|
|
|
|
|
methods:
|
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
__init__(defaults=None)
|
|
|
|
create the parser and specify a dictionary of intrinsic defaults. The
|
|
|
|
keys must be strings, the values must be appropriate for %()s string
|
|
|
|
interpolation. Note that `__name__' is always an intrinsic default;
|
2005-07-22 18:49:32 -03:00
|
|
|
its value is the section's name.
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
sections()
|
|
|
|
return all the configuration section names, sans DEFAULT
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-10-04 16:58:22 -03:00
|
|
|
has_section(section)
|
|
|
|
return whether the given section exists
|
|
|
|
|
2000-07-14 11:28:22 -03:00
|
|
|
has_option(section, option)
|
|
|
|
return whether the given option exists in the given section
|
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
options(section)
|
|
|
|
return list of configuration options for the named section
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-01-30 00:35:47 -04:00
|
|
|
read(filenames)
|
1999-10-04 15:57:27 -03:00
|
|
|
read and parse the list of named configuration files, given by
|
|
|
|
name. A single filename is also allowed. Non-existing files
|
2004-05-18 01:24:02 -03:00
|
|
|
are ignored. Return list of successfully read files.
|
1999-10-04 15:57:27 -03:00
|
|
|
|
|
|
|
readfp(fp, filename=None)
|
|
|
|
read and parse one configuration file, given as a file object.
|
|
|
|
The filename defaults to fp.name; it is only used in error
|
1999-10-12 13:12:48 -03:00
|
|
|
messages (if fp has no `name' attribute, the string `<???>' is used).
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2002-12-16 21:56:47 -04:00
|
|
|
get(section, option, raw=False, vars=None)
|
1999-01-26 18:01:37 -04:00
|
|
|
return a string value for the named option. All % interpolations are
|
|
|
|
expanded in the return values, based on the defaults passed into the
|
|
|
|
constructor and the DEFAULT section. Additional substitutions may be
|
|
|
|
provided using the `vars' argument, which must be a dictionary whose
|
|
|
|
contents override any pre-existing defaults.
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
getint(section, options)
|
|
|
|
like get(), but convert value to an integer
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
getfloat(section, options)
|
|
|
|
like get(), but convert value to a float
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-01-26 18:01:37 -04:00
|
|
|
getboolean(section, options)
|
2001-10-04 16:58:46 -03:00
|
|
|
like get(), but convert value to a boolean (currently case
|
2002-12-16 21:56:47 -04:00
|
|
|
insensitively defined as 0, false, no, off for False, and 1, true,
|
|
|
|
yes, on for True). Returns False or True.
|
2000-07-14 11:28:22 -03:00
|
|
|
|
2002-12-16 21:56:47 -04:00
|
|
|
items(section, raw=False, vars=None)
|
2002-09-27 12:49:56 -03:00
|
|
|
return a list of tuples with (name, value) for each option
|
|
|
|
in the section.
|
|
|
|
|
2000-07-14 11:28:22 -03:00
|
|
|
remove_section(section)
|
2001-01-14 19:36:06 -04:00
|
|
|
remove the given file section and all its options
|
2000-07-14 11:28:22 -03:00
|
|
|
|
|
|
|
remove_option(section, option)
|
2001-01-14 19:36:06 -04:00
|
|
|
remove the given option from the given section
|
2000-07-14 11:28:22 -03:00
|
|
|
|
|
|
|
set(section, option, value)
|
|
|
|
set the given option
|
|
|
|
|
|
|
|
write(fp)
|
2001-01-14 19:36:06 -04:00
|
|
|
write the configuration state in .ini format
|
1997-12-09 12:10:31 -04:00
|
|
|
"""
|
|
|
|
|
2009-03-03 01:00:37 -04:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict as _default_dict
|
|
|
|
except ImportError:
|
|
|
|
# fallback for setup.py which hasn't yet built _collections
|
|
|
|
_default_dict = dict
|
|
|
|
|
1998-07-01 17:41:12 -03:00
|
|
|
import re
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2002-12-30 19:51:45 -04:00
|
|
|
__all__ = ["NoSectionError", "DuplicateSectionError", "NoOptionError",
|
|
|
|
"InterpolationError", "InterpolationDepthError",
|
|
|
|
"InterpolationSyntaxError", "ParsingError",
|
2004-10-03 12:55:09 -03:00
|
|
|
"MissingSectionHeaderError",
|
|
|
|
"ConfigParser", "SafeConfigParser", "RawConfigParser",
|
2002-09-27 12:33:11 -03:00
|
|
|
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
|
2001-01-20 15:54:20 -04:00
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
DEFAULTSECT = "DEFAULT"
|
|
|
|
|
2000-09-27 19:43:54 -03:00
|
|
|
MAX_INTERPOLATION_DEPTH = 10
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2001-01-14 19:36:06 -04:00
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
# exception classes
|
2000-12-11 14:13:19 -04:00
|
|
|
class Error(Exception):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Base class for ConfigParser exceptions."""
|
|
|
|
|
2007-05-04 22:34:02 -03:00
|
|
|
def _get_message(self):
|
|
|
|
"""Getter for 'message'; needed only to override deprecation in
|
|
|
|
BaseException."""
|
|
|
|
return self.__message
|
|
|
|
|
|
|
|
def _set_message(self, value):
|
|
|
|
"""Setter for 'message'; needed only to override deprecation in
|
|
|
|
BaseException."""
|
|
|
|
self.__message = value
|
|
|
|
|
|
|
|
# BaseException.message has been deprecated since Python 2.6. To prevent
|
|
|
|
# DeprecationWarning from popping up over this pre-existing attribute, use
|
|
|
|
# a new property that takes lookup precedence.
|
|
|
|
message = property(_get_message, _set_message)
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def __init__(self, msg=''):
|
2002-12-31 13:23:27 -04:00
|
|
|
self.message = msg
|
2000-12-11 14:13:19 -04:00
|
|
|
Exception.__init__(self, msg)
|
2002-12-30 19:51:45 -04:00
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def __repr__(self):
|
2002-12-31 13:23:27 -04:00
|
|
|
return self.message
|
2002-12-30 19:51:45 -04:00
|
|
|
|
2000-12-11 14:13:19 -04:00
|
|
|
__str__ = __repr__
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
class NoSectionError(Error):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when no section matches a requested option."""
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def __init__(self, section):
|
2004-02-12 13:35:32 -04:00
|
|
|
Error.__init__(self, 'No section: %r' % (section,))
|
1998-03-26 17:13:24 -04:00
|
|
|
self.section = section
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
class DuplicateSectionError(Error):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when a section is multiply-created."""
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def __init__(self, section):
|
2002-12-31 13:23:27 -04:00
|
|
|
Error.__init__(self, "Section %r already exists" % section)
|
1998-03-26 17:13:24 -04:00
|
|
|
self.section = section
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
class NoOptionError(Error):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""A requested option was not found."""
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def __init__(self, option, section):
|
2002-12-31 13:23:27 -04:00
|
|
|
Error.__init__(self, "No option %r in section: %r" %
|
1998-03-26 17:13:24 -04:00
|
|
|
(option, section))
|
|
|
|
self.option = option
|
|
|
|
self.section = section
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
class InterpolationError(Error):
|
2002-12-31 13:23:27 -04:00
|
|
|
"""Base class for interpolation-related exceptions."""
|
2002-12-30 19:51:45 -04:00
|
|
|
|
2002-12-31 13:23:27 -04:00
|
|
|
def __init__(self, option, section, msg):
|
|
|
|
Error.__init__(self, msg)
|
1998-03-26 17:13:24 -04:00
|
|
|
self.option = option
|
|
|
|
self.section = section
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2002-12-31 13:23:27 -04:00
|
|
|
class InterpolationMissingOptionError(InterpolationError):
|
|
|
|
"""A string substitution required a setting which was not available."""
|
|
|
|
|
|
|
|
def __init__(self, option, section, rawval, reference):
|
|
|
|
msg = ("Bad value substitution:\n"
|
|
|
|
"\tsection: [%s]\n"
|
|
|
|
"\toption : %s\n"
|
|
|
|
"\tkey : %s\n"
|
|
|
|
"\trawval : %s\n"
|
|
|
|
% (section, option, reference, rawval))
|
|
|
|
InterpolationError.__init__(self, option, section, msg)
|
|
|
|
self.reference = reference
|
|
|
|
|
|
|
|
class InterpolationSyntaxError(InterpolationError):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when the source text into which substitutions are made
|
|
|
|
does not conform to the required syntax."""
|
2002-12-30 19:38:47 -04:00
|
|
|
|
2002-12-31 13:23:27 -04:00
|
|
|
class InterpolationDepthError(InterpolationError):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when substitutions are nested too deeply."""
|
|
|
|
|
2000-09-27 19:43:54 -03:00
|
|
|
def __init__(self, option, section, rawval):
|
2002-12-31 13:23:27 -04:00
|
|
|
msg = ("Value interpolation too deeply recursive:\n"
|
|
|
|
"\tsection: [%s]\n"
|
|
|
|
"\toption : %s\n"
|
|
|
|
"\trawval : %s\n"
|
|
|
|
% (section, option, rawval))
|
|
|
|
InterpolationError.__init__(self, option, section, msg)
|
1998-07-01 17:41:12 -03:00
|
|
|
|
|
|
|
class ParsingError(Error):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when a configuration file does not follow legal syntax."""
|
|
|
|
|
1998-07-01 17:41:12 -03:00
|
|
|
def __init__(self, filename):
|
|
|
|
Error.__init__(self, 'File contains parsing errors: %s' % filename)
|
|
|
|
self.filename = filename
|
|
|
|
self.errors = []
|
|
|
|
|
|
|
|
def append(self, lineno, line):
|
|
|
|
self.errors.append((lineno, line))
|
2002-12-31 13:23:27 -04:00
|
|
|
self.message += '\n\t[line %2d]: %s' % (lineno, line)
|
1998-07-01 17:41:12 -03:00
|
|
|
|
2000-09-27 19:43:54 -03:00
|
|
|
class MissingSectionHeaderError(ParsingError):
|
2002-12-30 19:51:45 -04:00
|
|
|
"""Raised when a key-value pair is found before any section header."""
|
|
|
|
|
2000-09-27 19:43:54 -03:00
|
|
|
def __init__(self, filename, lineno, line):
|
|
|
|
Error.__init__(
|
|
|
|
self,
|
2004-02-12 13:35:32 -04:00
|
|
|
'File contains no section headers.\nfile: %s, line: %d\n%r' %
|
2000-09-27 19:43:54 -03:00
|
|
|
(filename, lineno, line))
|
|
|
|
self.filename = filename
|
|
|
|
self.lineno = lineno
|
|
|
|
self.line = line
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
class RawConfigParser:
|
2010-02-19 01:24:30 -04:00
|
|
|
def __init__(self, defaults=None, dict_type=_default_dict,
|
|
|
|
allow_no_value=False):
|
2006-12-03 08:01:53 -04:00
|
|
|
self._dict = dict_type
|
|
|
|
self._sections = self._dict()
|
|
|
|
self._defaults = self._dict()
|
2010-02-19 01:24:30 -04:00
|
|
|
if allow_no_value:
|
|
|
|
self._optcre = self.OPTCRE_NV
|
|
|
|
else:
|
|
|
|
self._optcre = self.OPTCRE
|
2004-10-03 12:40:25 -03:00
|
|
|
if defaults:
|
|
|
|
for key, value in defaults.items():
|
|
|
|
self._defaults[self.optionxform(key)] = value
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def defaults(self):
|
2002-10-25 15:08:18 -03:00
|
|
|
return self._defaults
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def sections(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return a list of section names, excluding [DEFAULT]"""
|
2002-10-25 15:08:18 -03:00
|
|
|
# self._sections will never have [DEFAULT] in it
|
|
|
|
return self._sections.keys()
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def add_section(self, section):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Create a new section in the configuration.
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
Raise DuplicateSectionError if a section by the specified name
|
2008-02-23 08:46:10 -04:00
|
|
|
already exists. Raise ValueError if name is DEFAULT or any of it's
|
|
|
|
case-insensitive variants.
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
2008-02-23 08:46:10 -04:00
|
|
|
if section.lower() == "default":
|
|
|
|
raise ValueError, 'Invalid section name: %s' % section
|
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
if section in self._sections:
|
1998-03-26 17:13:24 -04:00
|
|
|
raise DuplicateSectionError(section)
|
2006-12-03 08:01:53 -04:00
|
|
|
self._sections[section] = self._dict()
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def has_section(self, section):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Indicate whether the named section is present in the configuration.
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
The DEFAULT section is not acknowledged.
|
|
|
|
"""
|
2002-10-25 15:08:18 -03:00
|
|
|
return section in self._sections
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def options(self, section):
|
1999-10-04 16:58:22 -03:00
|
|
|
"""Return a list of option names for the given section name."""
|
1998-03-26 17:13:24 -04:00
|
|
|
try:
|
2002-10-25 15:08:18 -03:00
|
|
|
opts = self._sections[section].copy()
|
1998-03-26 17:13:24 -04:00
|
|
|
except KeyError:
|
|
|
|
raise NoSectionError(section)
|
2002-10-25 15:08:18 -03:00
|
|
|
opts.update(self._defaults)
|
2002-06-01 11:18:47 -03:00
|
|
|
if '__name__' in opts:
|
2000-09-27 19:43:54 -03:00
|
|
|
del opts['__name__']
|
1998-03-26 17:13:24 -04:00
|
|
|
return opts.keys()
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def read(self, filenames):
|
1999-10-04 15:57:27 -03:00
|
|
|
"""Read and parse a filename or a list of filenames.
|
2001-01-14 19:36:06 -04:00
|
|
|
|
1999-10-04 15:57:27 -03:00
|
|
|
Files that cannot be opened are silently ignored; this is
|
1999-10-12 13:12:48 -03:00
|
|
|
designed so that you can specify a list of potential
|
1999-10-04 15:57:27 -03:00
|
|
|
configuration file locations (e.g. current directory, user's
|
|
|
|
home directory, systemwide directory), and all existing
|
|
|
|
configuration files in the list will be read. A single
|
|
|
|
filename may also be given.
|
2004-05-18 01:24:02 -03:00
|
|
|
|
|
|
|
Return list of successfully read files.
|
1999-10-04 15:57:27 -03:00
|
|
|
"""
|
Remove uses of the string and types modules:
x in string.whitespace => x.isspace()
type(x) in types.StringTypes => isinstance(x, basestring)
isinstance(x, types.StringTypes) => isinstance(x, basestring)
type(x) is types.StringType => isinstance(x, str)
type(x) == types.StringType => isinstance(x, str)
string.split(x, ...) => x.split(...)
string.join(x, y) => y.join(x)
string.zfill(x, ...) => x.zfill(...)
string.count(x, ...) => x.count(...)
hasattr(types, "UnicodeType") => try: unicode except NameError:
type(x) != types.TupleTuple => not isinstance(x, tuple)
isinstance(x, types.TupleType) => isinstance(x, tuple)
type(x) is types.IntType => isinstance(x, int)
Do not mention the string module in the rlcompleter docstring.
This partially applies SF patch http://www.python.org/sf/562373
(with basestring instead of string). (It excludes the changes to
unittest.py and does not change the os.stat stuff.)
2002-06-03 12:58:32 -03:00
|
|
|
if isinstance(filenames, basestring):
|
1998-03-26 17:13:24 -04:00
|
|
|
filenames = [filenames]
|
2004-05-18 01:24:02 -03:00
|
|
|
read_ok = []
|
1999-10-04 15:57:27 -03:00
|
|
|
for filename in filenames:
|
|
|
|
try:
|
|
|
|
fp = open(filename)
|
|
|
|
except IOError:
|
|
|
|
continue
|
2002-10-25 15:08:18 -03:00
|
|
|
self._read(fp, filename)
|
1999-10-04 15:11:56 -03:00
|
|
|
fp.close()
|
2004-05-18 01:24:02 -03:00
|
|
|
read_ok.append(filename)
|
|
|
|
return read_ok
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-10-04 15:57:27 -03:00
|
|
|
def readfp(self, fp, filename=None):
|
|
|
|
"""Like read() but the argument must be a file-like object.
|
|
|
|
|
|
|
|
The `fp' argument must have a `readline' method. Optional
|
|
|
|
second argument is the `filename', which if not given, is
|
|
|
|
taken from fp.name. If fp has no `name' attribute, `<???>' is
|
|
|
|
used.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if filename is None:
|
|
|
|
try:
|
|
|
|
filename = fp.name
|
|
|
|
except AttributeError:
|
|
|
|
filename = '<???>'
|
2002-10-25 15:08:18 -03:00
|
|
|
self._read(fp, filename)
|
1999-10-04 15:57:27 -03:00
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
def get(self, section, option):
|
|
|
|
opt = self.optionxform(option)
|
|
|
|
if section not in self._sections:
|
2002-09-27 12:33:11 -03:00
|
|
|
if section != DEFAULTSECT:
|
1998-03-26 17:13:24 -04:00
|
|
|
raise NoSectionError(section)
|
2002-10-25 15:08:18 -03:00
|
|
|
if opt in self._defaults:
|
|
|
|
return self._defaults[opt]
|
|
|
|
else:
|
|
|
|
raise NoOptionError(option, section)
|
|
|
|
elif opt in self._sections[section]:
|
|
|
|
return self._sections[section][opt]
|
|
|
|
elif opt in self._defaults:
|
|
|
|
return self._defaults[opt]
|
|
|
|
else:
|
1998-03-26 17:13:24 -04:00
|
|
|
raise NoOptionError(option, section)
|
2000-09-27 19:43:54 -03:00
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
def items(self, section):
|
2002-09-27 12:49:56 -03:00
|
|
|
try:
|
2002-10-25 15:08:18 -03:00
|
|
|
d2 = self._sections[section]
|
2002-09-27 12:49:56 -03:00
|
|
|
except KeyError:
|
|
|
|
if section != DEFAULTSECT:
|
|
|
|
raise NoSectionError(section)
|
2006-12-03 08:01:53 -04:00
|
|
|
d2 = self._dict()
|
2002-10-25 15:08:18 -03:00
|
|
|
d = self._defaults.copy()
|
|
|
|
d.update(d2)
|
2002-10-25 17:41:30 -03:00
|
|
|
if "__name__" in d:
|
|
|
|
del d["__name__"]
|
2002-10-25 15:08:18 -03:00
|
|
|
return d.items()
|
2001-01-14 19:36:06 -04:00
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
def _get(self, section, conv, option):
|
1998-03-26 17:13:24 -04:00
|
|
|
return conv(self.get(section, option))
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def getint(self, section, option):
|
2002-10-25 15:08:18 -03:00
|
|
|
return self._get(section, int, option)
|
1997-12-09 12:10:31 -04:00
|
|
|
|
|
|
|
def getfloat(self, section, option):
|
2002-10-25 15:08:18 -03:00
|
|
|
return self._get(section, float, option)
|
1997-12-09 12:10:31 -04:00
|
|
|
|
2002-09-27 12:33:11 -03:00
|
|
|
_boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
|
|
|
|
'0': False, 'no': False, 'false': False, 'off': False}
|
|
|
|
|
1997-12-09 12:10:31 -04:00
|
|
|
def getboolean(self, section, option):
|
2001-10-18 18:57:37 -03:00
|
|
|
v = self.get(section, option)
|
2002-09-27 12:33:11 -03:00
|
|
|
if v.lower() not in self._boolean_states:
|
1998-03-26 17:13:24 -04:00
|
|
|
raise ValueError, 'Not a boolean: %s' % v
|
2002-09-27 12:33:11 -03:00
|
|
|
return self._boolean_states[v.lower()]
|
1997-12-09 12:10:31 -04:00
|
|
|
|
1999-06-17 15:41:42 -03:00
|
|
|
def optionxform(self, optionstr):
|
2001-02-09 01:19:09 -04:00
|
|
|
return optionstr.lower()
|
1999-06-17 15:41:42 -03:00
|
|
|
|
2000-07-10 15:11:00 -03:00
|
|
|
def has_option(self, section, option):
|
|
|
|
"""Check for the existence of a given option in a given section."""
|
2002-09-27 12:33:11 -03:00
|
|
|
if not section or section == DEFAULTSECT:
|
|
|
|
option = self.optionxform(option)
|
2002-10-25 15:08:18 -03:00
|
|
|
return option in self._defaults
|
|
|
|
elif section not in self._sections:
|
2002-12-16 21:56:47 -04:00
|
|
|
return False
|
2000-07-10 15:11:00 -03:00
|
|
|
else:
|
2001-02-26 17:55:34 -04:00
|
|
|
option = self.optionxform(option)
|
2002-10-25 15:08:18 -03:00
|
|
|
return (option in self._sections[section]
|
|
|
|
or option in self._defaults)
|
2000-07-10 15:11:00 -03:00
|
|
|
|
2010-02-19 01:24:30 -04:00
|
|
|
def set(self, section, option, value=None):
|
2000-07-10 15:11:00 -03:00
|
|
|
"""Set an option."""
|
2002-09-27 12:33:11 -03:00
|
|
|
if not section or section == DEFAULTSECT:
|
2002-10-25 15:08:18 -03:00
|
|
|
sectdict = self._defaults
|
2000-07-10 15:11:00 -03:00
|
|
|
else:
|
|
|
|
try:
|
2002-10-25 15:08:18 -03:00
|
|
|
sectdict = self._sections[section]
|
2000-07-10 15:11:00 -03:00
|
|
|
except KeyError:
|
|
|
|
raise NoSectionError(section)
|
2002-09-27 12:33:11 -03:00
|
|
|
sectdict[self.optionxform(option)] = value
|
2000-07-10 15:11:00 -03:00
|
|
|
|
|
|
|
def write(self, fp):
|
|
|
|
"""Write an .ini-format representation of the configuration state."""
|
2002-10-25 15:08:18 -03:00
|
|
|
if self._defaults:
|
2002-09-27 12:33:11 -03:00
|
|
|
fp.write("[%s]\n" % DEFAULTSECT)
|
2002-10-25 15:08:18 -03:00
|
|
|
for (key, value) in self._defaults.items():
|
2002-03-08 14:08:47 -04:00
|
|
|
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
|
2000-07-10 15:11:00 -03:00
|
|
|
fp.write("\n")
|
2002-10-25 15:08:18 -03:00
|
|
|
for section in self._sections:
|
2002-09-27 12:33:11 -03:00
|
|
|
fp.write("[%s]\n" % section)
|
2002-10-25 15:08:18 -03:00
|
|
|
for (key, value) in self._sections[section].items():
|
2002-09-27 12:33:11 -03:00
|
|
|
if key != "__name__":
|
2010-02-19 01:24:30 -04:00
|
|
|
if value is None:
|
|
|
|
fp.write("%s\n" % (key))
|
|
|
|
else:
|
|
|
|
fp.write("%s = %s\n" %
|
|
|
|
(key, str(value).replace('\n', '\n\t')))
|
2000-07-10 15:11:00 -03:00
|
|
|
fp.write("\n")
|
|
|
|
|
2000-07-21 02:19:59 -03:00
|
|
|
def remove_option(self, section, option):
|
2000-07-14 11:28:22 -03:00
|
|
|
"""Remove an option."""
|
2002-09-27 12:33:11 -03:00
|
|
|
if not section or section == DEFAULTSECT:
|
2002-10-25 15:08:18 -03:00
|
|
|
sectdict = self._defaults
|
2000-07-14 11:28:22 -03:00
|
|
|
else:
|
|
|
|
try:
|
2002-10-25 15:08:18 -03:00
|
|
|
sectdict = self._sections[section]
|
2000-07-14 11:28:22 -03:00
|
|
|
except KeyError:
|
|
|
|
raise NoSectionError(section)
|
2001-02-26 17:55:34 -04:00
|
|
|
option = self.optionxform(option)
|
2002-06-01 11:18:47 -03:00
|
|
|
existed = option in sectdict
|
2000-07-14 11:28:22 -03:00
|
|
|
if existed:
|
2000-12-04 12:29:13 -04:00
|
|
|
del sectdict[option]
|
2000-07-14 11:28:22 -03:00
|
|
|
return existed
|
|
|
|
|
2000-07-21 02:19:59 -03:00
|
|
|
def remove_section(self, section):
|
2000-07-14 11:28:22 -03:00
|
|
|
"""Remove a file section."""
|
2002-10-25 15:08:18 -03:00
|
|
|
existed = section in self._sections
|
2002-09-27 12:33:11 -03:00
|
|
|
if existed:
|
2002-10-25 15:08:18 -03:00
|
|
|
del self._sections[section]
|
2002-09-27 12:33:11 -03:00
|
|
|
return existed
|
2000-07-14 11:28:22 -03:00
|
|
|
|
1998-07-01 17:41:12 -03:00
|
|
|
#
|
2002-09-27 12:33:11 -03:00
|
|
|
# Regular expressions for parsing section headers and options.
|
|
|
|
#
|
1999-06-17 15:41:42 -03:00
|
|
|
SECTCRE = re.compile(
|
1998-07-01 17:41:12 -03:00
|
|
|
r'\[' # [
|
2001-02-14 11:24:17 -04:00
|
|
|
r'(?P<header>[^]]+)' # very permissive!
|
1998-07-01 17:41:12 -03:00
|
|
|
r'\]' # ]
|
|
|
|
)
|
1999-06-17 15:41:42 -03:00
|
|
|
OPTCRE = re.compile(
|
2002-09-27 13:21:18 -03:00
|
|
|
r'(?P<option>[^:=\s][^:=]*)' # very permissive!
|
2002-09-27 12:33:11 -03:00
|
|
|
r'\s*(?P<vi>[:=])\s*' # any number of space/tab,
|
1998-07-01 17:41:12 -03:00
|
|
|
# followed by separator
|
|
|
|
# (either : or =), followed
|
|
|
|
# by any # space/tab
|
|
|
|
r'(?P<value>.*)$' # everything up to eol
|
|
|
|
)
|
2010-02-19 01:24:30 -04:00
|
|
|
OPTCRE_NV = re.compile(
|
|
|
|
r'(?P<option>[^:=\s][^:=]*)' # very permissive!
|
|
|
|
r'\s*(?:' # any number of space/tab,
|
|
|
|
r'(?P<vi>[:=])\s*' # optionally followed by
|
|
|
|
# separator (either : or
|
|
|
|
# =), followed by any #
|
|
|
|
# space/tab
|
|
|
|
r'(?P<value>.*))?$' # everything up to eol
|
|
|
|
)
|
1998-07-01 17:41:12 -03:00
|
|
|
|
2002-10-25 15:08:18 -03:00
|
|
|
def _read(self, fp, fpname):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Parse a sectioned setup file.
|
|
|
|
|
|
|
|
The sections in setup file contains a title line at the top,
|
|
|
|
indicated by a name in square brackets (`[]'), plus key/value
|
|
|
|
options lines, indicated by `name: value' format lines.
|
2002-11-06 10:51:20 -04:00
|
|
|
Continuations are represented by an embedded newline then
|
1998-03-26 17:13:24 -04:00
|
|
|
leading whitespace. Blank lines, lines beginning with a '#',
|
2002-11-06 10:51:20 -04:00
|
|
|
and just about everything else are ignored.
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
1998-07-01 17:41:12 -03:00
|
|
|
cursect = None # None, or a dictionary
|
1998-03-26 17:13:24 -04:00
|
|
|
optname = None
|
|
|
|
lineno = 0
|
1998-07-01 17:41:12 -03:00
|
|
|
e = None # None, or an exception
|
2002-12-16 21:56:47 -04:00
|
|
|
while True:
|
1998-03-26 17:13:24 -04:00
|
|
|
line = fp.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
lineno = lineno + 1
|
|
|
|
# comment or blank line?
|
2001-02-09 01:19:09 -04:00
|
|
|
if line.strip() == '' or line[0] in '#;':
|
1998-03-26 17:13:24 -04:00
|
|
|
continue
|
2002-09-27 13:21:18 -03:00
|
|
|
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
|
|
|
|
# no leading whitespace
|
1998-03-26 17:13:24 -04:00
|
|
|
continue
|
|
|
|
# continuation line?
|
2002-09-27 12:33:11 -03:00
|
|
|
if line[0].isspace() and cursect is not None and optname:
|
2001-02-09 01:19:09 -04:00
|
|
|
value = line.strip()
|
1998-03-26 17:13:24 -04:00
|
|
|
if value:
|
2002-09-27 12:33:11 -03:00
|
|
|
cursect[optname] = "%s\n%s" % (cursect[optname], value)
|
1998-07-01 17:41:12 -03:00
|
|
|
# a section header or option header?
|
1998-03-26 17:13:24 -04:00
|
|
|
else:
|
1998-07-01 17:41:12 -03:00
|
|
|
# is it a section header?
|
1999-06-17 15:41:42 -03:00
|
|
|
mo = self.SECTCRE.match(line)
|
1998-07-01 17:41:12 -03:00
|
|
|
if mo:
|
|
|
|
sectname = mo.group('header')
|
2002-10-25 15:08:18 -03:00
|
|
|
if sectname in self._sections:
|
|
|
|
cursect = self._sections[sectname]
|
1998-07-01 17:41:12 -03:00
|
|
|
elif sectname == DEFAULTSECT:
|
2002-10-25 15:08:18 -03:00
|
|
|
cursect = self._defaults
|
1998-07-01 17:41:12 -03:00
|
|
|
else:
|
2006-12-03 08:01:53 -04:00
|
|
|
cursect = self._dict()
|
|
|
|
cursect['__name__'] = sectname
|
2002-10-25 15:08:18 -03:00
|
|
|
self._sections[sectname] = cursect
|
1998-07-01 17:41:12 -03:00
|
|
|
# So sections can't start with a continuation line
|
|
|
|
optname = None
|
|
|
|
# no section header in the file?
|
|
|
|
elif cursect is None:
|
2004-02-12 13:35:32 -04:00
|
|
|
raise MissingSectionHeaderError(fpname, lineno, line)
|
1998-07-01 17:41:12 -03:00
|
|
|
# an option line?
|
|
|
|
else:
|
2010-02-19 01:24:30 -04:00
|
|
|
mo = self._optcre.match(line)
|
1998-07-01 17:41:12 -03:00
|
|
|
if mo:
|
2000-02-28 16:59:03 -04:00
|
|
|
optname, vi, optval = mo.group('option', 'vi', 'value')
|
2010-02-19 01:24:30 -04:00
|
|
|
# This check is fine because the OPTCRE cannot
|
|
|
|
# match if it would set optval to None
|
|
|
|
if optval is not None:
|
|
|
|
if vi in ('=', ':') and ';' in optval:
|
|
|
|
# ';' is a comment delimiter only if it follows
|
|
|
|
# a spacing character
|
|
|
|
pos = optval.find(';')
|
|
|
|
if pos != -1 and optval[pos-1].isspace():
|
|
|
|
optval = optval[:pos]
|
|
|
|
optval = optval.strip()
|
1998-07-01 17:41:12 -03:00
|
|
|
# allow empty values
|
|
|
|
if optval == '""':
|
|
|
|
optval = ''
|
2002-09-27 13:21:18 -03:00
|
|
|
optname = self.optionxform(optname.rstrip())
|
2002-09-27 12:33:11 -03:00
|
|
|
cursect[optname] = optval
|
1998-07-01 17:41:12 -03:00
|
|
|
else:
|
|
|
|
# a non-fatal parsing error occurred. set up the
|
|
|
|
# exception but keep going. the exception will be
|
|
|
|
# raised at the end of the file and will contain a
|
|
|
|
# list of all bogus lines
|
|
|
|
if not e:
|
1999-10-04 15:57:27 -03:00
|
|
|
e = ParsingError(fpname)
|
2004-02-12 13:35:32 -04:00
|
|
|
e.append(lineno, repr(line))
|
1998-07-01 17:41:12 -03:00
|
|
|
# if any parsing errors occurred, raise an exception
|
|
|
|
if e:
|
|
|
|
raise e
|
2002-10-25 15:08:18 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigParser(RawConfigParser):
|
|
|
|
|
2002-12-16 21:56:47 -04:00
|
|
|
def get(self, section, option, raw=False, vars=None):
|
2002-10-25 15:08:18 -03:00
|
|
|
"""Get an option value for a given section.
|
|
|
|
|
|
|
|
All % interpolations are expanded in the return values, based on the
|
|
|
|
defaults passed into the constructor, unless the optional argument
|
|
|
|
`raw' is true. Additional substitutions may be provided using the
|
|
|
|
`vars' argument, which must be a dictionary whose contents overrides
|
|
|
|
any pre-existing defaults.
|
|
|
|
|
|
|
|
The section DEFAULT is special.
|
|
|
|
"""
|
|
|
|
d = self._defaults.copy()
|
|
|
|
try:
|
|
|
|
d.update(self._sections[section])
|
|
|
|
except KeyError:
|
|
|
|
if section != DEFAULTSECT:
|
|
|
|
raise NoSectionError(section)
|
|
|
|
# Update with the entry specific variables
|
2004-10-03 12:40:25 -03:00
|
|
|
if vars:
|
|
|
|
for key, value in vars.items():
|
|
|
|
d[self.optionxform(key)] = value
|
2002-10-25 15:08:18 -03:00
|
|
|
option = self.optionxform(option)
|
|
|
|
try:
|
|
|
|
value = d[option]
|
|
|
|
except KeyError:
|
|
|
|
raise NoOptionError(option, section)
|
|
|
|
|
2010-02-19 01:24:30 -04:00
|
|
|
if raw or value is None:
|
2002-10-25 15:08:18 -03:00
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return self._interpolate(section, option, value, d)
|
|
|
|
|
2002-12-16 21:56:47 -04:00
|
|
|
def items(self, section, raw=False, vars=None):
|
2002-10-25 15:08:18 -03:00
|
|
|
"""Return a list of tuples with (name, value) for each option
|
|
|
|
in the section.
|
|
|
|
|
|
|
|
All % interpolations are expanded in the return values, based on the
|
|
|
|
defaults passed into the constructor, unless the optional argument
|
|
|
|
`raw' is true. Additional substitutions may be provided using the
|
|
|
|
`vars' argument, which must be a dictionary whose contents overrides
|
|
|
|
any pre-existing defaults.
|
|
|
|
|
|
|
|
The section DEFAULT is special.
|
|
|
|
"""
|
|
|
|
d = self._defaults.copy()
|
|
|
|
try:
|
|
|
|
d.update(self._sections[section])
|
|
|
|
except KeyError:
|
|
|
|
if section != DEFAULTSECT:
|
|
|
|
raise NoSectionError(section)
|
|
|
|
# Update with the entry specific variables
|
|
|
|
if vars:
|
2004-10-03 12:40:25 -03:00
|
|
|
for key, value in vars.items():
|
|
|
|
d[self.optionxform(key)] = value
|
2002-10-25 17:41:30 -03:00
|
|
|
options = d.keys()
|
|
|
|
if "__name__" in options:
|
|
|
|
options.remove("__name__")
|
2002-10-25 15:08:18 -03:00
|
|
|
if raw:
|
2003-10-21 13:45:00 -03:00
|
|
|
return [(option, d[option])
|
|
|
|
for option in options]
|
2002-10-25 15:08:18 -03:00
|
|
|
else:
|
2003-10-21 13:45:00 -03:00
|
|
|
return [(option, self._interpolate(section, option, d[option], d))
|
|
|
|
for option in options]
|
2002-10-25 15:08:18 -03:00
|
|
|
|
|
|
|
def _interpolate(self, section, option, rawval, vars):
|
|
|
|
# do the string interpolation
|
|
|
|
value = rawval
|
2002-11-09 01:08:07 -04:00
|
|
|
depth = MAX_INTERPOLATION_DEPTH
|
2002-10-25 15:08:18 -03:00
|
|
|
while depth: # Loop through this until it's done
|
|
|
|
depth -= 1
|
2010-02-19 01:24:30 -04:00
|
|
|
if value and "%(" in value:
|
2004-05-17 23:25:51 -03:00
|
|
|
value = self._KEYCRE.sub(self._interpolation_replace, value)
|
2002-10-25 15:08:18 -03:00
|
|
|
try:
|
|
|
|
value = value % vars
|
2002-12-31 02:55:41 -04:00
|
|
|
except KeyError, e:
|
2002-12-31 13:23:27 -04:00
|
|
|
raise InterpolationMissingOptionError(
|
2008-08-02 00:37:50 -03:00
|
|
|
option, section, rawval, e.args[0])
|
2002-10-25 15:08:18 -03:00
|
|
|
else:
|
|
|
|
break
|
2010-02-19 01:24:30 -04:00
|
|
|
if value and "%(" in value:
|
2002-10-25 15:08:18 -03:00
|
|
|
raise InterpolationDepthError(option, section, rawval)
|
|
|
|
return value
|
2002-10-25 18:52:00 -03:00
|
|
|
|
2004-05-17 23:25:51 -03:00
|
|
|
_KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
|
|
|
|
|
|
|
|
def _interpolation_replace(self, match):
|
|
|
|
s = match.group(1)
|
|
|
|
if s is None:
|
|
|
|
return match.group()
|
|
|
|
else:
|
|
|
|
return "%%(%s)s" % self.optionxform(s)
|
|
|
|
|
2002-10-25 18:52:00 -03:00
|
|
|
|
|
|
|
class SafeConfigParser(ConfigParser):
|
|
|
|
|
|
|
|
def _interpolate(self, section, option, rawval, vars):
|
|
|
|
# do the string interpolation
|
|
|
|
L = []
|
|
|
|
self._interpolate_some(option, L, rawval, section, vars, 1)
|
|
|
|
return ''.join(L)
|
|
|
|
|
2007-03-13 14:43:32 -03:00
|
|
|
_interpvar_re = re.compile(r"%\(([^)]+)\)s")
|
2002-10-25 18:52:00 -03:00
|
|
|
|
|
|
|
def _interpolate_some(self, option, accum, rest, section, map, depth):
|
|
|
|
if depth > MAX_INTERPOLATION_DEPTH:
|
|
|
|
raise InterpolationDepthError(option, section, rest)
|
|
|
|
while rest:
|
|
|
|
p = rest.find("%")
|
|
|
|
if p < 0:
|
|
|
|
accum.append(rest)
|
|
|
|
return
|
|
|
|
if p > 0:
|
|
|
|
accum.append(rest[:p])
|
|
|
|
rest = rest[p:]
|
|
|
|
# p is no longer used
|
|
|
|
c = rest[1:2]
|
|
|
|
if c == "%":
|
|
|
|
accum.append("%")
|
|
|
|
rest = rest[2:]
|
|
|
|
elif c == "(":
|
2007-03-13 14:43:32 -03:00
|
|
|
m = self._interpvar_re.match(rest)
|
2002-10-25 18:52:00 -03:00
|
|
|
if m is None:
|
2003-06-29 01:23:35 -03:00
|
|
|
raise InterpolationSyntaxError(option, section,
|
|
|
|
"bad interpolation variable reference %r" % rest)
|
2004-05-17 23:25:51 -03:00
|
|
|
var = self.optionxform(m.group(1))
|
2002-10-25 18:52:00 -03:00
|
|
|
rest = rest[m.end():]
|
|
|
|
try:
|
|
|
|
v = map[var]
|
|
|
|
except KeyError:
|
2002-12-31 13:23:27 -04:00
|
|
|
raise InterpolationMissingOptionError(
|
|
|
|
option, section, rest, var)
|
2002-10-25 18:52:00 -03:00
|
|
|
if "%" in v:
|
|
|
|
self._interpolate_some(option, accum, v,
|
|
|
|
section, map, depth + 1)
|
|
|
|
else:
|
|
|
|
accum.append(v)
|
|
|
|
else:
|
|
|
|
raise InterpolationSyntaxError(
|
2003-06-29 01:23:35 -03:00
|
|
|
option, section,
|
2004-02-12 13:35:32 -04:00
|
|
|
"'%%' must be followed by '%%' or '(', found: %r" % (rest,))
|
2004-10-03 12:55:09 -03:00
|
|
|
|
2010-02-19 01:24:30 -04:00
|
|
|
def set(self, section, option, value=None):
|
2004-10-03 12:55:09 -03:00
|
|
|
"""Set an option. Extend ConfigParser.set: check for string values."""
|
2010-02-19 01:24:30 -04:00
|
|
|
# The only legal non-string value if we allow valueless
|
|
|
|
# options is None, so we need to check if the value is a
|
|
|
|
# string if:
|
|
|
|
# - we do not allow valueless options, or
|
|
|
|
# - we allow valueless options but the value is not None
|
|
|
|
if self._optcre is self.OPTCRE or value:
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
raise TypeError("option values must be strings")
|
2007-03-13 14:43:32 -03:00
|
|
|
# check for bad percent signs:
|
|
|
|
# first, replace all "good" interpolations
|
2009-04-13 09:36:24 -03:00
|
|
|
tmp_value = value.replace('%%', '')
|
|
|
|
tmp_value = self._interpvar_re.sub('', tmp_value)
|
2007-03-13 14:43:32 -03:00
|
|
|
# then, check if there's a lone percent sign left
|
2009-04-12 14:24:11 -03:00
|
|
|
percent_index = tmp_value.find('%')
|
|
|
|
if percent_index != -1:
|
2007-03-13 14:43:32 -03:00
|
|
|
raise ValueError("invalid interpolation syntax in %r at "
|
2009-04-12 14:24:11 -03:00
|
|
|
"position %d" % (value, percent_index))
|
2004-10-03 12:55:09 -03:00
|
|
|
ConfigParser.set(self, section, option, value)
|