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 |
| 42 | \var{__name__} is always an intrinsic default; it's value is the |
| 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 | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 73 | \subsection{ConfigParser Objects \label{ConfigParser-objects}} |
| 74 | |
| 75 | \class{ConfigParser} instances have the following methods: |
| 76 | |
| 77 | \begin{methoddesc}{defaults}{} |
| 78 | Return a dictionairy containing the instance-wide defaults. |
| 79 | \end{methoddesc} |
| 80 | |
| 81 | \begin{methoddesc}{sections}{} |
| 82 | Return a list of the sections available. |
| 83 | \end{methoddesc} |
| 84 | |
| 85 | \begin{methoddesc}{has_section}{section} |
| 86 | Indicates whether the named section is present in the |
| 87 | configuration. The \code{DEFAULT} section is not acknowledged. |
| 88 | \end{methoddesc} |
| 89 | |
| 90 | \begin{methoddesc}{options}{section} |
| 91 | Returns a list of options available in the specified \var{section}. |
| 92 | \end{methoddesc} |
| 93 | |
| 94 | \begin{methoddesc}{read}{filenames} |
| 95 | Read and parse a list of filenames. |
| 96 | \end{methoddesc} |
| 97 | |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 98 | \begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 99 | Get an \var{option} value for the provided \var{section}. All the |
Fred Drake | 4747f7f | 1999-04-21 16:41:40 +0000 | [diff] [blame^] | 100 | \character{\%} interpolations are expanded in the return values, based on |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 101 | the defaults passed into the constructor, as well as the options |
| 102 | \var{vars} provided, unless the \var{raw} argument is true. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 103 | \end{methoddesc} |
| 104 | |
| 105 | \begin{methoddesc}{getint}{section, option} |
| 106 | A convenience method which coerces the \var{option} in the specified |
| 107 | \var{section} to an integer. |
| 108 | \end{methoddesc} |
| 109 | |
| 110 | \begin{methoddesc}{getfloat}{section, option} |
| 111 | A convenience method which coerces the \var{option} in the specified |
| 112 | \var{section} to a floating point number. |
| 113 | \end{methoddesc} |
| 114 | |
| 115 | \begin{methoddesc}{getboolean}{section, option} |
| 116 | A convenience method which coerces the \var{option} in the specified |
| 117 | \var{section} to a boolean value. Note that the only accepted values |
| 118 | for the option are \code{0} and \code{1}, any others will raise |
| 119 | \exception{ValueError}. |
| 120 | \end{methoddesc} |