annotate Lib/ConfigParser.py @ 10123:bc287c6cfa23 legacy-trunk

Restructured a bit.
author Guido van Rossum <guido@python.org>
date Mon, 08 Feb 1999 22:27:18 +0000
parents f132ecfdb285
children 8284927025be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Configuration file parser.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 A setup file consists of sections, lead by a "[section]" header,
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 and followed by "name: value" entries, with continuations and such in
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
5 the style of RFC 822.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
7 The option values can contain format strings which refer to other values in
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
8 the same section, or values in a special [DEFAULT] section.
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
9
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 For example:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 something: %(dir)s/whatever
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 would resolve the "%(dir)s" to the value of dir. All reference
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 expansions are done late, on demand.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Intrinsic defaults can be specified by passing them into the
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 ConfigParser constructor as a dictionary.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 class:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 ConfigParser -- responsible for for parsing a list of
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 configuration files, and managing the parsed database.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 methods:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
27 __init__(defaults=None)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
28 create the parser and specify a dictionary of intrinsic defaults. The
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
29 keys must be strings, the values must be appropriate for %()s string
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
30 interpolation. Note that `__name__' is always an intrinsic default;
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
31 it's value is the section's name.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
33 sections()
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
34 return all the configuration section names, sans DEFAULT
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
36 options(section)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
37 return list of configuration options for the named section
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
38
10037
f132ecfdb285 From: Mike Orr <mso@oz.net>
Guido van Rossum <guido@python.org>
parents: 9961
diff changeset
39 read(filenames)
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
40 read and parse the list of named configuration files
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
42 get(section, option, raw=0, vars=None)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
43 return a string value for the named option. All % interpolations are
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
44 expanded in the return values, based on the defaults passed into the
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
45 constructor and the DEFAULT section. Additional substitutions may be
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
46 provided using the `vars' argument, which must be a dictionary whose
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
47 contents override any pre-existing defaults.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
49 getint(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
50 like get(), but convert value to an integer
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
52 getfloat(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
53 like get(), but convert value to a float
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
54
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
55 getboolean(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
56 like get(), but convert value to a boolean (currently defined as 0 or
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
57 1, only)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 """
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 import sys
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 import string
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
62 import re
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 DEFAULTSECT = "DEFAULT"
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # exception classes
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 class Error:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 def __init__(self, msg=''):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
71 self._msg = msg
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def __repr__(self):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
73 return self._msg
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 class NoSectionError(Error):
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def __init__(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
77 Error.__init__(self, 'No section: %s' % section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
78 self.section = section
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 class DuplicateSectionError(Error):
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def __init__(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
82 Error.__init__(self, "Section %s already exists" % section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
83 self.section = section
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 class NoOptionError(Error):
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def __init__(self, option, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
87 Error.__init__(self, "No option `%s' in section: %s" %
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
88 (option, section))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
89 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
90 self.section = section
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 class InterpolationError(Error):
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
93 def __init__(self, reference, option, section, rawval):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
94 Error.__init__(self,
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
95 "Bad value substitution:\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
96 "\tsection: [%s]\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
97 "\toption : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
98 "\tkey : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
99 "\trawval : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
100 % (section, option, reference, rawval))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
101 self.reference = reference
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
102 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
103 self.section = section
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
105 class MissingSectionHeaderError(Error):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
106 def __init__(self, filename, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
107 Error.__init__(
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
108 self,
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
109 'File contains no section headers.\nfile: %s, line: %d\n%s' %
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
110 (filename, lineno, line))
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
111 self.filename = filename
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
112 self.lineno = lineno
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
113 self.line = line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
114
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
115 class ParsingError(Error):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
116 def __init__(self, filename):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
117 Error.__init__(self, 'File contains parsing errors: %s' % filename)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
118 self.filename = filename
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
119 self.errors = []
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
120
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
121 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
122 self.errors.append((lineno, line))
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
123 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
124
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 class ConfigParser:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def __init__(self, defaults=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
129 self.__sections = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
130 if defaults is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
131 self.__defaults = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
132 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
133 self.__defaults = defaults
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def defaults(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
136 return self.__defaults
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 def sections(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
139 """Return a list of section names, excluding [DEFAULT]"""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
140 # self.__sections will never have [DEFAULT] in it
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
141 return self.__sections.keys()
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def add_section(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
144 """Create a new section in the configuration.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
146 Raise DuplicateSectionError if a section by the specified name
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
147 already exists.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
148 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
149 if self.__sections.has_key(section):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
150 raise DuplicateSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
151 self.__sections[section] = {}
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def has_section(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
154 """Indicate whether the named section is present in the configuration.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
156 The DEFAULT section is not acknowledged.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
157 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
158 return self.__sections.has_key(section)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 def options(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
161 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
162 opts = self.__sections[section].copy()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
163 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
164 raise NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
165 opts.update(self.__defaults)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
166 return opts.keys()
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def read(self, filenames):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
169 """Read and parse a list of filenames."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
170 if type(filenames) is type(''):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
171 filenames = [filenames]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
172 for file in filenames:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
173 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
174 fp = open(file, 'r')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
175 self.__read(fp)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
176 except IOError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
177 pass
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
9959
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
179 def get(self, section, option, raw=0, vars=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
180 """Get an option value for a given section.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
182 All % interpolations are expanded in the return values, based on the
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
183 defaults passed into the constructor, unless the optional argument
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
184 `raw' is true. Additional substitutions may be provided using the
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
185 `vars' argument, which must be a dictionary whose contents overrides
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
186 any pre-existing defaults.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
188 The section DEFAULT is special.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
189 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
190 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
191 sectdict = self.__sections[section].copy()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
192 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
193 if section == DEFAULTSECT:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
194 sectdict = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
195 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
196 raise NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
197 d = self.__defaults.copy()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
198 d.update(sectdict)
9959
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
199 # Update with the entry specific variables
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
200 if vars:
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
201 d.update(vars)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
202 option = string.lower(option)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
203 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
204 rawval = d[option]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
205 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
206 raise NoOptionError(option, section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
207 # do the string interpolation
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
208 if raw:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
209 return rawval
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
9959
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
211 value = rawval # Make it a pretty variable name
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
212 while 1: # Loop through this until it's done
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
213 if not string.find(value, "%("):
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
214 try:
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
215 value = value % d
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
216 except KeyError, key:
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
217 raise InterpolationError(key, option, section, rawval)
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
218 else:
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
219 return value
572e131ae369 Patch by Chris Petrilli (not really tested since I don't know this
Guido van Rossum <guido@python.org>
parents: 8853
diff changeset
220
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def __get(self, section, conv, option):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
222 return conv(self.get(section, option))
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 def getint(self, section, option):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
225 return self.__get(section, string.atoi, option)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 def getfloat(self, section, option):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
228 return self.__get(section, string.atof, option)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 def getboolean(self, section, option):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
231 v = self.get(section, option)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
232 val = string.atoi(v)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
233 if val not in (0, 1):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
234 raise ValueError, 'Not a boolean: %s' % v
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
235 return val
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
237 #
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
238 # Regular expressions for parsing section headers and options. Note a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
239 # slight semantic change from the previous version, because of the use
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
240 # of \w, _ is allowed in section header names.
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
241 __SECTCRE = re.compile(
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
242 r'\[' # [
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
243 r'(?P<header>[-\w]+)' # `-', `_' or any alphanum
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
244 r'\]' # ]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
245 )
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
246 __OPTCRE = re.compile(
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
247 r'(?P<option>[-.\w]+)' # - . _ alphanum
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
248 r'[ \t]*[:=][ \t]*' # any number of space/tab,
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
249 # followed by separator
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
250 # (either : or =), followed
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
251 # by any # space/tab
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
252 r'(?P<value>.*)$' # everything up to eol
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
253 )
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
254
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 def __read(self, fp):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
256 """Parse a sectioned setup file.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
258 The sections in setup file contains a title line at the top,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
259 indicated by a name in square brackets (`[]'), plus key/value
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
260 options lines, indicated by `name: value' format lines.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
261 Continuation are represented by an embedded newline then
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
262 leading whitespace. Blank lines, lines beginning with a '#',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
263 and just about everything else is ignored.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
264 """
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
265 cursect = None # None, or a dictionary
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
266 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
267 lineno = 0
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
268 e = None # None, or an exception
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
269 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
270 line = fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
271 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
272 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
273 lineno = lineno + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
274 # comment or blank line?
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
275 if string.strip(line) == '' or line[0] in '#;':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
276 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
277 if string.lower(string.split(line)[0]) == 'rem' \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
278 and line[0] == "r": # no leading whitespace
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
279 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
280 # continuation line?
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
281 if line[0] in ' \t' and cursect is not None and optname:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
282 value = string.strip(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
283 if value:
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
284 cursect[optname] = cursect[optname] + '\n ' + value
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
285 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
286 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
287 # is it a section header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
288 mo = self.__SECTCRE.match(line)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
289 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
290 sectname = mo.group('header')
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
291 if self.__sections.has_key(sectname):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
292 cursect = self.__sections[sectname]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
293 elif sectname == DEFAULTSECT:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
294 cursect = self.__defaults
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
295 else:
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
296 cursect = {'__name__': sectname}
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
297 self.__sections[sectname] = cursect
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
298 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
299 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
300 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
301 elif cursect is None:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
302 raise MissingSectionHeaderError(fp.name, lineno, `line`)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
303 # an option line?
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
304 else:
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
305 mo = self.__OPTCRE.match(line)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
306 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
307 optname, optval = mo.group('option', 'value')
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
308 optname = string.lower(optname)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
309 optval = string.strip(optval)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
310 # allow empty values
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
311 if optval == '""':
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
312 optval = ''
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
313 cursect[optname] = optval
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
314 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
315 # a non-fatal parsing error occurred. set up the
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
316 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
317 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
318 # list of all bogus lines
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
319 if not e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
320 e = ParsingError(fp.name)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
321 e.append(lineno, `line`)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
322 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
323 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
324 raise e