Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Static Private Attributes
configparser.ConfigParser Class Reference
Inheritance diagram for configparser.ConfigParser:
Inheritance graph
[legend]
Collaboration diagram for configparser.ConfigParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def set
def add_section
def defaults
def sections
def has_section
def options
def read
def read_file
def read_string
def read_dict
def readfp
def get
def getint
def getfloat
def getboolean
def items
def optionxform
def has_option
def write
def remove_option
def remove_section
def __getitem__
def __setitem__
def __delitem__
def __contains__
def __len__
def __iter__

Public Attributes

 default_section

Static Public Attributes

tuple SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
tuple OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
tuple OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
tuple NONSPACECRE = re.compile(r"\S")
dictionary BOOLEAN_STATES

Static Private Attributes

tuple _DEFAULT_INTERPOLATION = BasicInterpolation()

Detailed Description

ConfigParser implementing interpolation.

Definition at line 1151 of file configparser.py.


Member Function Documentation

def configparser.RawConfigParser.__contains__ (   self,
  key 
) [inherited]

Definition at line 960 of file configparser.py.

00960 
00961     def __contains__(self, key):
00962         return key == self.default_section or self.has_section(key)

Here is the call graph for this function:

def configparser.RawConfigParser.__delitem__ (   self,
  key 
) [inherited]

Definition at line 953 of file configparser.py.

00953 
00954     def __delitem__(self, key):
00955         if key == self.default_section:
00956             raise ValueError("Cannot remove the default section.")
00957         if not self.has_section(key):
00958             raise KeyError(key)
00959         self.remove_section(key)

Here is the call graph for this function:

def configparser.RawConfigParser.__getitem__ (   self,
  key 
) [inherited]

Definition at line 939 of file configparser.py.

00939 
00940     def __getitem__(self, key):
00941         if key != self.default_section and not self.has_section(key):
00942             raise KeyError(key)
00943         return self._proxies[key]

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.__iter__ (   self) [inherited]

Definition at line 966 of file configparser.py.

00966 
00967     def __iter__(self):
00968         # XXX does it break when underlying container state changed?
00969         return itertools.chain((self.default_section,), self._sections.keys())

def configparser.RawConfigParser.__len__ (   self) [inherited]

Definition at line 963 of file configparser.py.

00963 
00964     def __len__(self):
00965         return len(self._sections) + 1 # the default section

def configparser.RawConfigParser.__setitem__ (   self,
  key,
  value 
) [inherited]

Definition at line 944 of file configparser.py.

00944 
00945     def __setitem__(self, key, value):
00946         # To conform with the mapping protocol, overwrites existing values in
00947         # the section.
00948 
00949         # XXX this is not atomic if read_dict fails at any point. Then again,
00950         # no update method in configparser is atomic in this implementation.
00951         self.remove_section(key)
00952         self.read_dict({key: value})

Here is the call graph for this function:

def configparser.ConfigParser.add_section (   self,
  section 
)
Create a new section in the configuration.  Extends
RawConfigParser.add_section by validating if the section name is
a string.

Reimplemented from configparser.RawConfigParser.

Definition at line 1162 of file configparser.py.

01162 
01163     def add_section(self, section):
01164         """Create a new section in the configuration.  Extends
01165         RawConfigParser.add_section by validating if the section name is
01166         a string."""
01167         self._validate_value_types(section=section)
01168         super().add_section(section)
01169 

Here is the call graph for this function:

def configparser.RawConfigParser.defaults (   self) [inherited]

Definition at line 633 of file configparser.py.

00633 
00634     def defaults(self):
00635         return self._defaults

def configparser.RawConfigParser.get (   self,
  section,
  option,
  raw = False,
  vars = None,
  fallback = _UNSET 
) [inherited]
Get an option value for a given section.

If `vars' is provided, it must be a dictionary. The option is looked up
in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
If the key is not found and `fallback' is provided, it is used as
a fallback value. `None' can be provided as a `fallback' value.

If interpolation is enabled and the optional argument `raw' is False,
all interpolations are expanded in the return values.

Arguments `raw', `vars', and `fallback' are keyword only.

The section DEFAULT is special.

Definition at line 755 of file configparser.py.

00755 
00756     def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
00757         """Get an option value for a given section.
00758 
00759         If `vars' is provided, it must be a dictionary. The option is looked up
00760         in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
00761         If the key is not found and `fallback' is provided, it is used as
00762         a fallback value. `None' can be provided as a `fallback' value.
00763 
00764         If interpolation is enabled and the optional argument `raw' is False,
00765         all interpolations are expanded in the return values.
00766 
00767         Arguments `raw', `vars', and `fallback' are keyword only.
00768 
00769         The section DEFAULT is special.
00770         """
00771         try:
00772             d = self._unify_values(section, vars)
00773         except NoSectionError:
00774             if fallback is _UNSET:
00775                 raise
00776             else:
00777                 return fallback
00778         option = self.optionxform(option)
00779         try:
00780             value = d[option]
00781         except KeyError:
00782             if fallback is _UNSET:
00783                 raise NoOptionError(option, section)
00784             else:
00785                 return fallback
00786 
00787         if raw or value is None:
00788             return value
00789         else:
00790             return self._interpolation.before_get(self, section, option, value,
00791                                                   d)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.getboolean (   self,
  section,
  option,
  raw = False,
  vars = None,
  fallback = _UNSET 
) [inherited]

Definition at line 816 of file configparser.py.

00816 
00817                    fallback=_UNSET):
00818         try:
00819             return self._get(section, self._convert_to_boolean, option,
00820                              raw=raw, vars=vars)
00821         except (NoSectionError, NoOptionError):
00822             if fallback is _UNSET:
00823                 raise
00824             else:
00825                 return fallback

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.getfloat (   self,
  section,
  option,
  raw = False,
  vars = None,
  fallback = _UNSET 
) [inherited]

Definition at line 806 of file configparser.py.

00806 
00807                  fallback=_UNSET):
00808         try:
00809             return self._get(section, float, option, raw=raw, vars=vars)
00810         except (NoSectionError, NoOptionError):
00811             if fallback is _UNSET:
00812                 raise
00813             else:
00814                 return fallback

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.getint (   self,
  section,
  option,
  raw = False,
  vars = None,
  fallback = _UNSET 
) [inherited]

Definition at line 796 of file configparser.py.

00796 
00797                fallback=_UNSET):
00798         try:
00799             return self._get(section, int, option, raw=raw, vars=vars)
00800         except (NoSectionError, NoOptionError):
00801             if fallback is _UNSET:
00802                 raise
00803             else:
00804                 return fallback

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.has_option (   self,
  section,
  option 
) [inherited]
Check for the existence of a given option in a given section.
If the specified `section' is None or an empty string, DEFAULT is
assumed. If the specified `section' does not exist, returns False.

Definition at line 858 of file configparser.py.

00858 
00859     def has_option(self, section, option):
00860         """Check for the existence of a given option in a given section.
00861         If the specified `section' is None or an empty string, DEFAULT is
00862         assumed. If the specified `section' does not exist, returns False."""
00863         if not section or section == self.default_section:
00864             option = self.optionxform(option)
00865             return option in self._defaults
00866         elif section not in self._sections:
00867             return False
00868         else:
00869             option = self.optionxform(option)
00870             return (option in self._sections[section]
00871                     or option in self._defaults)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.has_section (   self,
  section 
) [inherited]
Indicate whether the named section is present in the configuration.

The DEFAULT section is not acknowledged.

Definition at line 655 of file configparser.py.

00655 
00656     def has_section(self, section):
00657         """Indicate whether the named section is present in the configuration.
00658 
00659         The DEFAULT section is not acknowledged.
00660         """
00661         return section in self._sections

Here is the caller graph for this function:

def configparser.RawConfigParser.items (   self,
  section = _UNSET,
  raw = False,
  vars = None 
) [inherited]
Return a list of (name, value) tuples for each option in a 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.

Definition at line 826 of file configparser.py.

00826 
00827     def items(self, section=_UNSET, raw=False, vars=None):
00828         """Return a list of (name, value) tuples for each option in a section.
00829 
00830         All % interpolations are expanded in the return values, based on the
00831         defaults passed into the constructor, unless the optional argument
00832         `raw' is true.  Additional substitutions may be provided using the
00833         `vars' argument, which must be a dictionary whose contents overrides
00834         any pre-existing defaults.
00835 
00836         The section DEFAULT is special.
00837         """
00838         if section is _UNSET:
00839             return super().items()
00840         d = self._defaults.copy()
00841         try:
00842             d.update(self._sections[section])
00843         except KeyError:
00844             if section != self.default_section:
00845                 raise NoSectionError(section)
00846         # Update with the entry specific variables
00847         if vars:
00848             for key, value in vars.items():
00849                 d[self.optionxform(key)] = value
00850         value_getter = lambda option: self._interpolation.before_get(self,
00851             section, option, d[option], d)
00852         if raw:
00853             value_getter = lambda option: d[option]
00854         return [(option, value_getter(option)) for option in d.keys()]

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.options (   self,
  section 
) [inherited]
Return a list of option names for the given section name.

Definition at line 662 of file configparser.py.

00662 
00663     def options(self, section):
00664         """Return a list of option names for the given section name."""
00665         try:
00666             opts = self._sections[section].copy()
00667         except KeyError:
00668             raise NoSectionError(section)
00669         opts.update(self._defaults)
00670         return list(opts.keys())

Here is the caller graph for this function:

def configparser.RawConfigParser.optionxform (   self,
  optionstr 
) [inherited]

Definition at line 855 of file configparser.py.

00855 
00856     def optionxform(self, optionstr):
00857         return optionstr.lower()

Here is the caller graph for this function:

def configparser.RawConfigParser.read (   self,
  filenames,
  encoding = None 
) [inherited]
Read and parse a filename or a list of filenames.

Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
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.

Return list of successfully read files.

Definition at line 671 of file configparser.py.

00671 
00672     def read(self, filenames, encoding=None):
00673         """Read and parse a filename or a list of filenames.
00674 
00675         Files that cannot be opened are silently ignored; this is
00676         designed so that you can specify a list of potential
00677         configuration file locations (e.g. current directory, user's
00678         home directory, systemwide directory), and all existing
00679         configuration files in the list will be read.  A single
00680         filename may also be given.
00681 
00682         Return list of successfully read files.
00683         """
00684         if isinstance(filenames, str):
00685             filenames = [filenames]
00686         read_ok = []
00687         for filename in filenames:
00688             try:
00689                 with open(filename, encoding=encoding) as fp:
00690                     self._read(fp, filename)
00691             except IOError:
00692                 continue
00693             read_ok.append(filename)
00694         return read_ok

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.read_dict (   self,
  dictionary,
  source = '<dict>' 
) [inherited]
Read configuration from a dictionary.

Keys are section names, values are dictionaries with keys and values
that should be present in the section. If the used dictionary type
preserves order, sections and their keys will be added in order.

All types held in the dictionary are converted to strings during
reading, including section names, option names and keys.

Optional second argument is the `source' specifying the name of the
dictionary being read.

Definition at line 715 of file configparser.py.

00715 
00716     def read_dict(self, dictionary, source='<dict>'):
00717         """Read configuration from a dictionary.
00718 
00719         Keys are section names, values are dictionaries with keys and values
00720         that should be present in the section. If the used dictionary type
00721         preserves order, sections and their keys will be added in order.
00722 
00723         All types held in the dictionary are converted to strings during
00724         reading, including section names, option names and keys.
00725 
00726         Optional second argument is the `source' specifying the name of the
00727         dictionary being read.
00728         """
00729         elements_added = set()
00730         for section, keys in dictionary.items():
00731             section = str(section)
00732             try:
00733                 self.add_section(section)
00734             except (DuplicateSectionError, ValueError):
00735                 if self._strict and section in elements_added:
00736                     raise
00737             elements_added.add(section)
00738             for key, value in keys.items():
00739                 key = self.optionxform(str(key))
00740                 if value is not None:
00741                     value = str(value)
00742                 if self._strict and (section, key) in elements_added:
00743                     raise DuplicateOptionError(section, key, source)
00744                 elements_added.add((section, key))
00745                 self.set(section, key, value)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.read_file (   self,
  f,
  source = None 
) [inherited]
Like read() but the argument must be a file-like object.

The `f' argument must be iterable, returning one line at a time.
Optional second argument is the `source' specifying the name of the
file being read. If not given, it is taken from f.name. If `f' has no
`name' attribute, `<???>' is used.

Definition at line 695 of file configparser.py.

00695 
00696     def read_file(self, f, source=None):
00697         """Like read() but the argument must be a file-like object.
00698 
00699         The `f' argument must be iterable, returning one line at a time.
00700         Optional second argument is the `source' specifying the name of the
00701         file being read. If not given, it is taken from f.name. If `f' has no
00702         `name' attribute, `<???>' is used.
00703         """
00704         if source is None:
00705             try:
00706                 source = f.name
00707             except AttributeError:
00708                 source = '<???>'
00709         self._read(f, source)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.read_string (   self,
  string,
  source = '<string>' 
) [inherited]
Read configuration from a given string.

Definition at line 710 of file configparser.py.

00710 
00711     def read_string(self, string, source='<string>'):
00712         """Read configuration from a given string."""
00713         sfile = io.StringIO(string)
00714         self.read_file(sfile, source)

Here is the call graph for this function:

def configparser.RawConfigParser.readfp (   self,
  fp,
  filename = None 
) [inherited]
Deprecated, use read_file instead.

Definition at line 746 of file configparser.py.

00746 
00747     def readfp(self, fp, filename=None):
00748         """Deprecated, use read_file instead."""
00749         warnings.warn(
00750             "This method will be removed in future versions.  "
00751             "Use 'parser.read_file()' instead.",
00752             DeprecationWarning, stacklevel=2
00753         )
00754         self.read_file(fp, source=filename)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.remove_option (   self,
  section,
  option 
) [inherited]
Remove an option.

Definition at line 916 of file configparser.py.

00916 
00917     def remove_option(self, section, option):
00918         """Remove an option."""
00919         if not section or section == self.default_section:
00920             sectdict = self._defaults
00921         else:
00922             try:
00923                 sectdict = self._sections[section]
00924             except KeyError:
00925                 raise NoSectionError(section)
00926         option = self.optionxform(option)
00927         existed = option in sectdict
00928         if existed:
00929             del sectdict[option]
00930         return existed

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.remove_section (   self,
  section 
) [inherited]
Remove a file section.

Definition at line 931 of file configparser.py.

00931 
00932     def remove_section(self, section):
00933         """Remove a file section."""
00934         existed = section in self._sections
00935         if existed:
00936             del self._sections[section]
00937             del self._proxies[section]
00938         return existed

Here is the caller graph for this function:

def configparser.RawConfigParser.sections (   self) [inherited]
Return a list of section names, excluding [DEFAULT]

Definition at line 636 of file configparser.py.

00636 
00637     def sections(self):
00638         """Return a list of section names, excluding [DEFAULT]"""
00639         # self._sections will never have [DEFAULT] in it
00640         return list(self._sections.keys())

Here is the caller graph for this function:

def configparser.ConfigParser.set (   self,
  section,
  option,
  value = None 
)
Set an option.  Extends RawConfigParser.set by validating type and
interpolation syntax on the value.

Reimplemented from configparser.RawConfigParser.

Definition at line 1156 of file configparser.py.

01156 
01157     def set(self, section, option, value=None):
01158         """Set an option.  Extends RawConfigParser.set by validating type and
01159         interpolation syntax on the value."""
01160         self._validate_value_types(option=option, value=value)
01161         super().set(section, option, value)

Here is the call graph for this function:

Here is the caller graph for this function:

def configparser.RawConfigParser.write (   self,
  fp,
  space_around_delimiters = True 
) [inherited]
Write an .ini-format representation of the configuration state.

If `space_around_delimiters' is True (the default), delimiters
between keys and values are surrounded by spaces.

Definition at line 886 of file configparser.py.

00886 
00887     def write(self, fp, space_around_delimiters=True):
00888         """Write an .ini-format representation of the configuration state.
00889 
00890         If `space_around_delimiters' is True (the default), delimiters
00891         between keys and values are surrounded by spaces.
00892         """
00893         if space_around_delimiters:
00894             d = " {} ".format(self._delimiters[0])
00895         else:
00896             d = self._delimiters[0]
00897         if self._defaults:
00898             self._write_section(fp, self.default_section,
00899                                     self._defaults.items(), d)
00900         for section in self._sections:
00901             self._write_section(fp, section,
00902                                 self._sections[section].items(), d)

Here is the call graph for this function:


Member Data Documentation

Reimplemented from configparser.RawConfigParser.

Definition at line 1154 of file configparser.py.

Initial value:
{'1': True, 'yes': True, 'true': True, 'on': True,
                      '0': False, 'no': False, 'false': False, 'off': False}

Definition at line 592 of file configparser.py.

Definition at line 626 of file configparser.py.

tuple configparser.RawConfigParser.NONSPACECRE = re.compile(r"\S") [static, inherited]

Definition at line 590 of file configparser.py.

tuple configparser.RawConfigParser.OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE) [static, inherited]

Definition at line 585 of file configparser.py.

tuple configparser.RawConfigParser.OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE) [static, inherited]

Definition at line 588 of file configparser.py.

tuple configparser.RawConfigParser.SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE) [static, inherited]

Definition at line 583 of file configparser.py.


The documentation for this class was generated from the following file: