|
| 1 | +\section{\module{ConfigParser} --- |
| 2 | + Configuration file parser.} |
| 3 | + |
| 4 | +\declaremodule{standard}{ConfigParser} |
| 5 | + |
| 6 | +\modulesynopsis{Configuration file parser.} |
| 7 | +\sectionauthor{Christopher G. Petrilli}{ [email protected]} |
| 8 | + |
| 9 | +This module defines the class \class{ConfigParser}. The |
| 10 | +\class{ConfigParser} class implements a basic configuration file |
| 11 | +parser language which provides a structure similar to what you would |
| 12 | +find on Microsoft Windows INI files. You can use this to write Python |
| 13 | +programs which can be customized by end users easily. |
| 14 | + |
| 15 | +The configuration file consists of sections, lead by a |
| 16 | +\samp{[section]} header and followed by \samp{name: value} entries, |
| 17 | +with continuations in the style of \rfc{959}. The optional values |
| 18 | +can contain format strings which refer to other values in the same |
| 19 | +section, or values in a special \code{DEFAULT} section. Additional |
| 20 | +defaults can provided upon instantiation of the class. |
| 21 | + |
| 22 | +For example: |
| 23 | + |
| 24 | +\begin{verbatim} |
| 25 | +foodir: %(dir)s/whatever |
| 26 | +\end{verbatim} |
| 27 | + |
| 28 | +would resolve the \samp{\%(dir)s} to the value of dir. All reference |
| 29 | +expansions are done late, on demand. |
| 30 | + |
| 31 | +Intrinsic defaults can be specified by passing them into the |
| 32 | +\class{ConfigParser} constructor as a dictionary. |
| 33 | + |
| 34 | +\begin{classdesc}{ConfigParser}{\optional{defaults}} |
| 35 | +Return a new instance of the \class{ConfigParser} class. When |
| 36 | +\var{defaults} is given, it is initialized into the dictionairy of |
| 37 | +intrinsic defaults. They keys must be strings, and the values must be |
| 38 | +appropriate for the \samp{\%()s} string interpolation. Note that |
| 39 | +\var{__name__} is always an intrinsic default; it's value is the |
| 40 | +section name. |
| 41 | +\end{classdesc} |
| 42 | + |
| 43 | +\begin{excdesc}{NoSectionError} |
| 44 | +Exception raised when a specified section is not found. |
| 45 | +\end{excdesc} |
| 46 | + |
| 47 | +\begin{excdesc}{DuplicateSectionError} |
| 48 | +Exception raised when mutliple sections with the same name are found. |
| 49 | +\end{excdesc} |
| 50 | + |
| 51 | +\begin{excdesc}{NoOptionError} |
| 52 | +Exception raised when a specified option is not found in the specified |
| 53 | +section. |
| 54 | +\end{excdesc} |
| 55 | + |
| 56 | +\begin{excdesc}{InterpolationError} |
| 57 | +Exception raised when problems occur performing string interpolation. |
| 58 | +\end{excdesc} |
| 59 | + |
| 60 | +\begin{excdesc}{MissingSectionHeaderError} |
| 61 | +Exception raised when attempting to parse a file which has no section |
| 62 | +headers. |
| 63 | +\end{excdesc} |
| 64 | + |
| 65 | +\begin{excdesc}{ParsingError} |
| 66 | +Exception raised when errors occur attempting to parse a file. |
| 67 | +\end{excdesc} |
| 68 | + |
| 69 | +\subsection{ConfigParser Objects \label{ConfigParser-objects}} |
| 70 | + |
| 71 | +\class{ConfigParser} instances have the following methods: |
| 72 | + |
| 73 | +\begin{methoddesc}{defaults}{} |
| 74 | +Return a dictionairy containing the instance-wide defaults. |
| 75 | +\end{methoddesc} |
| 76 | + |
| 77 | +\begin{methoddesc}{sections}{} |
| 78 | +Return a list of the sections available. |
| 79 | +\end{methoddesc} |
| 80 | + |
| 81 | +\begin{methoddesc}{has_section}{section} |
| 82 | +Indicates whether the named section is present in the |
| 83 | +configuration. The \code{DEFAULT} section is not acknowledged. |
| 84 | +\end{methoddesc} |
| 85 | + |
| 86 | +\begin{methoddesc}{options}{section} |
| 87 | +Returns a list of options available in the specified \var{section}. |
| 88 | +\end{methoddesc} |
| 89 | + |
| 90 | +\begin{methoddesc}{read}{filenames} |
| 91 | +Read and parse a list of filenames. |
| 92 | +\end{methoddesc} |
| 93 | + |
| 94 | +\begin{methoddesc}{get}{section, option\optional{, raw}} |
| 95 | +Get an \var{option} value for the provided \var{section}. All the |
| 96 | +\samp{\%} interpolations are expanded in the return values, based on |
| 97 | +the defaults passed into the constructor, unless the \var{raw} |
| 98 | +argument is true. |
| 99 | +\end{methoddesc} |
| 100 | + |
| 101 | +\begin{methoddesc}{getint}{section, option} |
| 102 | +A convenience method which coerces the \var{option} in the specified |
| 103 | +\var{section} to an integer. |
| 104 | +\end{methoddesc} |
| 105 | + |
| 106 | +\begin{methoddesc}{getfloat}{section, option} |
| 107 | +A convenience method which coerces the \var{option} in the specified |
| 108 | +\var{section} to a floating point number. |
| 109 | +\end{methoddesc} |
| 110 | + |
| 111 | +\begin{methoddesc}{getboolean}{section, option} |
| 112 | +A convenience method which coerces the \var{option} in the specified |
| 113 | +\var{section} to a boolean value. Note that the only accepted values |
| 114 | +for the option are \code{0} and \code{1}, any others will raise |
| 115 | +\exception{ValueError}. |
| 116 | +\end{methoddesc} |
0 commit comments