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