annotate Doc/library/configparser.rst @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 57812944c961
children bbd1d90d158a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
1 :mod:`ConfigParser` --- Configuration file parser
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2 =================================================
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3
47010
7e18d6593824 Added documentation stub for ConfigParser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47009
diff changeset
4 .. module:: ConfigParser
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 :synopsis: Configuration file parser.
47009
708214f351fe Updated import statements to use the new `configparser` module name.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47008
diff changeset
6
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 .. moduleauthor:: Ken Manheimer <klm@zope.com>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 .. moduleauthor:: Barry Warsaw <bwarsaw@python.org>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9 .. moduleauthor:: Eric S. Raymond <esr@thyrsus.com>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 .. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org>
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11
47010
7e18d6593824 Added documentation stub for ConfigParser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47009
diff changeset
12 .. note::
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
13
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
14 The :mod:`ConfigParser` module has been renamed to :mod:`configparser` in
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
15 Python 3.0. The :term:`2to3` tool will automatically adapt imports when
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
16 converting your sources to 3.0.
47010
7e18d6593824 Added documentation stub for ConfigParser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47009
diff changeset
17
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18 .. index::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 pair: .ini; file
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 pair: configuration; file
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 single: ini file
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
22 single: Windows ini file
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 This module defines the class :class:`ConfigParser`. The :class:`ConfigParser`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 class implements a basic configuration file parser language which provides a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 structure similar to what you would find on Microsoft Windows INI files. You
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 can use this to write Python programs which can be customized by end users
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 easily.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
29
53508
57812944c961 Demote warnings to notices where appropriate, following the goal that as few "red box" warnings
Georg Brandl <georg@python.org>
parents: 51964
diff changeset
30 .. note::
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31
53508
57812944c961 Demote warnings to notices where appropriate, following the goal that as few "red box" warnings
Georg Brandl <georg@python.org>
parents: 51964
diff changeset
32 This library does *not* interpret or write the value-type prefixes used in
57812944c961 Demote warnings to notices where appropriate, following the goal that as few "red box" warnings
Georg Brandl <georg@python.org>
parents: 51964
diff changeset
33 the Windows Registry extended version of INI syntax.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
34
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 The configuration file consists of sections, led by a ``[section]`` header and
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36 followed by ``name: value`` entries, with continuations in the style of
46104
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
37 :rfc:`822` (see section 3.1.1, "LONG HEADER FIELDS"); ``name=value`` is also
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
38 accepted. Note that leading whitespace is removed from values. The optional
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
39 values can contain format strings which refer to other values in the same
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
40 section, or values in a special ``DEFAULT`` section. Additional defaults can be
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
41 provided on initialization and retrieval. Lines beginning with ``'#'`` or
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
42 ``';'`` are ignored and may be used to provide comments.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 For example::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 [My Section]
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 foodir: %(dir)s/whatever
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 dir=frob
46104
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
49 long: this value continues
1dd00016a69c Add an example for an RFC 822 continuation.
Georg Brandl <georg@python.org>
parents: 45445
diff changeset
50 in the next line
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
51
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 would resolve the ``%(dir)s`` to the value of ``dir`` (``frob`` in this case).
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 All reference expansions are done on demand.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
54
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
55 Default values can be specified by passing them into the :class:`ConfigParser`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
56 constructor as a dictionary. Additional defaults may be passed into the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 :meth:`get` method which will override all others.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
58
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
59 Sections are normally stored in a builtin dictionary. An alternative dictionary
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
60 type can be passed to the :class:`ConfigParser` constructor. For example, if a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 dictionary type is passed that sorts its keys, the sections will be sorted on
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
62 write-back, as will be the keys within each section.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 .. class:: RawConfigParser([defaults[, dict_type]])
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 The basic configuration object. When *defaults* is given, it is initialized
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 into the dictionary of intrinsic defaults. When *dict_type* is given, it will
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69 be used to create the dictionary objects for the list of sections, for the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70 options within a section, and for the default values. This class does not
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 support the magical interpolation behavior.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
72
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
73 .. versionadded:: 2.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
74
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75 .. versionchanged:: 2.6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
76 *dict_type* was added.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
77
51964
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
78 .. versionchanged:: 2.7
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
79 The default *dict_type* is :class:`collections.OrderedDict`.
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
80
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
81
51928
2c5903a02b36 Fix docs for ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 51745
diff changeset
82 .. class:: ConfigParser([defaults[, dict_type]])
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 Derived class of :class:`RawConfigParser` that implements the magical
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 interpolation feature and adds optional arguments to the :meth:`get` and
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 :meth:`items` methods. The values in *defaults* must be appropriate for the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 ``%()s`` string interpolation. Note that *__name__* is an intrinsic default;
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 its value is the section name, and will override any value provided in
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 *defaults*.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91 All option names used in interpolation will be passed through the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 :meth:`optionxform` method just like any other option name reference. For
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 example, using the default implementation of :meth:`optionxform` (which converts
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 option names to lower case), the values ``foo %(bar)s`` and ``foo %(BAR)s`` are
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 equivalent.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96
51964
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
97 .. versionadded:: 2.3
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
98
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
99 .. versionchanged:: 2.6
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
100 *dict_type* was added.
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
101
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
102 .. versionchanged:: 2.7
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
103 The default *dict_type* is :class:`collections.OrderedDict`.
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
104
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
105
51928
2c5903a02b36 Fix docs for ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 51745
diff changeset
106 .. class:: SafeConfigParser([defaults[, dict_type]])
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
107
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 Derived class of :class:`ConfigParser` that implements a more-sane variant of
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
109 the magical interpolation feature. This implementation is more predictable as
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
110 well. New applications should prefer this version if they don't need to be
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
111 compatible with older versions of Python.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112
44383
3625234bf3d2 Some cleanup in the docs.
Georg Brandl <georg@python.org>
parents: 44051
diff changeset
113 .. XXX Need to explain what's safer/more predictable about it.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
114
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 .. versionadded:: 2.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116
51964
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
117 .. versionchanged:: 2.6
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
118 *dict_type* was added.
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
119
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
120 .. versionchanged:: 2.7
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
121 The default *dict_type* is :class:`collections.OrderedDict`.
6b1b2e7a06e9 Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents: 51928
diff changeset
122
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
123
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
124 .. exception:: NoSectionError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
125
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
126 Exception raised when a specified section is not found.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
127
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
128
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
129 .. exception:: DuplicateSectionError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
130
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 Exception raised if :meth:`add_section` is called with the name of a section
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
132 that is already present.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
133
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 .. exception:: NoOptionError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
136
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 Exception raised when a specified option is not found in the specified section.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
138
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
139
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
140 .. exception:: InterpolationError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
141
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
142 Base class for exceptions raised when problems occur performing string
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 interpolation.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
144
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
145
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
146 .. exception:: InterpolationDepthError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
147
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
148 Exception raised when string interpolation cannot be completed because the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
149 number of iterations exceeds :const:`MAX_INTERPOLATION_DEPTH`. Subclass of
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
150 :exc:`InterpolationError`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
151
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
152
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 .. exception:: InterpolationMissingOptionError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
154
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 Exception raised when an option referenced from a value does not exist. Subclass
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
156 of :exc:`InterpolationError`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
157
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
158 .. versionadded:: 2.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
159
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
160
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
161 .. exception:: InterpolationSyntaxError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
162
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
163 Exception raised when the source text into which substitutions are made does not
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
164 conform to the required syntax. Subclass of :exc:`InterpolationError`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
165
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
166 .. versionadded:: 2.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
167
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
168
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
169 .. exception:: MissingSectionHeaderError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
170
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
171 Exception raised when attempting to parse a file which has no section headers.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
172
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
173
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
174 .. exception:: ParsingError
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
175
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
176 Exception raised when errors occur attempting to parse a file.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
177
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
178
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
179 .. data:: MAX_INTERPOLATION_DEPTH
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
180
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 The maximum depth for recursive interpolation for :meth:`get` when the *raw*
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
182 parameter is false. This is relevant only for the :class:`ConfigParser` class.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
183
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
184
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
185 .. seealso::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
186
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
187 Module :mod:`shlex`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
188 Support for a creating Unix shell-like mini-languages which can be used as an
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
189 alternate format for application configuration files.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
190
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
191
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
192 .. _rawconfigparser-objects:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
193
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
194 RawConfigParser Objects
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
195 -----------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
196
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
197 :class:`RawConfigParser` instances have the following methods:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
198
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
199
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
200 .. method:: RawConfigParser.defaults()
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
201
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
202 Return a dictionary containing the instance-wide defaults.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
203
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
205 .. method:: RawConfigParser.sections()
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
206
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 Return a list of the sections available; ``DEFAULT`` is not included in the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 list.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
209
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
210
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 .. method:: RawConfigParser.add_section(section)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
212
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 Add a section named *section* to the instance. If a section by the given name
45445
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 44838
diff changeset
214 already exists, :exc:`DuplicateSectionError` is raised. If the name
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 44838
diff changeset
215 ``DEFAULT`` (or any of it's case-insensitive variants) is passed,
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 44838
diff changeset
216 :exc:`ValueError` is raised.
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
217
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 .. method:: RawConfigParser.has_section(section)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
219
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
220 Indicates whether the named section is present in the configuration. The
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 ``DEFAULT`` section is not acknowledged.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
222
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
223
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 .. method:: RawConfigParser.options(section)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
225
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 Returns a list of options available in the specified *section*.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
227
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
228
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
229 .. method:: RawConfigParser.has_option(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
230
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
231 If the given section exists, and contains the given option, return
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
232 :const:`True`; otherwise return :const:`False`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
233
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 .. versionadded:: 1.6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
237 .. method:: RawConfigParser.read(filenames)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
238
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
239 Attempt to read and parse a list of filenames, returning a list of filenames
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 which were successfully parsed. If *filenames* is a string or Unicode string,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
241 it is treated as a single filename. If a file named in *filenames* cannot be
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
242 opened, that file will be ignored. This is designed so that you can specify a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
243 list of potential configuration file locations (for example, the current
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
244 directory, the user's home directory, and some system-wide directory), and all
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
245 existing configuration files in the list will be read. If none of the named
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
246 files exist, the :class:`ConfigParser` instance will contain an empty dataset.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
247 An application which requires initial values to be loaded from a file should
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
248 load the required file or files using :meth:`readfp` before calling :meth:`read`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
249 for any optional files::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
250
51745
9815da43a1ce revert r69777 since all the experts agree that extra import lines distract from the code
Benjamin Peterson <benjamin@python.org>
parents: 51742
diff changeset
251 import ConfigParser, os
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
252
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
253 config = ConfigParser.ConfigParser()
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
254 config.readfp(open('defaults.cfg'))
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
255 config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
256
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
257 .. versionchanged:: 2.4
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
258 Returns list of successfully parsed filenames.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
259
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
260
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
261 .. method:: RawConfigParser.readfp(fp[, filename])
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
262
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
263 Read and parse configuration data from the file or file-like object in *fp*
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
264 (only the :meth:`readline` method is used). If *filename* is omitted and *fp*
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
265 has a :attr:`name` attribute, that is used for *filename*; the default is
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
266 ``<???>``.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
267
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
268
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
269 .. method:: RawConfigParser.get(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
270
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
271 Get an *option* value for the named *section*.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
272
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
273
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
274 .. method:: RawConfigParser.getint(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
275
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
276 A convenience method which coerces the *option* in the specified *section* to an
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
277 integer.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
278
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
279
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
280 .. method:: RawConfigParser.getfloat(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
281
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
282 A convenience method which coerces the *option* in the specified *section* to a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
283 floating point number.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
284
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
285
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
286 .. method:: RawConfigParser.getboolean(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
287
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
288 A convenience method which coerces the *option* in the specified *section* to a
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
289 Boolean value. Note that the accepted values for the option are ``"1"``,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
290 ``"yes"``, ``"true"``, and ``"on"``, which cause this method to return ``True``,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
291 and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which cause it to return
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
292 ``False``. These string values are checked in a case-insensitive manner. Any
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
293 other value will cause it to raise :exc:`ValueError`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
294
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
295
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
296 .. method:: RawConfigParser.items(section)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
297
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
298 Return a list of ``(name, value)`` pairs for each option in the given *section*.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
299
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
300
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
301 .. method:: RawConfigParser.set(section, option, value)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
302
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
303 If the given section exists, set the given option to the specified value;
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
304 otherwise raise :exc:`NoSectionError`. While it is possible to use
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
305 :class:`RawConfigParser` (or :class:`ConfigParser` with *raw* parameters set to
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
306 true) for *internal* storage of non-string values, full functionality (including
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
307 interpolation and output to files) can only be achieved using string values.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
308
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
309 .. versionadded:: 1.6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
310
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
311
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
312 .. method:: RawConfigParser.write(fileobject)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
313
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
314 Write a representation of the configuration to the specified file object. This
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
315 representation can be parsed by a future :meth:`read` call.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
316
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
317 .. versionadded:: 1.6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
318
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
319
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
320 .. method:: RawConfigParser.remove_option(section, option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
321
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
322 Remove the specified *option* from the specified *section*. If the section does
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
323 not exist, raise :exc:`NoSectionError`. If the option existed to be removed,
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
324 return :const:`True`; otherwise return :const:`False`.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
325
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
326 .. versionadded:: 1.6
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
327
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
328
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
329 .. method:: RawConfigParser.remove_section(section)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
330
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
331 Remove the specified *section* from the configuration. If the section in fact
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
332 existed, return ``True``. Otherwise return ``False``.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
333
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
334
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
335 .. method:: RawConfigParser.optionxform(option)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
336
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
337 Transforms the option name *option* as found in an input file or as passed in by
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
338 client code to the form that should be used in the internal structures. The
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
339 default implementation returns a lower-case version of *option*; subclasses may
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
340 override this or client code can set an attribute of this name on instances to
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
341 affect this behavior. Setting this to :func:`str`, for example, would make
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
342 option names case sensitive.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
343
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
344
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
345 .. _configparser-objects:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
346
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
347 ConfigParser Objects
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
348 --------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
349
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
350 The :class:`ConfigParser` class extends some methods of the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
351 :class:`RawConfigParser` interface, adding some optional arguments.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
352
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
353
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
354 .. method:: ConfigParser.get(section, option[, raw[, vars]])
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
355
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
356 Get an *option* value for the named *section*. All the ``'%'`` interpolations
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
357 are expanded in the return values, based on the defaults passed into the
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
358 constructor, as well as the options *vars* provided, unless the *raw* argument
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
359 is true.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
360
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
361
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
362 .. method:: ConfigParser.items(section[, raw[, vars]])
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
363
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
364 Return a list of ``(name, value)`` pairs for each option in the given *section*.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
365 Optional arguments have the same meaning as for the :meth:`get` method.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
366
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
367 .. versionadded:: 2.3
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
368
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
369
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
370 .. _safeconfigparser-objects:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
371
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
372 SafeConfigParser Objects
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
373 ------------------------
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
374
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
375 The :class:`SafeConfigParser` class implements the same extended interface as
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
376 :class:`ConfigParser`, with the following addition:
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
377
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
378
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
379 .. method:: SafeConfigParser.set(section, option, value)
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
380
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
381 If the given section exists, set the given option to the specified value;
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
382 otherwise raise :exc:`NoSectionError`. *value* must be a string (:class:`str`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
383 or :class:`unicode`); if not, :exc:`TypeError` is raised.
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
384
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
385 .. versionadded:: 2.4
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
386
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
387
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
388 Examples
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
389 --------
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
390
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
391 An example of writing to a configuration file::
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
392
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
393 import ConfigParser
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
394
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
395 config = ConfigParser.RawConfigParser()
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
396
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
397 # When adding sections or items, add them in the reverse order of
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
398 # how you want them to be displayed in the actual file.
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
399 # In addition, please note that using RawConfigParser's and the raw
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
400 # mode of ConfigParser's respective set functions, you can assign
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
401 # non-string values to keys internally, but will receive an error
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
402 # when attempting to write to a file or when you get it in non-raw
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
403 # mode. SafeConfigParser does not allow such assignments to take place.
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
404 config.add_section('Section1')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
405 config.set('Section1', 'int', '15')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
406 config.set('Section1', 'bool', 'true')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
407 config.set('Section1', 'float', '3.1415')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
408 config.set('Section1', 'baz', 'fun')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
409 config.set('Section1', 'bar', 'Python')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
410 config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
411
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
412 # Writing our configuration file to 'example.cfg'
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
413 with open('example.cfg', 'wb') as configfile:
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
414 config.write(configfile)
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
415
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
416 An example of reading the configuration file again::
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
417
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
418 import ConfigParser
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
419
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
420 config = ConfigParser.RawConfigParser()
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
421 config.read('example.cfg')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
422
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
423 # getfloat() raises an exception if the value is not a float
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
424 # getint() and getboolean() also do this for their respective types
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
425 float = config.getfloat('Section1', 'float')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
426 int = config.getint('Section1', 'int')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
427 print float + int
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
428
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
429 # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
430 # This is because we are using a RawConfigParser().
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
431 if config.getboolean('Section1', 'bool'):
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
432 print config.get('Section1', 'foo')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
433
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
434 To get interpolation, you will need to use a :class:`ConfigParser` or
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
435 :class:`SafeConfigParser`::
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
436
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
437 import ConfigParser
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
438
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
439 config = ConfigParser.ConfigParser()
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
440 config.read('example.cfg')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
441
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
442 # Set the third, optional argument of get to 1 if you wish to use raw mode.
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
443 print config.get('Section1', 'foo', 0) # -> "Python is fun!"
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
444 print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!"
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
445
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
446 # The optional fourth argument is a dict with members that will take
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
447 # precedence in interpolation.
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
448 print config.get('Section1', 'foo', 0, {'bar': 'Documentation',
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
449 'baz': 'evil'})
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
450
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
451 Defaults are available in all three types of ConfigParsers. They are used in
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
452 interpolation if an option used is not defined elsewhere. ::
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
453
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
454 import ConfigParser
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
455
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
456 # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
457 config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
458 config.read('example.cfg')
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
459
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
460 print config.get('Section1', 'foo') # -> "Python is fun!"
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
461 config.remove_option('Section1', 'bar')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
462 config.remove_option('Section1', 'baz')
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
463 print config.get('Section1', 'foo') # -> "Life is hard!"
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
464
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
465 The function ``opt_move`` below can be used to move options between sections::
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
466
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
467 def opt_move(config, section1, section2, option):
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
468 try:
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
469 config.set(section2, option, config.get(section1, option, 1))
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47010
diff changeset
470 except ConfigParser.NoSectionError:
44051
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
471 # Create non-existent section
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
472 config.add_section(section2)
c024b894d929 Add examples to the ConfigParser documentation.
Georg Brandl <georg@python.org>
parents: 42604
diff changeset
473 opt_move(config, section1, section2, option)
44838
f6d4a2c49531 Fix example.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
474 else:
f6d4a2c49531 Fix example.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
475 config.remove_option(section1, option)