annotate Lib/ConfigParser.py @ 15164:8930ec3bc00a legacy-trunk

Added additional test cases for pulldom modifications.
author Lars Gustäbel <lars@gustaebel.de>
date Wed, 11 Oct 2000 22:36:00 +0000
parents 606a80834a86
children e98964bc2681
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
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
39 has_option(section, option)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
40 return whether the given option exists in the given section
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
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 options(section)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
43 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
44
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
45 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
46 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
47
10037
f132ecfdb285 From: Mike Orr <mso@oz.net>
Guido van Rossum <guido@python.org>
parents: 9961
diff changeset
48 read(filenames)
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
49 read and parse the list of named configuration files, given by
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
50 name. A single filename is also allowed. Non-existing files
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
51 are ignored.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
52
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
53 readfp(fp, filename=None)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
54 read and parse one configuration file, given as a file object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
55 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
56 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
57
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
58 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
59 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
60 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
61 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
62 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
63 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
64
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
65 getint(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 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
67
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
68 getfloat(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 float
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
70
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
71 getboolean(section, options)
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
72 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
73 1, only)
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
74
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
75 remove_section(section)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
76 remove the given file section and all its options
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
77
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
78 remove_option(section, option)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
79 remove the given option from the given section
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
80
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
81 set(section, option, value)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
82 set the given option
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
83
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
84 write(fp)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
85 write the configuration state in .ini format
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
86 """
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 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
89 import string
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
90 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
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 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
93
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
94 MAX_INTERPOLATION_DEPTH = 10
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
95
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
96
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 # 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
99 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
100 def __init__(self, msg=''):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
101 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
102 def __repr__(self):
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
103 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
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 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
106 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
107 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
108 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
109
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
110 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
111 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
112 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
113 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
114
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
115 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
116 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
117 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
118 (option, section))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
119 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
120 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
121
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
122 class InterpolationError(Error):
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
123 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
124 Error.__init__(self,
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
125 "Bad value substitution:\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
126 "\tsection: [%s]\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
127 "\toption : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
128 "\tkey : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
129 "\trawval : %s\n"
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
130 % (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
131 self.reference = reference
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
132 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
133 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
134
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
135 class InterpolationDepthError(Error):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
136 def __init__(self, option, section, rawval):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
137 Error.__init__(self,
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
138 "Value interpolation too deeply recursive:\n"
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
139 "\tsection: [%s]\n"
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
140 "\toption : %s\n"
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
141 "\trawval : %s\n"
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
142 % (section, option, rawval))
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
143 self.option = option
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
144 self.section = section
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
145
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
146 class ParsingError(Error):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
147 def __init__(self, filename):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
148 Error.__init__(self, 'File contains parsing errors: %s' % filename)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
149 self.filename = filename
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
150 self.errors = []
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
151
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
152 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
153 self.errors.append((lineno, line))
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
154 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
155
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
156 class MissingSectionHeaderError(ParsingError):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
157 def __init__(self, filename, lineno, line):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
158 Error.__init__(
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
159 self,
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
160 'File contains no section headers.\nfile: %s, line: %d\n%s' %
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
161 (filename, lineno, line))
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
162 self.filename = filename
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
163 self.lineno = lineno
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
164 self.line = line
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
165
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
166
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 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
169 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
170 self.__sections = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
171 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
172 self.__defaults = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
173 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
174 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
175
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
176 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
177 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
178
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
179 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
180 """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
181 # 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
182 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
183
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
184 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
185 """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
186
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
187 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
188 already exists.
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 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
191 raise DuplicateSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
192 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
193
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
194 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
195 """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
196
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
197 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
198 """
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
199 return section in self.sections()
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
200
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
201 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
202 """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
203 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
204 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
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 NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
207 opts.update(self.__defaults)
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
208 if opts.has_key('__name__'):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
209 del opts['__name__']
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
210 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
211
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
212 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
213 """Return whether the given section has the given option."""
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
214 return option in self.options(section)
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
215
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
216 def read(self, filenames):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
217 """Read and parse a filename or a list of filenames.
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 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
220 designed so that you can specify a list of potential
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
221 configuration file locations (e.g. current directory, user's
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
222 home directory, systemwide directory), and all existing
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
223 configuration files in the list will be read. A single
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
224 filename may also be given.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
225 """
12632
34d2b50eff2e ConfigParser.read():
Fred Drake <fdrake@acm.org>
parents: 11908
diff changeset
226 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
227 filenames = [filenames]
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
228 for filename in filenames:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
229 try:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
230 fp = open(filename)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
231 except IOError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
232 continue
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
233 self.__read(fp, filename)
11368
b9bbe88d9554 ConfigParser.read(): Don't mask IOError exceptions.
Fred Drake <fdrake@acm.org>
parents: 11007
diff changeset
234 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
235
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
236 def readfp(self, fp, filename=None):
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
237 """Like read() but the argument must be a file-like object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
238
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
239 The `fp' argument must have a `readline' method. Optional
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
240 second argument is the `filename', which if not given, is
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
241 taken from fp.name. If fp has no `name' attribute, `<???>' is
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
242 used.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
243
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
244 """
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
245 if filename is None:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
246 try:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
247 filename = fp.name
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
248 except AttributeError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
249 filename = '<???>'
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
250 self.__read(fp, filename)
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
251
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
252 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
253 """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
254
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
255 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
256 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
257 `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
258 `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
259 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
260
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
261 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
262 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
263 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
264 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
265 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
266 if section == DEFAULTSECT:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
267 sectdict = {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
268 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
269 raise NoSectionError(section)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
270 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
271 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
272 # 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
273 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
274 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
275 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
276 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
277 rawval = d[option]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
278 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
279 raise NoOptionError(option, section)
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
280
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
281 if raw:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
282 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
283
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
284 # do the string interpolation
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
285 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
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294 else:
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
295 break
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
296 if value.find("%(") >= 0:
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
297 raise InterpolationDepthError(option, section, rawval)
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
298 return value
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
299
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
300 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
301 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
302
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
303 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
304 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
305
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
306 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
307 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
308
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 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
310 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
311 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
312 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
313 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
314 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
315
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
316 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
317 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
318
13521
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
319 def has_option(self, section, option):
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
320 """Check for the existence of a given option in a given section."""
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
321 if not section or section == "DEFAULT":
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
322 return self.__defaults.has_key(option)
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
323 elif not self.has_section(section):
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
324 return 0
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
325 else:
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
326 return self.__sections[section].has_key(option)
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
327
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
328 def set(self, section, option, value):
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
329 """Set an option."""
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
330 if not section or section == "DEFAULT":
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
331 sectdict = self.__defaults
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
332 else:
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
333 try:
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
334 sectdict = self.__sections[section]
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
335 except KeyError:
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
336 raise NoSectionError(section)
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
337 sectdict[option] = value
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
338
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
339 def write(self, fp):
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
340 """Write an .ini-format representation of the configuration state."""
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
341 if self.__defaults:
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
342 fp.write("[DEFAULT]\n")
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
343 for (key, value) in self.__defaults.items():
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
344 fp.write("%s = %s\n" % (key, value))
13521
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
345 fp.write("\n")
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
346 for section in self.sections():
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
347 fp.write("[" + section + "]\n")
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
348 sectdict = self.__sections[section]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
349 for (key, value) in sectdict.items():
13521
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
350 if key == "__name__":
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
351 continue
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
352 fp.write("%s = %s\n" % (key, value))
13521
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
353 fp.write("\n")
4dbc75c189bc Give ConfigParser the capability to set as well as read options, and to write
Eric S. Raymond <esr@thyrsus.com>
parents: 12632
diff changeset
354
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
355 def remove_option(self, section, option):
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
356 """Remove an option."""
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
357 if not section or section == "DEFAULT":
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
358 sectdict = self.__defaults
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
359 else:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
360 try:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
361 sectdict = self.__sections[section]
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
362 except KeyError:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
363 raise NoSectionError(section)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
364 existed = sectdict.has_key(key)
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
365 if existed:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
366 del sectdict[key]
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
367 return existed
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
368
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
369 def remove_section(self, section):
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
370 """Remove a file section."""
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
371 if self.__sections.has_key(section):
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
372 del self.__sections[section]
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
373 return 1
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
374 else:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
375 return 0
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
376
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
377 #
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
378 # Regular expressions for parsing section headers and options. Note a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
379 # slight semantic change from the previous version, because of the use
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
380 # 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
381 SECTCRE = re.compile(
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
382 r'\[' # [
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
383 r'(?P<header>[-\w_.*,(){} ]+)' # a lot of stuff found by IvL
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
384 r'\]' # ]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
385 )
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
386 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
387 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
388 r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
389 # followed by separator
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
390 # (either : or =), followed
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
391 # by any # space/tab
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
392 r'(?P<value>.*)$' # everything up to eol
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
393 )
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
394
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
395 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
396 """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
397
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
398 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
399 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
400 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
401 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
402 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
403 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
404 """
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
405 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
406 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
407 lineno = 0
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
408 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
409 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
410 line = fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
411 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
412 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
413 lineno = lineno + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
414 # 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
415 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
416 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
417 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
418 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
419 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
420 # continuation line?
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
421 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
422 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
423 if value:
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
424 cursect[optname] = cursect[optname] + '\n ' + value
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
425 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
426 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
427 # 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
428 mo = self.SECTCRE.match(line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
429 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
430 sectname = mo.group('header')
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
431 if self.__sections.has_key(sectname):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
432 cursect = self.__sections[sectname]
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
433 elif sectname == DEFAULTSECT:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
434 cursect = self.__defaults
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
435 else:
8853
bbe9d72bccda Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents: 8614
diff changeset
436 cursect = {'__name__': sectname}
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
437 self.__sections[sectname] = cursect
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
438 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
439 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
440 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
441 elif cursect is None:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
442 raise MissingSectionHeaderError(fpname, lineno, `line`)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
443 # 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
444 else:
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
445 mo = self.OPTCRE.match(line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
446 if mo:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
447 optname, vi, optval = mo.group('option', 'vi', 'value')
11908
b975a056deb6 allow comments beginning with ; in key: value as well as key = value
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11875
diff changeset
448 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
449 # ';' 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
450 # a spacing character
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
451 pos = string.find(optval, ';')
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
452 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
453 optval = optval[:pos]
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
454 optval = string.strip(optval)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
455 # allow empty values
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
456 if optval == '""':
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
457 optval = ''
14840
625a9a695469 When reading the file, option names were not passed through
Guido van Rossum <guido@python.org>
parents: 13650
diff changeset
458 cursect[self.optionxform(optname)] = optval
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
459 else:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
460 # a non-fatal parsing error occurred. set up the
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
461 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
462 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
463 # list of all bogus lines
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
464 if not e:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
465 e = ParsingError(fpname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
466 e.append(lineno, `line`)
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
467 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
468 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
469 raise e