annotate Lib/ConfigParser.py @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents 34d2b50eff2e
children 4dbc75c189bc
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
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
36 has_section(section)
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
37 return whether the given section exists
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
38
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
39 options(section)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
40 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
41
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
42 has_option(section, option)
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
43 return whether the given section has the given option
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
44
10037
f132ecfdb285 From: Mike Orr <mso@oz.net>
Guido van Rossum <guido@python.org>
parents: 9961
diff changeset
45 read(filenames)
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
46 read and parse the list of named configuration files, given by
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
47 name. A single filename is also allowed. Non-existing files
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
48 are ignored.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
49
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
50 readfp(fp, filename=None)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
51 read and parse one configuration file, given as a file object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
52 The filename defaults to fp.name; it is only used in error
11381
4ad14e4dc7e5 fixed a typo in a docstring, and slightly expanded the module
Barry Warsaw <barry@python.org>
parents: 11370
diff changeset
53 messages (if fp has no `name' attribute, the string `<???>' is used).
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
54
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
55 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
56 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
57 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
58 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
59 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
60 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
61
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
62 getint(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
63 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
64
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
65 getfloat(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
66 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
67
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
68 getboolean(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
69 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
70 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
71 """
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
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
73 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
74 import string
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
75 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
76
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
77 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
78
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
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 # 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
82 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
83 def __init__(self, msg=''):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
84 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
85 def __repr__(self):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
86 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
87
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
88 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
89 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
90 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
91 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
92
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
93 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
94 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
95 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
96 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
97
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
98 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
99 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
100 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
101 (option, section))
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
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
105 class InterpolationError(Error):
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
106 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
107 Error.__init__(self,
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
108 "Bad value substitution:\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
109 "\tsection: [%s]\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
110 "\toption : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
111 "\tkey : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
112 "\trawval : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
113 % (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
114 self.reference = reference
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
115 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
116 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
117
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
118 class MissingSectionHeaderError(Error):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
119 def __init__(self, filename, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
120 Error.__init__(
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
121 self,
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
122 'File contains no section headers.\nfile: %s, line: %d\n%s' %
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
123 (filename, lineno, line))
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
124 self.filename = filename
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
125 self.lineno = lineno
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
126 self.line = line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
127
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
128 class ParsingError(Error):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
129 def __init__(self, filename):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
130 Error.__init__(self, 'File contains parsing errors: %s' % filename)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
131 self.filename = filename
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
132 self.errors = []
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
133
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
134 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
135 self.errors.append((lineno, line))
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
136 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
137
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
138
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
139
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
140 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
141 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
142 self.__sections = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
143 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
144 self.__defaults = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
145 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
146 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
147
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
148 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
149 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
150
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
151 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
152 """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
153 # 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
154 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
155
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
156 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
157 """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
158
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
159 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
160 already exists.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
161 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
162 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
163 raise DuplicateSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
164 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
165
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
166 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
167 """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
168
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
169 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
170 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
171 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
172
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
173 def options(self, section):
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
174 """Return a list of option names for the given section name."""
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
175 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
176 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
177 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
178 raise NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
179 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
180 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
181
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
182 def has_option(self, section, option):
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
183 """Return whether the given section has the given option."""
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
184 try:
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
185 opts = self.__sections[section]
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
186 except KeyError:
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
187 raise NoSectionError(section)
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
188 return opts.has_key(option)
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
189
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
190 def read(self, filenames):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
191 """Read and parse a filename or a list of filenames.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
192
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
193 Files that cannot be opened are silently ignored; this is
11381
4ad14e4dc7e5 fixed a typo in a docstring, and slightly expanded the module
Barry Warsaw <barry@python.org>
parents: 11370
diff changeset
194 designed so that you can specify a list of potential
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
195 configuration file locations (e.g. current directory, user's
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
196 home directory, systemwide directory), and all existing
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
197 configuration files in the list will be read. A single
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
198 filename may also be given.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
199 """
12632
34d2b50eff2e ConfigParser.read():
Fred Drake <fdrake@acm.org>
parents: 11908
diff changeset
200 if type(filenames) in [type(''), type(u'')]:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
201 filenames = [filenames]
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
202 for filename in filenames:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
203 try:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
204 fp = open(filename)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
205 except IOError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
206 continue
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
207 self.__read(fp, filename)
11368
b9bbe88d9554 ConfigParser.read(): Don't mask IOError exceptions.
Fred Drake <fdrake@acm.org>
parents: 11007
diff changeset
208 fp.close()
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
209
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
210 def readfp(self, fp, filename=None):
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
211 """Like read() but the argument must be a file-like object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
212
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
213 The `fp' argument must have a `readline' method. Optional
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
214 second argument is the `filename', which if not given, is
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
215 taken from fp.name. If fp has no `name' attribute, `<???>' is
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
216 used.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
217
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
218 """
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
219 if filename is None:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
220 try:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
221 filename = fp.name
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
222 except AttributeError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
223 filename = '<???>'
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
224 self.__read(fp, filename)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
225
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
226 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
227 """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
228
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
229 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
230 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
231 `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
232 `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
233 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
234
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
235 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
236 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
237 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
238 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
239 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
240 if section == DEFAULTSECT:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
241 sectdict = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
242 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
243 raise NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
244 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
245 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
246 # 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
247 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
248 d.update(vars)
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
249 option = self.optionxform(option)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
250 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
251 rawval = d[option]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
252 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
253 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
254 # 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
255 if raw:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
256 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
257
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
258 value = rawval # Make it a pretty variable name
10145
8284927025be Fix by Chris Petrilli (to his own code) to limit the number of
Guido van Rossum <guido@python.org>
parents: 10037
diff changeset
259 depth = 0
8284927025be Fix by Chris Petrilli (to his own code) to limit the number of
Guido van Rossum <guido@python.org>
parents: 10037
diff changeset
260 while depth < 10: # Loop through this until it's done
8284927025be Fix by Chris Petrilli (to his own code) to limit the number of
Guido van Rossum <guido@python.org>
parents: 10037
diff changeset
261 depth = depth + 1
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
262 if string.find(value, "%(") >= 0:
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
263 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
264 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
265 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
266 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
267 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
268 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
269
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
270 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
271 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
272
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
273 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
274 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
275
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
276 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
277 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
278
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
279 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
280 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
281 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
282 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
283 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
284 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
285
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
286 def optionxform(self, optionstr):
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
287 return string.lower(optionstr)
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
288
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
289 #
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
290 # Regular expressions for parsing section headers and options. Note a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
291 # slight semantic change from the previous version, because of the use
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
292 # of \w, _ is allowed in section header names.
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
293 SECTCRE = re.compile(
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
294 r'\[' # [
11875
286303f00546 Fix comments relating to the specific regexs used to parse section and
Fred Drake <fdrake@acm.org>
parents: 11873
diff changeset
295 r'(?P<header>[-\w_.*,(){}]+)' # a lot of stuff found by IvL
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
296 r'\]' # ]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
297 )
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
298 OPTCRE = re.compile(
11875
286303f00546 Fix comments relating to the specific regexs used to parse section and
Fred Drake <fdrake@acm.org>
parents: 11873
diff changeset
299 r'(?P<option>[-\w_.*,(){}]+)' # a lot of stuff found by IvL
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
300 r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
301 # followed by separator
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
302 # (either : or =), followed
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
303 # by any # space/tab
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
304 r'(?P<value>.*)$' # everything up to eol
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
305 )
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
306
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
307 def __read(self, fp, fpname):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
308 """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
309
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
310 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
311 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
312 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
313 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
314 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
315 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
316 """
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
317 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
318 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
319 lineno = 0
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
320 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
321 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
322 line = fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
323 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
324 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
325 lineno = lineno + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
326 # 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
327 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
328 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
329 if string.lower(string.split(line)[0]) == 'rem' \
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
330 and line[0] in "rR": # no leading whitespace
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
331 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
332 # continuation line?
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
333 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
334 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
335 if value:
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
336 cursect[optname] = cursect[optname] + '\n ' + value
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
337 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
338 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
339 # is it a section header?
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
340 mo = self.SECTCRE.match(line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
341 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
342 sectname = mo.group('header')
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
343 if self.__sections.has_key(sectname):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
344 cursect = self.__sections[sectname]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
345 elif sectname == DEFAULTSECT:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
346 cursect = self.__defaults
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
347 else:
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
348 cursect = {'__name__': sectname}
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
349 self.__sections[sectname] = cursect
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
350 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
351 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
352 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
353 elif cursect is None:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
354 raise MissingSectionHeaderError(fpname, lineno, `line`)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
355 # 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
356 else:
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
357 mo = self.OPTCRE.match(line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
358 if mo:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
359 optname, vi, optval = mo.group('option', 'vi', 'value')
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
360 optname = string.lower(optname)
11908
b975a056deb6 allow comments beginning with ; in key: value as well as key = value
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11875
diff changeset
361 if vi in ('=', ':') and ';' in optval:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
362 # ';' is a comment delimiter only if it follows
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
363 # a spacing character
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
364 pos = string.find(optval, ';')
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
365 if pos and optval[pos-1] in string.whitespace:
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
366 optval = optval[:pos]
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
367 optval = string.strip(optval)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
368 # allow empty values
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
369 if optval == '""':
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
370 optval = ''
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
371 cursect[optname] = optval
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
372 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
373 # a non-fatal parsing error occurred. set up the
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
374 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
375 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
376 # list of all bogus lines
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
377 if not e:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
378 e = ParsingError(fpname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
379 e.append(lineno, `line`)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
380 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
381 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
382 raise e