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