|
|
af3cbb |
From 85001e92986b3e3f6b7dcbbe5d71cde99962fd58 Mon Sep 17 00:00:00 2001
|
|
|
af3cbb |
From: Christian Heimes <cheimes@redhat.com>
|
|
|
af3cbb |
Date: Tue, 28 Mar 2017 17:49:39 +0200
|
|
|
af3cbb |
Subject: [PATCH 1/4] Vendor configparser-3.5.0
|
|
|
af3cbb |
|
|
|
af3cbb |
Signed-off-by: Christian Heimes <cheimes@redhat.com>
|
|
|
af3cbb |
---
|
|
|
af3cbb |
custodia/vendor/backports/__init__.py | 11 +
|
|
|
af3cbb |
custodia/vendor/backports/configparser/__init__.py | 1390 ++++++++++++++++++++
|
|
|
af3cbb |
custodia/vendor/backports/configparser/helpers.py | 171 +++
|
|
|
af3cbb |
custodia/vendor/configparser.py | 52 +
|
|
|
af3cbb |
4 files changed, 1624 insertions(+)
|
|
|
af3cbb |
create mode 100644 custodia/vendor/backports/__init__.py
|
|
|
af3cbb |
create mode 100644 custodia/vendor/backports/configparser/__init__.py
|
|
|
af3cbb |
create mode 100644 custodia/vendor/backports/configparser/helpers.py
|
|
|
af3cbb |
create mode 100644 custodia/vendor/configparser.py
|
|
|
af3cbb |
|
|
|
af3cbb |
diff --git a/custodia/vendor/backports/__init__.py b/custodia/vendor/backports/__init__.py
|
|
|
af3cbb |
new file mode 100644
|
|
|
af3cbb |
index 0000000..f84d25c
|
|
|
af3cbb |
--- /dev/null
|
|
|
af3cbb |
+++ b/custodia/vendor/backports/__init__.py
|
|
|
af3cbb |
@@ -0,0 +1,11 @@
|
|
|
af3cbb |
+# A Python "namespace package" http://www.python.org/dev/peps/pep-0382/
|
|
|
af3cbb |
+# This always goes inside of a namespace package's __init__.py
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from pkgutil import extend_path
|
|
|
af3cbb |
+__path__ = extend_path(__path__, __name__)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+try:
|
|
|
af3cbb |
+ import pkg_resources
|
|
|
af3cbb |
+ pkg_resources.declare_namespace(__name__)
|
|
|
af3cbb |
+except ImportError:
|
|
|
af3cbb |
+ pass
|
|
|
af3cbb |
diff --git a/custodia/vendor/backports/configparser/__init__.py b/custodia/vendor/backports/configparser/__init__.py
|
|
|
af3cbb |
new file mode 100644
|
|
|
af3cbb |
index 0000000..06d7a08
|
|
|
af3cbb |
--- /dev/null
|
|
|
af3cbb |
+++ b/custodia/vendor/backports/configparser/__init__.py
|
|
|
af3cbb |
@@ -0,0 +1,1390 @@
|
|
|
af3cbb |
+#!/usr/bin/env python
|
|
|
af3cbb |
+# -*- coding: utf-8 -*-
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+"""Configuration file parser.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+A configuration file consists of sections, lead by a "[section]" header,
|
|
|
af3cbb |
+and followed by "name: value" entries, with continuations and such in
|
|
|
af3cbb |
+the style of RFC 822.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+Intrinsic defaults can be specified by passing them into the
|
|
|
af3cbb |
+ConfigParser constructor as a dictionary.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class:
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ConfigParser -- responsible for parsing a list of
|
|
|
af3cbb |
+ configuration files, and managing the parsed database.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ methods:
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
|
|
|
af3cbb |
+ delimiters=('=', ':'), comment_prefixes=('#', ';'),
|
|
|
af3cbb |
+ inline_comment_prefixes=None, strict=True,
|
|
|
af3cbb |
+ empty_lines_in_values=True, default_section='DEFAULT',
|
|
|
af3cbb |
+ interpolation=<unset>, converters=<unset>):
|
|
|
af3cbb |
+ Create the parser. When `defaults' is given, it is initialized into the
|
|
|
af3cbb |
+ dictionary or intrinsic defaults. The keys must be strings, the values
|
|
|
af3cbb |
+ must be appropriate for %()s string interpolation.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `dict_type' is given, it will be used to create the dictionary
|
|
|
af3cbb |
+ objects for the list of sections, for the options within a section, and
|
|
|
af3cbb |
+ for the default values.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `delimiters' is given, it will be used as the set of substrings
|
|
|
af3cbb |
+ that divide keys from values.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `comment_prefixes' is given, it will be used as the set of
|
|
|
af3cbb |
+ substrings that prefix comments in empty lines. Comments can be
|
|
|
af3cbb |
+ indented.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `inline_comment_prefixes' is given, it will be used as the set of
|
|
|
af3cbb |
+ substrings that prefix comments in non-empty lines.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `strict` is True, the parser won't allow for any section or option
|
|
|
af3cbb |
+ duplicates while reading from a single source (file, string or
|
|
|
af3cbb |
+ dictionary). Default is True.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `empty_lines_in_values' is False (default: True), each empty line
|
|
|
af3cbb |
+ marks the end of an option. Otherwise, internal empty lines of
|
|
|
af3cbb |
+ a multiline option are kept as part of the value.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ When `allow_no_value' is True (default: False), options without
|
|
|
af3cbb |
+ values are accepted; the value presented for these is None.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ sections()
|
|
|
af3cbb |
+ Return all the configuration section names, sans DEFAULT.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ has_section(section)
|
|
|
af3cbb |
+ Return whether the given section exists.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ has_option(section, option)
|
|
|
af3cbb |
+ Return whether the given option exists in the given section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ options(section)
|
|
|
af3cbb |
+ Return list of configuration options for the named section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ read(filenames, encoding=None)
|
|
|
af3cbb |
+ Read and parse the list of named configuration files, given by
|
|
|
af3cbb |
+ name. A single filename is also allowed. Non-existing files
|
|
|
af3cbb |
+ are ignored. Return list of successfully read files.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ read_file(f, filename=None)
|
|
|
af3cbb |
+ Read and parse one configuration file, given as a file object.
|
|
|
af3cbb |
+ The filename defaults to f.name; it is only used in error
|
|
|
af3cbb |
+ messages (if f has no `name' attribute, the string `' is used).
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ read_string(string)
|
|
|
af3cbb |
+ Read configuration from a given string.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ read_dict(dictionary)
|
|
|
af3cbb |
+ Read configuration from a dictionary. Keys are section names,
|
|
|
af3cbb |
+ values are dictionaries with keys and values that should be present
|
|
|
af3cbb |
+ in the section. If the used dictionary type preserves order, sections
|
|
|
af3cbb |
+ and their keys will be added in order. Values are automatically
|
|
|
af3cbb |
+ converted to strings.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ get(section, option, raw=False, vars=None, fallback=_UNSET)
|
|
|
af3cbb |
+ Return a string value for the named option. All % interpolations are
|
|
|
af3cbb |
+ expanded in the return values, based on the defaults passed into the
|
|
|
af3cbb |
+ constructor and the DEFAULT section. Additional substitutions may be
|
|
|
af3cbb |
+ provided using the `vars' argument, which must be a dictionary whose
|
|
|
af3cbb |
+ contents override any pre-existing defaults. If `option' is a key in
|
|
|
af3cbb |
+ `vars', the value from `vars' is used.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ getint(section, options, raw=False, vars=None, fallback=_UNSET)
|
|
|
af3cbb |
+ Like get(), but convert value to an integer.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
|
|
|
af3cbb |
+ Like get(), but convert value to a float.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
|
|
|
af3cbb |
+ Like get(), but convert value to a boolean (currently case
|
|
|
af3cbb |
+ insensitively defined as 0, false, no, off for False, and 1, true,
|
|
|
af3cbb |
+ yes, on for True). Returns False or True.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ items(section=_UNSET, raw=False, vars=None)
|
|
|
af3cbb |
+ If section is given, return a list of tuples with (name, value) for
|
|
|
af3cbb |
+ each option in the section. Otherwise, return a list of tuples with
|
|
|
af3cbb |
+ (section_name, section_proxy) for each section, including DEFAULTSECT.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ remove_section(section)
|
|
|
af3cbb |
+ Remove the given file section and all its options.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ remove_option(section, option)
|
|
|
af3cbb |
+ Remove the given option from the given section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ set(section, option, value)
|
|
|
af3cbb |
+ Set the given option.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ write(fp, space_around_delimiters=True)
|
|
|
af3cbb |
+ Write the configuration state in .ini format. If
|
|
|
af3cbb |
+ `space_around_delimiters' is True (the default), delimiters
|
|
|
af3cbb |
+ between keys and values are surrounded by spaces.
|
|
|
af3cbb |
+"""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from __future__ import absolute_import
|
|
|
af3cbb |
+from __future__ import division
|
|
|
af3cbb |
+from __future__ import print_function
|
|
|
af3cbb |
+from __future__ import unicode_literals
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from collections import MutableMapping
|
|
|
af3cbb |
+import functools
|
|
|
af3cbb |
+import io
|
|
|
af3cbb |
+import itertools
|
|
|
af3cbb |
+import re
|
|
|
af3cbb |
+import sys
|
|
|
af3cbb |
+import warnings
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from backports.configparser.helpers import OrderedDict as _default_dict
|
|
|
af3cbb |
+from backports.configparser.helpers import ChainMap as _ChainMap
|
|
|
af3cbb |
+from backports.configparser.helpers import from_none, open, str, PY2
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
|
|
|
af3cbb |
+ "NoOptionError", "InterpolationError", "InterpolationDepthError",
|
|
|
af3cbb |
+ "InterpolationMissingOptionError", "InterpolationSyntaxError",
|
|
|
af3cbb |
+ "ParsingError", "MissingSectionHeaderError",
|
|
|
af3cbb |
+ "ConfigParser", "SafeConfigParser", "RawConfigParser",
|
|
|
af3cbb |
+ "Interpolation", "BasicInterpolation", "ExtendedInterpolation",
|
|
|
af3cbb |
+ "LegacyInterpolation", "SectionProxy", "ConverterMapping",
|
|
|
af3cbb |
+ "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+DEFAULTSECT = "DEFAULT"
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+MAX_INTERPOLATION_DEPTH = 10
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+# exception classes
|
|
|
af3cbb |
+class Error(Exception):
|
|
|
af3cbb |
+ """Base class for ConfigParser exceptions."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, msg=''):
|
|
|
af3cbb |
+ self.message = msg
|
|
|
af3cbb |
+ Exception.__init__(self, msg)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __repr__(self):
|
|
|
af3cbb |
+ return self.message
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ __str__ = __repr__
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class NoSectionError(Error):
|
|
|
af3cbb |
+ """Raised when no section matches a requested option."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, section):
|
|
|
af3cbb |
+ Error.__init__(self, 'No section: %r' % (section,))
|
|
|
af3cbb |
+ self.section = section
|
|
|
af3cbb |
+ self.args = (section, )
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class DuplicateSectionError(Error):
|
|
|
af3cbb |
+ """Raised when a section is repeated in an input source.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Possible repetitions that raise this exception are: multiple creation
|
|
|
af3cbb |
+ using the API or in strict parsers when a section is found more than once
|
|
|
af3cbb |
+ in a single input file, string or dictionary.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, section, source=None, lineno=None):
|
|
|
af3cbb |
+ msg = [repr(section), " already exists"]
|
|
|
af3cbb |
+ if source is not None:
|
|
|
af3cbb |
+ message = ["While reading from ", repr(source)]
|
|
|
af3cbb |
+ if lineno is not None:
|
|
|
af3cbb |
+ message.append(" [line {0:2d}]".format(lineno))
|
|
|
af3cbb |
+ message.append(": section ")
|
|
|
af3cbb |
+ message.extend(msg)
|
|
|
af3cbb |
+ msg = message
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ msg.insert(0, "Section ")
|
|
|
af3cbb |
+ Error.__init__(self, "".join(msg))
|
|
|
af3cbb |
+ self.section = section
|
|
|
af3cbb |
+ self.source = source
|
|
|
af3cbb |
+ self.lineno = lineno
|
|
|
af3cbb |
+ self.args = (section, source, lineno)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class DuplicateOptionError(Error):
|
|
|
af3cbb |
+ """Raised by strict parsers when an option is repeated in an input source.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Current implementation raises this exception only when an option is found
|
|
|
af3cbb |
+ more than once in a single file, string or dictionary.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, section, option, source=None, lineno=None):
|
|
|
af3cbb |
+ msg = [repr(option), " in section ", repr(section),
|
|
|
af3cbb |
+ " already exists"]
|
|
|
af3cbb |
+ if source is not None:
|
|
|
af3cbb |
+ message = ["While reading from ", repr(source)]
|
|
|
af3cbb |
+ if lineno is not None:
|
|
|
af3cbb |
+ message.append(" [line {0:2d}]".format(lineno))
|
|
|
af3cbb |
+ message.append(": option ")
|
|
|
af3cbb |
+ message.extend(msg)
|
|
|
af3cbb |
+ msg = message
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ msg.insert(0, "Option ")
|
|
|
af3cbb |
+ Error.__init__(self, "".join(msg))
|
|
|
af3cbb |
+ self.section = section
|
|
|
af3cbb |
+ self.option = option
|
|
|
af3cbb |
+ self.source = source
|
|
|
af3cbb |
+ self.lineno = lineno
|
|
|
af3cbb |
+ self.args = (section, option, source, lineno)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class NoOptionError(Error):
|
|
|
af3cbb |
+ """A requested option was not found."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, option, section):
|
|
|
af3cbb |
+ Error.__init__(self, "No option %r in section: %r" %
|
|
|
af3cbb |
+ (option, section))
|
|
|
af3cbb |
+ self.option = option
|
|
|
af3cbb |
+ self.section = section
|
|
|
af3cbb |
+ self.args = (option, section)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class InterpolationError(Error):
|
|
|
af3cbb |
+ """Base class for interpolation-related exceptions."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, option, section, msg):
|
|
|
af3cbb |
+ Error.__init__(self, msg)
|
|
|
af3cbb |
+ self.option = option
|
|
|
af3cbb |
+ self.section = section
|
|
|
af3cbb |
+ self.args = (option, section, msg)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class InterpolationMissingOptionError(InterpolationError):
|
|
|
af3cbb |
+ """A string substitution required a setting which was not available."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, option, section, rawval, reference):
|
|
|
af3cbb |
+ msg = ("Bad value substitution: option {0!r} in section {1!r} contains "
|
|
|
af3cbb |
+ "an interpolation key {2!r} which is not a valid option name. "
|
|
|
af3cbb |
+ "Raw value: {3!r}".format(option, section, reference, rawval))
|
|
|
af3cbb |
+ InterpolationError.__init__(self, option, section, msg)
|
|
|
af3cbb |
+ self.reference = reference
|
|
|
af3cbb |
+ self.args = (option, section, rawval, reference)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class InterpolationSyntaxError(InterpolationError):
|
|
|
af3cbb |
+ """Raised when the source text contains invalid syntax.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Current implementation raises this exception when the source text into
|
|
|
af3cbb |
+ which substitutions are made does not conform to the required syntax.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class InterpolationDepthError(InterpolationError):
|
|
|
af3cbb |
+ """Raised when substitutions are nested too deeply."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, option, section, rawval):
|
|
|
af3cbb |
+ msg = ("Recursion limit exceeded in value substitution: option {0!r} "
|
|
|
af3cbb |
+ "in section {1!r} contains an interpolation key which "
|
|
|
af3cbb |
+ "cannot be substituted in {2} steps. Raw value: {3!r}"
|
|
|
af3cbb |
+ "".format(option, section, MAX_INTERPOLATION_DEPTH,
|
|
|
af3cbb |
+ rawval))
|
|
|
af3cbb |
+ InterpolationError.__init__(self, option, section, msg)
|
|
|
af3cbb |
+ self.args = (option, section, rawval)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class ParsingError(Error):
|
|
|
af3cbb |
+ """Raised when a configuration file does not follow legal syntax."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, source=None, filename=None):
|
|
|
af3cbb |
+ # Exactly one of `source'/`filename' arguments has to be given.
|
|
|
af3cbb |
+ # `filename' kept for compatibility.
|
|
|
af3cbb |
+ if filename and source:
|
|
|
af3cbb |
+ raise ValueError("Cannot specify both `filename' and `source'. "
|
|
|
af3cbb |
+ "Use `source'.")
|
|
|
af3cbb |
+ elif not filename and not source:
|
|
|
af3cbb |
+ raise ValueError("Required argument `source' not given.")
|
|
|
af3cbb |
+ elif filename:
|
|
|
af3cbb |
+ source = filename
|
|
|
af3cbb |
+ Error.__init__(self, 'Source contains parsing errors: %r' % source)
|
|
|
af3cbb |
+ self.source = source
|
|
|
af3cbb |
+ self.errors = []
|
|
|
af3cbb |
+ self.args = (source, )
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @property
|
|
|
af3cbb |
+ def filename(self):
|
|
|
af3cbb |
+ """Deprecated, use `source'."""
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "The 'filename' attribute will be removed in future versions. "
|
|
|
af3cbb |
+ "Use 'source' instead.",
|
|
|
af3cbb |
+ DeprecationWarning, stacklevel=2
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+ return self.source
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @filename.setter
|
|
|
af3cbb |
+ def filename(self, value):
|
|
|
af3cbb |
+ """Deprecated, user `source'."""
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "The 'filename' attribute will be removed in future versions. "
|
|
|
af3cbb |
+ "Use 'source' instead.",
|
|
|
af3cbb |
+ DeprecationWarning, stacklevel=2
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+ self.source = value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def append(self, lineno, line):
|
|
|
af3cbb |
+ self.errors.append((lineno, line))
|
|
|
af3cbb |
+ self.message += '\n\t[line %2d]: %s' % (lineno, line)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class MissingSectionHeaderError(ParsingError):
|
|
|
af3cbb |
+ """Raised when a key-value pair is found before any section header."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, filename, lineno, line):
|
|
|
af3cbb |
+ Error.__init__(
|
|
|
af3cbb |
+ self,
|
|
|
af3cbb |
+ 'File contains no section headers.\nfile: %r, line: %d\n%r' %
|
|
|
af3cbb |
+ (filename, lineno, line))
|
|
|
af3cbb |
+ self.source = filename
|
|
|
af3cbb |
+ self.lineno = lineno
|
|
|
af3cbb |
+ self.line = line
|
|
|
af3cbb |
+ self.args = (filename, lineno, line)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+# Used in parser getters to indicate the default behaviour when a specific
|
|
|
af3cbb |
+# option is not found it to raise an exception. Created to enable `None' as
|
|
|
af3cbb |
+# a valid fallback value.
|
|
|
af3cbb |
+_UNSET = object()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class Interpolation(object):
|
|
|
af3cbb |
+ """Dummy interpolation that passes the value through with no changes."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_get(self, parser, section, option, value, defaults):
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_set(self, parser, section, option, value):
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_read(self, parser, section, option, value):
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_write(self, parser, section, option, value):
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class BasicInterpolation(Interpolation):
|
|
|
af3cbb |
+ """Interpolation as implemented in the classic ConfigParser.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The option values can contain format strings which refer to other values in
|
|
|
af3cbb |
+ the same section, or values in the special default section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ For example:
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ something: %(dir)s/whatever
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ would resolve the "%(dir)s" to the value of dir. All reference
|
|
|
af3cbb |
+ expansions are done late, on demand. If a user needs to use a bare % in
|
|
|
af3cbb |
+ a configuration file, she can escape it by writing %%. Other % usage
|
|
|
af3cbb |
+ is considered a user error and raises `InterpolationSyntaxError'."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ _KEYCRE = re.compile(r"%\(([^)]+)\)s")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_get(self, parser, section, option, value, defaults):
|
|
|
af3cbb |
+ L = []
|
|
|
af3cbb |
+ self._interpolate_some(parser, option, L, value, section, defaults, 1)
|
|
|
af3cbb |
+ return ''.join(L)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_set(self, parser, section, option, value):
|
|
|
af3cbb |
+ tmp_value = value.replace('%%', '') # escaped percent signs
|
|
|
af3cbb |
+ tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
|
|
|
af3cbb |
+ if '%' in tmp_value:
|
|
|
af3cbb |
+ raise ValueError("invalid interpolation syntax in %r at "
|
|
|
af3cbb |
+ "position %d" % (value, tmp_value.find('%')))
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _interpolate_some(self, parser, option, accum, rest, section, map,
|
|
|
af3cbb |
+ depth):
|
|
|
af3cbb |
+ rawval = parser.get(section, option, raw=True, fallback=rest)
|
|
|
af3cbb |
+ if depth > MAX_INTERPOLATION_DEPTH:
|
|
|
af3cbb |
+ raise InterpolationDepthError(option, section, rawval)
|
|
|
af3cbb |
+ while rest:
|
|
|
af3cbb |
+ p = rest.find("%")
|
|
|
af3cbb |
+ if p < 0:
|
|
|
af3cbb |
+ accum.append(rest)
|
|
|
af3cbb |
+ return
|
|
|
af3cbb |
+ if p > 0:
|
|
|
af3cbb |
+ accum.append(rest[:p])
|
|
|
af3cbb |
+ rest = rest[p:]
|
|
|
af3cbb |
+ # p is no longer used
|
|
|
af3cbb |
+ c = rest[1:2]
|
|
|
af3cbb |
+ if c == "%":
|
|
|
af3cbb |
+ accum.append("%")
|
|
|
af3cbb |
+ rest = rest[2:]
|
|
|
af3cbb |
+ elif c == "(":
|
|
|
af3cbb |
+ m = self._KEYCRE.match(rest)
|
|
|
af3cbb |
+ if m is None:
|
|
|
af3cbb |
+ raise InterpolationSyntaxError(option, section,
|
|
|
af3cbb |
+ "bad interpolation variable reference %r" % rest)
|
|
|
af3cbb |
+ var = parser.optionxform(m.group(1))
|
|
|
af3cbb |
+ rest = rest[m.end():]
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ v = map[var]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise from_none(InterpolationMissingOptionError(
|
|
|
af3cbb |
+ option, section, rawval, var))
|
|
|
af3cbb |
+ if "%" in v:
|
|
|
af3cbb |
+ self._interpolate_some(parser, option, accum, v,
|
|
|
af3cbb |
+ section, map, depth + 1)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ accum.append(v)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ raise InterpolationSyntaxError(
|
|
|
af3cbb |
+ option, section,
|
|
|
af3cbb |
+ "'%%' must be followed by '%%' or '(', "
|
|
|
af3cbb |
+ "found: %r" % (rest,))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class ExtendedInterpolation(Interpolation):
|
|
|
af3cbb |
+ """Advanced variant of interpolation, supports the syntax used by
|
|
|
af3cbb |
+ `zc.buildout'. Enables interpolation between sections."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ _KEYCRE = re.compile(r"\$\{([^}]+)\}")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_get(self, parser, section, option, value, defaults):
|
|
|
af3cbb |
+ L = []
|
|
|
af3cbb |
+ self._interpolate_some(parser, option, L, value, section, defaults, 1)
|
|
|
af3cbb |
+ return ''.join(L)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_set(self, parser, section, option, value):
|
|
|
af3cbb |
+ tmp_value = value.replace('$$', '') # escaped dollar signs
|
|
|
af3cbb |
+ tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
|
|
|
af3cbb |
+ if '$' in tmp_value:
|
|
|
af3cbb |
+ raise ValueError("invalid interpolation syntax in %r at "
|
|
|
af3cbb |
+ "position %d" % (value, tmp_value.find('$')))
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _interpolate_some(self, parser, option, accum, rest, section, map,
|
|
|
af3cbb |
+ depth):
|
|
|
af3cbb |
+ rawval = parser.get(section, option, raw=True, fallback=rest)
|
|
|
af3cbb |
+ if depth > MAX_INTERPOLATION_DEPTH:
|
|
|
af3cbb |
+ raise InterpolationDepthError(option, section, rawval)
|
|
|
af3cbb |
+ while rest:
|
|
|
af3cbb |
+ p = rest.find("$")
|
|
|
af3cbb |
+ if p < 0:
|
|
|
af3cbb |
+ accum.append(rest)
|
|
|
af3cbb |
+ return
|
|
|
af3cbb |
+ if p > 0:
|
|
|
af3cbb |
+ accum.append(rest[:p])
|
|
|
af3cbb |
+ rest = rest[p:]
|
|
|
af3cbb |
+ # p is no longer used
|
|
|
af3cbb |
+ c = rest[1:2]
|
|
|
af3cbb |
+ if c == "$":
|
|
|
af3cbb |
+ accum.append("$")
|
|
|
af3cbb |
+ rest = rest[2:]
|
|
|
af3cbb |
+ elif c == "{":
|
|
|
af3cbb |
+ m = self._KEYCRE.match(rest)
|
|
|
af3cbb |
+ if m is None:
|
|
|
af3cbb |
+ raise InterpolationSyntaxError(option, section,
|
|
|
af3cbb |
+ "bad interpolation variable reference %r" % rest)
|
|
|
af3cbb |
+ path = m.group(1).split(':')
|
|
|
af3cbb |
+ rest = rest[m.end():]
|
|
|
af3cbb |
+ sect = section
|
|
|
af3cbb |
+ opt = option
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ if len(path) == 1:
|
|
|
af3cbb |
+ opt = parser.optionxform(path[0])
|
|
|
af3cbb |
+ v = map[opt]
|
|
|
af3cbb |
+ elif len(path) == 2:
|
|
|
af3cbb |
+ sect = path[0]
|
|
|
af3cbb |
+ opt = parser.optionxform(path[1])
|
|
|
af3cbb |
+ v = parser.get(sect, opt, raw=True)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ raise InterpolationSyntaxError(
|
|
|
af3cbb |
+ option, section,
|
|
|
af3cbb |
+ "More than one ':' found: %r" % (rest,))
|
|
|
af3cbb |
+ except (KeyError, NoSectionError, NoOptionError):
|
|
|
af3cbb |
+ raise from_none(InterpolationMissingOptionError(
|
|
|
af3cbb |
+ option, section, rawval, ":".join(path)))
|
|
|
af3cbb |
+ if "$" in v:
|
|
|
af3cbb |
+ self._interpolate_some(parser, opt, accum, v, sect,
|
|
|
af3cbb |
+ dict(parser.items(sect, raw=True)),
|
|
|
af3cbb |
+ depth + 1)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ accum.append(v)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ raise InterpolationSyntaxError(
|
|
|
af3cbb |
+ option, section,
|
|
|
af3cbb |
+ "'$' must be followed by '$' or '{', "
|
|
|
af3cbb |
+ "found: %r" % (rest,))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class LegacyInterpolation(Interpolation):
|
|
|
af3cbb |
+ """Deprecated interpolation used in old versions of ConfigParser.
|
|
|
af3cbb |
+ Use BasicInterpolation or ExtendedInterpolation instead."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_get(self, parser, section, option, value, vars):
|
|
|
af3cbb |
+ rawval = value
|
|
|
af3cbb |
+ depth = MAX_INTERPOLATION_DEPTH
|
|
|
af3cbb |
+ while depth: # Loop through this until it's done
|
|
|
af3cbb |
+ depth -= 1
|
|
|
af3cbb |
+ if value and "%(" in value:
|
|
|
af3cbb |
+ replace = functools.partial(self._interpolation_replace,
|
|
|
af3cbb |
+ parser=parser)
|
|
|
af3cbb |
+ value = self._KEYCRE.sub(replace, value)
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ value = value % vars
|
|
|
af3cbb |
+ except KeyError as e:
|
|
|
af3cbb |
+ raise from_none(InterpolationMissingOptionError(
|
|
|
af3cbb |
+ option, section, rawval, e.args[0]))
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ break
|
|
|
af3cbb |
+ if value and "%(" in value:
|
|
|
af3cbb |
+ raise InterpolationDepthError(option, section, rawval)
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def before_set(self, parser, section, option, value):
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @staticmethod
|
|
|
af3cbb |
+ def _interpolation_replace(match, parser):
|
|
|
af3cbb |
+ s = match.group(1)
|
|
|
af3cbb |
+ if s is None:
|
|
|
af3cbb |
+ return match.group()
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ return "%%(%s)s" % parser.optionxform(s)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class RawConfigParser(MutableMapping):
|
|
|
af3cbb |
+ """ConfigParser that does not do interpolation."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ # Regular expressions for parsing section headers and options
|
|
|
af3cbb |
+ _SECT_TMPL = r"""
|
|
|
af3cbb |
+ \[ # [
|
|
|
af3cbb |
+ (?P<header>[^]]+) # very permissive!
|
|
|
af3cbb |
+ \] # ]
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ _OPT_TMPL = r"""
|
|
|
af3cbb |
+ (?P<option>.*?) # very permissive!
|
|
|
af3cbb |
+ \s*(?P<vi>{delim})\s* # any number of space/tab,
|
|
|
af3cbb |
+ # followed by any of the
|
|
|
af3cbb |
+ # allowed delimiters,
|
|
|
af3cbb |
+ # followed by any space/tab
|
|
|
af3cbb |
+ (?P<value>.*)$ # everything up to eol
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ _OPT_NV_TMPL = r"""
|
|
|
af3cbb |
+ (?P<option>.*?) # very permissive!
|
|
|
af3cbb |
+ \s*(?: # any number of space/tab,
|
|
|
af3cbb |
+ (?P<vi>{delim})\s* # optionally followed by
|
|
|
af3cbb |
+ # any of the allowed
|
|
|
af3cbb |
+ # delimiters, followed by any
|
|
|
af3cbb |
+ # space/tab
|
|
|
af3cbb |
+ (?P<value>.*))?$ # everything up to eol
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ # Interpolation algorithm to be used if the user does not specify another
|
|
|
af3cbb |
+ _DEFAULT_INTERPOLATION = Interpolation()
|
|
|
af3cbb |
+ # Compiled regular expression for matching sections
|
|
|
af3cbb |
+ SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
|
|
|
af3cbb |
+ # Compiled regular expression for matching options with typical separators
|
|
|
af3cbb |
+ OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
|
|
|
af3cbb |
+ # Compiled regular expression for matching options with optional values
|
|
|
af3cbb |
+ # delimited using typical separators
|
|
|
af3cbb |
+ OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
|
|
|
af3cbb |
+ # Compiled regular expression for matching leading whitespace in a line
|
|
|
af3cbb |
+ NONSPACECRE = re.compile(r"\S")
|
|
|
af3cbb |
+ # Possible boolean values in the configuration.
|
|
|
af3cbb |
+ BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
|
|
|
af3cbb |
+ '0': False, 'no': False, 'false': False, 'off': False}
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, defaults=None, dict_type=_default_dict,
|
|
|
af3cbb |
+ allow_no_value=False, **kwargs):
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ delimiters = kwargs.get('delimiters', ('=', ':'))
|
|
|
af3cbb |
+ comment_prefixes = kwargs.get('comment_prefixes', ('#', ';'))
|
|
|
af3cbb |
+ inline_comment_prefixes = kwargs.get('inline_comment_prefixes', None)
|
|
|
af3cbb |
+ strict = kwargs.get('strict', True)
|
|
|
af3cbb |
+ empty_lines_in_values = kwargs.get('empty_lines_in_values', True)
|
|
|
af3cbb |
+ default_section = kwargs.get('default_section', DEFAULTSECT)
|
|
|
af3cbb |
+ interpolation = kwargs.get('interpolation', _UNSET)
|
|
|
af3cbb |
+ converters = kwargs.get('converters', _UNSET)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ self._dict = dict_type
|
|
|
af3cbb |
+ self._sections = self._dict()
|
|
|
af3cbb |
+ self._defaults = self._dict()
|
|
|
af3cbb |
+ self._converters = ConverterMapping(self)
|
|
|
af3cbb |
+ self._proxies = self._dict()
|
|
|
af3cbb |
+ self._proxies[default_section] = SectionProxy(self, default_section)
|
|
|
af3cbb |
+ if defaults:
|
|
|
af3cbb |
+ for key, value in defaults.items():
|
|
|
af3cbb |
+ self._defaults[self.optionxform(key)] = value
|
|
|
af3cbb |
+ self._delimiters = tuple(delimiters)
|
|
|
af3cbb |
+ if delimiters == ('=', ':'):
|
|
|
af3cbb |
+ self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ d = "|".join(re.escape(d) for d in delimiters)
|
|
|
af3cbb |
+ if allow_no_value:
|
|
|
af3cbb |
+ self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
|
|
|
af3cbb |
+ re.VERBOSE)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
|
|
|
af3cbb |
+ re.VERBOSE)
|
|
|
af3cbb |
+ self._comment_prefixes = tuple(comment_prefixes or ())
|
|
|
af3cbb |
+ self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
|
|
|
af3cbb |
+ self._strict = strict
|
|
|
af3cbb |
+ self._allow_no_value = allow_no_value
|
|
|
af3cbb |
+ self._empty_lines_in_values = empty_lines_in_values
|
|
|
af3cbb |
+ self.default_section=default_section
|
|
|
af3cbb |
+ self._interpolation = interpolation
|
|
|
af3cbb |
+ if self._interpolation is _UNSET:
|
|
|
af3cbb |
+ self._interpolation = self._DEFAULT_INTERPOLATION
|
|
|
af3cbb |
+ if self._interpolation is None:
|
|
|
af3cbb |
+ self._interpolation = Interpolation()
|
|
|
af3cbb |
+ if converters is not _UNSET:
|
|
|
af3cbb |
+ self._converters.update(converters)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def defaults(self):
|
|
|
af3cbb |
+ return self._defaults
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def sections(self):
|
|
|
af3cbb |
+ """Return a list of section names, excluding [DEFAULT]"""
|
|
|
af3cbb |
+ # self._sections will never have [DEFAULT] in it
|
|
|
af3cbb |
+ return list(self._sections.keys())
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def add_section(self, section):
|
|
|
af3cbb |
+ """Create a new section in the configuration.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Raise DuplicateSectionError if a section by the specified name
|
|
|
af3cbb |
+ already exists. Raise ValueError if name is DEFAULT.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if section == self.default_section:
|
|
|
af3cbb |
+ raise ValueError('Invalid section name: %r' % section)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ if section in self._sections:
|
|
|
af3cbb |
+ raise DuplicateSectionError(section)
|
|
|
af3cbb |
+ self._sections[section] = self._dict()
|
|
|
af3cbb |
+ self._proxies[section] = SectionProxy(self, section)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def has_section(self, section):
|
|
|
af3cbb |
+ """Indicate whether the named section is present in the configuration.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The DEFAULT section is not acknowledged.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ return section in self._sections
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def options(self, section):
|
|
|
af3cbb |
+ """Return a list of option names for the given section name."""
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ opts = self._sections[section].copy()
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise from_none(NoSectionError(section))
|
|
|
af3cbb |
+ opts.update(self._defaults)
|
|
|
af3cbb |
+ return list(opts.keys())
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def read(self, filenames, encoding=None):
|
|
|
af3cbb |
+ """Read and parse a filename or a list of filenames.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Files that cannot be opened are silently ignored; this is
|
|
|
af3cbb |
+ designed so that you can specify a list of potential
|
|
|
af3cbb |
+ configuration file locations (e.g. current directory, user's
|
|
|
af3cbb |
+ home directory, systemwide directory), and all existing
|
|
|
af3cbb |
+ configuration files in the list will be read. A single
|
|
|
af3cbb |
+ filename may also be given.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Return list of successfully read files.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if PY2 and isinstance(filenames, bytes):
|
|
|
af3cbb |
+ # we allow for a little unholy magic for Python 2 so that
|
|
|
af3cbb |
+ # people not using unicode_literals can still use the library
|
|
|
af3cbb |
+ # conveniently
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "You passed a bytestring as `filenames`. This will not work"
|
|
|
af3cbb |
+ " on Python 3. Use `cp.read_file()` or switch to using Unicode"
|
|
|
af3cbb |
+ " strings across the board.",
|
|
|
af3cbb |
+ DeprecationWarning,
|
|
|
af3cbb |
+ stacklevel=2,
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+ filenames = [filenames]
|
|
|
af3cbb |
+ elif isinstance(filenames, str):
|
|
|
af3cbb |
+ filenames = [filenames]
|
|
|
af3cbb |
+ read_ok = []
|
|
|
af3cbb |
+ for filename in filenames:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ with open(filename, encoding=encoding) as fp:
|
|
|
af3cbb |
+ self._read(fp, filename)
|
|
|
af3cbb |
+ except IOError:
|
|
|
af3cbb |
+ continue
|
|
|
af3cbb |
+ read_ok.append(filename)
|
|
|
af3cbb |
+ return read_ok
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def read_file(self, f, source=None):
|
|
|
af3cbb |
+ """Like read() but the argument must be a file-like object.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The `f' argument must be iterable, returning one line at a time.
|
|
|
af3cbb |
+ Optional second argument is the `source' specifying the name of the
|
|
|
af3cbb |
+ file being read. If not given, it is taken from f.name. If `f' has no
|
|
|
af3cbb |
+ `name' attribute, `' is used.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if source is None:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ source = f.name
|
|
|
af3cbb |
+ except AttributeError:
|
|
|
af3cbb |
+ source = ''
|
|
|
af3cbb |
+ self._read(f, source)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def read_string(self, string, source='<string>'):
|
|
|
af3cbb |
+ """Read configuration from a given string."""
|
|
|
af3cbb |
+ sfile = io.StringIO(string)
|
|
|
af3cbb |
+ self.read_file(sfile, source)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def read_dict(self, dictionary, source='<dict>'):
|
|
|
af3cbb |
+ """Read configuration from a dictionary.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Keys are section names, values are dictionaries with keys and values
|
|
|
af3cbb |
+ that should be present in the section. If the used dictionary type
|
|
|
af3cbb |
+ preserves order, sections and their keys will be added in order.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ All types held in the dictionary are converted to strings during
|
|
|
af3cbb |
+ reading, including section names, option names and keys.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Optional second argument is the `source' specifying the name of the
|
|
|
af3cbb |
+ dictionary being read.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ elements_added = set()
|
|
|
af3cbb |
+ for section, keys in dictionary.items():
|
|
|
af3cbb |
+ section = str(section)
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ self.add_section(section)
|
|
|
af3cbb |
+ except (DuplicateSectionError, ValueError):
|
|
|
af3cbb |
+ if self._strict and section in elements_added:
|
|
|
af3cbb |
+ raise
|
|
|
af3cbb |
+ elements_added.add(section)
|
|
|
af3cbb |
+ for key, value in keys.items():
|
|
|
af3cbb |
+ key = self.optionxform(str(key))
|
|
|
af3cbb |
+ if value is not None:
|
|
|
af3cbb |
+ value = str(value)
|
|
|
af3cbb |
+ if self._strict and (section, key) in elements_added:
|
|
|
af3cbb |
+ raise DuplicateOptionError(section, key, source)
|
|
|
af3cbb |
+ elements_added.add((section, key))
|
|
|
af3cbb |
+ self.set(section, key, value)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def readfp(self, fp, filename=None):
|
|
|
af3cbb |
+ """Deprecated, use read_file instead."""
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "This method will be removed in future versions. "
|
|
|
af3cbb |
+ "Use 'parser.read_file()' instead.",
|
|
|
af3cbb |
+ DeprecationWarning, stacklevel=2
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+ self.read_file(fp, source=filename)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def get(self, section, option, **kwargs):
|
|
|
af3cbb |
+ """Get an option value for a given section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ If `vars' is provided, it must be a dictionary. The option is looked up
|
|
|
af3cbb |
+ in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
|
|
|
af3cbb |
+ If the key is not found and `fallback' is provided, it is used as
|
|
|
af3cbb |
+ a fallback value. `None' can be provided as a `fallback' value.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ If interpolation is enabled and the optional argument `raw' is False,
|
|
|
af3cbb |
+ all interpolations are expanded in the return values.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Arguments `raw', `vars', and `fallback' are keyword only.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The section DEFAULT is special.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ raw = kwargs.get('raw', False)
|
|
|
af3cbb |
+ vars = kwargs.get('vars', None)
|
|
|
af3cbb |
+ fallback = kwargs.get('fallback', _UNSET)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ d = self._unify_values(section, vars)
|
|
|
af3cbb |
+ except NoSectionError:
|
|
|
af3cbb |
+ if fallback is _UNSET:
|
|
|
af3cbb |
+ raise
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ return fallback
|
|
|
af3cbb |
+ option = self.optionxform(option)
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ value = d[option]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ if fallback is _UNSET:
|
|
|
af3cbb |
+ raise NoOptionError(option, section)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ return fallback
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ if raw or value is None:
|
|
|
af3cbb |
+ return value
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ return self._interpolation.before_get(self, section, option, value,
|
|
|
af3cbb |
+ d)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _get(self, section, conv, option, **kwargs):
|
|
|
af3cbb |
+ return conv(self.get(section, option, **kwargs))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _get_conv(self, section, option, conv, **kwargs):
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ kwargs.setdefault('raw', False)
|
|
|
af3cbb |
+ kwargs.setdefault('vars', None)
|
|
|
af3cbb |
+ fallback = kwargs.pop('fallback', _UNSET)
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ return self._get(section, conv, option, **kwargs)
|
|
|
af3cbb |
+ except (NoSectionError, NoOptionError):
|
|
|
af3cbb |
+ if fallback is _UNSET:
|
|
|
af3cbb |
+ raise
|
|
|
af3cbb |
+ return fallback
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ # getint, getfloat and getboolean provided directly for backwards compat
|
|
|
af3cbb |
+ def getint(self, section, option, **kwargs):
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ kwargs.setdefault('raw', False)
|
|
|
af3cbb |
+ kwargs.setdefault('vars', None)
|
|
|
af3cbb |
+ kwargs.setdefault('fallback', _UNSET)
|
|
|
af3cbb |
+ return self._get_conv(section, option, int, **kwargs)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def getfloat(self, section, option, **kwargs):
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ kwargs.setdefault('raw', False)
|
|
|
af3cbb |
+ kwargs.setdefault('vars', None)
|
|
|
af3cbb |
+ kwargs.setdefault('fallback', _UNSET)
|
|
|
af3cbb |
+ return self._get_conv(section, option, float, **kwargs)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def getboolean(self, section, option, **kwargs):
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ kwargs.setdefault('raw', False)
|
|
|
af3cbb |
+ kwargs.setdefault('vars', None)
|
|
|
af3cbb |
+ kwargs.setdefault('fallback', _UNSET)
|
|
|
af3cbb |
+ return self._get_conv(section, option, self._convert_to_boolean,
|
|
|
af3cbb |
+ **kwargs)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def items(self, section=_UNSET, raw=False, vars=None):
|
|
|
af3cbb |
+ """Return a list of (name, value) tuples for each option in a section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ All % interpolations are expanded in the return values, based on the
|
|
|
af3cbb |
+ defaults passed into the constructor, unless the optional argument
|
|
|
af3cbb |
+ `raw' is true. Additional substitutions may be provided using the
|
|
|
af3cbb |
+ `vars' argument, which must be a dictionary whose contents overrides
|
|
|
af3cbb |
+ any pre-existing defaults.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The section DEFAULT is special.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if section is _UNSET:
|
|
|
af3cbb |
+ return super(RawConfigParser, self).items()
|
|
|
af3cbb |
+ d = self._defaults.copy()
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ d.update(self._sections[section])
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ if section != self.default_section:
|
|
|
af3cbb |
+ raise NoSectionError(section)
|
|
|
af3cbb |
+ # Update with the entry specific variables
|
|
|
af3cbb |
+ if vars:
|
|
|
af3cbb |
+ for key, value in vars.items():
|
|
|
af3cbb |
+ d[self.optionxform(key)] = value
|
|
|
af3cbb |
+ value_getter = lambda option: self._interpolation.before_get(self,
|
|
|
af3cbb |
+ section, option, d[option], d)
|
|
|
af3cbb |
+ if raw:
|
|
|
af3cbb |
+ value_getter = lambda option: d[option]
|
|
|
af3cbb |
+ return [(option, value_getter(option)) for option in d.keys()]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def popitem(self):
|
|
|
af3cbb |
+ """Remove a section from the parser and return it as
|
|
|
af3cbb |
+ a (section_name, section_proxy) tuple. If no section is present, raise
|
|
|
af3cbb |
+ KeyError.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The section DEFAULT is never returned because it cannot be removed.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ for key in self.sections():
|
|
|
af3cbb |
+ value = self[key]
|
|
|
af3cbb |
+ del self[key]
|
|
|
af3cbb |
+ return key, value
|
|
|
af3cbb |
+ raise KeyError
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def optionxform(self, optionstr):
|
|
|
af3cbb |
+ return optionstr.lower()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def has_option(self, section, option):
|
|
|
af3cbb |
+ """Check for the existence of a given option in a given section.
|
|
|
af3cbb |
+ If the specified `section' is None or an empty string, DEFAULT is
|
|
|
af3cbb |
+ assumed. If the specified `section' does not exist, returns False."""
|
|
|
af3cbb |
+ if not section or section == self.default_section:
|
|
|
af3cbb |
+ option = self.optionxform(option)
|
|
|
af3cbb |
+ return option in self._defaults
|
|
|
af3cbb |
+ elif section not in self._sections:
|
|
|
af3cbb |
+ return False
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ option = self.optionxform(option)
|
|
|
af3cbb |
+ return (option in self._sections[section]
|
|
|
af3cbb |
+ or option in self._defaults)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def set(self, section, option, value=None):
|
|
|
af3cbb |
+ """Set an option."""
|
|
|
af3cbb |
+ if value:
|
|
|
af3cbb |
+ value = self._interpolation.before_set(self, section, option,
|
|
|
af3cbb |
+ value)
|
|
|
af3cbb |
+ if not section or section == self.default_section:
|
|
|
af3cbb |
+ sectdict = self._defaults
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ sectdict = self._sections[section]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise from_none(NoSectionError(section))
|
|
|
af3cbb |
+ sectdict[self.optionxform(option)] = value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def write(self, fp, space_around_delimiters=True):
|
|
|
af3cbb |
+ """Write an .ini-format representation of the configuration state.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ If `space_around_delimiters' is True (the default), delimiters
|
|
|
af3cbb |
+ between keys and values are surrounded by spaces.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if space_around_delimiters:
|
|
|
af3cbb |
+ d = " {0} ".format(self._delimiters[0])
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ d = self._delimiters[0]
|
|
|
af3cbb |
+ if self._defaults:
|
|
|
af3cbb |
+ self._write_section(fp, self.default_section,
|
|
|
af3cbb |
+ self._defaults.items(), d)
|
|
|
af3cbb |
+ for section in self._sections:
|
|
|
af3cbb |
+ self._write_section(fp, section,
|
|
|
af3cbb |
+ self._sections[section].items(), d)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _write_section(self, fp, section_name, section_items, delimiter):
|
|
|
af3cbb |
+ """Write a single section to the specified `fp'."""
|
|
|
af3cbb |
+ fp.write("[{0}]\n".format(section_name))
|
|
|
af3cbb |
+ for key, value in section_items:
|
|
|
af3cbb |
+ value = self._interpolation.before_write(self, section_name, key,
|
|
|
af3cbb |
+ value)
|
|
|
af3cbb |
+ if value is not None or not self._allow_no_value:
|
|
|
af3cbb |
+ value = delimiter + str(value).replace('\n', '\n\t')
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ value = ""
|
|
|
af3cbb |
+ fp.write("{0}{1}\n".format(key, value))
|
|
|
af3cbb |
+ fp.write("\n")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def remove_option(self, section, option):
|
|
|
af3cbb |
+ """Remove an option."""
|
|
|
af3cbb |
+ if not section or section == self.default_section:
|
|
|
af3cbb |
+ sectdict = self._defaults
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ sectdict = self._sections[section]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise from_none(NoSectionError(section))
|
|
|
af3cbb |
+ option = self.optionxform(option)
|
|
|
af3cbb |
+ existed = option in sectdict
|
|
|
af3cbb |
+ if existed:
|
|
|
af3cbb |
+ del sectdict[option]
|
|
|
af3cbb |
+ return existed
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def remove_section(self, section):
|
|
|
af3cbb |
+ """Remove a file section."""
|
|
|
af3cbb |
+ existed = section in self._sections
|
|
|
af3cbb |
+ if existed:
|
|
|
af3cbb |
+ del self._sections[section]
|
|
|
af3cbb |
+ del self._proxies[section]
|
|
|
af3cbb |
+ return existed
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __getitem__(self, key):
|
|
|
af3cbb |
+ if key != self.default_section and not self.has_section(key):
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+ return self._proxies[key]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __setitem__(self, key, value):
|
|
|
af3cbb |
+ # To conform with the mapping protocol, overwrites existing values in
|
|
|
af3cbb |
+ # the section.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ # XXX this is not atomic if read_dict fails at any point. Then again,
|
|
|
af3cbb |
+ # no update method in configparser is atomic in this implementation.
|
|
|
af3cbb |
+ if key == self.default_section:
|
|
|
af3cbb |
+ self._defaults.clear()
|
|
|
af3cbb |
+ elif key in self._sections:
|
|
|
af3cbb |
+ self._sections[key].clear()
|
|
|
af3cbb |
+ self.read_dict({key: value})
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __delitem__(self, key):
|
|
|
af3cbb |
+ if key == self.default_section:
|
|
|
af3cbb |
+ raise ValueError("Cannot remove the default section.")
|
|
|
af3cbb |
+ if not self.has_section(key):
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+ self.remove_section(key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __contains__(self, key):
|
|
|
af3cbb |
+ return key == self.default_section or self.has_section(key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __len__(self):
|
|
|
af3cbb |
+ return len(self._sections) + 1 # the default section
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __iter__(self):
|
|
|
af3cbb |
+ # XXX does it break when underlying container state changed?
|
|
|
af3cbb |
+ return itertools.chain((self.default_section,), self._sections.keys())
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _read(self, fp, fpname):
|
|
|
af3cbb |
+ """Parse a sectioned configuration file.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Each section in a configuration file contains a header, indicated by
|
|
|
af3cbb |
+ a name in square brackets (`[]'), plus key/value options, indicated by
|
|
|
af3cbb |
+ `name' and `value' delimited with a specific substring (`=' or `:' by
|
|
|
af3cbb |
+ default).
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Values can span multiple lines, as long as they are indented deeper
|
|
|
af3cbb |
+ than the first line of the value. Depending on the parser's mode, blank
|
|
|
af3cbb |
+ lines may be treated as parts of multiline values or ignored.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Configuration files may include comments, prefixed by specific
|
|
|
af3cbb |
+ characters (`#' and `;' by default). Comments may appear on their own
|
|
|
af3cbb |
+ in an otherwise empty line or may be entered in lines holding values or
|
|
|
af3cbb |
+ section names.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ elements_added = set()
|
|
|
af3cbb |
+ cursect = None # None, or a dictionary
|
|
|
af3cbb |
+ sectname = None
|
|
|
af3cbb |
+ optname = None
|
|
|
af3cbb |
+ lineno = 0
|
|
|
af3cbb |
+ indent_level = 0
|
|
|
af3cbb |
+ e = None # None, or an exception
|
|
|
af3cbb |
+ for lineno, line in enumerate(fp, start=1):
|
|
|
af3cbb |
+ comment_start = sys.maxsize
|
|
|
af3cbb |
+ # strip inline comments
|
|
|
af3cbb |
+ inline_prefixes = dict(
|
|
|
af3cbb |
+ (p, -1) for p in self._inline_comment_prefixes)
|
|
|
af3cbb |
+ while comment_start == sys.maxsize and inline_prefixes:
|
|
|
af3cbb |
+ next_prefixes = {}
|
|
|
af3cbb |
+ for prefix, index in inline_prefixes.items():
|
|
|
af3cbb |
+ index = line.find(prefix, index+1)
|
|
|
af3cbb |
+ if index == -1:
|
|
|
af3cbb |
+ continue
|
|
|
af3cbb |
+ next_prefixes[prefix] = index
|
|
|
af3cbb |
+ if index == 0 or (index > 0 and line[index-1].isspace()):
|
|
|
af3cbb |
+ comment_start = min(comment_start, index)
|
|
|
af3cbb |
+ inline_prefixes = next_prefixes
|
|
|
af3cbb |
+ # strip full line comments
|
|
|
af3cbb |
+ for prefix in self._comment_prefixes:
|
|
|
af3cbb |
+ if line.strip().startswith(prefix):
|
|
|
af3cbb |
+ comment_start = 0
|
|
|
af3cbb |
+ break
|
|
|
af3cbb |
+ if comment_start == sys.maxsize:
|
|
|
af3cbb |
+ comment_start = None
|
|
|
af3cbb |
+ value = line[:comment_start].strip()
|
|
|
af3cbb |
+ if not value:
|
|
|
af3cbb |
+ if self._empty_lines_in_values:
|
|
|
af3cbb |
+ # add empty line to the value, but only if there was no
|
|
|
af3cbb |
+ # comment on the line
|
|
|
af3cbb |
+ if (comment_start is None and
|
|
|
af3cbb |
+ cursect is not None and
|
|
|
af3cbb |
+ optname and
|
|
|
af3cbb |
+ cursect[optname] is not None):
|
|
|
af3cbb |
+ cursect[optname].append('') # newlines added at join
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ # empty line marks end of value
|
|
|
af3cbb |
+ indent_level = sys.maxsize
|
|
|
af3cbb |
+ continue
|
|
|
af3cbb |
+ # continuation line?
|
|
|
af3cbb |
+ first_nonspace = self.NONSPACECRE.search(line)
|
|
|
af3cbb |
+ cur_indent_level = first_nonspace.start() if first_nonspace else 0
|
|
|
af3cbb |
+ if (cursect is not None and optname and
|
|
|
af3cbb |
+ cur_indent_level > indent_level):
|
|
|
af3cbb |
+ cursect[optname].append(value)
|
|
|
af3cbb |
+ # a section header or option header?
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ indent_level = cur_indent_level
|
|
|
af3cbb |
+ # is it a section header?
|
|
|
af3cbb |
+ mo = self.SECTCRE.match(value)
|
|
|
af3cbb |
+ if mo:
|
|
|
af3cbb |
+ sectname = mo.group('header')
|
|
|
af3cbb |
+ if sectname in self._sections:
|
|
|
af3cbb |
+ if self._strict and sectname in elements_added:
|
|
|
af3cbb |
+ raise DuplicateSectionError(sectname, fpname,
|
|
|
af3cbb |
+ lineno)
|
|
|
af3cbb |
+ cursect = self._sections[sectname]
|
|
|
af3cbb |
+ elements_added.add(sectname)
|
|
|
af3cbb |
+ elif sectname == self.default_section:
|
|
|
af3cbb |
+ cursect = self._defaults
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ cursect = self._dict()
|
|
|
af3cbb |
+ self._sections[sectname] = cursect
|
|
|
af3cbb |
+ self._proxies[sectname] = SectionProxy(self, sectname)
|
|
|
af3cbb |
+ elements_added.add(sectname)
|
|
|
af3cbb |
+ # So sections can't start with a continuation line
|
|
|
af3cbb |
+ optname = None
|
|
|
af3cbb |
+ # no section header in the file?
|
|
|
af3cbb |
+ elif cursect is None:
|
|
|
af3cbb |
+ raise MissingSectionHeaderError(fpname, lineno, line)
|
|
|
af3cbb |
+ # an option line?
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ mo = self._optcre.match(value)
|
|
|
af3cbb |
+ if mo:
|
|
|
af3cbb |
+ optname, vi, optval = mo.group('option', 'vi', 'value')
|
|
|
af3cbb |
+ if not optname:
|
|
|
af3cbb |
+ e = self._handle_error(e, fpname, lineno, line)
|
|
|
af3cbb |
+ optname = self.optionxform(optname.rstrip())
|
|
|
af3cbb |
+ if (self._strict and
|
|
|
af3cbb |
+ (sectname, optname) in elements_added):
|
|
|
af3cbb |
+ raise DuplicateOptionError(sectname, optname,
|
|
|
af3cbb |
+ fpname, lineno)
|
|
|
af3cbb |
+ elements_added.add((sectname, optname))
|
|
|
af3cbb |
+ # This check is fine because the OPTCRE cannot
|
|
|
af3cbb |
+ # match if it would set optval to None
|
|
|
af3cbb |
+ if optval is not None:
|
|
|
af3cbb |
+ optval = optval.strip()
|
|
|
af3cbb |
+ cursect[optname] = [optval]
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ # valueless option handling
|
|
|
af3cbb |
+ cursect[optname] = None
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ # a non-fatal parsing error occurred. set up the
|
|
|
af3cbb |
+ # exception but keep going. the exception will be
|
|
|
af3cbb |
+ # raised at the end of the file and will contain a
|
|
|
af3cbb |
+ # list of all bogus lines
|
|
|
af3cbb |
+ e = self._handle_error(e, fpname, lineno, line)
|
|
|
af3cbb |
+ # if any parsing errors occurred, raise an exception
|
|
|
af3cbb |
+ if e:
|
|
|
af3cbb |
+ raise e
|
|
|
af3cbb |
+ self._join_multiline_values()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _join_multiline_values(self):
|
|
|
af3cbb |
+ defaults = self.default_section, self._defaults
|
|
|
af3cbb |
+ all_sections = itertools.chain((defaults,),
|
|
|
af3cbb |
+ self._sections.items())
|
|
|
af3cbb |
+ for section, options in all_sections:
|
|
|
af3cbb |
+ for name, val in options.items():
|
|
|
af3cbb |
+ if isinstance(val, list):
|
|
|
af3cbb |
+ val = '\n'.join(val).rstrip()
|
|
|
af3cbb |
+ options[name] = self._interpolation.before_read(self,
|
|
|
af3cbb |
+ section,
|
|
|
af3cbb |
+ name, val)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _handle_error(self, exc, fpname, lineno, line):
|
|
|
af3cbb |
+ if not exc:
|
|
|
af3cbb |
+ exc = ParsingError(fpname)
|
|
|
af3cbb |
+ exc.append(lineno, repr(line))
|
|
|
af3cbb |
+ return exc
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _unify_values(self, section, vars):
|
|
|
af3cbb |
+ """Create a sequence of lookups with 'vars' taking priority over
|
|
|
af3cbb |
+ the 'section' which takes priority over the DEFAULTSECT.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ sectiondict = {}
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ sectiondict = self._sections[section]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ if section != self.default_section:
|
|
|
af3cbb |
+ raise NoSectionError(section)
|
|
|
af3cbb |
+ # Update with the entry specific variables
|
|
|
af3cbb |
+ vardict = {}
|
|
|
af3cbb |
+ if vars:
|
|
|
af3cbb |
+ for key, value in vars.items():
|
|
|
af3cbb |
+ if value is not None:
|
|
|
af3cbb |
+ value = str(value)
|
|
|
af3cbb |
+ vardict[self.optionxform(key)] = value
|
|
|
af3cbb |
+ return _ChainMap(vardict, sectiondict, self._defaults)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _convert_to_boolean(self, value):
|
|
|
af3cbb |
+ """Return a boolean value translating from other types if necessary.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ if value.lower() not in self.BOOLEAN_STATES:
|
|
|
af3cbb |
+ raise ValueError('Not a boolean: %s' % value)
|
|
|
af3cbb |
+ return self.BOOLEAN_STATES[value.lower()]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _validate_value_types(self, **kwargs):
|
|
|
af3cbb |
+ """Raises a TypeError for non-string values.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The only legal non-string value if we allow valueless
|
|
|
af3cbb |
+ options is None, so we need to check if the value is a
|
|
|
af3cbb |
+ string if:
|
|
|
af3cbb |
+ - we do not allow valueless options, or
|
|
|
af3cbb |
+ - we allow valueless options but the value is not None
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ For compatibility reasons this method is not used in classic set()
|
|
|
af3cbb |
+ for RawConfigParsers. It is invoked in every case for mapping protocol
|
|
|
af3cbb |
+ access and in ConfigParser.set().
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ section = kwargs.get('section', "")
|
|
|
af3cbb |
+ option = kwargs.get('option', "")
|
|
|
af3cbb |
+ value = kwargs.get('value', "")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ if PY2 and bytes in (type(section), type(option), type(value)):
|
|
|
af3cbb |
+ # we allow for a little unholy magic for Python 2 so that
|
|
|
af3cbb |
+ # people not using unicode_literals can still use the library
|
|
|
af3cbb |
+ # conveniently
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "You passed a bytestring. Implicitly decoding as UTF-8 string."
|
|
|
af3cbb |
+ " This will not work on Python 3. Please switch to using"
|
|
|
af3cbb |
+ " Unicode strings across the board.",
|
|
|
af3cbb |
+ DeprecationWarning,
|
|
|
af3cbb |
+ stacklevel=2,
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+ if isinstance(section, bytes):
|
|
|
af3cbb |
+ section = section.decode('utf8')
|
|
|
af3cbb |
+ if isinstance(option, bytes):
|
|
|
af3cbb |
+ option = option.decode('utf8')
|
|
|
af3cbb |
+ if isinstance(value, bytes):
|
|
|
af3cbb |
+ value = value.decode('utf8')
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ if not isinstance(section, str):
|
|
|
af3cbb |
+ raise TypeError("section names must be strings")
|
|
|
af3cbb |
+ if not isinstance(option, str):
|
|
|
af3cbb |
+ raise TypeError("option keys must be strings")
|
|
|
af3cbb |
+ if not self._allow_no_value or value:
|
|
|
af3cbb |
+ if not isinstance(value, str):
|
|
|
af3cbb |
+ raise TypeError("option values must be strings")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ return section, option, value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @property
|
|
|
af3cbb |
+ def converters(self):
|
|
|
af3cbb |
+ return self._converters
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class ConfigParser(RawConfigParser):
|
|
|
af3cbb |
+ """ConfigParser implementing interpolation."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ _DEFAULT_INTERPOLATION = BasicInterpolation()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def set(self, section, option, value=None):
|
|
|
af3cbb |
+ """Set an option. Extends RawConfigParser.set by validating type and
|
|
|
af3cbb |
+ interpolation syntax on the value."""
|
|
|
af3cbb |
+ _, option, value = self._validate_value_types(option=option, value=value)
|
|
|
af3cbb |
+ super(ConfigParser, self).set(section, option, value)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def add_section(self, section):
|
|
|
af3cbb |
+ """Create a new section in the configuration. Extends
|
|
|
af3cbb |
+ RawConfigParser.add_section by validating if the section name is
|
|
|
af3cbb |
+ a string."""
|
|
|
af3cbb |
+ section, _, _ = self._validate_value_types(section=section)
|
|
|
af3cbb |
+ super(ConfigParser, self).add_section(section)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class SafeConfigParser(ConfigParser):
|
|
|
af3cbb |
+ """ConfigParser alias for backwards compatibility purposes."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, *args, **kwargs):
|
|
|
af3cbb |
+ super(SafeConfigParser, self).__init__(*args, **kwargs)
|
|
|
af3cbb |
+ warnings.warn(
|
|
|
af3cbb |
+ "The SafeConfigParser class has been renamed to ConfigParser "
|
|
|
af3cbb |
+ "in Python 3.2. This alias will be removed in future versions."
|
|
|
af3cbb |
+ " Use ConfigParser directly instead.",
|
|
|
af3cbb |
+ DeprecationWarning, stacklevel=2
|
|
|
af3cbb |
+ )
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class SectionProxy(MutableMapping):
|
|
|
af3cbb |
+ """A proxy for a single section from a parser."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, parser, name):
|
|
|
af3cbb |
+ """Creates a view on a section of the specified `name` in `parser`."""
|
|
|
af3cbb |
+ self._parser = parser
|
|
|
af3cbb |
+ self._name = name
|
|
|
af3cbb |
+ for conv in parser.converters:
|
|
|
af3cbb |
+ key = 'get' + conv
|
|
|
af3cbb |
+ getter = functools.partial(self.get, _impl=getattr(parser, key))
|
|
|
af3cbb |
+ setattr(self, key, getter)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __repr__(self):
|
|
|
af3cbb |
+ return '<Section: {0}>'.format(self._name)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __getitem__(self, key):
|
|
|
af3cbb |
+ if not self._parser.has_option(self._name, key):
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+ return self._parser.get(self._name, key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __setitem__(self, key, value):
|
|
|
af3cbb |
+ _, key, value = self._parser._validate_value_types(option=key, value=value)
|
|
|
af3cbb |
+ return self._parser.set(self._name, key, value)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __delitem__(self, key):
|
|
|
af3cbb |
+ if not (self._parser.has_option(self._name, key) and
|
|
|
af3cbb |
+ self._parser.remove_option(self._name, key)):
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __contains__(self, key):
|
|
|
af3cbb |
+ return self._parser.has_option(self._name, key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __len__(self):
|
|
|
af3cbb |
+ return len(self._options())
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __iter__(self):
|
|
|
af3cbb |
+ return self._options().__iter__()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def _options(self):
|
|
|
af3cbb |
+ if self._name != self._parser.default_section:
|
|
|
af3cbb |
+ return self._parser.options(self._name)
|
|
|
af3cbb |
+ else:
|
|
|
af3cbb |
+ return self._parser.defaults()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @property
|
|
|
af3cbb |
+ def parser(self):
|
|
|
af3cbb |
+ # The parser object of the proxy is read-only.
|
|
|
af3cbb |
+ return self._parser
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @property
|
|
|
af3cbb |
+ def name(self):
|
|
|
af3cbb |
+ # The name of the section on a proxy is read-only.
|
|
|
af3cbb |
+ return self._name
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def get(self, option, fallback=None, **kwargs):
|
|
|
af3cbb |
+ """Get an option value.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Unless `fallback` is provided, `None` will be returned if the option
|
|
|
af3cbb |
+ is not found.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+ # keyword-only arguments
|
|
|
af3cbb |
+ kwargs.setdefault('raw', False)
|
|
|
af3cbb |
+ kwargs.setdefault('vars', None)
|
|
|
af3cbb |
+ _impl = kwargs.pop('_impl', None)
|
|
|
af3cbb |
+ # If `_impl` is provided, it should be a getter method on the parser
|
|
|
af3cbb |
+ # object that provides the desired type conversion.
|
|
|
af3cbb |
+ if not _impl:
|
|
|
af3cbb |
+ _impl = self._parser.get
|
|
|
af3cbb |
+ return _impl(self._name, option, fallback=fallback, **kwargs)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+class ConverterMapping(MutableMapping):
|
|
|
af3cbb |
+ """Enables reuse of get*() methods between the parser and section proxies.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ If a parser class implements a getter directly, the value for the given
|
|
|
af3cbb |
+ key will be ``None``. The presence of the converter name here enables
|
|
|
af3cbb |
+ section proxies to find and use the implementation on the parser class.
|
|
|
af3cbb |
+ """
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ GETTERCRE = re.compile(r"^get(?P<name>.+)$")
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, parser):
|
|
|
af3cbb |
+ self._parser = parser
|
|
|
af3cbb |
+ self._data = {}
|
|
|
af3cbb |
+ for getter in dir(self._parser):
|
|
|
af3cbb |
+ m = self.GETTERCRE.match(getter)
|
|
|
af3cbb |
+ if not m or not callable(getattr(self._parser, getter)):
|
|
|
af3cbb |
+ continue
|
|
|
af3cbb |
+ self._data[m.group('name')] = None # See class docstring.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __getitem__(self, key):
|
|
|
af3cbb |
+ return self._data[key]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __setitem__(self, key, value):
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ k = 'get' + key
|
|
|
af3cbb |
+ except TypeError:
|
|
|
af3cbb |
+ raise ValueError('Incompatible key: {} (type: {})'
|
|
|
af3cbb |
+ ''.format(key, type(key)))
|
|
|
af3cbb |
+ if k == 'get':
|
|
|
af3cbb |
+ raise ValueError('Incompatible key: cannot use "" as a name')
|
|
|
af3cbb |
+ self._data[key] = value
|
|
|
af3cbb |
+ func = functools.partial(self._parser._get_conv, conv=value)
|
|
|
af3cbb |
+ func.converter = value
|
|
|
af3cbb |
+ setattr(self._parser, k, func)
|
|
|
af3cbb |
+ for proxy in self._parser.values():
|
|
|
af3cbb |
+ getter = functools.partial(proxy.get, _impl=func)
|
|
|
af3cbb |
+ setattr(proxy, k, getter)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __delitem__(self, key):
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ k = 'get' + (key or None)
|
|
|
af3cbb |
+ except TypeError:
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+ del self._data[key]
|
|
|
af3cbb |
+ for inst in itertools.chain((self._parser,), self._parser.values()):
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ delattr(inst, k)
|
|
|
af3cbb |
+ except AttributeError:
|
|
|
af3cbb |
+ # don't raise since the entry was present in _data, silently
|
|
|
af3cbb |
+ # clean up
|
|
|
af3cbb |
+ continue
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __iter__(self):
|
|
|
af3cbb |
+ return iter(self._data)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __len__(self):
|
|
|
af3cbb |
+ return len(self._data)
|
|
|
af3cbb |
diff --git a/custodia/vendor/backports/configparser/helpers.py b/custodia/vendor/backports/configparser/helpers.py
|
|
|
af3cbb |
new file mode 100644
|
|
|
af3cbb |
index 0000000..c47662f
|
|
|
af3cbb |
--- /dev/null
|
|
|
af3cbb |
+++ b/custodia/vendor/backports/configparser/helpers.py
|
|
|
af3cbb |
@@ -0,0 +1,171 @@
|
|
|
af3cbb |
+#!/usr/bin/env python
|
|
|
af3cbb |
+# -*- coding: utf-8 -*-
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from __future__ import absolute_import
|
|
|
af3cbb |
+from __future__ import division
|
|
|
af3cbb |
+from __future__ import print_function
|
|
|
af3cbb |
+from __future__ import unicode_literals
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from collections import MutableMapping
|
|
|
af3cbb |
+try:
|
|
|
af3cbb |
+ from collections import UserDict
|
|
|
af3cbb |
+except ImportError:
|
|
|
af3cbb |
+ from UserDict import UserDict
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+try:
|
|
|
af3cbb |
+ from collections import OrderedDict
|
|
|
af3cbb |
+except ImportError:
|
|
|
af3cbb |
+ from ordereddict import OrderedDict
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from io import open
|
|
|
af3cbb |
+import sys
|
|
|
af3cbb |
+try:
|
|
|
af3cbb |
+ from thread import get_ident
|
|
|
af3cbb |
+except ImportError:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ from _thread import get_ident
|
|
|
af3cbb |
+ except ImportError:
|
|
|
af3cbb |
+ from _dummy_thread import get_ident
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+PY2 = sys.version_info[0] == 2
|
|
|
af3cbb |
+PY3 = sys.version_info[0] == 3
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+str = type('str')
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+def from_none(exc):
|
|
|
af3cbb |
+ """raise from_none(ValueError('a')) == raise ValueError('a') from None"""
|
|
|
af3cbb |
+ exc.__cause__ = None
|
|
|
af3cbb |
+ exc.__suppress_context__ = True
|
|
|
af3cbb |
+ return exc
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+# from reprlib 3.2.1
|
|
|
af3cbb |
+def recursive_repr(fillvalue='...'):
|
|
|
af3cbb |
+ 'Decorator to make a repr function return fillvalue for a recursive call'
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def decorating_function(user_function):
|
|
|
af3cbb |
+ repr_running = set()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def wrapper(self):
|
|
|
af3cbb |
+ key = id(self), get_ident()
|
|
|
af3cbb |
+ if key in repr_running:
|
|
|
af3cbb |
+ return fillvalue
|
|
|
af3cbb |
+ repr_running.add(key)
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ result = user_function(self)
|
|
|
af3cbb |
+ finally:
|
|
|
af3cbb |
+ repr_running.discard(key)
|
|
|
af3cbb |
+ return result
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ # Can't use functools.wraps() here because of bootstrap issues
|
|
|
af3cbb |
+ wrapper.__module__ = getattr(user_function, '__module__')
|
|
|
af3cbb |
+ wrapper.__doc__ = getattr(user_function, '__doc__')
|
|
|
af3cbb |
+ wrapper.__name__ = getattr(user_function, '__name__')
|
|
|
af3cbb |
+ wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
|
|
|
af3cbb |
+ return wrapper
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ return decorating_function
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+# from collections 3.2.1
|
|
|
af3cbb |
+class _ChainMap(MutableMapping):
|
|
|
af3cbb |
+ ''' A ChainMap groups multiple dicts (or other mappings) together
|
|
|
af3cbb |
+ to create a single, updateable view.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ The underlying mappings are stored in a list. That list is public and can
|
|
|
af3cbb |
+ accessed or updated using the *maps* attribute. There is no other state.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Lookups search the underlying mappings successively until a key is found.
|
|
|
af3cbb |
+ In contrast, writes, updates, and deletions only operate on the first
|
|
|
af3cbb |
+ mapping.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ '''
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __init__(self, *maps):
|
|
|
af3cbb |
+ '''Initialize a ChainMap by setting *maps* to the given mappings.
|
|
|
af3cbb |
+ If no mappings are provided, a single empty dictionary is used.
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ '''
|
|
|
af3cbb |
+ self.maps = list(maps) or [{}] # always at least one map
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __missing__(self, key):
|
|
|
af3cbb |
+ raise KeyError(key)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __getitem__(self, key):
|
|
|
af3cbb |
+ for mapping in self.maps:
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ return mapping[key] # can't use 'key in mapping' with defaultdict
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ pass
|
|
|
af3cbb |
+ return self.__missing__(key) # support subclasses that define __missing__
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def get(self, key, default=None):
|
|
|
af3cbb |
+ return self[key] if key in self else default
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __len__(self):
|
|
|
af3cbb |
+ return len(set().union(*self.maps)) # reuses stored hash values if possible
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __iter__(self):
|
|
|
af3cbb |
+ return iter(set().union(*self.maps))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __contains__(self, key):
|
|
|
af3cbb |
+ return any(key in m for m in self.maps)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @recursive_repr()
|
|
|
af3cbb |
+ def __repr__(self):
|
|
|
af3cbb |
+ return '{0.__class__.__name__}({1})'.format(
|
|
|
af3cbb |
+ self, ', '.join(map(repr, self.maps)))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @classmethod
|
|
|
af3cbb |
+ def fromkeys(cls, iterable, *args):
|
|
|
af3cbb |
+ 'Create a ChainMap with a single dict created from the iterable.'
|
|
|
af3cbb |
+ return cls(dict.fromkeys(iterable, *args))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def copy(self):
|
|
|
af3cbb |
+ 'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
|
|
|
af3cbb |
+ return self.__class__(self.maps[0].copy(), *self.maps[1:])
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ __copy__ = copy
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def new_child(self): # like Django's Context.push()
|
|
|
af3cbb |
+ 'New ChainMap with a new dict followed by all previous maps.'
|
|
|
af3cbb |
+ return self.__class__({}, *self.maps)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ @property
|
|
|
af3cbb |
+ def parents(self): # like Django's Context.pop()
|
|
|
af3cbb |
+ 'New ChainMap from maps[1:].'
|
|
|
af3cbb |
+ return self.__class__(*self.maps[1:])
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __setitem__(self, key, value):
|
|
|
af3cbb |
+ self.maps[0][key] = value
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def __delitem__(self, key):
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ del self.maps[0][key]
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise KeyError('Key not found in the first mapping: {!r}'.format(key))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def popitem(self):
|
|
|
af3cbb |
+ 'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ return self.maps[0].popitem()
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise KeyError('No keys found in the first mapping.')
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def pop(self, key, *args):
|
|
|
af3cbb |
+ 'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
|
|
|
af3cbb |
+ try:
|
|
|
af3cbb |
+ return self.maps[0].pop(key, *args)
|
|
|
af3cbb |
+ except KeyError:
|
|
|
af3cbb |
+ raise KeyError('Key not found in the first mapping: {!r}'.format(key))
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ def clear(self):
|
|
|
af3cbb |
+ 'Clear maps[0], leaving maps[1:] intact.'
|
|
|
af3cbb |
+ self.maps[0].clear()
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+try:
|
|
|
af3cbb |
+ from collections import ChainMap
|
|
|
af3cbb |
+except ImportError:
|
|
|
af3cbb |
+ ChainMap = _ChainMap
|
|
|
af3cbb |
diff --git a/custodia/vendor/configparser.py b/custodia/vendor/configparser.py
|
|
|
af3cbb |
new file mode 100644
|
|
|
af3cbb |
index 0000000..b899f9e
|
|
|
af3cbb |
--- /dev/null
|
|
|
af3cbb |
+++ b/custodia/vendor/configparser.py
|
|
|
af3cbb |
@@ -0,0 +1,52 @@
|
|
|
af3cbb |
+#!/usr/bin/env python
|
|
|
af3cbb |
+# -*- coding: utf-8 -*-
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+"""Convenience module importing everything from backports.configparser."""
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from __future__ import absolute_import
|
|
|
af3cbb |
+from __future__ import division
|
|
|
af3cbb |
+from __future__ import print_function
|
|
|
af3cbb |
+from __future__ import unicode_literals
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+from backports.configparser import (
|
|
|
af3cbb |
+ RawConfigParser,
|
|
|
af3cbb |
+ ConfigParser,
|
|
|
af3cbb |
+ SafeConfigParser,
|
|
|
af3cbb |
+ SectionProxy,
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Interpolation,
|
|
|
af3cbb |
+ BasicInterpolation,
|
|
|
af3cbb |
+ ExtendedInterpolation,
|
|
|
af3cbb |
+ LegacyInterpolation,
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ Error,
|
|
|
af3cbb |
+ NoSectionError,
|
|
|
af3cbb |
+ DuplicateSectionError,
|
|
|
af3cbb |
+ DuplicateOptionError,
|
|
|
af3cbb |
+ NoOptionError,
|
|
|
af3cbb |
+ InterpolationError,
|
|
|
af3cbb |
+ InterpolationMissingOptionError,
|
|
|
af3cbb |
+ InterpolationSyntaxError,
|
|
|
af3cbb |
+ InterpolationDepthError,
|
|
|
af3cbb |
+ ParsingError,
|
|
|
af3cbb |
+ MissingSectionHeaderError,
|
|
|
af3cbb |
+ ConverterMapping,
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+ _UNSET,
|
|
|
af3cbb |
+ DEFAULTSECT,
|
|
|
af3cbb |
+ MAX_INTERPOLATION_DEPTH,
|
|
|
af3cbb |
+ _default_dict,
|
|
|
af3cbb |
+ _ChainMap,
|
|
|
af3cbb |
+)
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
|
|
|
af3cbb |
+ "NoOptionError", "InterpolationError", "InterpolationDepthError",
|
|
|
af3cbb |
+ "InterpolationMissingOptionError", "InterpolationSyntaxError",
|
|
|
af3cbb |
+ "ParsingError", "MissingSectionHeaderError",
|
|
|
af3cbb |
+ "ConfigParser", "SafeConfigParser", "RawConfigParser",
|
|
|
af3cbb |
+ "Interpolation", "BasicInterpolation", "ExtendedInterpolation",
|
|
|
af3cbb |
+ "LegacyInterpolation", "SectionProxy", "ConverterMapping",
|
|
|
af3cbb |
+ "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
|
|
|
af3cbb |
+
|
|
|
af3cbb |
+# NOTE: names missing from __all__ imported anyway for backwards compatibility.
|
|
|
af3cbb |
--
|
|
|
af3cbb |
2.9.3
|
|
|
af3cbb |
|