1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>Reading and Writing Config Files</title>
<meta name="authors" content="Michael Foord Nicola Larosa" />
<meta name="date" content="2006/06/04" />
<meta content="ConfigObj - a Python module for easy reading and writing of config files." name="description" />
<meta content="python, script, module, config, configuration, data, persistence, developer, configparser" name="keywords" />
<link rel="stylesheet" href="stylesheets/voidspace_docutils.css" type="text/css" />
</head>
<body>
<div class="document" id="reading-and-writing-config-files">
<h1 class="title">Reading and Writing Config Files</h1>
<h2 class="subtitle" id="configobj-4-introduction-and-reference">ConfigObj 4 Introduction and Reference</h2>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Authors:</th>
<td>Michael Foord
<br />Nicola Larosa</td></tr>
<tr><th class="docinfo-name">Version:</th>
<td>ConfigObj 4.3.2</td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>2006/06/04</td></tr>
<tr class="field"><th class="docinfo-name">Homepage:</th><td class="field-body"><a class="reference" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a></td>
</tr>
<tr class="field"><th class="docinfo-name">Sourceforge:</th><td class="field-body"><a class="reference" href="http://sourceforge.net/projects/configobj">Sourceforge</a></td>
</tr>
<tr class="field"><th class="docinfo-name">Development:</th><td class="field-body"><a class="reference" href="http://svn.pythonutils.python-hosting.com">SVN Repository</a></td>
</tr>
<tr class="field"><th class="docinfo-name">License:</th><td class="field-body"><a class="reference" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></td>
</tr>
<tr class="field"><th class="docinfo-name">Support:</th><td class="field-body"><a class="reference" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a></td>
</tr>
</tbody>
</table>
<div class="contents topic">
<p class="topic-title first"><a id="configobj-manual" name="configobj-manual">ConfigObj Manual</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#introduction" id="id32" name="id32">1 Introduction</a></li>
<li><a class="reference" href="#downloading" id="id33" name="id33">2 Downloading</a><ul class="auto-toc">
<li><a class="reference" href="#files" id="id34" name="id34">2.1 Files</a></li>
<li><a class="reference" href="#documentation" id="id35" name="id35">2.2 Documentation</a></li>
<li><a class="reference" href="#pythonutils" id="id36" name="id36">2.3 Pythonutils</a></li>
<li><a class="reference" href="#development-version" id="id37" name="id37">2.4 Development Version</a></li>
</ul>
</li>
<li><a class="reference" href="#getting-started" id="id38" name="id38">3 Getting Started</a><ul class="auto-toc">
<li><a class="reference" href="#reading-a-config-file" id="id39" name="id39">3.1 Reading a Config File</a></li>
<li><a class="reference" href="#writing-a-config-file" id="id40" name="id40">3.2 Writing a Config File</a></li>
<li><a class="reference" href="#config-files" id="id41" name="id41">3.3 Config Files</a></li>
</ul>
</li>
<li><a class="reference" href="#configobj-specifications" id="id42" name="id42">4 ConfigObj specifications</a><ul class="auto-toc">
<li><a class="reference" href="#infile" id="id43" name="id43">4.1 infile</a></li>
<li><a class="reference" href="#options" id="id44" name="id44">4.2 options</a></li>
<li><a class="reference" href="#methods" id="id45" name="id45">4.3 Methods</a><ul class="auto-toc">
<li><a class="reference" href="#write" id="id46" name="id46">4.3.1 write</a></li>
<li><a class="reference" href="#validate" id="id47" name="id47">4.3.2 validate</a><ul class="auto-toc">
<li><a class="reference" href="#return-value" id="id48" name="id48">4.3.2.1 Return Value</a></li>
<li><a class="reference" href="#mentioning-default-values" id="id49" name="id49">4.3.2.2 Mentioning Default Values</a></li>
<li><a class="reference" href="#mentioning-repeated-sections" id="id50" name="id50">4.3.2.3 Mentioning Repeated Sections</a></li>
<li><a class="reference" href="#mentioning-simpleval" id="id51" name="id51">4.3.2.4 Mentioning SimpleVal</a></li>
<li><a class="reference" href="#mentioning-copy-mode" id="id52" name="id52">4.3.2.5 Mentioning copy Mode</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference" href="#attributes" id="id53" name="id53">4.4 Attributes</a><ul class="auto-toc">
<li><a class="reference" href="#interpolate" id="id54" name="id54">4.4.1 interpolate</a></li>
<li><a class="reference" href="#stringify" id="id55" name="id55">4.4.2 stringify</a></li>
<li><a class="reference" href="#bom" id="id56" name="id56">4.4.3 BOM</a></li>
<li><a class="reference" href="#initial-comment" id="id57" name="id57">4.4.4 initial_comment</a></li>
<li><a class="reference" href="#final-comment" id="id58" name="id58">4.4.5 final_comment</a></li>
<li><a class="reference" href="#list-values" id="id59" name="id59">4.4.6 list_values</a></li>
<li><a class="reference" href="#encoding" id="id60" name="id60">4.4.7 encoding</a></li>
<li><a class="reference" href="#default-encoding" id="id61" name="id61">4.4.8 default_encoding</a></li>
<li><a class="reference" href="#unrepr" id="id62" name="id62">4.4.9 unrepr</a></li>
<li><a class="reference" href="#write-empty-values" id="id63" name="id63">4.4.10 write_empty_values</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference" href="#the-config-file-format" id="id64" name="id64">5 The Config File Format</a></li>
<li><a class="reference" href="#sections" id="id65" name="id65">6 Sections</a><ul class="auto-toc">
<li><a class="reference" href="#section-attributes" id="id66" name="id66">6.1 Section Attributes</a></li>
<li><a class="reference" href="#section-methods" id="id67" name="id67">6.2 Section Methods</a></li>
<li><a class="reference" href="#walking-a-section" id="id68" name="id68">6.3 Walking a Section</a></li>
<li><a class="reference" href="#examples" id="id69" name="id69">6.4 Examples</a></li>
</ul>
</li>
<li><a class="reference" href="#exceptions" id="id70" name="id70">7 Exceptions</a></li>
<li><a class="reference" href="#validation" id="id71" name="id71">8 Validation</a><ul class="auto-toc">
<li><a class="reference" href="#configspec" id="id72" name="id72">8.1 configspec</a></li>
<li><a class="reference" href="#type-conversion" id="id73" name="id73">8.2 Type Conversion</a></li>
<li><a class="reference" href="#default-values" id="id74" name="id74">8.3 Default Values</a><ul class="auto-toc">
<li><a class="reference" href="#id16" id="id75" name="id75">8.3.1 List Values</a></li>
</ul>
</li>
<li><a class="reference" href="#repeated-sections" id="id76" name="id76">8.4 Repeated Sections</a></li>
<li><a class="reference" href="#copy-mode" id="id77" name="id77">8.5 Copy Mode</a></li>
<li><a class="reference" href="#validation-and-interpolation" id="id78" name="id78">8.6 Validation and Interpolation</a></li>
<li><a class="reference" href="#simpleval" id="id79" name="id79">8.7 SimpleVal</a></li>
</ul>
</li>
<li><a class="reference" href="#empty-values" id="id80" name="id80">9 Empty values</a></li>
<li><a class="reference" href="#unrepr-mode" id="id81" name="id81">10 unrepr mode</a></li>
<li><a class="reference" href="#interpolation" id="id82" name="id82">11 Interpolation</a></li>
<li><a class="reference" href="#comments" id="id83" name="id83">12 Comments</a></li>
<li><a class="reference" href="#flatten-errors" id="id84" name="id84">13 flatten_errors</a><ul class="auto-toc">
<li><a class="reference" href="#example-usage" id="id85" name="id85">13.1 Example Usage</a></li>
</ul>
</li>
<li><a class="reference" href="#backwards-compatibility" id="id86" name="id86">14 Backwards Compatibility</a><ul class="auto-toc">
<li><a class="reference" href="#incompatible-changes" id="id87" name="id87">14.1 Incompatible Changes</a></li>
<li><a class="reference" href="#configobj-3" id="id88" name="id88">14.2 ConfigObj 3</a></li>
</ul>
</li>
<li><a class="reference" href="#credits" id="id89" name="id89">15 CREDITS</a></li>
<li><a class="reference" href="#license" id="id90" name="id90">16 LICENSE</a></li>
<li><a class="reference" href="#todo" id="id91" name="id91">17 TODO</a></li>
<li><a class="reference" href="#issues" id="id92" name="id92">18 ISSUES</a></li>
<li><a class="reference" href="#changelog" id="id93" name="id93">19 CHANGELOG</a><ul class="auto-toc">
<li><a class="reference" href="#version-4-3-2" id="id94" name="id94">19.1 2006/06/04 - Version 4.3.2</a></li>
<li><a class="reference" href="#version-4-3-1" id="id95" name="id95">19.2 2006/04/29 - Version 4.3.1</a></li>
<li><a class="reference" href="#version-4-3-0" id="id96" name="id96">19.3 2006/03/24 - Version 4.3.0</a></li>
<li><a class="reference" href="#version-4-2-0" id="id97" name="id97">19.4 2006/02/16 - Version 4.2.0</a></li>
<li><a class="reference" href="#version-4-1-0" id="id98" name="id98">19.5 2005/12/14 - Version 4.1.0</a></li>
<li><a class="reference" href="#version-4-0-2" id="id99" name="id99">19.6 2005/12/02 - Version 4.0.2</a></li>
<li><a class="reference" href="#version-4-0-1" id="id100" name="id100">19.7 2005/11/05 - Version 4.0.1</a></li>
<li><a class="reference" href="#version-4-0-0" id="id101" name="id101">19.8 2005/10/17 - Version 4.0.0</a></li>
<li><a class="reference" href="#version-4-0-0-beta-5" id="id102" name="id102">19.9 2005/09/09 - Version 4.0.0 beta 5</a></li>
<li><a class="reference" href="#version-4-0-0-beta-4" id="id103" name="id103">19.10 2005/09/07 - Version 4.0.0 beta 4</a></li>
<li><a class="reference" href="#version-4-0-0-beta-3" id="id104" name="id104">19.11 2005/08/28 - Version 4.0.0 beta 3</a></li>
<li><a class="reference" href="#version-4-0-0-beta-2" id="id105" name="id105">19.12 2005/08/25 - Version 4.0.0 beta 2</a></li>
<li><a class="reference" href="#version-4-0-0-beta-1" id="id106" name="id106">19.13 2005/08/21 - Version 4.0.0 beta 1</a></li>
<li><a class="reference" href="#version-3-0-0" id="id107" name="id107">19.14 2004/05/24 - Version 3.0.0</a></li>
<li><a class="reference" href="#version-2-0-0-beta" id="id108" name="id108">19.15 2004/03/14 - Version 2.0.0 beta</a></li>
<li><a class="reference" href="#version-1-0-5" id="id109" name="id109">19.16 2004/01/29 - Version 1.0.5</a></li>
<li><a class="reference" href="#origins" id="id110" name="id110">19.17 Origins</a></li>
</ul>
</li>
<li><a class="reference" href="#footnotes" id="id111" name="id111">20 Footnotes</a></li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id32" id="introduction" name="introduction">1 Introduction</a></h1>
<p><strong>ConfigObj</strong> is a simple but powerful config file reader and writer: an <em>ini
file round tripper</em>. Its main feature is that it is very easy to use, with a
straightforward programmer's interface and a simple syntax for config files.
It has lots of other features though :</p>
<div class="sidebar">
<p class="first sidebar-title">ConfigObj in the Real World</p>
<p><strong>ConfigObj</strong> is now used as the config file parser for <a class="reference" href="http://bazaar-ng.org">Bazaar</a>.</p>
<p>Bazaar is <em>the</em> Python distributed <acronym title="Version Control System">VCS</acronym>.
ConfigObj is used to read <tt class="docutils literal"><span class="pre">bazaar.conf</span></tt> and <tt class="docutils literal"><span class="pre">branches.conf</span></tt>.</p>
<p>Other projects that use <strong>ConfigObj</strong> include :</p>
<ul class="last">
<li><p class="first"><a class="reference" href="http://planetplus.python-hosting.com/">Planet Plus</a></p>
<blockquote>
<p>A new web application version of <a class="reference" href="http://www.planetplanet.org/">Planet</a>,
the web aggregator.</p>
</blockquote>
</li>
<li><p class="first"><a class="reference" href="http://projects.scipy.org/neuroimaging/ni/wiki">NeuroImaging in Python</a></p>
<blockquote>
<p>BrainSTAT is a project with the ultimate goal to produce a
platform-independent python environment for the analysis of brain
imaging data.</p>
</blockquote>
</li>
<li><p class="first"><a class="reference" href="http://www.tracos.org/gruik/wiki">Gruik</a></p>
<blockquote>
<p>Gruik is a free software network packet sniffer.</p>
</blockquote>
</li>
<li><p class="first"><a class="reference" href="http://www.turbogears.org/">Turbogears</a></p>
<blockquote>
<p><strong>ConfigObj</strong> will be used for reading Turbogears config files, from
version 1.1.</p>
</blockquote>
</li>
</ul>
</div>
<ul>
<li><p class="first">Nested sections (subsections), to any level</p>
</li>
<li><p class="first">List values</p>
</li>
<li><p class="first">Multiple line values</p>
</li>
<li><p class="first">String interpolation (substitution)</p>
</li>
<li><p class="first">Integrated with a powerful validation system</p>
<blockquote>
<ul class="simple">
<li>including automatic type checking/conversion</li>
<li>repeated sections</li>
<li>and allowing default values</li>
</ul>
</blockquote>
</li>
<li><p class="first">All comments in the file are preserved</p>
</li>
<li><p class="first">The order of keys/sections is preserved</p>
</li>
<li><p class="first">No external dependencies</p>
</li>
<li><p class="first">Full Unicode support</p>
</li>
<li><p class="first">A powerful <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode for storing basic datatypes</p>
</li>
</ul>
<p>ConfigObj has a barrage of doctests <a class="footnote-reference" href="#id18" id="id1" name="id1">[1]</a> built into it, testing almost every
feature. Run <tt class="docutils literal"><span class="pre">python</span> <span class="pre">configobj_test.py</span> <span class="pre">-v</span></tt> to see them in action.</p>
<p>For support and bug reports please use the ConfigObj <a class="reference" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id33" id="downloading" name="downloading">2 Downloading</a></h1>
<p>The current version is <strong>4.3.2</strong>, dated 4th June 2006. ConfigObj 4 is
now stable. We still expect to pick up a few bugs along the way though <a class="footnote-reference" href="#id19" id="id2" name="id2">[2]</a>.
<img src="smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
<p>You can get ConfigObj in the following ways :</p>
<div class="section">
<h2><a class="toc-backref" href="#id34" id="files" name="files">2.1 Files</a></h2>
<ul>
<li><p class="first"><a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj.py">configobj.py</a> from Voidspace</p>
<blockquote>
<p>ConfigObj has no external dependencies. This file is sufficient to access
all the functionality except <a class="reference" href="#validation">Validation</a>.</p>
</blockquote>
</li>
<li><p class="first"><a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.3.2.zip">configobj.zip</a> from Voidspace</p>
<blockquote>
<p>This also contains <a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a> , the <a class="reference" href="http://www.voidspace.org.uk/python/configobj-api/">API Docs</a> and <a class="reference" href="http://www.voidspace.org.uk/python/configobj.html">this document</a>.</p>
</blockquote>
</li>
<li><p class="first">The latest development version can be obtained from the <a class="reference" href="http://svn.pythonutils.python-hosting.com/trunk/pythonutils/">Subversion
Repository</a>.</p>
</li>
<li><p class="first"><a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a> from Voidspace</p>
</li>
<li><p class="first">You can also download <em>configobj.zip</em> from <a class="reference" href="http://sourceforge.net/projects/configobj">Sourceforge</a></p>
</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id35" id="documentation" name="documentation">2.2 Documentation</a></h2>
<p><em>configobj.zip</em> contains <a class="reference" href="http://www.voidspace.org.uk/python/configobj.html">this document</a> and full <a class="reference" href="http://www.voidspace.org.uk/python/configobj-api/">API Docs</a>, generated by
the <a class="reference" href="http://epydoc.sourceforge.net">EpyDoc</a> program.</p>
<ul class="simple">
<li>You can view <a class="reference" href="http://www.voidspace.org.uk/python/configobj.html">this document</a> online as the <a class="reference" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a>.</li>
<li>You can also browse the <a class="reference" href="http://www.voidspace.org.uk/python/configobj-api/">API Docs</a> online.</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id36" id="pythonutils" name="pythonutils">2.3 Pythonutils</a></h2>
<p>ConfigObj is also part of the <a class="reference" href="http://www.voidspace.org.uk/python/pythonutils.html">Pythonutils</a> set of modules. This contains
various other useful modules, and is required by many of the <a class="reference" href="http://www.voidspace.org.uk/python">Voidspace Python
Projects</a>.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id37" id="development-version" name="development-version">2.4 Development Version</a></h2>
<p>It is sometimes possible to get the latest <em>development version</em> of ConfigObj
from the <a class="reference" href="http://svn.pythonutils.python-hosting.com/trunk/pythonutils/">Subversion Repository</a>.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id38" id="getting-started" name="getting-started">3 Getting Started</a></h1>
<p>The outstanding feature of using ConfigObj is simplicity. Most functions can be
performed with single line commands.</p>
<div class="section">
<h2><a class="toc-backref" href="#id39" id="reading-a-config-file" name="reading-a-config-file">3.1 Reading a Config File</a></h2>
<p>The normal way to read a config file, is to give ConfigObj the filename :</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>You can also pass the config file in as a list of lines, or a <tt class="docutils literal"><span class="pre">StringIO</span></tt>
instance, so it doesn't matter where your config data comes from.</p>
<p>You can then access members of your config file as a dictionary. Subsections
will also be dictionaries.</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">)</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">value1</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword1'</span><span class="pyoperator">]</span><br />
<span class="pytext">value2</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword2'</span><span class="pyoperator">]</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">section1</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><br />
<span class="pytext">value3</span> <span class="pyoperator">=</span> <span class="pytext">section1</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span><br />
<span class="pytext">value4</span> <span class="pyoperator">=</span> <span class="pytext">section1</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span><br />
<span class="pycomment">#<br />
</span><span class="pycomment"># you could also write<br />
</span><span class="pytext">value3</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span><br />
<span class="pytext">value4</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span><span class="pytext"></span></div></div>
<div class="section">
<h2><a class="toc-backref" href="#id40" id="writing-a-config-file" name="writing-a-config-file">3.2 Writing a Config File</a></h2>
<p>Creating a new config file is just as easy as reading one. You can specify a
filename when you create the ConfigObj, or do it later <a class="footnote-reference" href="#id20" id="id4" name="id4">[3]</a>.</p>
<p>If you <em>don't</em> set a filename, then the <tt class="docutils literal"><span class="pre">write</span></tt> method will return a list of
lines instead of writing to file. See the <a class="reference" href="#write">write</a> method for more details.</p>
<p>Here we show creating an empty ConfigObj, setting a filename and some values,
and then writing to file :</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">filename</span> <span class="pyoperator">=</span> <span class="pytext">filename</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword1'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value1</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'keyword2'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value2</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pyoperator">}</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword3'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value3</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section1'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword4'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">value4</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">section2</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword5'</span><span class="pyoperator">:</span> <span class="pytext">value5</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword6'</span><span class="pyoperator">:</span> <span class="pytext">value6</span><span class="pyoperator">,</span><br />
<span class="pystring">'sub-section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword7'</span><span class="pyoperator">:</span> <span class="pytext">value7</span><br />
<span class="pyoperator">}</span><br />
<span class="pyoperator">}</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section2'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">section2</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pyoperator">}</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword 8'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pytext">value8</span><span class="pyoperator">,</span> <span class="pytext">value9</span><span class="pyoperator">,</span> <span class="pytext">value10</span><span class="pyoperator">]</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'section3'</span><span class="pyoperator">]</span><span class="pyoperator">[</span><span class="pystring">'keyword 9'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pytext">value11</span><span class="pyoperator">,</span> <span class="pytext">value12</span><span class="pyoperator">,</span> <span class="pytext">value13</span><span class="pyoperator">]</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="caution">
<p class="first admonition-title">Caution!</p>
<p class="last">Keywords and section names can only be strings <a class="footnote-reference" href="#id21" id="id5" name="id5">[4]</a>. Attempting to set
anything else will raise a <tt class="docutils literal"><span class="pre">ValueError</span></tt>.</p>
</div>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id41" id="config-files" name="config-files">3.3 Config Files</a></h2>
<p>The config files that ConfigObj will read and write are based on the 'INI'
format. This means it will read and write files created for <tt class="docutils literal"><span class="pre">ConfigParser</span></tt>
<a class="footnote-reference" href="#id22" id="id6" name="id6">[5]</a>.</p>
<p>Keywords and values are separated by an <tt class="docutils literal"><span class="pre">'='</span></tt>, and section markers are
between square brackets. Keywords, values, and section names can be surrounded
by single or double quotes. Indentation is not significant, but can be
preserved.</p>
<p>Subsections are indicated by repeating the square brackets in the section
marker. You nest levels by using more brackets.</p>
<p>You can have list values by separating items with a comma, and values spanning
multiple lines by using triple quotes (single or double).</p>
<p>For full details on all these see <a class="reference" href="#the-config-file-format">the config file format</a>. Here's an example
to illustrate :</p>
<pre class="literal-block">
# This is the 'initial_comment'
# Which may be several lines
keyword1 = value1
'keyword 2' = 'value 2'
[ "section 1" ]
# This comment goes with keyword 3
keyword 3 = value 3
'keyword 4' = value4, value 5, 'value 6'
[[ sub-section ]] # an inline comment
# sub-section is inside "section 1"
'keyword 5' = 'value 7'
'keyword 6' = '''A multiline value,
that spans more than one line :-)
The line breaks are included in the value.'''
[[[ sub-sub-section ]]]
# sub-sub-section is *in* 'sub-section'
# which is in 'section 1'
'keyword 7' = 'value 8'
[section 2] # an inline comment
keyword8 = "value 9"
keyword9 = value10 # an inline comment
# The 'final_comment'
# Which also may be several lines
</pre>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id42" id="configobj-specifications" name="configobj-specifications">4 ConfigObj specifications</a></h1>
<div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">infile</span><span class="pyoperator">=</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pytext">options</span><span class="pyoperator">=</span><span class="pytext">None</span><span class="pyoperator">,</span> <span class="pyoperator">**</span><span class="pytext">keywargs</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="section">
<h2><a class="toc-backref" href="#id43" id="infile" name="infile">4.1 infile</a></h2>
<p>You don't need to specify an infile. If you omit it, an empty ConfigObj will be
created. <tt class="docutils literal"><span class="pre">infile</span></tt> <em>can</em> be :</p>
<ul class="simple">
<li>Nothing. In which case the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be
<tt class="docutils literal"><span class="pre">None</span></tt>. You can set a filename at any time.</li>
<li>A filename. What happens if the file doesn't already exist is determined by
the <a class="reference" href="#options">options</a> <tt class="docutils literal"><span class="pre">file_error</span></tt> and <tt class="docutils literal"><span class="pre">create_empty</span></tt>. The filename will be
preserved as the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute. This can be changed at any time.</li>
<li>A list of lines. Any trailing newlines will be removed from the lines. The
<tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
<li>A <tt class="docutils literal"><span class="pre">StringIO</span></tt> instance or file object, or any object with a <tt class="docutils literal"><span class="pre">read</span></tt> method.
The <tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt> <a class="footnote-reference" href="#id23" id="id7" name="id7">[6]</a>.</li>
<li>A dictionary. You can initialise a ConfigObj from a dictionary <a class="footnote-reference" href="#id24" id="id8" name="id8">[7]</a>. The
<tt class="docutils literal"><span class="pre">filename</span></tt> attribute of your ConfigObj will be <tt class="docutils literal"><span class="pre">None</span></tt>. All keys must be
strings. In this case, the order of values and sections is arbitrary.</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id44" id="options" name="options">4.2 options</a></h2>
<p>There are various options that control the way ConfigObj behaves. They can be
passed in as a dictionary of options, or as keyword arguments. Explicit keyword
arguments override the dictionary.</p>
<p>All of the options are available as attributes after the config file has been
parsed.</p>
<p>ConfigObj has the following options (with the default values shown) :</p>
<ul>
<li><p class="first">'raise_errors': <tt class="docutils literal"><span class="pre">False</span></tt></p>
<blockquote>
<p>When parsing, it is possible that the config file will be badly formed. The
default is to parse the whole file and raise a single error at the end. You
can set <tt class="docutils literal"><span class="pre">raise_errors</span> <span class="pre">=</span> <span class="pre">True</span></tt> to have errors raised immediately. See the
<a class="reference" href="#exceptions">exceptions</a> section for more details.</p>
<p>Altering this value after initial parsing has no effect.</p>
</blockquote>
</li>
<li><p class="first">'list_values': <tt class="docutils literal"><span class="pre">True</span></tt></p>
<blockquote>
<p>If <tt class="docutils literal"><span class="pre">True</span></tt> (the default) then list values are possible. If <tt class="docutils literal"><span class="pre">False</span></tt>, the
values are not parsed for lists.</p>
<blockquote>
<p>If <tt class="docutils literal"><span class="pre">list_values</span> <span class="pre">=</span> <span class="pre">False</span></tt> then single line values are not quoted or
unquoted when reading and writing.</p>
</blockquote>
<p>Changing this value affects whether single line values will be quoted or
not when writing.</p>
</blockquote>
</li>
<li><p class="first">'create_empty': <tt class="docutils literal"><span class="pre">False</span></tt></p>
<blockquote>
<p>If this value is <tt class="docutils literal"><span class="pre">True</span></tt> and the file specified by <tt class="docutils literal"><span class="pre">infile</span></tt> doesn't
exist, ConfigObj will create an empty file. This can be a useful test that
the filename makes sense: an impossible filename will cause an error.</p>
<p>Altering this value after initial parsing has no effect.</p>
</blockquote>
</li>
<li><p class="first">'file_error': <tt class="docutils literal"><span class="pre">False</span></tt></p>
<blockquote>
<p>If this value is <tt class="docutils literal"><span class="pre">True</span></tt> and the file specified by <tt class="docutils literal"><span class="pre">infile</span></tt> doesn't
exist, ConfigObj will raise an <tt class="docutils literal"><span class="pre">IOError</span></tt>.</p>
<p>Altering this value after initial parsing has no effect.</p>
</blockquote>
</li>
<li><p class="first">'interpolation': <tt class="docutils literal"><span class="pre">True</span></tt></p>
<blockquote>
<p>Whether string interpolation is switched on or not. It is on (<tt class="docutils literal"><span class="pre">True</span></tt>) by
default.</p>
<p>You can set this attribute to change whether string interpolation is done
when values are fetched. See the <a class="reference" href="#interpolation">interpolation</a> section for more details.</p>
</blockquote>
</li>
<li><p class="first">'configspec': <tt class="docutils literal"><span class="pre">None</span></tt></p>
<blockquote>
<p>If you want to use the validation system, you supply a configspec. This is
effectively a type of config file that specifies a check for each member.
This check can be used to do type conversion as well as check that the
value is within your required parameters.</p>
<p>You provide a configspec in the same way as you do the initial file: a
filename, or list of lines, etc. See the <a class="reference" href="#validation">validation</a> section for full
details on how to use the system.</p>
<p>When parsed, every section has a <tt class="docutils literal"><span class="pre">configspec</span></tt> with a dictionary of
configspec checks for <em>that section</em>.</p>
</blockquote>
</li>
<li><p class="first">'stringify': <tt class="docutils literal"><span class="pre">True</span></tt></p>
<blockquote>
<p>If you use the validation scheme, it can do type checking <em>and</em> conversion
for you. This means you may want to set members to integers, or other
non-string values.</p>
<p>If 'stringify' is set to <tt class="docutils literal"><span class="pre">True</span></tt> (default) then non-string values will
be converted to strings when you write the config file. The <a class="reference" href="#validation">validation</a>
process converts values from strings to the required type.</p>
<p>If 'stringify' is set to <tt class="docutils literal"><span class="pre">False</span></tt>, attempting to set a member to a
non-string value <a class="footnote-reference" href="#id25" id="id9" name="id9">[8]</a> will raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> (no type conversion is
done by validation).</p>
</blockquote>
</li>
<li><p class="first">'indent_type': <tt class="docutils literal"><span class="pre">'</span> <span class="pre">'</span></tt></p>
<blockquote>
<p>Indentation is not significant; it can however be present in the output
config. Allowable values are: <tt class="docutils literal"><span class="pre">''</span></tt> (no indentation), <tt class="docutils literal"><span class="pre">'</span> <span class="pre">'</span></tt> (indentation
with spaces, fixed at four per level), or <tt class="docutils literal"><span class="pre">'\t'</span></tt> (indentation with tabs,
one tab per level).</p>
<p>If this option is not specified, and the ConfigObj is initialised with a
dictionary, the indentation used in the output is the default one, that is,
spaces.</p>
<p>If this option is not specified, and the ConfigObj is initialised with a
list of lines or a file, the indentation used in the first indented line is
selected and used in all output lines. If no input line is indented, no
output line will be either.</p>
<p>If this option <em>is</em> specified, the option value is used in the output
config, overriding the type of indentation in the input config (if any).</p>
</blockquote>
</li>
<li><p class="first">'encoding': <tt class="docutils literal"><span class="pre">None</span></tt></p>
<blockquote>
<p>By default <strong>ConfigObj</strong> does not decode the file/strings you pass it into
Unicode <a class="footnote-reference" href="#id26" id="id10" name="id10">[9]</a>. If you want your config file as Unicode (keys and members)
you need to provide an encoding to decode the file with. This encoding will
also be used to encode the config file when writing.</p>
<p>You can change the encoding attribute at any time.</p>
<p>Any characters in your strings that can't be encoded with the specified
encoding will raise a <tt class="docutils literal"><span class="pre">UnicodeEncodeError</span></tt>.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p><tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded files will automatically be detected and decoded,
even if <tt class="docutils literal"><span class="pre">encoding</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p class="last">This is because it is a 16-bit encoding, and ConfigObj will mangle it
(split characters on byte boundaries) if it parses it without decoding.</p>
</div>
</blockquote>
</li>
<li><p class="first">'default_encoding': <tt class="docutils literal"><span class="pre">None</span></tt></p>
<blockquote>
<p>When using the <tt class="docutils literal"><span class="pre">write</span></tt> method, <strong>ConfigObj</strong> uses the <tt class="docutils literal"><span class="pre">encoding</span></tt>
attribute to encode the Unicode strings. If any members (or keys) have
been set as byte strings instead of Unicode, these must first be decoded
to Unicode before outputting in the specified encoding.</p>
<p><tt class="docutils literal"><span class="pre">default_encoding</span></tt>, if specified, is the encoding used to decode byte
strings in the <strong>ConfigObj</strong> before writing. If this is <tt class="docutils literal"><span class="pre">None</span></tt>, then
the Python default encoding (<tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> - usually ASCII) is
used.</p>
<p>For most Western European users, a value of <tt class="docutils literal"><span class="pre">latin-1</span></tt> is sensible.</p>
<p><tt class="docutils literal"><span class="pre">default_encoding</span></tt> is <em>only</em> used if an <tt class="docutils literal"><span class="pre">encoding</span></tt> is specified.</p>
<p>Any characters in byte-strings that can't be decoded using the
<tt class="docutils literal"><span class="pre">default_encoding</span></tt> will raise a <tt class="docutils literal"><span class="pre">UnicodeDecodeError</span></tt>.</p>
</blockquote>
</li>
<li><p class="first">'unrepr': <tt class="docutils literal"><span class="pre">False</span></tt></p>
<blockquote>
<p>The <tt class="docutils literal"><span class="pre">unrepr</span></tt> option reads and writes files in a different mode. This
allows you to store and retrieve the basic Python data-types using config
files.</p>
<p>This uses Python syntax for lists and quoting. See <a class="reference" href="#unrepr-mode">unrepr mode</a> for the
full details.</p>
</blockquote>
</li>
<li><p class="first">'write_empty_values': <tt class="docutils literal"><span class="pre">False</span></tt></p>
<blockquote>
<p>If <tt class="docutils literal"><span class="pre">write_empty_values</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, empty strings are written as
empty values. See <a class="reference" href="#empty-values">Empty Values</a> for more details.</p>
</blockquote>
</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id45" id="methods" name="methods">4.3 Methods</a></h2>
<p>The ConfigObj is a subclass of an object called <tt class="docutils literal"><span class="pre">Section</span></tt>, which is itself a
subclass of <tt class="docutils literal"><span class="pre">dict</span></tt>, the builtin dictionary type. This means it also has
<strong>all</strong> the normal dictionary methods.</p>
<p>In addition, the following <a class="reference" href="#section-methods">Section Methods</a> may be useful :</p>
<ul class="simple">
<li><em>encode</em></li>
<li><em>decode</em></li>
<li><em>walk</em></li>
<li><em>merge</em></li>
<li><em>dict</em></li>
<li><em>as_bool</em></li>
<li><em>as_float</em></li>
<li><em>as_int</em></li>
</ul>
<p>Read about <a class="reference" href="#sections">Sections</a> for details of all the methods.</p>
<div class="hint">
<p class="first admonition-title">Hint</p>
<p>The <em>merge</em> method of sections is a recursive update.</p>
<p>You can use this to merge sections, or even whole ConfigObjs, into each
other.</p>
<p class="last">You would typically use this to create a default ConfigObj and then merge
in user settings. This way users only need to specify values that are
different from the default.</p>
</div>
<p>The public methods available on ConfigObj are :</p>
<ul class="simple">
<li>'write'</li>
<li>'validate'</li>
</ul>
<div class="section">
<h3><a class="toc-backref" href="#id46" id="write" name="write">4.3.1 write</a></h3>
<pre class="literal-block">
write(file_object=None)
</pre>
<p>This method writes the current ConfigObj and takes a single, optional argument
<a class="footnote-reference" href="#id27" id="id11" name="id11">[10]</a>.</p>
<p>If you pass in a file like object to the <tt class="docutils literal"><span class="pre">write</span></tt> method, the config file will
be written to this. (The only method of this object that is used is its
<tt class="docutils literal"><span class="pre">write</span></tt> method, so a <tt class="docutils literal"><span class="pre">StringIO</span></tt> instance, or any other file like object
will work.)</p>
<p>Otherwise, the behaviour of this method depends on the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute
of the ConfigObj.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">filename</span></tt></dt>
<dd>ConfigObj will write the configuration to the file specified.</dd>
<dt><tt class="docutils literal"><span class="pre">None</span></tt></dt>
<dd><tt class="docutils literal"><span class="pre">write</span></tt> returns a list of lines. (Not <tt class="docutils literal"><span class="pre">'\n'</span></tt> terminated)</dd>
</dl>
<p>First the 'initial_comment' is written, then the config file, followed by the
'final_comment'. Comment lines and inline comments are written with each
key/value.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id47" id="validate" name="validate">4.3.2 validate</a></h3>
<pre class="literal-block">
validate(validator, preserve_errors=False, copy=False)
</pre>
<div class="pysrc"><span class="pycomment"># filename is the config file<br />
</span><span class="pycomment"># filename2 is the configspec<br />
</span><span class="pycomment"># (which could also be hardcoded into your program)<br />
</span><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">filename2</span><span class="pyoperator">)</span><br />
<span class="pycomment">#<br />
</span><span class="pykeyword">from</span> <span class="pytext">validate</span> <span class="pykeyword">import</span> <span class="pytext">Validator</span><br />
<span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
<span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pystring">'Succeeded.'</span><span class="pytext"></span></div><p>The validate method uses the <a class="reference" href="http://www.voidspace.org.uk/python/validate.html">validate</a> module to do the
validation.</p>
<p>This method validates the ConfigObj against the configspec. By doing type
conversion as well, it can abstract away the config file altogether and present
the config <em>data</em> to your application (in the types it expects it to be).</p>
<p>If the <tt class="docutils literal"><span class="pre">configspec</span></tt> attribute of the ConfigObj is <tt class="docutils literal"><span class="pre">None</span></tt>, it raises a
<tt class="docutils literal"><span class="pre">ValueError</span></tt>.</p>
<p>If the <a class="reference" href="#stringify">stringify</a> attribute is set, this process will convert values to the
type defined in the configspec.</p>
<p>The validate method uses checks specified in the configspec and defined in the
<tt class="docutils literal"><span class="pre">Validator</span></tt> object. It is very easy to extend.</p>
<p>The configspec looks like the config file, but instead of the value, you
specify the check (and any default value). See the <a class="reference" href="#validation">validation</a> section for
details.</p>
<div class="hint">
<p class="first admonition-title">Hint</p>
<p>The system of configspecs can seem confusing at first, but is actually
quite simple and powerful. For a concrete example of how to use it, you may
find this blog entry helpful :
<a class="reference" href="http://www.voidspace.org.uk/python/weblog/arch_d7_2006_03_04.shtml#e257">Transforming Values with ConfigObj</a>.</p>
<p class="last">There is also a module to assist in auto-generating configspecs called
<a class="reference" href="http://www.voidspace.org.uk/python/configpersist.html">ConfigPersist.py</a>. Its use is explained in <a class="reference" href="http://www.voidspace.org.uk/python/articles/configobj_for_data_persistence.shtml">ConfigObj for Data Persistence</a>.</p>
</div>
<p>The <tt class="docutils literal"><span class="pre">copy</span></tt> parameter fills in missing values from the configspec (default
values), <em>without</em> marking the values as defaults. It also causes comments to
be copied from the configspec into the config file. This allows you to use a
configspec to create default config files. (Normally default values aren't
written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.)</p>
<p>As of ConfigObj 4.3.0 you can also pass in a ConfigObj instance as your
configspec. This is especially useful if you need to specify the encoding of
your configspec file. When you read your configspec file, you <em>must</em> specify
<tt class="docutils literal"><span class="pre">list_values=False</span></tt>.</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">configspec</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspecfilename</span><span class="pyoperator">,</span> <span class="pytext">encoding</span><span class="pyoperator">=</span><span class="pystring">'UTF8'</span><span class="pyoperator">,</span><br />
<span class="pytext">list_values</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><span class="pytext"></span></div><div class="section">
<h4><a class="toc-backref" href="#id48" id="return-value" name="return-value">4.3.2.1 Return Value</a></h4>
<p>By default, the validate method either returns <tt class="docutils literal"><span class="pre">True</span></tt> (everything passed)
or a dictionary of <tt class="docutils literal"><span class="pre">True</span></tt>/<tt class="docutils literal"><span class="pre">False</span></tt> representing pass/fail. The dictionary
follows the structure of the ConfigObj.</p>
<p>If a whole section passes then it is replaced with the value <tt class="docutils literal"><span class="pre">True</span></tt>. If a
whole section fails, then it is replaced with the value <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>If a value is missing, and there is no default in the check, then the check
automatically fails.</p>
<p>The <tt class="docutils literal"><span class="pre">validate</span></tt> method takes an optional keyword argument <tt class="docutils literal"><span class="pre">preserve_errors</span></tt>.
If you set this to <tt class="docutils literal"><span class="pre">True</span></tt>, instead of getting <tt class="docutils literal"><span class="pre">False</span></tt> for failed checks you
get the actual error object from the <strong>validate</strong> module. This usually contains
useful information about why the check failed.</p>
<p>See the <a class="reference" href="#flatten-errors">flatten_errors</a> function for how to turn your results dictionary into
a useful list of error messages.</p>
<p>Even if <tt class="docutils literal"><span class="pre">preserve_errors</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, missing keys or sections will still be
represented by a <tt class="docutils literal"><span class="pre">False</span></tt> in the results dictionary.</p>
</div>
<div class="section">
<h4><a class="toc-backref" href="#id49" id="mentioning-default-values" name="mentioning-default-values">4.3.2.2 Mentioning Default Values</a></h4>
<p>In the check in your configspec, you can specify a default to be used - by
using the <tt class="docutils literal"><span class="pre">default</span></tt> keyword. E.g.</p>
<pre class="literal-block">
key1 = integer(0, 30, default=15)
key2 = integer(default=15)
key3 = boolean(default=True)
key4 = option('Hello', 'Goodbye', 'Not Today', default='Not Today')
</pre>
<p>If the configspec check supplies a default and the value is missing in the
config, then the default will be set in your ConfigObj. (It is still passed to
the <tt class="docutils literal"><span class="pre">Validator</span></tt> so that type conversion can be done: this means the default
value must still pass the check.)</p>
<p>ConfigObj keeps a record of which values come from defaults, using the
<tt class="docutils literal"><span class="pre">defaults</span></tt> attribute of <a class="reference" href="#sections">sections</a>. Any key in this list isn't written out by
the <tt class="docutils literal"><span class="pre">write</span></tt> method. If a key is set from outside (even to the same value)
then it is removed from the <tt class="docutils literal"><span class="pre">defaults</span></tt> list.</p>
<!-- note:
Even if all the keys in a section are in the defaults list, the section
marker is still written out. -->
<p>There is additionally a special case default value of <tt class="docutils literal"><span class="pre">None</span></tt>. If you set the
default value to <tt class="docutils literal"><span class="pre">None</span></tt> and the value is missing, the value will always be
set to <tt class="docutils literal"><span class="pre">None</span></tt>. As the other checks don't return <tt class="docutils literal"><span class="pre">None</span></tt> (unless you
implement your own that do), you can tell that this value came from a default
value (and was missing from the config file). It allows an easy way of
implementing optional values. Simply check (and ignore) members that are set
to <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">If <a class="reference" href="#stringify">stringify</a> is <tt class="docutils literal"><span class="pre">False</span></tt> then <tt class="docutils literal"><span class="pre">default=None</span></tt> returns <tt class="docutils literal"><span class="pre">''</span></tt> instead of
<tt class="docutils literal"><span class="pre">None</span></tt>. This is because setting a value to a non-string raises an error
if stringify is unset.</p>
</div>
<p>The default value can be a list. See <a class="reference" href="#id16">List Values</a> for the way to do this.</p>
<p>Writing invalid default values is a <em>guaranteed</em> way of confusing your users.
Default values <strong>must</strong> pass the check.</p>
</div>
<div class="section">
<h4><a class="toc-backref" href="#id50" id="mentioning-repeated-sections" name="mentioning-repeated-sections">4.3.2.3 Mentioning Repeated Sections</a></h4>
<p>In the configspec it is possible to cause <em>every</em> sub-section in a section to
be validated using the same configspec. You do this with a section in the
configspec called <tt class="docutils literal"><span class="pre">__many__</span></tt>. Every sub-section in that section has the
<tt class="docutils literal"><span class="pre">__many__</span></tt> configspec applied to it (without you having to explicitly name
them in advance).</p>
<p>If you define a <tt class="docutils literal"><span class="pre">__many__</span></tt> type section it must the only sub-section in that
section. Having a <tt class="docutils literal"><span class="pre">__many__</span></tt> <em>and</em> other sub-sections defined in the same
section will raise a <tt class="docutils literal"><span class="pre">RepeatSectionError</span></tt>.</p>
<p>Your <tt class="docutils literal"><span class="pre">__many__</span></tt> section can have nested subsections, which can also include
<tt class="docutils literal"><span class="pre">__many__</span></tt> type sections.</p>
<p>See <a class="reference" href="#repeated-sections">Repeated Sections</a> for examples.</p>
</div>
<div class="section">
<h4><a class="toc-backref" href="#id51" id="mentioning-simpleval" name="mentioning-simpleval">4.3.2.4 Mentioning SimpleVal</a></h4>
<p>If you just want to check if all members are present, then you can use the
<tt class="docutils literal"><span class="pre">SimpleVal</span></tt> object that comes with ConfigObj. It only fails members if they
are missing.</p>
<p>Write a configspec that has all the members you want to check for, but set
every section to <tt class="docutils literal"><span class="pre">''</span></tt>.</p>
<div class="pysrc"><span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">SimpleVal</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
<span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pykeyword">is</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pystring">'Succeeded.'</span><span class="pytext"></span></div></div>
<div class="section">
<h4><a class="toc-backref" href="#id52" id="mentioning-copy-mode" name="mentioning-copy-mode">4.3.2.5 Mentioning copy Mode</a></h4>
<p>As discussed in <a class="reference" href="#mentioning-default-values">Mentioning Default Values</a>, you can use a configspec to
supply default values. These are marked in the ConfigObj instance as defaults,
and <em>not</em> written out by the <tt class="docutils literal"><span class="pre">write</span></tt> mode. This means that your users only
need to supply values that are different from the defaults.</p>
<p>This can be inconvenient if you <em>do</em> want to write out the default values,
for example to write out a default config file.</p>
<p>If you set <tt class="docutils literal"><span class="pre">copy=True</span></tt> when you call validate, then no values are marked as
defaults. In addition, all comments from the configspec are copied into
your ConfigObj instance. You can then call <tt class="docutils literal"><span class="pre">write</span></tt> to create your config
file.</p>
<p>There is a limitation with this. In order to allow <a class="reference" href="#interpolation">Interpolation</a> to work
within configspecs, <tt class="docutils literal"><span class="pre">DEFAULT</span></tt> sections are not processed by
validation; even in copy mode.</p>
</div>
</div>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id53" id="attributes" name="attributes">4.4 Attributes</a></h2>
<p>A ConfigObj has the following attributes :</p>
<ul class="simple">
<li>indent_type</li>
<li>interpolate</li>
<li>stringify</li>
<li>BOM</li>
<li>initial_comment</li>
<li>final_comment</li>
<li>list_values</li>
<li>encoding</li>
<li>default_encoding</li>
<li>unrepr</li>
<li>write_empty_values</li>
</ul>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">This doesn't include <em>comments</em>, <em>inline_comments</em>, <em>defaults</em>, or
<em>configspec</em>. These are actually attributes of <a class="reference" href="#sections">Sections</a>.</p>
</div>
<p>It also has the following attributes as a result of parsing. They correspond to
<a class="reference" href="#options">options</a> when the ConfigObj was created, but changing them has no effect.</p>
<ul class="simple">
<li>raise_errors</li>
<li>create_empty</li>
<li>file_error</li>
</ul>
<div class="section">
<h3><a class="toc-backref" href="#id54" id="interpolate" name="interpolate">4.4.1 interpolate</a></h3>
<p>ConfigObj can perform string interpolation in a <em>similar</em> way to
<tt class="docutils literal"><span class="pre">ConfigParser</span></tt>. See the <a class="reference" href="#interpolation">interpolation</a> section for full details.</p>
<p>If <tt class="docutils literal"><span class="pre">interpolate</span></tt> is set to <tt class="docutils literal"><span class="pre">False</span></tt>, then interpolation is <em>not</em> done when
you fetch values.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id55" id="stringify" name="stringify">4.4.2 stringify</a></h3>
<p>If this attribute is set (<tt class="docutils literal"><span class="pre">True</span></tt>) then the <a class="reference" href="#validate">validate</a> method changes the
values in the ConfigObj. These are turned back into strings when <a class="reference" href="#write">write</a> is
called.</p>
<p>If stringify is unset (<tt class="docutils literal"><span class="pre">False</span></tt>) then attempting to set a value to a non
string (or a list of strings) will raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt>.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id56" id="bom" name="bom">4.4.3 BOM</a></h3>
<p>If the initial config file <em>started</em> with the UTF8 Unicode signature (known
slightly incorrectly as the <acronym title="Byte Order Mark">BOM</acronym>), or the UTF16 BOM, then
this attribute is set to <tt class="docutils literal"><span class="pre">True</span></tt>. Otherwise it is <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>If it is set to <tt class="docutils literal"><span class="pre">True</span></tt> when <tt class="docutils literal"><span class="pre">write</span></tt> is called then, if <tt class="docutils literal"><span class="pre">encoding</span></tt> is set
to <tt class="docutils literal"><span class="pre">None</span></tt> <em>or</em> to <tt class="docutils literal"><span class="pre">utf_8</span></tt> (and variants) a UTF BOM will be written.</p>
<p>For UTF16 encodings, a BOM is <em>always</em> written.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id57" id="initial-comment" name="initial-comment">4.4.4 initial_comment</a></h3>
<p>This is a list of lines. If the ConfigObj is created from an existing file, it
will contain any lines of comments before the start of the members.</p>
<p>If you create a new ConfigObj, this will be an empty list.</p>
<p>The write method puts these lines before it starts writing out the members.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id58" id="final-comment" name="final-comment">4.4.5 final_comment</a></h3>
<p>This is a list of lines. If the ConfigObj is created from an existing file, it
will contain any lines of comments after the last member.</p>
<p>If you create a new ConfigObj, this will be an empty list.</p>
<p>The <tt class="docutils literal"><span class="pre">write</span></tt> method puts these lines after it finishes writing out the
members.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id59" id="list-values" name="list-values">4.4.6 list_values</a></h3>
<p>This attribute is <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>. If set to <tt class="docutils literal"><span class="pre">False</span></tt> then values are
not parsed for list values. In addition single line values are not unquoted.</p>
<p>This allows you to do your own parsing of values. It exists primarily to
support the reading of the <a class="reference" href="#configspec">configspec</a> - but has other use cases.</p>
<p>For example you could use the <tt class="docutils literal"><span class="pre">LineParser</span></tt> from the
<a class="reference" href="http://www.voidspace.org.uk/python/listquote.html#lineparser">listquote module</a>
to read values for nested lists.</p>
<p>Single line values aren't quoted when writing - but multiline values are
handled as normal.</p>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<dl class="docutils">
<dt>Because values aren't quoted, leading or trailing whitespace can be</dt>
<dd>lost.</dd>
</dl>
<p>This behaviour was changed in version 4.0.1.</p>
<blockquote class="last">
Prior to this, single line values might have been quoted; even with
<tt class="docutils literal"><span class="pre">list_values=False</span></tt>. This means that files written by <strong>ConfigObj</strong>
<em>could</em> now be incompatible - and need the quotes removing by hand.</blockquote>
</div>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id60" id="encoding" name="encoding">4.4.7 encoding</a></h3>
<p>This is the encoding used to encode the output, when you call <tt class="docutils literal"><span class="pre">write</span></tt>. It
must be a valid encoding <a class="reference" href="http://docs.python.org/lib/standard-encodings.html">recognised by Python</a>.</p>
<p>If this value is <tt class="docutils literal"><span class="pre">None</span></tt> then no encoding is done when <tt class="docutils literal"><span class="pre">write</span></tt> is called.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id61" id="default-encoding" name="default-encoding">4.4.8 default_encoding</a></h3>
<p>If encoding is set, any byte-strings in your ConfigObj instance (keys or
members) will first be decoded to Unicode using the encoding specified by the
<tt class="docutils literal"><span class="pre">default_encoding</span></tt> attribute. This ensures that the output is in the encoding
specified.</p>
<p>If this value is <tt class="docutils literal"><span class="pre">None</span></tt> then <tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> is used instead.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id62" id="unrepr" name="unrepr">4.4.9 unrepr</a></h3>
<p>Another boolean value. If this is set, then <tt class="docutils literal"><span class="pre">repr(value)</span></tt> is used to write
values. This writes values in a slightly different way to the normal ConfigObj
file syntax.</p>
<p>This preserves basic Python data-types when read back in. See <a class="reference" href="#unrepr-mode">unrepr mode</a>
for more details.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id63" id="write-empty-values" name="write-empty-values">4.4.10 write_empty_values</a></h3>
<p>Also boolean. If set, values that are an empty string (<tt class="docutils literal"><span class="pre">''</span></tt>) are written as
empty values. See <a class="reference" href="#empty-values">Empty Values</a> for more details.</p>
</div>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id64" id="the-config-file-format" name="the-config-file-format">5 The Config File Format</a></h1>
<p>You saw an example config file in the <a class="reference" href="#config-files">Config Files</a> section. Here is a fuller
specification of the config files used and created by ConfigObj.</p>
<p>The basic pattern for keywords is :</p>
<pre class="literal-block">
# comment line
# comment line
keyword = value # inline comment
</pre>
<p>Both keyword and value can optionally be surrounded in quotes. The equals sign
is the only valid divider.</p>
<p>Values can have comments on the lines above them, and an inline comment after
them. This, of course, is optional. See the <a class="reference" href="#comments">comments</a> section for details.</p>
<p>If a keyword or value starts or ends with whitespace, or contains a quote mark
or comma, then it should be surrounded by quotes. Quotes are not necessary if
whitespace is surrounded by non-whitespace.</p>
<p>Values can also be lists. Lists are comma separated. You indicate a single
member list by a trailing comma. An empty list is shown by a single comma :</p>
<pre class="literal-block">
keyword1 = value1, value2, value3
keyword2 = value1, # a single member list
keyword3 = , # an empty list
</pre>
<p>Values that contain line breaks (multi-line values) can be surrounded by triple
quotes. These can also be used if a value contains both types of quotes. List
members cannot be surrounded by triple quotes :</p>
<pre class="literal-block">
keyword1 = ''' A multi line value
on several
lines''' # with a comment
keyword2 = '''I won't be "afraid".'''
#
keyword3 = """ A multi line value
on several
lines""" # with a comment
keyword4 = """I won't be "afraid"."""
</pre>
<div class="warning">
<p class="first admonition-title">Warning</p>
<p class="last">There is no way of safely quoting values that contain both types of triple
quotes.</p>
</div>
<p>A line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
<p>New sections are indicated by a section marker line. That is the section name
in square brackets. Whitespace around the section name is ignored. The name can
be quoted with single or double quotes. The marker can have comments before it
and an inline comment after it :</p>
<pre class="literal-block">
# The First Section
[ section name 1 ] # first section
keyword1 = value1
# The Second Section
[ "section name 2" ] # second section
keyword2 = value2
</pre>
<p>Any subsections (sections that are <em>inside</em> the current section) are
designated by repeating the square brackets before and after the section name.
The number of square brackets represents the nesting level of the sub-section.
Square brackets may be separated by whitespace; such whitespace, however, will
not be present in the output config written by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
<p>Indentation is not significant, but can be preserved. See the description of
the <tt class="docutils literal"><span class="pre">indent_type</span></tt> option, in the <a class="reference" href="#configobj-specifications">ConfigObj specifications</a> chapter, for the
details.</p>
<p>A <em>NestingError</em> will be raised if the number of the opening and the closing
brackets in a section marker is not the same, or if a sub-section's nesting
level is greater than the nesting level of it parent plus one.</p>
<p>In the outer section, single values can only appear before any sub-section.
Otherwise they will belong to the sub-section immediately before them.</p>
<pre class="literal-block">
# initial comment
keyword1 = value1
keyword2 = value2
[section 1]
keyword1 = value1
keyword2 = value2
[[sub-section]]
# this is in section 1
keyword1 = value1
keyword2 = value2
[[[nested section]]]
# this is in sub section
keyword1 = value1
keyword2 = value2
[[sub-section2]]
# this is in section 1 again
keyword1 = value1
keyword2 = value2
[[sub-section3]]
# this is also in section 1, indentation is misleading here
keyword1 = value1
keyword2 = value2
# final comment
</pre>
<p>When parsed, the above config file produces the following data structure :</p>
<div class="pysrc"><span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pystring">'section 1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pystring">'sub-section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pystring">'nested section'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pystring">'sub-section2'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pystring">'sub-section3'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><br />
<span class="pystring">'keyword1'</span><span class="pyoperator">:</span> <span class="pystring">'value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'keyword2'</span><span class="pyoperator">:</span> <span class="pystring">'value2'</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pyoperator">}</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>Sections are ordered: note how the structure of the resulting ConfigObj is in
the same order as the original file.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p>In ConfigObj 4.3.0 <em>empty values</em> became valid syntax. They are read as the
empty string. There is also an option/attribute (<tt class="docutils literal"><span class="pre">write_empty_values</span></tt>) to
allow the writing of these.</p>
<p>This is mainly to support 'legacy' config files, written from other
applications. This is documented under <a class="reference" href="#empty-values">Empty Values</a>.</p>
<p class="last"><a class="reference" href="#unrepr-mode">unrepr mode</a> introduces <em>another</em> syntax variation, used for storing
basic Python datatypes in config files. <img src="smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id65" id="sections" name="sections">6 Sections</a></h1>
<p>Every section in a ConfigObj has certain properties. The ConfigObj itself also
has these properties, because it too is a section (sometimes called the <em>root
section</em>).</p>
<p><tt class="docutils literal"><span class="pre">Section</span></tt> is a subclass of the standard new-class dictionary, therefore it
has <strong>all</strong> the methods of a normal dictionary. This means you can <tt class="docutils literal"><span class="pre">update</span></tt>
and <tt class="docutils literal"><span class="pre">clear</span></tt> sections.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p>You create a new section by assigning a member to be a dictionary.</p>
<p>The new <tt class="docutils literal"><span class="pre">Section</span></tt> is created <em>from</em> the dictionary, but isn't the same
thing as the dictionary. (So references to the dictionary you use to create
the section <em>aren't</em> references to the new section).</p>
<p>Note the following.</p>
<div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">vals</span> <span class="pyoperator">=</span> <span class="pyoperator">{</span><span class="pystring">'key1'</span><span class="pyoperator">:</span> <span class="pystring">'value 1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'key2'</span><span class="pyoperator">:</span> <span class="pystring">'value 2'</span><br />
<span class="pyoperator">}</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">vals</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pyoperator">==</span> <span class="pytext">vals</span><br />
<span class="pytext">True</span><br />
<span class="pytext">config</span><span class="pyoperator">[</span><span class="pystring">'vals'</span><span class="pyoperator">]</span> <span class="pykeyword">is</span> <span class="pytext">vals</span><br />
<span class="pytext">False</span><span class="pytext"></span></div><p class="last">If you now change <tt class="docutils literal"><span class="pre">vals</span></tt>, the changes won't be reflected in <tt class="docutils literal"><span class="pre">config['vals']</span></tt>.</p>
</div>
<p>A section is ordered, following its <tt class="docutils literal"><span class="pre">scalars</span></tt> and <tt class="docutils literal"><span class="pre">sections</span></tt>
attributes documented below. This means that the following dictionary
attributes return their results in order.</p>
<ul>
<li><p class="first">'__iter__'</p>
<blockquote>
<p>More commonly known as <tt class="docutils literal"><span class="pre">for</span> <span class="pre">member</span> <span class="pre">in</span> <span class="pre">section:</span></tt>.</p>
</blockquote>
</li>
<li><p class="first">'__repr__' and '__str__'</p>
<blockquote>
<p>Any time you print or display the ConfigObj.</p>
</blockquote>
</li>
<li><p class="first">'items'</p>
</li>
<li><p class="first">'iteritems'</p>
</li>
<li><p class="first">'iterkeys'</p>
</li>
<li><p class="first">'itervalues'</p>
</li>
<li><p class="first">'keys'</p>
</li>
<li><p class="first">'popitem'</p>
</li>
<li><p class="first">'values'</p>
</li>
</ul>
<div class="section">
<h2><a class="toc-backref" href="#id66" id="section-attributes" name="section-attributes">6.1 Section Attributes</a></h2>
<ul>
<li><p class="first">main</p>
<blockquote>
<p>A reference to the main ConfigObj.</p>
</blockquote>
</li>
<li><p class="first">parent</p>
<blockquote>
<p>A reference to the 'parent' section, the section that this section is a
member of.</p>
<p>On the ConfigObj this attribute is a reference to itself. You can use this
to walk up the sections, stopping when <tt class="docutils literal"><span class="pre">section.parent</span> <span class="pre">is</span> <span class="pre">section</span></tt>.</p>
</blockquote>
</li>
<li><p class="first">depth</p>
<blockquote>
<p>The nesting level of the current section.</p>
<p>If you create a new ConfigObj and add sections, 1 will be added to the
depth level between sections.</p>
</blockquote>
</li>
<li><p class="first">defaults</p>
<blockquote>
<p>This attribute is a list of scalars that came from default values. Values
that came from defaults aren't written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.
Setting any of these values in the section removes them from the defaults
list.</p>
</blockquote>
</li>
<li><p class="first">scalars, sections</p>
<blockquote>
<p>These attributes are normal lists, representing the order that members,
single values and subsections appear in the section. The order will either
be the order of the original config file, <em>or</em> the order that you added
members.</p>
<p>The order of members in this lists is the order that <tt class="docutils literal"><span class="pre">write</span></tt> creates in
the config file. The <tt class="docutils literal"><span class="pre">scalars</span></tt> list is output before the <tt class="docutils literal"><span class="pre">sections</span></tt>
list.</p>
<p>Adding or removing members also alters these lists. You can manipulate the
lists directly to alter the order of members.</p>
<div class="warning">
<p class="first admonition-title">Warning</p>
<p class="last">If you alter the <tt class="docutils literal"><span class="pre">scalars</span></tt>, <tt class="docutils literal"><span class="pre">sections</span></tt>, or <tt class="docutils literal"><span class="pre">defaults</span></tt> attributes
so that they no longer reflect the contents of the section, you will
break your ConfigObj.</p>
</div>
<p>See also the <tt class="docutils literal"><span class="pre">rename</span></tt> method.</p>
</blockquote>
</li>
<li><p class="first">comments</p>
<blockquote>
<p>This is a dictionary of comments associated with each member. Each entry is
a list of lines. These lines are written out before the member.</p>
</blockquote>
</li>
<li><p class="first">inline_comments</p>
<blockquote>
<p>This is <em>another</em> dictionary of comments associated with each member. Each
entry is a string that is put inline with the member.</p>
</blockquote>
</li>
<li><p class="first">configspec</p>
<blockquote>
<p>The configspec attribute is a dictionary mapping scalars to <em>checks</em>. A
check defines the expected type and possibly the allowed values for a
member.</p>
<p>The configspec has the same format as a config file, but instead of values
it has a specification for the value (which may include a default value).
The <a class="reference" href="#validate">validate</a> method uses it to check the config file makes sense. If a
configspec is passed in when the ConfigObj is created, then it is parsed
and broken up to become the <tt class="docutils literal"><span class="pre">configspec</span></tt> attribute of each section.</p>
<p>If you didn't pass in a configspec, this attribute will be <tt class="docutils literal"><span class="pre">None</span></tt> on the
root section (the main ConfigObj).</p>
<p>You can set the configspec attribute directly on a section.</p>
<p>See the <a class="reference" href="#validation">validation</a> section for full details of how to write configspecs.</p>
</blockquote>
</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id67" id="section-methods" name="section-methods">6.2 Section Methods</a></h2>
<ul>
<li><p class="first"><strong>dict</strong></p>
<blockquote>
<p>This method takes no arguments. It returns a deep copy of the section as a
dictionary. All subsections will also be dictionaries, and list values will
be copies, rather than references to the original <a class="footnote-reference" href="#id28" id="id12" name="id12">[11]</a>.</p>
</blockquote>
</li>
<li><p class="first"><strong>rename</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">rename(oldkey,</span> <span class="pre">newkey)</span></tt></p>
<p>This method renames a key, without affecting its position in the sequence.</p>
<p>It is mainly implemented for the <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods, which
provide some Unicode support.</p>
</blockquote>
</li>
<li><p class="first"><strong>merge</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">merge(indict)</span></tt></p>
<p>This method is a <em>recursive update</em> method. It allows you to merge two
config files together.</p>
<p>You would typically use this to create a default ConfigObj and then merge
in user settings. This way users only need to specify values that are
different from the default.</p>
<p>For example :</p>
<div class="pysrc"><span class="pycomment"># def_cfg contains your default config settings<br />
</span><span class="pycomment"># user_cfg contains the user settings<br />
</span><span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">def_cfg</span><span class="pyoperator">)</span><br />
<span class="pytext">usr</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">user_cfg</span><span class="pyoperator">)</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">merge</span><span class="pyoperator">(</span><span class="pytext">usr</span><span class="pyoperator">)</span><br />
<br />
<span class="pystring">"""<br />
cfg now contains a combination of the default settings and the user<br />
settings.<br />
<br />
The user settings will have overwritten any of the default ones.<br />
"""</span><span class="pytext"></span></div></blockquote>
</li>
<li><p class="first"><strong>walk</strong></p>
<blockquote>
<p>This method can be used to transform values and names. See <a class="reference" href="#walking-a-section">walking a
section</a> for examples and explanation.</p>
</blockquote>
</li>
<li><p class="first"><strong>decode</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">decode(encoding)</span></tt></p>
<p>This method decodes names and values into Unicode objects, using the
supplied encoding.</p>
</blockquote>
</li>
<li><p class="first"><strong>encode</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">encode(encoding)</span></tt></p>
<p>This method is the opposite of <tt class="docutils literal"><span class="pre">decode</span></tt> <img src="smilies/exclaim.gif" alt="Exclamation" height="15" width="15" /> .</p>
<p>It encodes names and values using the supplied encoding. If any of your
names/values are strings rather than Unicode, Python will have to do an
implicit decode first. (This method uses <tt class="docutils literal"><span class="pre">sys.defaultencoding</span></tt> for
implicit decodes.)</p>
</blockquote>
</li>
<li><p class="first"><strong>as_bool</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">as_bool(key)</span></tt></p>
<p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the key contains a string that represents <tt class="docutils literal"><span class="pre">True</span></tt>, or
is the <tt class="docutils literal"><span class="pre">True</span></tt> object.</p>
<p>Returns <tt class="docutils literal"><span class="pre">False</span></tt> if the key contains a string that represents <tt class="docutils literal"><span class="pre">False</span></tt>,
or is the <tt class="docutils literal"><span class="pre">False</span></tt> object.</p>
<p>Raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the key contains anything else.</p>
<p>Strings that represent <tt class="docutils literal"><span class="pre">True</span></tt> are (not case sensitive) :</p>
<pre class="literal-block">
true, yes, on, 1
</pre>
<p>Strings that represent <tt class="docutils literal"><span class="pre">False</span></tt> are :</p>
<pre class="literal-block">
false, no, off, 0
</pre>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">In ConfigObj 4.1.0, this method was called <tt class="docutils literal"><span class="pre">istrue</span></tt>. That method is
now deprecated and will issue a warning when used. It will go away
in a future release.</p>
</div>
</blockquote>
</li>
<li><p class="first"><strong>as_int</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">as_int(key)</span></tt></p>
<p>This returns the value contained in the specified key as an integer.</p>
<p>It raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the conversion can't be done.</p>
</blockquote>
</li>
<li><p class="first"><strong>as_float</strong></p>
<blockquote>
<p><tt class="docutils literal"><span class="pre">as_float(key)</span></tt></p>
<p>This returns the value contained in the specified key as a float.</p>
<p>It raises a <tt class="docutils literal"><span class="pre">ValueError</span></tt> if the conversion can't be done.</p>
</blockquote>
</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id68" id="walking-a-section" name="walking-a-section">6.3 Walking a Section</a></h2>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">The walk method allows you to call a function on every member/name.</p>
</div>
<div class="pysrc"><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">function</span><span class="pyoperator">,</span> <span class="pytext">raise_errors</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">,</span><br />
<span class="pytext">call_on_sections</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">,</span> <span class="pyoperator">**</span><span class="pytext">keywargs</span><span class="pyoperator">)</span><span class="pyoperator">:</span><span class="pytext"></span></div><p><tt class="docutils literal"><span class="pre">walk</span></tt> is a method of the <tt class="docutils literal"><span class="pre">Section</span></tt> object. This means it is also a method
of ConfigObj.</p>
<p>It walks through every member and calls a function on the keyword and value. It
walks recursively through subsections.</p>
<p>It returns a dictionary of all the computed values.</p>
<p>If the function raises an exception, the default is to propagate the error, and
stop. If <tt class="docutils literal"><span class="pre">raise_errors=False</span></tt> then it sets the return value for that keyword
to <tt class="docutils literal"><span class="pre">False</span></tt> instead, and continues. This is similar to the way <a class="reference" href="#validation">validation</a>
works.</p>
<p>Your function receives the arguments <tt class="docutils literal"><span class="pre">(section,</span> <span class="pre">key)</span></tt>. The current value is
then <tt class="docutils literal"><span class="pre">section[key]</span></tt> <a class="footnote-reference" href="#id29" id="id13" name="id13">[12]</a>. Any unrecognised keyword arguments you pass to
walk, are passed on to the function.</p>
<p>Normally <tt class="docutils literal"><span class="pre">walk</span></tt> just recurses into subsections. If you are transforming (or
checking) names as well as values, then you want to be able to change the names
of sections. In this case set <tt class="docutils literal"><span class="pre">call_on_sections</span></tt> to <tt class="docutils literal"><span class="pre">True</span></tt>. Now, on
encountering a sub-section, <em>first</em> the function is called for the <em>whole</em>
sub-section, and <em>then</em> it recurses into it's members. This means your function
must be able to handle receiving dictionaries as well as strings and lists.</p>
<p>If you are using the return value from <tt class="docutils literal"><span class="pre">walk</span></tt> <em>and</em> <tt class="docutils literal"><span class="pre">call_on_sections</span></tt>,
note that walk discards the return value when it calls your function.</p>
<div class="caution">
<p class="first admonition-title">Caution!</p>
<p class="last">You can use <tt class="docutils literal"><span class="pre">walk</span></tt> to transform the names of members of a section
but you mustn't add or delete members.</p>
</div>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id69" id="examples" name="examples">6.4 Examples</a></h2>
<p>Examples that use the walk method are the <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods.
They both define a function and pass it to walk. Because these functions
transform names as well as values (from byte strings to Unicode) they set
<tt class="docutils literal"><span class="pre">call_on_sections=True</span></tt>.</p>
<p>To see how they do it, <em>read the source Luke</em> <img src="smilies/cool.gif" alt="Cool" height="15" width="15" /> .</p>
<p>You can use this for transforming all values in your ConfigObj. For example
you might like the nested lists from ConfigObj 3. This was provided by the
<a class="reference" href="http://www.voidspace.org.uk/python/modules.shtml#listquote">listquote</a> module. You could switch off the parsing for list values
(<tt class="docutils literal"><span class="pre">list_values=False</span></tt>) and use listquote to parse every value.</p>
<p>Another thing you might want to do is use the Python escape codes in your
values. You might be <em>used</em> to using <tt class="docutils literal"><span class="pre">\n</span></tt> for line feed and <tt class="docutils literal"><span class="pre">\t</span></tt> for tab.
Obviously we'd need to decode strings that come from the config file (using the
escape codes). Before writing out we'll need to put the escape codes back in
encode.</p>
<p>As an example we'll write a function to use with walk, that encodes or decodes
values using the <tt class="docutils literal"><span class="pre">string-escape</span></tt> codec.</p>
<p>The function has to take each value and set the new value. As a bonus we'll
create one function that will do decode <em>or</em> encode depending on a keyword
argument.</p>
<p>We don't want to work with section names, we're only transforming values, so
we can leave <tt class="docutils literal"><span class="pre">call_on_sections</span></tt> as <tt class="docutils literal"><span class="pre">False</span></tt>. This means the two datatypes we
have to handle are strings and lists, we can ignore everything else. (We'll
treat tuples as lists as well).</p>
<p>We're not using the return values, so it doesn't need to return anything, just
change the values if appropriate.</p>
<div class="pysrc"><span class="pykeyword">def</span> <span class="pytext">string_escape</span><span class="pyoperator">(</span><span class="pytext">section</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">encode</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pystring">"""<br />
A function to encode or decode using the 'string-escape' codec.<br />
To be passed to the walk method of a ConfigObj.<br />
By default it decodes.<br />
To encode, pass in the keyword argument ``encode=True``.<br />
"""</span><br />
<span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span><br />
<span class="pycomment"># is it a type we can work with<br />
</span> <span class="pycomment"># NOTE: for platforms where Python > 2.2<br />
</span> <span class="pycomment"># you can use basestring instead of (str, unicode)<br />
</span> <span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">,</span> <span class="pytext">list</span><span class="pyoperator">,</span> <span class="pytext">tuple</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pycomment"># no !<br />
</span> <span class="pykeyword">return</span><br />
<span class="pykeyword">elif</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pycomment"># it's a string !<br />
</span> <span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">encode</span><span class="pyoperator">:</span><br />
<span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">decode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">encode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pycomment"># it must be a list or tuple!<br />
</span> <span class="pycomment"># we'll be lazy and create a new list<br />
</span> <span class="pytext">newval</span> <span class="pyoperator">=</span> <span class="pyoperator">[</span><span class="pyoperator">]</span><br />
<span class="pycomment"># we'll check every member of the list<br />
</span> <span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">val</span><span class="pyoperator">:</span><br />
<span class="pykeyword">if</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">str</span><span class="pyoperator">,</span> <span class="pytext">unicode</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">if</span> <span class="pykeyword">not</span> <span class="pytext">encode</span><span class="pyoperator">:</span><br />
<span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">.</span><span class="pytext">decode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><span class="pyoperator">)</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">.</span><span class="pytext">encode</span><span class="pyoperator">(</span><span class="pystring">'string-escape'</span><span class="pyoperator">)</span><span class="pyoperator">)</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pytext">newval</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">entry</span><span class="pyoperator">)</span><br />
<span class="pycomment"># done !<br />
</span> <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">newval</span><br />
<br />
<span class="pycomment"># assume we have a ConfigObj called ``config``<br />
</span><span class="pycomment">#<br />
</span><span class="pycomment"># To decode<br />
</span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">string_escape</span><span class="pyoperator">)</span><br />
<span class="pycomment">#<br />
</span><span class="pycomment"># To encode.<br />
</span><span class="pycomment"># Because ``walk`` doesn't recognise the ``encode`` argument<br />
</span><span class="pycomment"># it passes it to our function.<br />
</span><span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">string_escape</span><span class="pyoperator">,</span> <span class="pytext">encode</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><span class="pytext"></span></div><p>Here's a simple example of using <tt class="docutils literal"><span class="pre">walk</span></tt> to transform names and values. One
usecase of this would be to create a <em>standard</em> config file with placeholders
for section and keynames. You can then use walk to create new config files
and change values and member names :</p>
<div class="pysrc"><span class="pycomment"># We use 'XXXX' as a placeholder<br />
</span><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pystring">'''<br />
XXXXkey1 = XXXXvalue1<br />
XXXXkey2 = XXXXvalue2<br />
XXXXkey3 = XXXXvalue3<br />
[XXXXsection1]<br />
XXXXkey1 = XXXXvalue1<br />
XXXXkey2 = XXXXvalue2<br />
XXXXkey3 = XXXXvalue3<br />
[XXXXsection2]<br />
XXXXkey1 = XXXXvalue1<br />
XXXXkey2 = XXXXvalue2<br />
XXXXkey3 = XXXXvalue3<br />
[[XXXXsection1]]<br />
XXXXkey1 = XXXXvalue1<br />
XXXXkey2 = XXXXvalue2<br />
XXXXkey3 = XXXXvalue3<br />
'''</span><span class="pyoperator">.</span><span class="pytext">splitlines</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">config</span><span class="pyoperator">)</span><br />
<span class="pycomment">#<br />
</span><span class="pykeyword">def</span> <span class="pytext">transform</span><span class="pyoperator">(</span><span class="pytext">section</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">key</span><span class="pyoperator">]</span><br />
<span class="pytext">newkey</span> <span class="pyoperator">=</span> <span class="pytext">key</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'XXXX'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1'</span><span class="pyoperator">)</span><br />
<span class="pytext">section</span><span class="pyoperator">.</span><span class="pytext">rename</span><span class="pyoperator">(</span><span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">newkey</span><span class="pyoperator">)</span><br />
<span class="pykeyword">if</span> <span class="pytext">isinstance</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">,</span> <span class="pyoperator">(</span><span class="pytext">tuple</span><span class="pyoperator">,</span> <span class="pytext">list</span><span class="pyoperator">,</span> <span class="pytext">dict</span><span class="pyoperator">)</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">pass</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">val</span><span class="pyoperator">.</span><span class="pytext">replace</span><span class="pyoperator">(</span><span class="pystring">'XXXX'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1'</span><span class="pyoperator">)</span><br />
<span class="pytext">section</span><span class="pyoperator">[</span><span class="pytext">newkey</span><span class="pyoperator">]</span> <span class="pyoperator">=</span> <span class="pytext">val</span><br />
<span class="pycomment">#<br />
</span><span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">walk</span><span class="pyoperator">(</span><span class="pytext">transform</span><span class="pyoperator">,</span> <span class="pytext">call_on_sections</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
<span class="pykeyword">print</span> <span class="pytext">cfg</span><br />
<span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1section1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">}</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1section2'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1section1'</span><span class="pyoperator">:</span> <span class="pyoperator">{</span><span class="pystring">'CLIENT1key1'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value1'</span><span class="pyoperator">,</span><br />
<span class="pystring">'CLIENT1key2'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value2'</span><span class="pyoperator">,</span> <span class="pystring">'CLIENT1key3'</span><span class="pyoperator">:</span> <span class="pystring">'CLIENT1value3'</span><span class="pyoperator">}</span><span class="pyoperator">}</span><span class="pyoperator">}</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id70" id="exceptions" name="exceptions">7 Exceptions</a></h1>
<p>There are several places where ConfigObj may raise exceptions (other than
because of bugs).</p>
<ol class="arabic">
<li><dl class="first docutils">
<dt>If a configspec filename you pass in doesn't exist, or a config file</dt>
<dd><p class="first last">filename doesn't exist <em>and</em> <tt class="docutils literal"><span class="pre">file_error=True</span></tt>, an <tt class="docutils literal"><span class="pre">IOError</span></tt> will be
raised.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>If you try to set a non-string key, or a non string value when</dt>
<dd><p class="first last"><tt class="docutils literal"><span class="pre">stringify=False</span></tt>, a <tt class="docutils literal"><span class="pre">TypeError</span></tt> will be raised.</p>
</dd>
</dl>
</li>
<li><p class="first">A badly built config file will cause parsing errors.</p>
</li>
<li><p class="first">A parsing error can also occur when reading a configspec.</p>
</li>
<li><dl class="first docutils">
<dt>In string interpolation you can specify a value that doesn't exist, or</dt>
<dd><p class="first last">create circular references (recursion).</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>If you have a <tt class="docutils literal"><span class="pre">__many__</span></tt> repeated section with other section definitions</dt>
<dd><p class="first last">(in a configspec), a <tt class="docutils literal"><span class="pre">RepeatSectionError</span></tt> will be raised.</p>
</dd>
</dl>
</li>
</ol>
<dl class="docutils">
<dt>Number 5 (which is actually two different types of exceptions) is documented</dt>
<dd>in <a class="reference" href="#interpolation">interpolation</a>.</dd>
</dl>
<p>Number 6 is explained in the <a class="reference" href="#validation">validation</a> section.</p>
<p><em>This</em> section is about errors raised during parsing.</p>
<p>The base error class is <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>. This is a subclass of
<tt class="docutils literal"><span class="pre">SyntaxError</span></tt>, so you can trap for <tt class="docutils literal"><span class="pre">SyntaxError</span></tt> without needing to
directly import any of the ConfigObj exceptions.</p>
<p>The following other exceptions are defined (all deriving from
<tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>) :</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">NestingError</span></tt></p>
<blockquote>
<p>This error indicates either a mismatch in the brackets in a section marker,
or an excessive level of nesting.</p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">ParseError</span></tt></p>
<blockquote>
<p>This error indicates that a line is badly written. It is neither a valid
<tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span> <span class="pre">value</span></tt> line, nor a valid section marker line, nor a comment line.</p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">DuplicateError</span></tt></p>
<blockquote>
<p>The keyword or section specified already exists.</p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">ConfigspecError</span></tt></p>
<blockquote>
<p>An error occurred whilst parsing a configspec.</p>
</blockquote>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">UnreprError</span></tt></p>
<blockquote>
<p>An error occurred when parsing a value in <a class="reference" href="#unrepr-mode">unrepr mode</a>.</p>
</blockquote>
</li>
</ul>
<p>When parsing a configspec, ConfigObj will stop on the first error it
encounters. It will raise a <tt class="docutils literal"><span class="pre">ConfigspecError</span></tt>. This will have an <tt class="docutils literal"><span class="pre">error</span></tt>
attribute, which is the actual error that was raised.</p>
<p>Behaviour when parsing a config file depends on the option <tt class="docutils literal"><span class="pre">raise_errors</span></tt>.
If ConfigObj encounters an error while parsing a config file:</p>
<blockquote>
<p>If <tt class="docutils literal"><span class="pre">raise_errors=True</span></tt> then ConfigObj will raise the appropriate error
and parsing will stop.</p>
<p>If <tt class="docutils literal"><span class="pre">raise_errors=False</span></tt> (the default) then parsing will continue to the
end and <em>all</em> errors will be collected.</p>
</blockquote>
<p>If <tt class="docutils literal"><span class="pre">raise_errors</span></tt> is False and multiple errors are found a <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>
is raised. The error raised has a <tt class="docutils literal"><span class="pre">config</span></tt> attribute, which is the parts of
the ConfigObj that parsed successfully. It also has an attribute <tt class="docutils literal"><span class="pre">errors</span></tt>,
which is a list of <em>all</em> the errors raised. Each entry in the list is an
instance of the appropriate error type. Each one has the following attributes
(useful for delivering a sensible error message to your user) :</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">line</span></tt>: the original line that caused the error.</li>
<li><tt class="docutils literal"><span class="pre">line_number</span></tt>: its number in the config file.</li>
<li><tt class="docutils literal"><span class="pre">message</span></tt>: the error message that accompanied the error.</li>
</ul>
<p>If only one error is found, then that error is re-raised. The error still has
the <tt class="docutils literal"><span class="pre">config</span></tt> and <tt class="docutils literal"><span class="pre">errors</span></tt> attributes. This means that your error handling
code can be the same whether one error is raised in parsing , or several.</p>
<p>It also means that in the most common case (a single error) a useful error
message will be raised.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">One wrongly written line could break the basic structure of your config
file. This could cause every line after it to flag an error, so having a
list of all the lines that caused errors may not be as useful as it sounds.
<img src="smilies/sad.gif" alt="Sad" height="15" width="15" /> .</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id71" id="validation" name="validation">8 Validation</a></h1>
<div class="hint">
<p class="first admonition-title">Hint</p>
<p>The system of configspecs can seem confusing at first, but is actually
quite simple and powerful. For a concrete example of how to use it, you may
find this blog entry helpful :
<a class="reference" href="http://www.voidspace.org.uk/python/weblog/arch_d7_2006_03_04.shtml#e257">Transforming Values with ConfigObj</a>.</p>
<p class="last">There is also a module to assist in auto-generating configspecs called
<a class="reference" href="http://www.voidspace.org.uk/python/configpersist.html">ConfigPersist.py</a>. Its use is explained in <a class="reference" href="http://www.voidspace.org.uk/python/articles/configobj_for_data_persistence.shtml">ConfigObj for Data Persistence</a>.</p>
</div>
<p>Validation is done through a combination of the <a class="reference" href="#configspec">configspec</a> and a <tt class="docutils literal"><span class="pre">Validator</span></tt>
object. For this you need <em>validate.py</em> <a class="footnote-reference" href="#id30" id="id15" name="id15">[13]</a>. See <a class="reference" href="#downloading">downloading</a> if you don't
have a copy.</p>
<p>Validation can perform two different operations :</p>
<ol class="arabic">
<li><dl class="first docutils">
<dt>Check that a value meets a specification. For example, check that a value</dt>
<dd><p class="first last">is an integer between one and six, or is a choice from a specific set of
options.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>It can convert the value into the type required. For example, if one of</dt>
<dd><p class="first last">your values is a port number, validation will turn it into an integer for
you.</p>
</dd>
</dl>
</li>
</ol>
<p>So validation can act as a transparent layer between the datatypes of your
application configuration (boolean, integers, floats, etc) and the text format
of your config file.</p>
<div class="section">
<h2><a class="toc-backref" href="#id72" id="configspec" name="configspec">8.1 configspec</a></h2>
<p>The <tt class="docutils literal"><span class="pre">validate</span></tt> method checks members against an entry in the configspec. Your
configspec therefore resembles your config file, with a check for every member.</p>
<p>In order to perform validation you need a <tt class="docutils literal"><span class="pre">Validator</span></tt> object. This has
several useful built-in check functions. You can also create your own custom
functions and register them with your Validator object.</p>
<p>Each check is the name of one of these functions, including any parameters and
keyword arguments. The configspecs look like function calls, and they map to
function calls.</p>
<p>The basic datatypes that an un-extended Validator can test for are :</p>
<ul class="simple">
<li>boolean values (True and False)</li>
<li>integers (including minimum and maximum values)</li>
<li>floats (including min and max)</li>
<li>strings (including min and max length)</li>
<li>IP addresses (v4 only)</li>
</ul>
<p>It can also handle lists of these types and restrict a value to being one from
a set of options.</p>
<p>An example configspec is going to look something like :</p>
<pre class="literal-block">
port = integer(0, 100)
user = string(max=25)
mode = option('quiet', 'loud', 'silent')
</pre>
<p>You can specify default values, and also have the same configspec applied to
several sections. This is called <a class="reference" href="#repeated-sections">repeated sections</a>.</p>
<p>For full details on writing configspecs, please refer to the <a class="reference" href="http://www.voidspace.org.uk/python/validate.html">validate.py
documentation</a>.</p>
<div class="important">
<p class="first admonition-title">Important</p>
<p>Your configspec is read by ConfigObj in the same way as a config file.</p>
<p>That means you can do interpolation <em>within</em> your configspec.</p>
<p class="last">In order to allow this, checks in the 'DEFAULT' section (of the root level
of your configspec) are <em>not</em> used.</p>
</div>
<p>If you need to specify the encoding of your configspec, then you can pass in a
ConfigObj instance as your configspec. When you read your configspec file, you
<em>must</em> specify <tt class="docutils literal"><span class="pre">list_values=False</span></tt>.</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">configspec</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspecfilename</span><span class="pyoperator">,</span> <span class="pytext">encoding</span><span class="pyoperator">=</span><span class="pystring">'UTF8'</span><span class="pyoperator">,</span><br />
<span class="pytext">list_values</span><span class="pyoperator">=</span><span class="pytext">False</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
<div class="section">
<h2><a class="toc-backref" href="#id73" id="type-conversion" name="type-conversion">8.2 Type Conversion</a></h2>
<p>By default, validation does type conversion. This means that if you specify
<tt class="docutils literal"><span class="pre">integer</span></tt> as the check, then calling <a class="reference" href="#validate">validate</a> will actually change the value
to an integer (so long as the check succeeds).</p>
<p>It also means that when you call the <a class="reference" href="#write">write</a> method, the value will be converted
back into a string using the <tt class="docutils literal"><span class="pre">str</span></tt> function.</p>
<p>To switch this off, and leave values as strings after validation, you need to
set the <a class="reference" href="#stringify">stringify</a> attribute to <tt class="docutils literal"><span class="pre">False</span></tt>. If this is the case, attempting to
set a value to a non-string will raise an error.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id74" id="default-values" name="default-values">8.3 Default Values</a></h2>
<p>You can set a default value in your check. If the value is missing from the
config file then this value will be used instead. This means that your user
only has to supply values that differ from the defaults.</p>
<p>If you <em>don't</em> supply a default then for a value to be missing is an error,
and this will show in the <a class="reference" href="#return-value">return value</a> from validate.</p>
<p>Additionally you can set the default to be <tt class="docutils literal"><span class="pre">None</span></tt>. This means the value will
be set to <tt class="docutils literal"><span class="pre">None</span></tt> (the object) <em>whichever check is used</em>. (It will be set to
<tt class="docutils literal"><span class="pre">''</span></tt> rather than <tt class="docutils literal"><span class="pre">None</span></tt> if <a class="reference" href="#stringify">stringify</a> is <tt class="docutils literal"><span class="pre">False</span></tt>). You can use this
to easily implement optional values in your config files.</p>
<pre class="literal-block">
port = integer(0, 100, default=80)
user = string(max=25, default=0)
mode = option('quiet', 'loud', 'silent', default='loud')
nick = string(default=None)
</pre>
<div class="note">
<p class="first admonition-title">Note</p>
<p>Because the default goes through type conversion, it also has to pass the
check.</p>
<p class="last">Note that <tt class="docutils literal"><span class="pre">default=None</span></tt> is case sensitive.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id75" id="id16" name="id16">8.3.1 List Values</a></h3>
<p>It's possible that you will want to specify a list as a default value. To avoid
confusing syntax with commas and quotes you use a list constructor to specify
that keyword arguments are lists. This includes the <tt class="docutils literal"><span class="pre">default</span></tt> value. This
makes checks look something like :</p>
<pre class="literal-block">
checkname(default=list('val1', 'val2', 'val3'))
</pre>
<p>This works with all keyword arguments, but is most useful for default values.</p>
</div>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id76" id="repeated-sections" name="repeated-sections">8.4 Repeated Sections</a></h2>
<p>Repeated sections are a way of specifying a configspec for a section that
should be applied to <em>all</em> subsections in the same section.</p>
<p>The easiest way of explaining this is to give an example. Suppose you have a
config file that describes a dog. That dog has various attributes, but it can
also have many fleas. You don't know in advance how many fleas there will be,
or what they will be called, but you want each flea validated against the same
configspec.</p>
<p>We can define a section called <em>fleas</em>. We want every flea in that section
(every sub-section) to have the same configspec applied to it. We do this by
defining a single section called <tt class="docutils literal"><span class="pre">__many__</span></tt>.</p>
<pre class="literal-block">
[dog]
name = string(default=Rover)
age = float(0, 99, default=0)
[[fleas]]
[[[__many__]]]
bloodsucker = boolean(default=True)
children = integer(default=10000)
size = option(small, tiny, micro, default=tiny)
</pre>
<p>Every flea on our dog will now be validated using the <tt class="docutils literal"><span class="pre">__many__</span></tt> configspec.</p>
<p>If you define another sub-section in a section <em>as well as</em> a <tt class="docutils literal"><span class="pre">__many__</span></tt> then
you will get an error.</p>
<p><tt class="docutils literal"><span class="pre">__many__</span></tt> sections can have sub-sections, including their own <tt class="docutils literal"><span class="pre">__many__</span></tt>
sub-sections. Defaults work in the normal way in repeated sections.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id77" id="copy-mode" name="copy-mode">8.5 Copy Mode</a></h2>
<p>Because you can specify default values in your configspec, you can use
ConfigObj to write out default config files for your application.</p>
<p>However, normally values supplied from a default in a configspec are <em>not</em>
written out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
<p>To do this, you need to specify <tt class="docutils literal"><span class="pre">copy=True</span></tt> when you call validate. As well
as not marking values as default, all the comments in the configspec file
will be copied into your ConfigObj instance.</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pykeyword">from</span> <span class="pytext">validate</span> <span class="pykeyword">import</span> <span class="pytext">Validator</span><br />
<span class="pytext">vdt</span> <span class="pyoperator">=</span> <span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pystring">'default.ini'</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">filename</span> <span class="pyoperator">=</span> <span class="pystring">'new_default.ini'</span><br />
<span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">vdt</span><span class="pyoperator">,</span> <span class="pytext">copy</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pytext"></span></div></div>
<div class="section">
<h2><a class="toc-backref" href="#id78" id="validation-and-interpolation" name="validation-and-interpolation">8.6 Validation and Interpolation</a></h2>
<p>String interpolation and validation don't play well together. When validation
changes type it sets the value. If the value uses interpolation, then the
interpolation reference would normally be overwritten. Calling <tt class="docutils literal"><span class="pre">write</span></tt> would
then use the absolute value and the interpolation reference would be lost.</p>
<p>As a compromise - if the value is unchanged by validation then it is not reset.
This means strings that pass through validation unmodified will not be
overwritten. If validation changes type - the value has to be overwritten, and
any interpolation references are lost <img src="smilies/sad.gif" alt="Sad" height="15" width="15" /> .</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id79" id="simpleval" name="simpleval">8.7 SimpleVal</a></h2>
<p>You may not need a full validation process, but still want to check if all the
expected values are present.</p>
<p>Provided as part of the ConfigObj module is the <tt class="docutils literal"><span class="pre">SimpleVal</span></tt> object. This has
a dummy <tt class="docutils literal"><span class="pre">test</span></tt> method that always passes.</p>
<p>The only reason a test will fail is if the value is missing. The return value
from <tt class="docutils literal"><span class="pre">validate</span></tt> will either be <tt class="docutils literal"><span class="pre">True</span></tt>, meaning all present, or a dictionary
with <tt class="docutils literal"><span class="pre">False</span></tt> for all missing values/sections.</p>
<p>To use it, you still need to pass in a valid configspec when you create the
ConfigObj, but just set all the values to <tt class="docutils literal"><span class="pre">''</span></tt>. Then create an instance of
<tt class="docutils literal"><span class="pre">SimpleVal</span></tt> and pass it to the <tt class="docutils literal"><span class="pre">validate</span></tt> method.</p>
<p>As a trivial example if you had the following config file :</p>
<pre class="literal-block">
# config file for an application
port = 80
protocol = http
domain = voidspace
top_level_domain = org.uk
</pre>
<p>You would write the following configspec :</p>
<pre class="literal-block">
port = ''
protocol = ''
domain = ''
top_level_domain = ''
</pre>
<div class="pysrc"><span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">Configobj</span><span class="pyoperator">(</span><span class="pytext">filename</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">configspec</span><span class="pyoperator">)</span><br />
<span class="pytext">val</span> <span class="pyoperator">=</span> <span class="pytext">SimpleVal</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">test</span> <span class="pyoperator">=</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">val</span><span class="pyoperator">)</span><br />
<span class="pykeyword">if</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">True</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pystring">'All values present.'</span><br />
<span class="pykeyword">elif</span> <span class="pytext">test</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pystring">'No values present!'</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">test</span><span class="pyoperator">:</span><br />
<span class="pykeyword">if</span> <span class="pytext">test</span><span class="pyoperator">[</span><span class="pytext">entry</span><span class="pyoperator">]</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pystring">'"%s" missing.'</span> <span class="pyoperator">%</span> <span class="pytext">entry</span><span class="pytext"></span></div></div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id80" id="empty-values" name="empty-values">9 Empty values</a></h1>
<p>Many config files from other applications allow empty values. As of version
4.3.0, ConfigObj will read these as an empty string.</p>
<p>A new option/attribute has been added (<tt class="docutils literal"><span class="pre">write_empty_values</span></tt>) to allow
ConfigObj to write empty strings as empty values.</p>
<div class="pysrc"><span class="pykeyword">from</span> <span class="pytext">configobj</span> <span class="pykeyword">import</span> <span class="pytext">ConfigObj</span><br />
<span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pystring">'''<br />
key =<br />
key2 = # a comment<br />
'''</span><span class="pyoperator">.</span><span class="pytext">splitlines</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pytext">config</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">cfg</span><span class="pyoperator">)</span><br />
<span class="pykeyword">print</span> <span class="pytext">config</span><br />
<span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pyoperator">{</span><span class="pystring">'key'</span><span class="pyoperator">:</span> <span class="pystring">''</span><span class="pyoperator">,</span> <span class="pystring">'key2'</span><span class="pyoperator">:</span> <span class="pystring">''</span><span class="pyoperator">}</span><span class="pyoperator">)</span><br />
<br />
<span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write_empty_values</span> <span class="pyoperator">=</span> <span class="pytext">True</span><br />
<span class="pykeyword">for</span> <span class="pytext">line</span> <span class="pykeyword">in</span> <span class="pytext">config</span><span class="pyoperator">.</span><span class="pytext">write</span><span class="pyoperator">(</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pykeyword">print</span> <span class="pytext">line</span><br />
<br />
<span class="pytext">key</span> <span class="pyoperator">=</span><br />
<span class="pytext">key2</span> <span class="pyoperator">=</span> <span class="pycomment"># a comment</span><span class="pytext"></span></div></div>
<div class="section">
<h1><a class="toc-backref" href="#id81" id="unrepr-mode" name="unrepr-mode">10 unrepr mode</a></h1>
<p>The <tt class="docutils literal"><span class="pre">unrepr</span></tt> option allows you to store and retrieve the basic Python
data-types using config files. It has to use a slightly different syntax to
normal ConfigObj files. Unsurprisingly it uses Python syntax.</p>
<p>This means that lists are different (they are surrounded by square brackets),
and strings <em>must</em> be quoted.</p>
<p>The types that <tt class="docutils literal"><span class="pre">unrepr</span></tt> can work with are :</p>
<blockquote>
<div class="line-block">
<div class="line">strings, lists tuples</div>
<div class="line">None, True, False</div>
<div class="line">dictionaries, integers, floats</div>
<div class="line">longs and complex numbers</div>
</div>
</blockquote>
<p>You can't store classes, types or instances.</p>
<p><tt class="docutils literal"><span class="pre">unrepr</span></tt> uses <tt class="docutils literal"><span class="pre">repr(object)</span></tt> to write out values, so it currently <em>doesn't</em>
check that you are writing valid objects. If you attempt to read an unsupported
value, ConfigObj will raise a <tt class="docutils literal"><span class="pre">configobj.UnknownType</span></tt> exception.</p>
<p>Values that are triple quoted cased. The triple quotes are removed <em>before</em>
converting. This means that you can use triple quotes to write dictionaries
over several lines in your config files. They won't be written like this
though.</p>
<p>If you are writing config files by hand, for use with <tt class="docutils literal"><span class="pre">unrepr</span></tt>, you should
be aware of the following differences from normal ConfigObj syntax :</p>
<blockquote>
<div class="line-block">
<div class="line">List : <tt class="docutils literal"><span class="pre">['A</span> <span class="pre">List',</span> <span class="pre">'With',</span> <span class="pre">'Strings']</span></tt></div>
<div class="line">Strings : <tt class="docutils literal"><span class="pre">"Must</span> <span class="pre">be</span> <span class="pre">quoted."</span></tt></div>
<div class="line">Backslash : <tt class="docutils literal"><span class="pre">"The</span> <span class="pre">backslash</span> <span class="pre">must</span> <span class="pre">be</span> <span class="pre">escaped</span> <span class="pre">\\"</span></tt></div>
</div>
</blockquote>
<p>These all follow normal Python syntax.</p>
<p>In unrepr mode <em>inline comments</em> are not saved. This is because lines are
parsed using the <a class="reference" href="http://docs.python.org/lib/compiler.html">compiler package</a>
which discards comments.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id82" id="interpolation" name="interpolation">11 Interpolation</a></h1>
<p>ConfigObj allows string interpolation <em>similar</em> to the way <tt class="docutils literal"><span class="pre">ConfigParser</span></tt></p>
<p>You specify a value to be substituted by including <tt class="docutils literal"><span class="pre">%(name)s</span></tt> in the value.</p>
<p>Interpolation checks first the 'DEFAULT' sub-section of the current section to
see if <tt class="docutils literal"><span class="pre">name</span></tt> is the key to a value. ('name' is case sensitive).</p>
<p>If it doesn't find it, next it checks the 'DEFAULT' section of the parent
section, last it checks the 'DEFAULT' section of the main section.</p>
<p>If the value specified isn't found then a <tt class="docutils literal"><span class="pre">MissingInterpolationOption</span></tt> error
is raised (a subclass of <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>).</p>
<p>If it is found then the returned value is also checked for substitutions. This
allows you to make up compound values (for example directory paths) that use
more than one default value. It also means it's possible to create circular
references. If after ten replacements there are still values to substitute, an
<tt class="docutils literal"><span class="pre">InterpolationDepthError</span></tt> is raised.</p>
<p>Both of these errors are subclasses of <tt class="docutils literal"><span class="pre">InterpolationError</span></tt>, which is a
subclass of <tt class="docutils literal"><span class="pre">ConfigObjError</span></tt>.</p>
<p>String interpolation and validation don't play well together. This is because
validation overwrites values - and so may erase the interpolation references.
See <a class="reference" href="#validation-and-interpolation">Validation and Interpolation</a>. (This can only happen if validation
has to <em>change</em> the value).</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id83" id="comments" name="comments">12 Comments</a></h1>
<p>Any line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
<p>If a config file starts with comments then these are preserved as the
<a class="reference" href="#initial-comment">initial_comment</a>.</p>
<p>If a config file ends with comments then these are preserved as the
<a class="reference" href="#final-comment">final_comment</a>.</p>
<p>Every key or section marker may have lines of comments immediately above it.
These are saved as the <tt class="docutils literal"><span class="pre">comments</span></tt> attribute of the section. Each member is a
list of lines.</p>
<p>You can also have a comment inline with a value. These are saved as the
<tt class="docutils literal"><span class="pre">inline_comments</span></tt> attribute of the section, with one entry per member of the
section.</p>
<p>Subsections (section markers in the config file) can also have comments.</p>
<p>See <a class="reference" href="#section-attributes">Section Attributes</a> for more on these attributes.</p>
<p>These comments are all written back out by the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id84" id="flatten-errors" name="flatten-errors">13 flatten_errors</a></h1>
<pre class="literal-block">
flatten_errors(cfg, res)
</pre>
<p><a class="reference" href="#validation">Validation</a> is a powerful way of checking that the values supplied by the user
make sense.</p>
<p>The <a class="reference" href="#validate">validate</a> method returns a results dictionary that represents pass or fail
for each value. This doesn't give you any information about <em>why</em> the check
failed.</p>
<p><tt class="docutils literal"><span class="pre">flatten_errors</span></tt> is an example function that turns a results dictionary into
a flat list, that only contains values that <em>failed</em>.</p>
<p><tt class="docutils literal"><span class="pre">cfg</span></tt> is the ConfigObj instance being checked, <tt class="docutils literal"><span class="pre">res</span></tt> is the results
dictionary returned by <tt class="docutils literal"><span class="pre">validate</span></tt>.</p>
<p>It returns a list of keys that failed. Each member of the list is a tuple :</p>
<pre class="literal-block">
([list of sections...], key, result)
</pre>
<p>If <tt class="docutils literal"><span class="pre">validate</span></tt> was called with <tt class="docutils literal"><span class="pre">preserve_errors=False</span></tt> (the default)
then <tt class="docutils literal"><span class="pre">result</span></tt> will always be <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p><em>list of sections</em> is a flattened list of sections that the key was found
in.</p>
<p>If the section was missing then key will be <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>If the value (or section) was missing then <tt class="docutils literal"><span class="pre">result</span></tt> will be <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>If <tt class="docutils literal"><span class="pre">validate</span></tt> was called with <tt class="docutils literal"><span class="pre">preserve_errors=True</span></tt> and a value
was present, but failed the check, then <tt class="docutils literal"><span class="pre">result</span></tt> will be the exception
object returned. You can use this as a string that describes the failure.</p>
<p>For example :</p>
<blockquote>
<em>The value "3" is of the wrong type</em>.</blockquote>
<div class="section">
<h2><a class="toc-backref" href="#id85" id="example-usage" name="example-usage">13.1 Example Usage</a></h2>
<p>The output from <tt class="docutils literal"><span class="pre">flatten_errors</span></tt> is a list of tuples.</p>
<p>Here is an example of how you could present this information to the user.</p>
<div class="pysrc"><span class="pytext">vtor</span> <span class="pyoperator">=</span> <span class="pytext">validate</span><span class="pyoperator">.</span><span class="pytext">Validator</span><span class="pyoperator">(</span><span class="pyoperator">)</span><br />
<span class="pycomment"># ini is your config file - cs is the configspec<br />
</span><span class="pytext">cfg</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">ini</span><span class="pyoperator">,</span> <span class="pytext">configspec</span><span class="pyoperator">=</span><span class="pytext">cs</span><span class="pyoperator">)</span><br />
<span class="pytext">res</span> <span class="pyoperator">=</span> <span class="pytext">cfg</span><span class="pyoperator">.</span><span class="pytext">validate</span><span class="pyoperator">(</span><span class="pytext">vtor</span><span class="pyoperator">,</span> <span class="pytext">preserve_errors</span><span class="pyoperator">=</span><span class="pytext">True</span><span class="pyoperator">)</span><br />
<span class="pykeyword">for</span> <span class="pytext">entry</span> <span class="pykeyword">in</span> <span class="pytext">flatten_errors</span><span class="pyoperator">(</span><span class="pytext">cfg</span><span class="pyoperator">,</span> <span class="pytext">res</span><span class="pyoperator">)</span><span class="pyoperator">:</span><br />
<span class="pycomment"># each entry is a tuple<br />
</span> <span class="pytext">section_list</span><span class="pyoperator">,</span> <span class="pytext">key</span><span class="pyoperator">,</span> <span class="pytext">error</span> <span class="pyoperator">=</span> <span class="pytext">entry</span><br />
<span class="pykeyword">if</span> <span class="pytext">key</span> <span class="pykeyword">is</span> <span class="pykeyword">not</span> <span class="pytext">None</span><span class="pyoperator">:</span><br />
<span class="pytext">section_list</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pytext">key</span><span class="pyoperator">)</span><br />
<span class="pykeyword">else</span><span class="pyoperator">:</span><br />
<span class="pytext">section_list</span><span class="pyoperator">.</span><span class="pytext">append</span><span class="pyoperator">(</span><span class="pystring">'[missing section]'</span><span class="pyoperator">)</span><br />
<span class="pytext">section_string</span> <span class="pyoperator">=</span> <span class="pystring">', '</span><span class="pyoperator">.</span><span class="pytext">join</span><span class="pyoperator">(</span><span class="pytext">section_list</span><span class="pyoperator">)</span><br />
<span class="pykeyword">if</span> <span class="pytext">error</span> <span class="pyoperator">==</span> <span class="pytext">False</span><span class="pyoperator">:</span><br />
<span class="pytext">error</span> <span class="pyoperator">=</span> <span class="pystring">'Missing value or section.'</span><br />
<span class="pykeyword">print</span> <span class="pytext">section_string</span><span class="pyoperator">,</span> <span class="pystring">' = '</span><span class="pyoperator">,</span> <span class="pytext">error</span><span class="pytext"></span></div></div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id86" id="backwards-compatibility" name="backwards-compatibility">14 Backwards Compatibility</a></h1>
<p>There have been a lot of changes since ConfigObj 3. The core parser is now
based on regular expressions, and is a lot faster and smaller. There is now no
difference in the way we treat flat files and non-flatfiles, that is, no empty
sections. This means some of the code can be a lot simpler, less code does
more of the work <a class="footnote-reference" href="#id31" id="id17" name="id17">[14]</a>.</p>
<p>There have been other simplifications: for example we only have eight options
instead of seventeen.</p>
<p>Most config files created for ConfigObj 3 will be read with no changes and many
programs will work without having to alter code. Some of the changes do break
backwards compatibility: for example, code that uses the previous options will
now raise an error. It should be very easy to fix these, though.</p>
<p>Below is a list of all the changes that affect backwards compatibility. This
doesn't include details of method signatures that have changed, because almost
all of them have.</p>
<div class="section">
<h2><a class="toc-backref" href="#id87" id="incompatible-changes" name="incompatible-changes">14.1 Incompatible Changes</a></h2>
<p>(I have removed a lot of needless complications: this list is probably not
conclusive, many option/attribute/method names have changed.)</p>
<p>Case sensitive.</p>
<p>The only valid divider is '='.</p>
<p>Line continuations with <tt class="docutils literal"><span class="pre">\</span></tt> removed.</p>
<p>No recursive lists in values.</p>
<p>No empty sections.</p>
<p>No distinction between flatfiles and non flatfiles.</p>
<p>Change in list syntax: use commas to indicate list, not parentheses (square
brackets and parentheses are no longer recognised as lists).</p>
<p>';' is no longer valid for comments, and no multiline comments.</p>
<p>No attribute-style access to values.</p>
<p>Empty values not allowed: use '' or "".</p>
<p>In ConfigObj 3, setting a non-flatfile member to <tt class="docutils literal"><span class="pre">None</span></tt> would initialise it
as an empty section.</p>
<p>The escape entities '&mjf-lf;' and '&mjf-quot;' have gone, replaced by triple
quote, multiple line values.</p>
<p>The <tt class="docutils literal"><span class="pre">newline</span></tt>, <tt class="docutils literal"><span class="pre">force_return</span></tt>, and <tt class="docutils literal"><span class="pre">default</span></tt> options have gone.</p>
<p><tt class="docutils literal"><span class="pre">fileerror</span></tt> and <tt class="docutils literal"><span class="pre">createempty</span></tt> options have become <tt class="docutils literal"><span class="pre">file_error</span></tt> and
<tt class="docutils literal"><span class="pre">create_empty</span></tt>.</p>
<p>Partial configspecs (for specifying the order members should be written out,
and which should be present) have gone. The configspec is no longer used to
specify order for the <tt class="docutils literal"><span class="pre">write</span></tt> method.</p>
<p>Exceeding the maximum depth of recursion in string interpolation now raises an
error <tt class="docutils literal"><span class="pre">InterpolationDepthError</span></tt>.</p>
<p>Specifying a value for interpolation which doesn't exist now raises a
<tt class="docutils literal"><span class="pre">MissingInterpolationOption</span></tt> error (instead of merely being ignored).</p>
<p>The <tt class="docutils literal"><span class="pre">writein</span></tt> method has been removed.</p>
<p>The comments attribute is now a list (<tt class="docutils literal"><span class="pre">inline_comments</span></tt> equates to the old
comments attribute).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id88" id="configobj-3" name="configobj-3">14.2 ConfigObj 3</a></h2>
<p>ConfigObj 3 is now deprecated in favour of ConfigObj 4. I can fix bugs in
ConfigObj 3 if needed, though.</p>
<p>For anyone who still needs it, you can download it here: <a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj3.zip">ConfigObj 3.3.1</a></p>
<p>You can read the old docs at : <a class="reference" href="http://www.voidspace.org.uk/python/configobj3.html">ConfigObj 3 Docs</a></p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id89" id="credits" name="credits">15 CREDITS</a></h1>
<p>ConfigObj 4 is written by (and copyright) <a class="reference" href="http://www.voidspace.org.uk/index2.shtml">Michael Foord</a> and
<a class="reference" href="http://www.teknico.net">Nicola Larosa</a>.</p>
<p>Particularly thanks to Nicola Larosa for help on the config file spec, the
validation system and the doctests.</p>
<p><em>validate.py</em> was originally written by Michael Foord and <a class="reference" href="http://www.la-la.com">Mark Andrews</a>.</p>
<p>Thanks to others for input and bugfixes.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id90" id="license" name="license">16 LICENSE</a></h1>
<p>ConfigObj, and related files, are licensed under the BSD license. This is a
very unrestrictive license, but it comes with the usual disclaimer. This is
free software: test it, break it, just don't blame us if it eats your data !
Of course if it does, let us know and we'll fix the problem so it doesn't
happen to anyone else <img src="smilies/smile.gif" alt="Smile" height="15" width="15" /> .</p>
<pre class="literal-block">
Copyright (c) 2004 - 2006, Michael Foord & Nicola Larosa
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Michael Foord nor Nicola Larosa
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>
<p>You should also be able to find a copy of this license at : <a class="reference" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id91" id="todo" name="todo">17 TODO</a></h1>
<p>Better support for configuration from multiple files, including tracking
<em>where</em> the original file came from and writing changes to the correct
file.</p>
<p>Make <tt class="docutils literal"><span class="pre">newline</span></tt> an option (as well as an attribute) ?</p>
<p><tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded files, when returned as a list of lines, will have the
BOM at the start of every line. Should this be removed from all but the
first line ?</p>
<p>Option to set warning type for unicode decode ? (Defaults to strict).</p>
<p>A method to optionally remove uniform indentation from multiline values.
(do as an example of using <tt class="docutils literal"><span class="pre">walk</span></tt> - along with string-escape)</p>
<p>Should the results dictionary from validate be an ordered dictionary if
<a class="reference" href="http://www.voidspace.org.uk/python/odict.html">odict</a> is available ?</p>
<p>Implement some of the sequence methods (which include slicing) from the
newer <tt class="docutils literal"><span class="pre">odict</span></tt> ?</p>
<p>Preserve line numbers of values (and possibly the original text of each value).</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id92" id="issues" name="issues">18 ISSUES</a></h1>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Please file any bug reports to <a class="reference" href="http://www.voidspace.org.uk/index2.shtml">Michael Foord</a> or the <strong>ConfigObj</strong>
<a class="reference" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
</div>
<p>There is currently no way to specify the encoding of a configspec file.</p>
<p>When using <tt class="docutils literal"><span class="pre">copy</span></tt> mode for validation, it won't copy <tt class="docutils literal"><span class="pre">DEFAULT</span></tt>
sections. This is so that you <em>can</em> use interpolation in configspec
files.</p>
<p><tt class="docutils literal"><span class="pre">validate</span></tt> doesn't report <em>extra</em> values or sections.</p>
<p>You can't have a keyword with the same name as a section (in the same
section). They are both dictionary keys - so they would overlap.</p>
<p>ConfigObj doesn't quote and unquote values if <tt class="docutils literal"><span class="pre">list_values=False</span></tt>.
This means that leading or trailing whitespace in values will be lost when
writing. (Unless you manually quote).</p>
<p>Interpolation checks first the 'DEFAULT' subsection of the current
section, next it checks the 'DEFAULT' section of the parent section,
last it checks the 'DEFAULT' section of the main section.</p>
<p>Logically a 'DEFAULT' section should apply to all subsections of the <em>same
parent</em> - this means that checking the 'DEFAULT' subsection in the
<em>current section</em> is not necessarily logical ?</p>
<p>Does it matter that we don't support the ':' divider, which is supported
by <tt class="docutils literal"><span class="pre">ConfigParser</span></tt> ?</p>
<p>String interpolation and validation don't play well together. When
validation changes type it sets the value. This will correctly fetch the
value using interpolation - but then overwrite the interpolation reference.
If the value is unchanged by validation (it's a string) - but other types
will be.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id93" id="changelog" name="changelog">19 CHANGELOG</a></h1>
<p>This is an abbreviated changelog showing the major releases up to version 4.
From version 4 it lists all releases and changes. <em>More</em> data on individual
changes may be found in the source code or the CHANGELOG file.</p>
<div class="section">
<h2><a class="toc-backref" href="#id94" id="version-4-3-2" name="version-4-3-2">19.1 2006/06/04 - Version 4.3.2</a></h2>
<p>Changed error handling, if parsing finds a single error then that error will
be re-raised. That error will still have an <tt class="docutils literal"><span class="pre">errors</span></tt> and a <tt class="docutils literal"><span class="pre">config</span></tt>
attribute.</p>
<p>Fixed bug where 'n' terminated files could be truncated.</p>
<p>Bugfix in <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode, it couldn't handle '#' in values. (Thanks to
Philippe Normand for the report.)</p>
<p>As a consequence of this fix, ConfigObj doesn't now keep inline comments in
<tt class="docutils literal"><span class="pre">unrepr</span></tt> mode. This is because the parser in the <a class="reference" href="http://docs.python.org/lib/compiler.html">compiler package</a>
doesn't keep comments. <img src="smilies/smile.gif" alt="Smile" height="15" width="15" /> </p>
<p>Error messages are now more useful. They tell you the number of parsing errors
and the line number of the first error. (In the case of multiple errors.)</p>
<p>Line numbers in exceptions now start at 1, not 0.</p>
<p>Errors in <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode are now handled the same way as in the normal mode.
The errors stored will be an <tt class="docutils literal"><span class="pre">UnreprError</span></tt>.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id95" id="version-4-3-1" name="version-4-3-1">19.2 2006/04/29 - Version 4.3.1</a></h2>
<p>Added <tt class="docutils literal"><span class="pre">validate.py</span></tt> back into <tt class="docutils literal"><span class="pre">configobj.zip</span></tt>. (Thanks to Stewart
Midwinter)</p>
<p>Updated to <a class="reference" href="http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py">validate.py</a> 0.2.2.</p>
<p>Preserve tuples when calling the <tt class="docutils literal"><span class="pre">dict</span></tt> method. (Thanks to Gustavo Niemeyer.)</p>
<p>Changed <tt class="docutils literal"><span class="pre">__repr__</span></tt> to return a string that contains <tt class="docutils literal"><span class="pre">ConfigObj({</span> <span class="pre">...</span> <span class="pre">})</span></tt>.</p>
<p>Change so that an options dictionary isn't modified by passing it to ConfigObj.
(Thanks to Artarious.)</p>
<p>Added ability to handle negative integers in <tt class="docutils literal"><span class="pre">unrepr</span></tt>. (Thanks to Kevin
Dangoor.)</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id96" id="version-4-3-0" name="version-4-3-0">19.3 2006/03/24 - Version 4.3.0</a></h2>
<p>Moved the tests and the CHANGELOG (etc) into a separate file. This has reduced
the size of <tt class="docutils literal"><span class="pre">configobj.py</span></tt> by about 40%.</p>
<p>Added the <tt class="docutils literal"><span class="pre">unrepr</span></tt> mode to reading and writing config files. Thanks to Kevin
Dangoor for this suggestion.</p>
<p>Empty values are now valid syntax. They are read as an empty string <tt class="docutils literal"><span class="pre">''</span></tt>.
(<tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span></tt>, or <tt class="docutils literal"><span class="pre">key</span> <span class="pre">=</span> <span class="pre">#</span> <span class="pre">comment</span></tt>.)</p>
<p><tt class="docutils literal"><span class="pre">validate</span></tt> now honours the order of the configspec.</p>
<p>Added the <tt class="docutils literal"><span class="pre">copy</span></tt> mode to validate. Thanks to Louis Cordier for this
suggestion.</p>
<p>Fixed bug where files written on windows could be given <tt class="docutils literal"><span class="pre">'\r\r\n'</span></tt> line
terminators.</p>
<p>Fixed bug where last occurring comment line could be interpreted as the
final comment if the last line isn't terminated.</p>
<p>Fixed bug where nested list values would be flattened when <tt class="docutils literal"><span class="pre">write</span></tt> is
called. Now sub-lists have a string representation written instead.</p>
<p>Deprecated <tt class="docutils literal"><span class="pre">encode</span></tt> and <tt class="docutils literal"><span class="pre">decode</span></tt> methods instead.</p>
<p>You can now pass in a ConfigObj instance as a configspec (remember to read
the configspec file using <tt class="docutils literal"><span class="pre">list_values=False</span></tt>).</p>
<p>Sorted footnotes in the docs.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id97" id="version-4-2-0" name="version-4-2-0">19.4 2006/02/16 - Version 4.2.0</a></h2>
<p>Removed <tt class="docutils literal"><span class="pre">BOM_UTF8</span></tt> from <tt class="docutils literal"><span class="pre">__all__</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">BOM</span></tt> attribute has become a boolean. (Defaults to <tt class="docutils literal"><span class="pre">False</span></tt>.) It is
<em>only</em> <tt class="docutils literal"><span class="pre">True</span></tt> for the <tt class="docutils literal"><span class="pre">UTF16/UTF8</span></tt> encodings.</p>
<p>File like objects no longer need a <tt class="docutils literal"><span class="pre">seek</span></tt> attribute.</p>
<p>Full unicode support added. New options/attributes <tt class="docutils literal"><span class="pre">encoding</span></tt>,
<tt class="docutils literal"><span class="pre">default_encoding</span></tt>.</p>
<p>ConfigObj no longer keeps a reference to file like objects. Instead the
<tt class="docutils literal"><span class="pre">write</span></tt> method takes a file like object as an optional argument. (Which
will be used in preference of the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute if that exists as
well.)</p>
<p>utf16 files decoded to unicode.</p>
<p>If <tt class="docutils literal"><span class="pre">BOM</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, but no encoding specified, then the utf8 BOM is
written out at the start of the file. (It will normally only be <tt class="docutils literal"><span class="pre">True</span></tt> if
the utf8 BOM was found when the file was read.)</p>
<p>Thanks to Aaron Bentley for help and testing on the unicode issues.</p>
<p>File paths are <em>not</em> converted to absolute paths, relative paths will
remain relative as the <tt class="docutils literal"><span class="pre">filename</span></tt> attribute.</p>
<p>Fixed bug where <tt class="docutils literal"><span class="pre">final_comment</span></tt> wasn't returned if <tt class="docutils literal"><span class="pre">write</span></tt> is returning
a list of lines.</p>
<p>Deprecated <tt class="docutils literal"><span class="pre">istrue</span></tt>, replaced it with <tt class="docutils literal"><span class="pre">as_bool</span></tt>.</p>
<p>Added <tt class="docutils literal"><span class="pre">as_int</span></tt> and <tt class="docutils literal"><span class="pre">as_float</span></tt>.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id98" id="version-4-1-0" name="version-4-1-0">19.5 2005/12/14 - Version 4.1.0</a></h2>
<p>Added <tt class="docutils literal"><span class="pre">merge</span></tt>, a recursive update.</p>
<p>Added <tt class="docutils literal"><span class="pre">preserve_errors</span></tt> to <tt class="docutils literal"><span class="pre">validate</span></tt> and the <tt class="docutils literal"><span class="pre">flatten_errors</span></tt>
example function.</p>
<p>Thanks to Matthew Brett for suggestions and helping me iron out bugs.</p>
<p>Fixed bug where a config file is <em>all</em> comment, the comment will now be
<tt class="docutils literal"><span class="pre">initial_comment</span></tt> rather than <tt class="docutils literal"><span class="pre">final_comment</span></tt>.</p>
<p>Validation no longer done on the 'DEFAULT' section (only in the root level).
This allows interpolation in configspecs.</p>
<p>Also use the new list syntax in <a class="reference" href="#validate">validate</a> 0.2.1. (For configspecs).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id99" id="version-4-0-2" name="version-4-0-2">19.6 2005/12/02 - Version 4.0.2</a></h2>
<p>Fixed bug in <tt class="docutils literal"><span class="pre">create_empty</span></tt>. Thanks to Paul Jimenez for the report.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id100" id="version-4-0-1" name="version-4-0-1">19.7 2005/11/05 - Version 4.0.1</a></h2>
<p>Fixed bug in <tt class="docutils literal"><span class="pre">Section.walk</span></tt> when transforming names as well as values.</p>
<p>Added the <tt class="docutils literal"><span class="pre">istrue</span></tt> method. (Fetches the boolean equivalent of a string
value).</p>
<p>Fixed <tt class="docutils literal"><span class="pre">list_values=False</span></tt> - they are now only quoted/unquoted if they
are multiline values.</p>
<p>List values are written as <tt class="docutils literal"><span class="pre">item,</span> <span class="pre">item</span></tt> rather than <tt class="docutils literal"><span class="pre">item,item</span></tt>.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id101" id="version-4-0-0" name="version-4-0-0">19.8 2005/10/17 - Version 4.0.0</a></h2>
<p><strong>ConfigObj 4.0.0 Final</strong></p>
<p>Fixed bug in <tt class="docutils literal"><span class="pre">setdefault</span></tt>. When creating a new section with setdefault the
reference returned would be to the dictionary passed in <em>not</em> to the new
section. Bug fixed and behaviour documented.</p>
<p>Obscure typo/bug fixed in <tt class="docutils literal"><span class="pre">write</span></tt>. Wouldn't have affected anyone though.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id102" id="version-4-0-0-beta-5" name="version-4-0-0-beta-5">19.9 2005/09/09 - Version 4.0.0 beta 5</a></h2>
<p>Removed <tt class="docutils literal"><span class="pre">PositionError</span></tt>.</p>
<p>Allowed quotes around keys as documented.</p>
<p>Fixed bug with commas in comments. (matched as a list value)</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id103" id="version-4-0-0-beta-4" name="version-4-0-0-beta-4">19.10 2005/09/07 - Version 4.0.0 beta 4</a></h2>
<p>Fixed bug in <tt class="docutils literal"><span class="pre">__delitem__</span></tt>. Deleting an item no longer deletes the
<tt class="docutils literal"><span class="pre">inline_comments</span></tt> attribute.</p>
<p>Fixed bug in initialising ConfigObj from a ConfigObj.</p>
<p>Changed the mailing list address.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id104" id="version-4-0-0-beta-3" name="version-4-0-0-beta-3">19.11 2005/08/28 - Version 4.0.0 beta 3</a></h2>
<p>Interpolation is switched off before writing out files.</p>
<p>Fixed bug in handling <tt class="docutils literal"><span class="pre">StringIO</span></tt> instances. (Thanks to report from
"Gustavo Niemeyer" <<a class="reference" href="mailto:gustavo%40niemeyer.net">gustavo<span>@</span>niemeyer<span>.</span>net</a>>)</p>
<p>Moved the doctests from the <tt class="docutils literal"><span class="pre">__init__</span></tt> method to a separate function.
(For the sake of IDE calltips).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id105" id="version-4-0-0-beta-2" name="version-4-0-0-beta-2">19.12 2005/08/25 - Version 4.0.0 beta 2</a></h2>
<p>Amendments to <em>validate.py</em>.</p>
<p>Official release.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id106" id="version-4-0-0-beta-1" name="version-4-0-0-beta-1">19.13 2005/08/21 - Version 4.0.0 beta 1</a></h2>
<p>Reads nested subsections to any depth.</p>
<p>Multiline values.</p>
<p>Simplified options and methods.</p>
<p>New list syntax.</p>
<p>Faster, smaller, and better parser.</p>
<p>Validation greatly improved. Includes:</p>
<blockquote>
<ul class="simple">
<li>type conversion</li>
<li>default values</li>
<li>repeated sections</li>
</ul>
</blockquote>
<p>Improved error handling.</p>
<p>Plus lots of other improvements <img src="smilies/biggrin.gif" alt="Very Happy" height="15" width="15" /> .</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id107" id="version-3-0-0" name="version-3-0-0">19.14 2004/05/24 - Version 3.0.0</a></h2>
<p>Several incompatible changes: another major overhaul and change. (Lots of
improvements though).</p>
<p>Added support for standard config files with sections. This has an entirely
new interface: each section is a dictionary of values.</p>
<p>Changed the update method to be called writein: update clashes with a dict
method.</p>
<p>Made various attributes keyword arguments, added several.</p>
<p>Configspecs and orderlists have changed a great deal.</p>
<p>Removed support for adding dictionaries: use update instead.</p>
<p>Now subclasses a new class called caselessDict. This should add various
dictionary methods that could have caused errors before.</p>
<p>It also preserves the original casing of keywords when writing them back out.</p>
<p>Comments are also saved using a <tt class="docutils literal"><span class="pre">caselessDict</span></tt>.</p>
<p>Using a non-string key will now raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> rather than converting
the key.</p>
<p>Added an exceptions keyword for <em>much</em> better handling of errors.</p>
<p>Made <tt class="docutils literal"><span class="pre">creatempty=False</span></tt> the default.</p>
<p>Now checks indict <em>and</em> any keyword args. Keyword args take precedence over
indict.</p>
<p><tt class="docutils literal"><span class="pre">'</span> <span class="pre">',</span> <span class="pre">':',</span> <span class="pre">'=',</span> <span class="pre">','</span></tt> and <tt class="docutils literal"><span class="pre">'\t'</span></tt> are now all valid dividers where the
keyword is unquoted.</p>
<p>ConfigObj now does no type checking against configspec when you set items.</p>
<p>delete and add methods removed (they were unnecessary).</p>
<p>Docs rewritten to include all this gumph and more; actually ConfigObj is
<em>really</em> easy to use.</p>
<p>Support for stdout was removed.</p>
<p>A few new methods added.</p>
<p>Charmap is now incorporated into ConfigObj.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id108" id="version-2-0-0-beta" name="version-2-0-0-beta">19.15 2004/03/14 - Version 2.0.0 beta</a></h2>
<p>Re-written it to subclass dict. My first forays into inheritance and operator
overloading.</p>
<p>The config object now behaves like a dictionary.</p>
<dl class="docutils">
<dt>I've completely broken the interface, but I don't think anyone was really</dt>
<dd>using it anyway.</dd>
</dl>
<p>This new version is much more 'classy' <img src="smilies/wink.gif" alt="Wink" height="15" width="15" /> </p>
<p>It will also read straight from/to a filename and completely parse a config
file without you <em>having</em> to supply a config spec.</p>
<p>Uses listparse, so can handle nested list items as values.</p>
<p>No longer has getval and setval methods: use normal dictionary methods, or add
and delete.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id109" id="version-1-0-5" name="version-1-0-5">19.16 2004/01/29 - Version 1.0.5</a></h2>
<p>Version 1.0.5 has a couple of bugfixes as well as a couple of useful additions
over previous versions.</p>
<p>Since 1.0.0 the buildconfig function has been moved into this distribution,
and the methods reset, verify, getval and setval have been added.</p>
<p>A couple of bugs have been fixed.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id110" id="origins" name="origins">19.17 Origins</a></h2>
<p>ConfigObj originated in a set of functions for reading config files in the
<a class="reference" href="http://www.voidspace.org.uk/atlantibots/">atlantibots</a> project. The original
functions were written by Rob McNeur.</p>
</div>
</div>
<hr class="docutils" />
<div class="section">
<h1><a class="toc-backref" href="#id111" id="footnotes" name="footnotes">20 Footnotes</a></h1>
<table class="docutils footnote" frame="void" id="id18" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1" name="id18">[1]</a></td><td>315 of them, at the time of writing.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id19" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2" name="id19">[2]</a></td><td>And if you discover any bugs, let us know. We'll fix them quickly.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id20" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4" name="id20">[3]</a></td><td>If you specify a filename that doesn't exist, ConfigObj will assume you
are creating a new one. See the <em>create_empty</em> and <em>file_error</em> <a class="reference" href="#options">options</a>.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id21" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id5" name="id21">[4]</a></td><td>They can be byte strings (<em>ordinary</em> strings) or Unicode.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id22" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id6" name="id22">[5]</a></td><td>Except we don't support the RFC822 style line continuations, nor ':' as
a divider.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id23" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id7" name="id23">[6]</a></td><td>This is a change in ConfigObj 4.2.0. Note that ConfigObj doesn't call
the seek method of any file like object you pass in. You may want to call
<tt class="docutils literal"><span class="pre">file_object.seek(0)</span></tt> yourself, first.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id24" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id8" name="id24">[7]</a></td><td><p class="first">A side effect of this is that it enables you to copy a ConfigObj :</p>
<div class="pysrc"><span class="pycomment"># only copies members<br />
</span><span class="pycomment"># not attributes/comments<br />
</span><span class="pytext">config2</span> <span class="pyoperator">=</span> <span class="pytext">ConfigObj</span><span class="pyoperator">(</span><span class="pytext">config1</span><span class="pyoperator">)</span><span class="pytext"></span></div><p class="last">The order of values and sections will not be preserved, though.</p>
</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id25" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id9" name="id25">[8]</a></td><td>Other than lists of strings.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id26" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id10" name="id26">[9]</a></td><td>The exception is if it detects a <tt class="docutils literal"><span class="pre">UTF16</span></tt> encoded file which it
must decode before parsing.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id27" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id11" name="id27">[10]</a></td><td>The method signature in the API docs will show that this method takes
two arguments. The second is the section to be written. This is because the
<tt class="docutils literal"><span class="pre">write</span></tt> method is called recursively.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id28" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id12" name="id28">[11]</a></td><td>The dict method doesn't actually use the deepcopy mechanism. This means
if you add nested lists (etc) to your ConfigObj, then the dictionary
returned by dict may contain some references. For all <em>normal</em> ConfigObjs
it will return a deepcopy.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id29" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id13" name="id29">[12]</a></td><td>Passing <tt class="docutils literal"><span class="pre">(section,</span> <span class="pre">key)</span></tt> rather than <tt class="docutils literal"><span class="pre">(value,</span> <span class="pre">key)</span></tt> allows you to
change the value by setting <tt class="docutils literal"><span class="pre">section[key]</span> <span class="pre">=</span> <span class="pre">newval</span></tt>. It also gives you
access to the <em>rename</em> method of the section.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id30" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id15" name="id30">[13]</a></td><td>Minimum required version of <em>validate.py</em> 0.2.0 .</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id31" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id17" name="id31">[14]</a></td><td>It also makes ConfigObj a lot simpler to <em>use</em>.</td></tr>
</tbody>
</table>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Rendering this document with docutils also needs the
textmacros module and the PySrc CSS stuff. See
<a class="reference" href="http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml">http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml</a></p>
</div>
<div align="center">
<p>
<a href="http://www.python.org">
<img src="images/new_python.gif" width="100" height="103" border="0"
alt="Powered by Python" />
</a>
<a href="http://sourceforge.net">
<img src="http://sourceforge.net/sflogo.php?group_id=123265&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
</a>
<a href="http://www.opensource.org">
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0"
alt="Certified Open Source"/>
</a>
</p>
<p>
<a href="http://www.voidspace.org.uk/python/index.shtml">
<img src="images/pythonbanner.gif" width="468" height="60"
alt="Python on Voidspace" border="0" />
</a>
</p>
<p>
<a href="http://sourceforge.net/donate/index.php?group_id=123265">
<img src="http://images.sourceforge.net/images/project-support.jpg" width="88" height="32" border="0" alt="Support This Project" />
</a>
</p>
<p>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-203625-1";
urchinTracker();
</script>
</p>
</div></div>
</div>
<div class="footer">
<hr class="footer" />
<a class="reference" href="configobj.txt">View document source</a>.
Generated on: 2006-06-04 20:37 UTC.
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div>
</body>
</html>
|