Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/configparser.rst

    r2 r391  
    1313
    1414   The :mod:`ConfigParser` module has been renamed to :mod:`configparser` in
    15    Python 3.0.  The :term:`2to3` tool will automatically adapt imports when
    16    converting your sources to 3.0.
     15   Python 3.  The :term:`2to3` tool will automatically adapt imports when
     16   converting your sources to Python 3.
    1717
    1818.. index::
     
    3232   This library does *not* interpret or write the value-type prefixes used in
    3333   the Windows Registry extended version of INI syntax.
     34
     35.. seealso::
     36
     37   Module :mod:`shlex`
     38      Support for a creating Unix shell-like mini-languages which can be used
     39      as an alternate format for application configuration files.
     40
     41   Module :mod:`json`
     42      The json module implements a subset of JavaScript syntax which can also
     43      be used for this purpose.
    3444
    3545The configuration file consists of sections, led by a ``[section]`` header and
     
    4252``';'`` are ignored and may be used to provide comments.
    4353
     54Configuration files may include comments, prefixed by specific characters (``#``
     55and ``;``).  Comments may appear on their own in an otherwise empty line, or may
     56be entered in lines holding values or section names.  In the latter case, they
     57need to be preceded by a whitespace character to be recognized as a comment.
     58(For backwards compatibility, only ``;`` starts an inline comment, while ``#``
     59does not.)
     60
     61On top of the core functionality, :class:`SafeConfigParser` supports
     62interpolation.  This means values can contain format strings which refer to
     63other values in the same section, or values in a special ``DEFAULT`` section.
     64Additional defaults can be provided on initialization.
     65
    4466For example::
    4567
     
    6385
    6486
    65 .. class:: RawConfigParser([defaults[, dict_type]])
     87.. class:: RawConfigParser([defaults[, dict_type[, allow_no_value]]])
    6688
    6789   The basic configuration object.  When *defaults* is given, it is initialized
    6890   into the dictionary of intrinsic defaults.  When *dict_type* is given, it will
    6991   be used to create the dictionary objects for the list of sections, for the
    70    options within a section, and for the default values. This class does not
     92   options within a section, and for the default values.  When *allow_no_value*
     93   is true (default: ``False``), options without values are accepted; the value
     94   presented for these is ``None``.
     95
     96   This class does not
    7197   support the magical interpolation behavior.
     98
     99   All option names are passed through the :meth:`optionxform` method.  Its
     100   default implementation converts option names to lower case.
    72101
    73102   .. versionadded:: 2.3
     
    76105      *dict_type* was added.
    77106
    78 
    79 .. class:: ConfigParser([defaults[, dict_type]])
     107   .. versionchanged:: 2.7
     108      The default *dict_type* is :class:`collections.OrderedDict`.
     109      *allow_no_value* was added.
     110
     111
     112.. class:: ConfigParser([defaults[, dict_type[, allow_no_value]]])
    80113
    81114   Derived class of :class:`RawConfigParser` that implements the magical
     
    87120
    88121   All option names used in interpolation will be passed through the
    89    :meth:`optionxform` method just like any other option name reference.  For
    90    example, using the default implementation of :meth:`optionxform` (which converts
    91    option names to lower case), the values ``foo %(bar)s`` and ``foo %(BAR)s`` are
    92    equivalent.
    93 
    94 
    95 .. class:: SafeConfigParser([defaults[, dict_type]])
     122   :meth:`optionxform` method just like any other option name reference.  Using
     123   the default implementation of :meth:`optionxform`, the values ``foo %(bar)s``
     124   and ``foo %(BAR)s`` are equivalent.
     125
     126   .. versionadded:: 2.3
     127
     128   .. versionchanged:: 2.6
     129      *dict_type* was added.
     130
     131   .. versionchanged:: 2.7
     132      The default *dict_type* is :class:`collections.OrderedDict`.
     133      *allow_no_value* was added.
     134
     135
     136.. class:: SafeConfigParser([defaults[, dict_type[, allow_no_value]]])
    96137
    97138   Derived class of :class:`ConfigParser` that implements a more-sane variant of
     
    103144
    104145   .. versionadded:: 2.3
     146
     147   .. versionchanged:: 2.6
     148      *dict_type* was added.
     149
     150   .. versionchanged:: 2.7
     151      The default *dict_type* is :class:`collections.OrderedDict`.
     152      *allow_no_value* was added.
     153
     154
     155.. exception:: Error
     156
     157   Base class for all other configparser exceptions.
    105158
    106159
     
    348401.. method:: ConfigParser.get(section, option[, raw[, vars]])
    349402
    350    Get an *option* value for the named *section*.  All the ``'%'`` interpolations
    351    are expanded in the return values, based on the defaults passed into the
    352    constructor, as well as the options *vars* provided, unless the *raw* argument
    353    is true.
    354 
     403   Get an *option* value for the named *section*.  If *vars* is provided, it
     404   must be a dictionary.  The *option* is looked up in *vars* (if provided),
     405   *section*, and in *defaults* in that order.
     406
     407   All the ``'%'`` interpolations are expanded in the return values, unless the
     408   *raw* argument is true.  Values for interpolation keys are looked up in the
     409   same manner as the option.
    355410
    356411.. method:: ConfigParser.items(section[, raw[, vars]])
     
    397452   # mode. SafeConfigParser does not allow such assignments to take place.
    398453   config.add_section('Section1')
    399    config.set('Section1', 'int', '15')
    400    config.set('Section1', 'bool', 'true')
    401    config.set('Section1', 'float', '3.1415')
     454   config.set('Section1', 'an_int', '15')
     455   config.set('Section1', 'a_bool', 'true')
     456   config.set('Section1', 'a_float', '3.1415')
    402457   config.set('Section1', 'baz', 'fun')
    403458   config.set('Section1', 'bar', 'Python')
     
    417472   # getfloat() raises an exception if the value is not a float
    418473   # getint() and getboolean() also do this for their respective types
    419    float = config.getfloat('Section1', 'float')
    420    int = config.getint('Section1', 'int')
    421    print float + int
     474   a_float = config.getfloat('Section1', 'a_float')
     475   an_int = config.getint('Section1', 'an_int')
     476   print a_float + an_int
    422477
    423478   # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
    424479   # This is because we are using a RawConfigParser().
    425    if config.getboolean('Section1', 'bool'):
     480   if config.getboolean('Section1', 'a_bool'):
    426481       print config.get('Section1', 'foo')
    427482
     
    468523       else:
    469524           config.remove_option(section1, option)
     525
     526Some configuration files are known to include settings without values, but which
     527otherwise conform to the syntax supported by :mod:`ConfigParser`.  The
     528*allow_no_value* parameter to the constructor can be used to indicate that such
     529values should be accepted:
     530
     531.. doctest::
     532
     533   >>> import ConfigParser
     534   >>> import io
     535
     536   >>> sample_config = """
     537   ... [mysqld]
     538   ... user = mysql
     539   ... pid-file = /var/run/mysqld/mysqld.pid
     540   ... skip-external-locking
     541   ... old_passwords = 1
     542   ... skip-bdb
     543   ... skip-innodb
     544   ... """
     545   >>> config = ConfigParser.RawConfigParser(allow_no_value=True)
     546   >>> config.readfp(io.BytesIO(sample_config))
     547
     548   >>> # Settings with values are treated as before:
     549   >>> config.get("mysqld", "user")
     550   'mysql'
     551
     552   >>> # Settings without values provide None:
     553   >>> config.get("mysqld", "skip-bdb")
     554
     555   >>> # Settings which aren't specified still raise an error:
     556   >>> config.get("mysqld", "does-not-exist")
     557   Traceback (most recent call last):
     558     ...
     559   ConfigParser.NoOptionError: No option 'does-not-exist' in section: 'mysqld'
Note: See TracChangeset for help on using the changeset viewer.