Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 1 | \section{\module{ConfigParser} --- |
| 2 | Configuration file parser.} |
| 3 | |
| 4 | \declaremodule{standard}{ConfigParser} |
| 5 | |
| 6 | \modulesynopsis{Configuration file parser.} |
| 7 | \sectionauthor{Christopher G. Petrilli}{petrilli@amber.org} |
| 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, |
Fred Drake | 00a3a65 | 1999-02-22 17:12:42 +0000 | [diff] [blame^] | 17 | with continuations in the style of \rfc{822}. The optional values |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 18 | can contain format strings which refer to other values in the same |
| 19 | section, or values in a special \code{DEFAULT} section. Additional |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 20 | defaults can be provided upon initialization and retrieval. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 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 |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 32 | \class{ConfigParser} constructor as a dictionary. Additional defaults |
| 33 | may be passed into the \method{get} method which will override all |
| 34 | others. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 35 | |
| 36 | \begin{classdesc}{ConfigParser}{\optional{defaults}} |
| 37 | Return a new instance of the \class{ConfigParser} class. When |
| 38 | \var{defaults} is given, it is initialized into the dictionairy of |
| 39 | intrinsic defaults. They keys must be strings, and the values must be |
| 40 | appropriate for the \samp{\%()s} string interpolation. Note that |
| 41 | \var{__name__} is always an intrinsic default; it's value is the |
| 42 | section name. |
| 43 | \end{classdesc} |
| 44 | |
| 45 | \begin{excdesc}{NoSectionError} |
| 46 | Exception raised when a specified section is not found. |
| 47 | \end{excdesc} |
| 48 | |
| 49 | \begin{excdesc}{DuplicateSectionError} |
| 50 | Exception raised when mutliple sections with the same name are found. |
| 51 | \end{excdesc} |
| 52 | |
| 53 | \begin{excdesc}{NoOptionError} |
| 54 | Exception raised when a specified option is not found in the specified |
| 55 | section. |
| 56 | \end{excdesc} |
| 57 | |
| 58 | \begin{excdesc}{InterpolationError} |
| 59 | Exception raised when problems occur performing string interpolation. |
| 60 | \end{excdesc} |
| 61 | |
| 62 | \begin{excdesc}{MissingSectionHeaderError} |
| 63 | Exception raised when attempting to parse a file which has no section |
| 64 | headers. |
| 65 | \end{excdesc} |
| 66 | |
| 67 | \begin{excdesc}{ParsingError} |
| 68 | Exception raised when errors occur attempting to parse a file. |
| 69 | \end{excdesc} |
| 70 | |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 71 | |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 72 | \subsection{ConfigParser Objects \label{ConfigParser-objects}} |
| 73 | |
| 74 | \class{ConfigParser} instances have the following methods: |
| 75 | |
| 76 | \begin{methoddesc}{defaults}{} |
| 77 | Return a dictionairy containing the instance-wide defaults. |
| 78 | \end{methoddesc} |
| 79 | |
| 80 | \begin{methoddesc}{sections}{} |
| 81 | Return a list of the sections available. |
| 82 | \end{methoddesc} |
| 83 | |
| 84 | \begin{methoddesc}{has_section}{section} |
| 85 | Indicates whether the named section is present in the |
| 86 | configuration. The \code{DEFAULT} section is not acknowledged. |
| 87 | \end{methoddesc} |
| 88 | |
| 89 | \begin{methoddesc}{options}{section} |
| 90 | Returns a list of options available in the specified \var{section}. |
| 91 | \end{methoddesc} |
| 92 | |
| 93 | \begin{methoddesc}{read}{filenames} |
| 94 | Read and parse a list of filenames. |
| 95 | \end{methoddesc} |
| 96 | |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 97 | \begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 98 | Get an \var{option} value for the provided \var{section}. All the |
| 99 | \samp{\%} interpolations are expanded in the return values, based on |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 100 | the defaults passed into the constructor, as well as the options |
| 101 | \var{vars} provided, unless the \var{raw} argument is true. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 102 | \end{methoddesc} |
| 103 | |
| 104 | \begin{methoddesc}{getint}{section, option} |
| 105 | A convenience method which coerces the \var{option} in the specified |
| 106 | \var{section} to an integer. |
| 107 | \end{methoddesc} |
| 108 | |
| 109 | \begin{methoddesc}{getfloat}{section, option} |
| 110 | A convenience method which coerces the \var{option} in the specified |
| 111 | \var{section} to a floating point number. |
| 112 | \end{methoddesc} |
| 113 | |
| 114 | \begin{methoddesc}{getboolean}{section, option} |
| 115 | A convenience method which coerces the \var{option} in the specified |
| 116 | \var{section} to a boolean value. Note that the only accepted values |
| 117 | for the option are \code{0} and \code{1}, any others will raise |
| 118 | \exception{ValueError}. |
| 119 | \end{methoddesc} |