Mercurial > cpython-lru_cache-weakref
annotate Lib/ConfigParser.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | a0005d26773d |
| children |
| 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 | 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 | 7 The option values can contain format strings which refer to other values in |
| 8 the same section, or values in a special [DEFAULT] section. | |
| 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 |
|
10037
f132ecfdb285
From: Mike Orr <mso@oz.net>
Guido van Rossum <guido@python.org>
parents:
9961
diff
changeset
|
45 read(filenames) |
| 11369 | 46 read and parse the list of named configuration files, given by |
| 47 name. A single filename is also allowed. Non-existing files | |
| 48 are ignored. | |
| 49 | |
| 50 readfp(fp, filename=None) | |
| 51 read and parse one configuration file, given as a file object. | |
| 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) |
|
20261
56f17810534c
Apply modified SF patch 467580: ConfigParser.getboolean(): FALSE, TRUE.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
69 like get(), but convert value to a boolean (currently case |
|
56f17810534c
Apply modified SF patch 467580: ConfigParser.getboolean(): FALSE, TRUE.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
70 insensitively defined as 0, false, no, off for 0, and 1, true, |
|
56f17810534c
Apply modified SF patch 467580: ConfigParser.getboolean(): FALSE, TRUE.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
71 yes, on for 1). Returns 0 or 1. |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
72 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
73 remove_section(section) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
74 remove the given file section and all its options |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
75 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
76 remove_option(section, option) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
77 remove the given option from the given section |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
78 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
79 set(section, option, value) |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
80 set the given option |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
81 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
82 write(fp) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
83 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
|
84 """ |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
| 8614 | 86 import re |
|
25541
47aa9fc74da8
use types.StringTypes instead of (str, unicode)
Skip Montanaro <skip@pobox.com>
parents:
25540
diff
changeset
|
87 import types |
|
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
|
88 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
89 __all__ = ["NoSectionError","DuplicateSectionError","NoOptionError", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
90 "InterpolationError","InterpolationDepthError","ParsingError", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
91 "MissingSectionHeaderError","ConfigParser", |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
92 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
93 |
|
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
|
94 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
|
95 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
96 MAX_INTERPOLATION_DEPTH = 10 |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
97 |
|
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
|
98 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
99 |
|
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
|
100 # exception classes |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
101 class Error(Exception): |
|
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 __init__(self, msg=''): |
| 8614 | 103 self._msg = msg |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
104 Exception.__init__(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
|
105 def __repr__(self): |
| 8614 | 106 return self._msg |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
107 __str__ = __repr__ |
|
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
|
108 |
|
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 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
|
110 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
|
111 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
|
112 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
|
113 |
|
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 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
|
115 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
|
116 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
|
117 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
|
118 |
|
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
|
119 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
|
120 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
|
121 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
|
122 (option, section)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
123 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
124 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
|
125 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 class InterpolationError(Error): |
|
8853
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
127 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
|
128 Error.__init__(self, |
|
8853
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
129 "Bad value substitution:\n" |
|
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
130 "\tsection: [%s]\n" |
|
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
131 "\toption : %s\n" |
|
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
132 "\tkey : %s\n" |
|
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
133 "\trawval : %s\n" |
|
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
134 % (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
|
135 self.reference = reference |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
136 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
137 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
|
138 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
139 class InterpolationDepthError(Error): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
140 def __init__(self, option, section, rawval): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
141 Error.__init__(self, |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
142 "Value interpolation too deeply recursive:\n" |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
143 "\tsection: [%s]\n" |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
144 "\toption : %s\n" |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
145 "\trawval : %s\n" |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
146 % (section, option, rawval)) |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
147 self.option = option |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
148 self.section = section |
| 8614 | 149 |
| 150 class ParsingError(Error): | |
| 151 def __init__(self, filename): | |
| 152 Error.__init__(self, 'File contains parsing errors: %s' % filename) | |
| 153 self.filename = filename | |
| 154 self.errors = [] | |
| 155 | |
| 156 def append(self, lineno, line): | |
| 157 self.errors.append((lineno, line)) | |
| 158 self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) | |
| 159 | |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
160 class MissingSectionHeaderError(ParsingError): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
161 def __init__(self, filename, lineno, line): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
162 Error.__init__( |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
163 self, |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
164 '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
|
165 (filename, lineno, line)) |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
166 self.filename = filename |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
167 self.lineno = lineno |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
168 self.line = line |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
169 |
|
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
|
170 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
171 |
|
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 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
|
173 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
|
174 self.__sections = {} |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
175 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
|
176 self.__defaults = {} |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
177 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
178 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
|
179 |
|
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
|
180 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
|
181 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
|
182 |
|
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 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
|
184 """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
|
185 # 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
|
186 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
|
187 |
|
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
|
188 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
|
189 """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
|
190 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
191 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
|
192 already exists. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
193 """ |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
194 if section in self.__sections: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
195 raise DuplicateSectionError(section) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
196 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
|
197 |
|
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
|
198 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
|
199 """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
|
200 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
201 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
|
202 """ |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
203 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
|
204 |
|
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
|
205 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
|
206 """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
|
207 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
208 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
|
209 except KeyError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
210 raise NoSectionError(section) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
211 opts.update(self.__defaults) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
212 if '__name__' in opts: |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
213 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
|
214 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
|
215 |
|
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 | 217 """Read and parse a filename or a list of filenames. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
218 |
| 11369 | 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 | 221 configuration file locations (e.g. current directory, user's |
| 222 home directory, systemwide directory), and all existing | |
| 223 configuration files in the list will be read. A single | |
| 224 filename may also be given. | |
| 225 """ | |
|
25541
47aa9fc74da8
use types.StringTypes instead of (str, unicode)
Skip Montanaro <skip@pobox.com>
parents:
25540
diff
changeset
|
226 if isinstance(filenames, types.StringTypes): |
|
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 | 228 for filename in filenames: |
| 229 try: | |
| 230 fp = open(filename) | |
| 231 except IOError: | |
| 232 continue | |
| 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 | 236 def readfp(self, fp, filename=None): |
| 237 """Like read() but the argument must be a file-like object. | |
| 238 | |
| 239 The `fp' argument must have a `readline' method. Optional | |
| 240 second argument is the `filename', which if not given, is | |
| 241 taken from fp.name. If fp has no `name' attribute, `<???>' is | |
| 242 used. | |
| 243 | |
| 244 """ | |
| 245 if filename is None: | |
| 246 try: | |
| 247 filename = fp.name | |
| 248 except AttributeError: | |
| 249 filename = '<???>' | |
| 250 self.__read(fp, filename) | |
| 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 """ |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
263 d = self.__defaults.copy() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
264 try: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
265 d.update(self.__sections[section]) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
266 except KeyError: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
267 if section != DEFAULTSECT: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
268 raise NoSectionError(section) |
|
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
|
269 # Update with the entry specific variables |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
270 if vars is not None: |
|
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
|
271 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
|
272 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
|
273 try: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
274 value = d[option] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
275 except KeyError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
276 raise NoOptionError(option, section) |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
277 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
278 if raw: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
279 return value |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
280 return self._interpolate(section, option, value, d) |
|
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
|
281 |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
282 def _interpolate(self, section, option, rawval, vars): |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
283 # do the string interpolation |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
284 value = rawval |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25541
diff
changeset
|
285 depth = MAX_INTERPOLATION_DEPTH |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
286 while depth: # Loop through this until it's done |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
287 depth -= 1 |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
288 if value.find("%(") != -1: |
|
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
|
289 try: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
290 value = value % vars |
|
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
|
291 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
|
292 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
|
293 else: |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
294 break |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
295 if value.find("%(") != -1: |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
296 raise InterpolationDepthError(option, section, rawval) |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
297 return value |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
298 |
|
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
|
299 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
|
300 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
|
301 |
|
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 def getint(self, section, option): |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
303 return self.__get(section, int, 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
|
304 |
|
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 def getfloat(self, section, option): |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
306 return self.__get(section, float, option) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
307 |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
308 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
309 '0': False, 'no': False, 'false': False, 'off': False} |
|
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
|
310 |
|
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
|
311 def getboolean(self, section, option): |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20261
diff
changeset
|
312 v = self.get(section, option) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
313 if v.lower() not in self._boolean_states: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
314 raise ValueError, 'Not a boolean: %s' % v |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
315 return self._boolean_states[v.lower()] |
|
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
|
316 |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
317 def optionxform(self, optionstr): |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
318 return optionstr.lower() |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
319 |
|
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
|
320 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
|
321 """Check for the existence of a given option in a given section.""" |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
322 if not section or section == DEFAULTSECT: |
|
25450
f8180a74b0fa
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
22294
diff
changeset
|
323 option = self.optionxform(option) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
324 return option in self.__defaults |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
325 elif section not in self.__sections: |
|
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
|
326 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
|
327 else: |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
328 option = self.optionxform(option) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
329 return (option in self.__sections[section] |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
330 or option in self.__defaults) |
|
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
|
331 |
|
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 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
|
333 """Set an option.""" |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
334 if not section or section == DEFAULTSECT: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
335 sectdict = self.__defaults |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
336 else: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
337 try: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
338 sectdict = self.__sections[section] |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
339 except KeyError: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
340 raise NoSectionError(section) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
341 sectdict[self.optionxform(option)] = value |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
342 |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
343 def write(self, fp): |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
344 """Write an .ini-format representation of the configuration state.""" |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
345 if self.__defaults: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
346 fp.write("[%s]\n" % DEFAULTSECT) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
347 for (key, value) in self.__defaults.items(): |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
348 fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
349 fp.write("\n") |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
350 for section in self.__sections: |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
351 fp.write("[%s]\n" % section) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
352 for (key, value) in self.__sections[section].items(): |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
353 if key != "__name__": |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
354 fp.write("%s = %s\n" % |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
355 (key, str(value).replace('\n', '\n\t'))) |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
356 fp.write("\n") |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
357 |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
358 def remove_option(self, section, option): |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
359 """Remove an option.""" |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
360 if not section or section == DEFAULTSECT: |
|
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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 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
|
366 raise NoSectionError(section) |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
367 option = self.optionxform(option) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
368 existed = option in sectdict |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
369 if existed: |
|
15479
e98964bc2681
remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents:
14899
diff
changeset
|
370 del sectdict[option] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
371 return existed |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
372 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
373 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
|
374 """Remove a file section.""" |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
375 existed = section in self.__sections |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
376 if existed: |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
377 del self.__sections[section] |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
378 return existed |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
379 |
| 8614 | 380 # |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
381 # Regular expressions for parsing section headers and options. |
|
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
382 # |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
383 SECTCRE = re.compile( |
| 8614 | 384 r'\[' # [ |
|
16594
9ef978c6f039
Be much more permissive in what we accept in section names; there has been
Fred Drake <fdrake@acm.org>
parents:
16575
diff
changeset
|
385 r'(?P<header>[^]]+)' # very permissive! |
| 8614 | 386 r'\]' # ] |
| 387 ) | |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
388 OPTCRE = re.compile( |
|
25463
1d91edfd75f2
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25451
diff
changeset
|
389 r'(?P<option>[^:=\s][^:=]*)' # very permissive! |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
390 r'\s*(?P<vi>[:=])\s*' # any number of space/tab, |
| 8614 | 391 # followed by separator |
| 392 # (either : or =), followed | |
| 393 # by any # space/tab | |
| 394 r'(?P<value>.*)$' # everything up to eol | |
| 395 ) | |
| 396 | |
| 11369 | 397 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
|
398 """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
|
399 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 """ |
| 8614 | 407 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
|
408 optname = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
409 lineno = 0 |
| 8614 | 410 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
|
411 while 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
412 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
413 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
414 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
415 lineno = lineno + 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
416 # comment or blank line? |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
417 if line.strip() == '' or line[0] in '#;': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
418 continue |
|
25463
1d91edfd75f2
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25451
diff
changeset
|
419 if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR": |
|
1d91edfd75f2
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25451
diff
changeset
|
420 # 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
|
421 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
422 # continuation line? |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
423 if line[0].isspace() and cursect is not None and optname: |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
424 value = line.strip() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
425 if value: |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
426 cursect[optname] = "%s\n%s" % (cursect[optname], value) |
| 8614 | 427 # a section header or option header? |
| 428 else: | |
| 429 # 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
|
430 mo = self.SECTCRE.match(line) |
| 8614 | 431 if mo: |
| 432 sectname = mo.group('header') | |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
433 if sectname in self.__sections: |
| 8614 | 434 cursect = self.__sections[sectname] |
| 435 elif sectname == DEFAULTSECT: | |
| 436 cursect = self.__defaults | |
| 437 else: | |
|
8853
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
438 cursect = {'__name__': sectname} |
| 8614 | 439 self.__sections[sectname] = cursect |
| 440 # So sections can't start with a continuation line | |
| 441 optname = None | |
| 442 # no section header in the file? | |
| 443 elif cursect is None: | |
| 11369 | 444 raise MissingSectionHeaderError(fpname, lineno, `line`) |
| 8614 | 445 # 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
|
446 else: |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
447 mo = self.OPTCRE.match(line) |
| 8614 | 448 if mo: |
|
11873
7ed104c40db3
(Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents:
11381
diff
changeset
|
449 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
|
450 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
|
451 # ';' 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
|
452 # a spacing character |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
453 pos = optval.find(';') |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
454 if pos != -1 and optval[pos-1].isspace(): |
|
11873
7ed104c40db3
(Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents:
11381
diff
changeset
|
455 optval = optval[:pos] |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
456 optval = optval.strip() |
| 8614 | 457 # allow empty values |
| 458 if optval == '""': | |
| 459 optval = '' | |
|
25463
1d91edfd75f2
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25451
diff
changeset
|
460 optname = self.optionxform(optname.rstrip()) |
|
25451
05ffe201e902
Previous fix depended on the "code cleanup and general bug fix patch" already
Fred Drake <fdrake@acm.org>
parents:
25450
diff
changeset
|
461 cursect[optname] = optval |
| 8614 | 462 else: |
| 463 # a non-fatal parsing error occurred. set up the | |
| 464 # exception but keep going. the exception will be | |
| 465 # raised at the end of the file and will contain a | |
| 466 # list of all bogus lines | |
| 467 if not e: | |
| 11369 | 468 e = ParsingError(fpname) |
| 8614 | 469 e.append(lineno, `line`) |
| 470 # if any parsing errors occurred, raise an exception | |
| 471 if e: | |
| 472 raise e |
