| 1 |
<?xml version="1.0" encoding="utf-8"?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 3 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 4 |
|
| 5 |
<html> |
| 6 |
<head> |
| 7 |
<title>iniparse</title> |
| 8 |
<style type="text/css" title="stylesheet"> @import "style.css"; </style> |
| 9 |
<link rel="SHORTCUT ICON" href="http://www.python.org/pics/pyfav.gif" /> |
| 10 |
</head> |
| 11 |
|
| 12 |
<body> |
| 13 |
<div id="title"> |
| 14 |
<h1 style="font-family: monospace">iniparse</h1> |
| 15 |
<p>Better INI parser for Python</p> |
| 16 |
</div> |
| 17 |
|
| 18 |
<div class="box"> |
| 19 |
<div class="boxtitle">Introduction</div> |
| 20 |
|
| 21 |
<div class="boxitem"> |
| 22 |
<p><code>iniparse</code> is a INI parser for |
| 23 |
<a href="http://www.python.org/">Python</a> |
| 24 |
which is:</p> |
| 25 |
|
| 26 |
<ul> |
| 27 |
|
| 28 |
<li><b>Compatible with <code> |
| 29 |
<a href="http://docs.python.org/lib/module-ConfigParser.html">ConfigParser</a></code></b>: |
| 30 |
Backward compatible implementations of <code>ConfigParser</code>, |
| 31 |
<code>RawConfigParser</code>, and <code>SafeConfigParser</code> |
| 32 |
are included that are API-compatible with the Python standard |
| 33 |
library.</li> |
| 34 |
|
| 35 |
<li><b>Preserves structure of INI files</b>: Order of sections & |
| 36 |
options, indentation, comments, and blank lines are preserved as far |
| 37 |
as possible when data is updated.</li> |
| 38 |
|
| 39 |
<li><b>More convenient</b>: Values can be accessed using dotted |
| 40 |
notation (<code>cfg.user.name</code>), or using container syntax |
| 41 |
(<code>cfg['user']['name']</code>).</li> |
| 42 |
|
| 43 |
<li><b>Extensible</b>: It is possible to add other configuration |
| 44 |
formats, and to convert between different formats (as long as the |
| 45 |
data models are compatible).</li> |
| 46 |
|
| 47 |
</ul> |
| 48 |
|
| 49 |
<p>It is very useful for config files that are updated both by users |
| 50 |
and by programs, since it is very disorienting for a user to have |
| 51 |
her config file completely rearranged whenever a program changes it. |
| 52 |
iniparse also allows making the order of entries in a config file |
| 53 |
significant, which is desirable in applications like image |
| 54 |
galleries.</p> |
| 55 |
|
| 56 |
<p><b>Website</b>: <a href="http://code.google.com/p/iniparse/" |
| 57 |
>http://code.google.com/p/iniparse/</a></p> |
| 58 |
|
| 59 |
</div> |
| 60 |
</div> |
| 61 |
|
| 62 |
<div class="box"> |
| 63 |
<div class="boxtitle">Examples</div> |
| 64 |
|
| 65 |
<div class="boxitem"> |
| 66 |
<b>New API:</b> |
| 67 |
<ul> |
| 68 |
<li>Open an INI file: |
| 69 |
<pre> |
| 70 |
>>> from iniparse import INIConfig |
| 71 |
>>> cfg = INIConfig(file('options.ini')) |
| 72 |
</pre> |
| 73 |
</li> |
| 74 |
<li>Access/Modify data: |
| 75 |
<pre> |
| 76 |
>>> print cfg.playlist.expand_playlist |
| 77 |
True |
| 78 |
>>> print cfg.ui.width |
| 79 |
150 |
| 80 |
>>> cfg.ui.width = 200 |
| 81 |
>>> print cfg['ui']['width'] |
| 82 |
200 |
| 83 |
</pre> |
| 84 |
</li> |
| 85 |
<li>Print data: |
| 86 |
<pre> |
| 87 |
>>> print cfg |
| 88 |
[playlist] |
| 89 |
expand_playlist = True |
| 90 |
|
| 91 |
[ui] |
| 92 |
display_clock = True |
| 93 |
display_qlength = True |
| 94 |
width = 200 |
| 95 |
</pre> |
| 96 |
</li> |
| 97 |
</ul> |
| 98 |
|
| 99 |
<b>Backward Compatible API:</b> |
| 100 |
<ul> |
| 101 |
<li>The entire ConfigParser API is supported. This is just a brief |
| 102 |
example: |
| 103 |
<pre> |
| 104 |
>>> from iniparse import ConfigParser |
| 105 |
>>> cfgpr = ConfigParser() |
| 106 |
>>> cfgpr.read('options.ini') |
| 107 |
>>> print cfgpr.get('ui', 'width') |
| 108 |
150 |
| 109 |
>>> cfgpr.set('ui', 'width', 175) |
| 110 |
</pre> |
| 111 |
</li> |
| 112 |
<li>The new API can also be accessed via backward-compatible objects: |
| 113 |
<pre> |
| 114 |
>>> print cfgpr.data.playlist.expand_playlist |
| 115 |
True |
| 116 |
>>> cfgpr.data.ui.width = 200 |
| 117 |
>>> print cfgpr.data.ui.width |
| 118 |
200 |
| 119 |
</pre> |
| 120 |
</li> |
| 121 |
</ul> |
| 122 |
|
| 123 |
<b>A non-INI example:</b> |
| 124 |
<ul> |
| 125 |
<li>A simple dotted format is also implemented: |
| 126 |
<pre> |
| 127 |
>>> from iniparse import BasicConfig |
| 128 |
>>> n = BasicConfig() |
| 129 |
>>> n.x = 7 |
| 130 |
>>> n.name.first = 'paramjit' |
| 131 |
>>> n.name.last = 'oberoi' |
| 132 |
>>> print n.x |
| 133 |
7 |
| 134 |
>>> print n.name.first |
| 135 |
'paramjit' |
| 136 |
>>> print n |
| 137 |
name.first = paramjit |
| 138 |
name.last = oberoi |
| 139 |
x = 7 |
| 140 |
</pre> |
| 141 |
</li> |
| 142 |
<li>Convert to INI: |
| 143 |
<pre> |
| 144 |
>>> from iniparse import INIConfig |
| 145 |
>>> i = INIConfig() |
| 146 |
>>> del n.x # since INI doesn't support top-level values |
| 147 |
>>> i.import_config(n) |
| 148 |
>>> print i |
| 149 |
[name] |
| 150 |
first = paramjit |
| 151 |
last = oberoi |
| 152 |
</pre> |
| 153 |
</li> |
| 154 |
</ul> |
| 155 |
|
| 156 |
</div> |
| 157 |
|
| 158 |
<!-- div class="boxitem"> |
| 159 |
<p>For more information, see the automatically generated |
| 160 |
<a href="iniparse.html">API documentation</a>.</p> |
| 161 |
</div --> |
| 162 |
|
| 163 |
</div> |
| 164 |
<div id="footer"> |
| 165 |
<p> |
| 166 |
Updated on 15 July 2007 |
| 167 |
</p> |
| 168 |
</div> |
| 169 |
</body> |
| 170 |
</html> |