98
135
various other useful modules, and is required by many of the `Voidspace Python
105
It is sometimes possible to get the latest *development version* of ConfigObj
106
from the `Subversion Repository <http://svn.pythonutils.python-hosting.com/trunk/pythonutils/>`_.
108
138
.. _configobj.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj.py
109
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.5.2.zip
139
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.2.0.zip
110
140
.. _validate.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py
141
.. _API Docs: http://www.voidspace.org.uk/python/configobj-api/
111
142
.. _this document:
112
143
.. _configobj homepage: http://www.voidspace.org.uk/python/configobj.html
144
.. _Subversion Repository: http://svn.rest2web.python-hosting.com/branches/configobj4
113
145
.. _Sourceforge: http://sourceforge.net/projects/configobj
146
.. _EpyDoc: http://epydoc.sourceforge.net
114
147
.. _pythonutils: http://www.voidspace.org.uk/python/pythonutils.html
115
.. _Voidspace Python Projects: http://www.voidspace.org.uk/python/index.shtml
119
ConfigObj in the Real World
120
===========================
122
**ConfigObj** is widely used. Projects using it include:
124
* `Bazaar <http://bazaar-ng.org>`_.
126
Bazaar is a Python distributed {acro;VCS;Version Control System}.
127
ConfigObj is used to read ``bazaar.conf`` and ``branches.conf``.
129
* `Turbogears <http://www.turbogears.org/>`_
131
Turbogears is a web application framework.
133
* `Chandler <http://chandler.osafoundation.org/>`_
135
A Python and `wxPython <http://www.wxpython.org>`_
136
{acro;PIM;Personal Information Manager}, being developed by the
137
`OSAFoundation <http://www.osafoundation.org/>`_.
139
* `matplotlib <http://matplotlib.sourceforge.net/>`_
141
A 2D plotting library.
143
* `IPython <http://ipython.scipy.org/moin/>`_
145
IPython is an enhanced interactive Python shell. IPython uses ConfigObj in a module called 'TConfig' that combines it with enthought `Traits <http://code.enthought.com/traits/>`_: `tconfig <http://ipython.scipy.org/ipython/ipython/browser/ipython/branches/saw/sandbox/tconfig>`_.
147
* `Elisa - the Fluendo Mediacenter <http://elisa.fluendo.com/>`_
149
Elisa is an open source cross-platform media center solution designed to be simple for people not particularly familiar with computers.
148
.. _Voidspace Python Projects: http://www.voidspace.org.uk/python
1974
Many config files from other applications allow empty values. As of version
1975
4.3.0, ConfigObj will read these as an empty string.
1977
A new option/attribute has been added (``write_empty_values``) to allow
1978
ConfigObj to write empty strings as empty values.
1983
from configobj import ConfigObj
1988
config = ConfigObj(cfg)
1990
ConfigObj({'key': '', 'key2': ''})
1992
config.write_empty_values = True
1993
for line in config.write():
2004
The ``unrepr`` option allows you to store and retrieve the basic Python
2005
data-types using config files. It has to use a slightly different syntax to
2006
normal ConfigObj files. Unsurprisingly it uses Python syntax.
2008
This means that lists are different (they are surrounded by square brackets),
2009
and strings *must* be quoted.
2011
The types that ``unrepr`` can work with are :
2013
| strings, lists tuples
2015
| dictionaries, integers, floats
2016
| longs and complex numbers
2018
You can't store classes, types or instances.
2020
``unrepr`` uses ``repr(object)`` to write out values, so it currently *doesn't*
2021
check that you are writing valid objects. If you attempt to read an unsupported
2022
value, ConfigObj will raise a ``configobj.UnknownType`` exception.
2024
Values that are triple quoted cased. The triple quotes are removed *before*
2025
converting. This means that you can use triple quotes to write dictionaries
2026
over several lines in your config files. They won't be written like this
2029
If you are writing config files by hand, for use with ``unrepr``, you should
2030
be aware of the following differences from normal ConfigObj syntax :
2032
| List : ``['A List', 'With', 'Strings']``
2033
| Strings : ``"Must be quoted."``
2034
| Backslash : ``"The backslash must be escaped \\"``
2036
These all follow normal Python syntax.
2038
In unrepr mode *inline comments* are not saved. This is because lines are
2039
parsed using the `compiler package <http://docs.python.org/lib/compiler.html>`_
2040
which discards comments.
2043
String Interpolation
2044
====================
2046
1740
ConfigObj allows string interpolation *similar* to the way ``ConfigParser``
2047
or ``string.Template`` work. The value of the ``interpolation`` attribute
2048
determines which style of interpolation you want to use. Valid values are
2049
"ConfigParser" or "Template" (case-insensitive, so "configparser" and
2050
"template" will also work). For backwards compatibility reasons, the value
2051
``True`` is also a valid value for the ``interpolation`` attribute, and
2052
will select ``ConfigParser``-style interpolation. At some undetermined point
2053
in the future, that default *may* change to ``Template``-style interpolation.
2055
For ``ConfigParser``-style interpolation, you specify a value to be
2056
substituted by including ``%(name)s`` in the value.
2058
For ``Template``-style interpolation, you specify a value to be substituted
2059
by including ``${cl}name{cr}`` in the value. Alternately, if 'name' is a valid
2060
Python identifier (i.e., is composed of nothing but alphanumeric characters,
2061
plus the underscore character), then the braces are optional and the value
2062
can be written as ``$name``.
2064
Note that ``ConfigParser``-style interpolation and ``Template``-style
2065
interpolation are mutually exclusive; you cannot have a configuration file
2066
that's a mix of one or the other. Pick one and stick to it. ``Template``-style
2067
interpolation is simpler to read and write by hand, and is recommended if
2068
you don't have a particular reason to use ``ConfigParser``-style.
2070
Interpolation checks first the current section to see if ``name`` is the key
2071
to a value. ('name' is case sensitive).
2073
If it doesn't find it, next it checks the 'DEFAULT' sub-section of the current
2076
If it still doesn't find it, it moves on to check the parent section and the
2077
parent section's 'DEFAULT' subsection, and so on all the way up to the main
2080
If the value specified isn't found in any of these locations, then a
2081
``MissingInterpolationOption`` error is raised (a subclass of
2082
``ConfigObjError``).
1742
You specify a value to be substituted by including ``%(name)s`` in the value.
1744
Interpolation checks first the 'DEFAULT' sub-section of the current section to
1745
see if ``name`` is the key to a value. ('name' is case sensitive).
1747
If it doesn't find it, next it checks the 'DEFAULT' section of the parent
1748
section, last it checks the 'DEFAULT' section of the main section.
1750
If the value specified isn't found then a ``MissingInterpolationOption`` error
1751
is raised (a subclass of ``ConfigObjError``).
2084
1753
If it is found then the returned value is also checked for substitutions. This
2085
1754
allows you to make up compound values (for example directory paths) that use
2086
1755
more than one default value. It also means it's possible to create circular
2087
references. If there are any circular references which would cause an infinite
2088
interpolation loop, an ``InterpolationLoopError`` is raised.
1756
references. If after ten replacements there are still values to substitute, an
1757
``InterpolationDepthError`` is raised.
2090
1759
Both of these errors are subclasses of ``InterpolationError``, which is a
2091
1760
subclass of ``ConfigObjError``.
1866
Backwards Compatibility
1867
=======================
1869
There have been a lot of changes since ConfigObj 3. The core parser is now
1870
based on regular expressions, and is a lot faster and smaller. There is now no
1871
difference in the way we treat flat files and non-flatfiles, that is, no empty
1872
sections. This means some of the code can be a lot simpler, less code does
1873
more of the work [#]_.
1875
There have been other simplifications: for example we only have eight options
1876
instead of seventeen.
1878
Most config files created for ConfigObj 3 will be read with no changes and many
1879
programs will work without having to alter code. Some of the changes do break
1880
backwards compatibility: for example, code that uses the previous options will
1881
now raise an error. It should be very easy to fix these, though.
1883
Below is a list of all the changes that affect backwards compatibility. This
1884
doesn't include details of method signatures that have changed, because almost
1887
Incompatible Changes
1888
--------------------
1890
(I have removed a lot of needless complications: this list is probably not
1891
conclusive, many option/attribute/method names have changed.)
1895
The only valid divider is '='.
1897
Line continuations with ``\`` removed.
1899
No recursive lists in values.
1903
No distinction between flatfiles and non flatfiles.
1905
Change in list syntax: use commas to indicate list, not parentheses (square
1906
brackets and parentheses are no longer recognised as lists).
1908
';' is no longer valid for comments, and no multiline comments.
1910
No attribute-style access to values.
1912
Empty values not allowed: use '' or "".
1914
In ConfigObj 3, setting a non-flatfile member to ``None`` would initialise it
1915
as an empty section.
1917
The escape entities '&mjf-lf;' and '&mjf-quot;' have gone, replaced by triple
1918
quote, multiple line values.
1920
The ``newline``, ``force_return``, and ``default`` options have gone.
1922
``fileerror`` and ``createempty`` options have become ``file_error`` and
1925
Partial configspecs (for specifying the order members should be written out,
1926
and which should be present) have gone. The configspec is no longer used to
1927
specify order for the ``write`` method.
1929
Exceeding the maximum depth of recursion in string interpolation now raises an
1930
error ``InterpolationDepthError``.
1932
Specifying a value for interpolation which doesn't exist now raises a
1933
``MissingInterpolationOption`` error (instead of merely being ignored).
1935
The ``writein`` method has been removed.
1937
The comments attribute is now a list (``inline_comments`` equates to the old
1938
comments attribute).
2201
1943
ConfigObj 3 is now deprecated in favour of ConfigObj 4. I can fix bugs in
2202
1944
ConfigObj 3 if needed, though.
2304
2033
Please file any bug reports to `Michael Foord`_ or the **ConfigObj**
2305
2034
`Mailing List`_.
2307
There is currently no way to specify the encoding of a configspec file.
2309
When using ``copy`` mode for validation, it won't copy ``DEFAULT``
2310
sections. This is so that you *can* use interpolation in configspec
2313
``validate`` doesn't report *extra* values or sections.
2315
You can't have a keyword with the same name as a section (in the same
2316
section). They are both dictionary keys - so they would overlap.
2318
ConfigObj doesn't quote and unquote values if ``list_values=False``.
2319
This means that leading or trailing whitespace in values will be lost when
2320
writing. (Unless you manually quote).
2322
Interpolation checks first the current section, then the 'DEFAULT' subsection
2323
of the current section, before moving on to the current section's parent and
2326
Does it matter that we don't support the ':' divider, which is supported
2327
by ``ConfigParser`` ?
2329
String interpolation and validation don't play well together. When
2330
validation changes type it sets the value. This will correctly fetch the
2331
value using interpolation - but then overwrite the interpolation reference.
2332
If the value is unchanged by validation (it's a string) - but other types
2036
You can't have a keyword with the same name as a section (in the same section).
2037
They are both dictionary keys, so they would overlap.
2039
Interpolation checks first the 'DEFAULT' sub-section of the current section,
2040
next it checks the 'DEFAULT' section of the parent section, last it checks the
2041
'DEFAULT' section of the main section.
2043
Logically a 'DEFAULT' section should apply to all subsections of the *same
2044
parent*: this means that checking the 'DEFAULT' sub-section in the *current
2045
section* is not necessarily logical ?
2047
Does it matter that we don't support the ':' divider, which is supported by
2050
String interpolation and validation don't play well together. See
2051
`Validation and Interpolation`_.
2053
Validation is no longer done on the 'DEFAULT' section (on the root level). This
2054
allows interpolation from within your configspec - but also prevents you
2055
validating the 'DEFAULT' section.
2339
2061
This is an abbreviated changelog showing the major releases up to version 4.
2340
From version 4 it lists all releases and changes.
2343
2008/02/05 - Version 4.5.2
2344
--------------------------
2346
Distribution updated to include version 0.3.2 of validate_. This means that
2347
``None`` as a default value win configspecs works.
2350
2008/02/05 - Version 4.5.1
2351
--------------------------
2353
Distribution updated to include version 0.3.1 of validate_. This means that
2354
Unicode configspecs now work.
2357
2008/02/05 - Version 4.5.0
2358
--------------------------
2360
ConfigObj will now guarantee that files will be written terminated with a
2363
ConfigObj will no longer attempt to import the ``validate`` module, until/unless
2364
you call ``ConfigObj.validate`` with ``preserve_errors=True``. This makes it
2367
New methods ``restore_default`` and ``restore_defaults``. ``restore_default``
2368
resets an entry to its default value (and returns that value). ``restore_defaults``
2369
resets all entries to their default value. It doesn't modify entries without a
2370
default value. You must have validated a ConfigObj (which populates the
2371
``default_values`` dictionary) before calling these methods.
2373
BUGFIX: Proper quoting of keys, values and list values that contain hashes
2374
(when writing). When ``list_values=False``, values containing hashes are
2377
Added the ``reload`` method. This reloads a ConfigObj from file. If the filename
2378
attribute is not set then a ``ReloadError`` (a new exception inheriting from
2379
``IOError``) is raised.
2381
BUGFIX: Files are read in with 'rb' mode, so that native/non-native line endings work!
2383
Minor efficiency improvement in ``unrepr`` mode.
2385
Added missing docstrings for some overidden dictionary methods.
2387
Added the ``reset`` method. This restores a ConfigObj to a freshly created state.
2389
Removed old CHANGELOG file.
2392
2007/02/04 - Version 4.4.0
2393
--------------------------
2395
Official release of 4.4.0
2398
2006/12/17 - Version 4.3.3-alpha4
2399
---------------------------------
2403
Allowed arbitrary indentation in the ``indent_type`` parameter, removed the
2404
``NUM_INDENT_SPACES`` and ``MAX_INTERPOL_DEPTH`` (a leftover) constants,
2405
added indentation tests (including another docutils workaround, sigh), updated
2410
Made the import of ``compiler`` conditional so that ``ConfigObj`` can be used
2411
with `IronPython <http://www.codeplex.com/IronPython>`_.
2414
2006/12/17 - Version 4.3.3-alpha3
2415
---------------------------------
2419
Added a missing ``self.`` in the _handle_comment method and a related test,
2420
per Sourceforge bug #1523975.
2423
2006/12/09 - Version 4.3.3-alpha2
2424
---------------------------------
2428
Changed interpolation search strategy, based on this patch by Robin Munn:
2429
http://sourceforge.net/mailarchive/message.php?msg_id=17125993
2432
2006/12/09 - Version 4.3.3-alpha1
2433
---------------------------------
2437
Added Template-style interpolation, with tests, based on this patch by
2438
Robin Munn: http://sourceforge.net/mailarchive/message.php?msg_id=17125991
2439
(awful archives, bad Sourceforge, bad).
2442
2006/06/04 - Version 4.3.2
2443
--------------------------
2445
Changed error handling, if parsing finds a single error then that error will
2446
be re-raised. That error will still have an ``errors`` and a ``config``
2449
Fixed bug where '\\n' terminated files could be truncated.
2451
Bugfix in ``unrepr`` mode, it couldn't handle '#' in values. (Thanks to
2452
Philippe Normand for the report.)
2454
As a consequence of this fix, ConfigObj doesn't now keep inline comments in
2455
``unrepr`` mode. This is because the parser in the `compiler package`_
2456
doesn't keep comments. {sm;:-)}
2458
Error messages are now more useful. They tell you the number of parsing errors
2459
and the line number of the first error. (In the case of multiple errors.)
2461
Line numbers in exceptions now start at 1, not 0.
2463
Errors in ``unrepr`` mode are now handled the same way as in the normal mode.
2464
The errors stored will be an ``UnreprError``.
2467
2006/04/29 - Version 4.3.1
2468
--------------------------
2470
Added ``validate.py`` back into ``configobj.zip``. (Thanks to Stewart
2473
Updated to `validate.py`_ 0.2.2.
2475
Preserve tuples when calling the ``dict`` method. (Thanks to Gustavo Niemeyer.)
2477
Changed ``__repr__`` to return a string that contains ``ConfigObj({ ... })``.
2479
Change so that an options dictionary isn't modified by passing it to ConfigObj.
2480
(Thanks to Artarious.)
2482
Added ability to handle negative integers in ``unrepr``. (Thanks to Kevin
2486
2006/03/24 - Version 4.3.0
2487
--------------------------
2489
Moved the tests and the CHANGELOG (etc) into a separate file. This has reduced
2490
the size of ``configobj.py`` by about 40%.
2492
Added the ``unrepr`` mode to reading and writing config files. Thanks to Kevin
2493
Dangoor for this suggestion.
2495
Empty values are now valid syntax. They are read as an empty string ``''``.
2496
(``key =``, or ``key = # comment``.)
2498
``validate`` now honours the order of the configspec.
2500
Added the ``copy`` mode to validate. Thanks to Louis Cordier for this
2503
Fixed bug where files written on windows could be given ``'\r\r\n'`` line
2506
Fixed bug where last occurring comment line could be interpreted as the
2507
final comment if the last line isn't terminated.
2509
Fixed bug where nested list values would be flattened when ``write`` is
2510
called. Now sub-lists have a string representation written instead.
2512
Deprecated ``encode`` and ``decode`` methods instead.
2514
You can now pass in a ConfigObj instance as a configspec (remember to read
2515
the configspec file using ``list_values=False``).
2517
Sorted footnotes in the docs.
2062
From version 4 it lists all releases and changes. *More* data on individual
2063
changes may be found in the source code.
2520
2065
2006/02/16 - Version 4.2.0
2521
2066
--------------------------
2825
2366
textmacros module and the PySrc CSS stuff. See
2826
2367
http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml
2831
2371
<div align="center">
2833
<a href="http://www.python.org">
2834
<img src="images/new_python.gif" width="100" height="103" border="0"
2835
alt="Powered by Python" />
2837
<a href="http://sourceforge.net">
2838
<img src="http://sourceforge.net/sflogo.php?group_id=123265&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
2840
<a href="http://www.opensource.org">
2841
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0"
2842
alt="Certified Open Source"/>
2846
<a href="http://www.voidspace.org.uk/python/index.shtml">
2847
<img src="images/pythonbanner.gif" width="468" height="60"
2848
alt="Python on Voidspace" border="0" />
2372
<a href="http://sourceforge.net/donate/index.php?group_id=123265">
2373
<img src="http://images.sourceforge.net/images/project-support.jpg" width="88" height="32" border="0" alt="Support This Project" />
2375
<a href="http://sourceforge.net">
2376
<img src="http://sourceforge.net/sflogo.php?group_id=123265&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
2379
<a href="http://www.python.org">
2380
<img src="images/powered_by_python.jpg" width="602" height="186" border="0" />
2382
<a href="http://www.opensource.org">
2383
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0" />
2384
<br /><strong>Certified Open Source</strong>
2387
<script type="text/javascript" language="JavaScript">var site="s16atlantibots"</script>
2388
<script type="text/javascript" language="JavaScript1.2" src="http://s16.sitemeter.com/js/counter.js?site=s16atlantibots"></script>
2390
<a href="http://s16.sitemeter.com/stats.asp?site=s16atlantibots">
2391
<img src="http://s16.sitemeter.com/meter.asp?site=s16atlantibots" alt="Site Meter" border=0 />
2854
2396
.. _listquote: http://www.voidspace.org.uk/python/modules.shtml#listquote
2855
.. _Michael Foord: http://www.voidspace.org.uk/python/weblog/index.shtml
2397
.. _Michael Foord: http://www.voidspace.org.uk/index2.shtml
2856
2398
.. _Nicola Larosa: http://www.teknico.net
2399
.. _Mark Andrews: http://www.la-la.com