Mercurial > cpython-lru_cache-weakref
annotate Lib/ConfigParser.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 776f270a6ea9 |
| children | 79303de9a05a |
| 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 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
55 get(section, option, raw=False, vars=None) |
|
9961
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 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
70 insensitively defined as 0, false, no, off for False, and 1, true, |
|
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
71 yes, on for True). Returns False or True. |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
72 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
73 items(section, raw=False, vars=None) |
|
25461
09551c6da299
items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents:
25459
diff
changeset
|
74 return a list of tuples with (name, value) for each option |
|
09551c6da299
items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents:
25459
diff
changeset
|
75 in the section. |
|
09551c6da299
items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents:
25459
diff
changeset
|
76 |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
77 remove_section(section) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
78 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
|
79 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
80 remove_option(section, option) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
81 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
|
82 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
83 set(section, option, value) |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
84 set the given option |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
85 |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
86 write(fp) |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
87 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
|
88 """ |
|
5b24cbb1f99b
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 |
| 8614 | 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 |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
92 __all__ = ["NoSectionError", "DuplicateSectionError", "NoOptionError", |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
93 "InterpolationError", "InterpolationDepthError", |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
94 "InterpolationSyntaxError", "ParsingError", |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
95 "MissingSectionHeaderError", "ConfigParser", |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
96 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
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 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
|
99 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
100 MAX_INTERPOLATION_DEPTH = 10 |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
101 |
|
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 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
103 |
|
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 # exception classes |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
105 class Error(Exception): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
106 """Base class for ConfigParser exceptions.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
107 |
|
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 def __init__(self, msg=''): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
109 self.message = msg |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
110 Exception.__init__(self, msg) |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
111 |
|
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
|
112 def __repr__(self): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
113 return self.message |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
114 |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
115 __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
|
116 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 class NoSectionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
118 """Raised when no section matches a requested option.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
119 |
|
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
|
120 def __init__(self, section): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
121 Error.__init__(self, 'No section: ' + `section`) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
122 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
|
123 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 class DuplicateSectionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
125 """Raised when a section is multiply-created.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
126 |
|
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
|
127 def __init__(self, section): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
128 Error.__init__(self, "Section %r already exists" % section) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
129 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
|
130 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 class NoOptionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
132 """A requested option was not found.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
133 |
|
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 def __init__(self, option, section): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
135 Error.__init__(self, "No option %r in section: %r" % |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
136 (option, section)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
137 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
138 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
|
139 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 class InterpolationError(Error): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
141 """Base class for interpolation-related exceptions.""" |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
142 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
143 def __init__(self, option, section, msg): |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
144 Error.__init__(self, msg) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
145 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
146 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
|
147 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
148 class InterpolationMissingOptionError(InterpolationError): |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
149 """A string substitution required a setting which was not available.""" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
150 |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
151 def __init__(self, option, section, rawval, reference): |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
152 msg = ("Bad value substitution:\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
153 "\tsection: [%s]\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
154 "\toption : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
155 "\tkey : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
156 "\trawval : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
157 % (section, option, reference, rawval)) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
158 InterpolationError.__init__(self, option, section, msg) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
159 self.reference = reference |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
160 |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
161 class InterpolationSyntaxError(InterpolationError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
162 """Raised when the source text into which substitutions are made |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
163 does not conform to the required syntax.""" |
|
26907
ccba3b66d676
Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26685
diff
changeset
|
164 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
165 class InterpolationDepthError(InterpolationError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
166 """Raised when substitutions are nested too deeply.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
167 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
168 def __init__(self, option, section, rawval): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
169 msg = ("Value interpolation too deeply recursive:\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
170 "\tsection: [%s]\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
171 "\toption : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
172 "\trawval : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
173 % (section, option, rawval)) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
174 InterpolationError.__init__(self, option, section, msg) |
| 8614 | 175 |
| 176 class ParsingError(Error): | |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
177 """Raised when a configuration file does not follow legal syntax.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
178 |
| 8614 | 179 def __init__(self, filename): |
| 180 Error.__init__(self, 'File contains parsing errors: %s' % filename) | |
| 181 self.filename = filename | |
| 182 self.errors = [] | |
| 183 | |
| 184 def append(self, lineno, line): | |
| 185 self.errors.append((lineno, line)) | |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
186 self.message += '\n\t[line %2d]: %s' % (lineno, line) |
| 8614 | 187 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
188 class MissingSectionHeaderError(ParsingError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
189 """Raised when a key-value pair is found before any section header.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
190 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
191 def __init__(self, filename, lineno, line): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
192 Error.__init__( |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
193 self, |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
194 '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
|
195 (filename, lineno, line)) |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
196 self.filename = filename |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
197 self.lineno = lineno |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
198 self.line = line |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
199 |
|
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 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
201 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
202 class RawConfigParser: |
|
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
|
203 def __init__(self, defaults=None): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
204 self._sections = {} |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
205 if defaults is None: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
206 self._defaults = {} |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
207 else: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
208 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
|
209 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 def defaults(self): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
211 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
|
212 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 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
|
214 """Return a list of section names, excluding [DEFAULT]""" |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
215 # self._sections will never have [DEFAULT] in it |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
216 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
|
217 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 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
|
219 """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
|
220 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
221 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
|
222 already exists. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
223 """ |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
224 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
|
225 raise DuplicateSectionError(section) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
226 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
|
227 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 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
|
229 """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
|
230 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
231 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
|
232 """ |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
233 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
|
234 |
|
5b24cbb1f99b
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 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
|
236 """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
|
237 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
238 opts = self._sections[section].copy() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
239 except KeyError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
240 raise NoSectionError(section) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
241 opts.update(self._defaults) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23611
diff
changeset
|
242 if '__name__' in opts: |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
243 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
|
244 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
|
245 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 def read(self, filenames): |
| 11369 | 247 """Read and parse a filename or a list of filenames. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
248 |
| 11369 | 249 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
|
250 designed so that you can specify a list of potential |
| 11369 | 251 configuration file locations (e.g. current directory, user's |
| 252 home directory, systemwide directory), and all existing | |
| 253 configuration files in the list will be read. A single | |
| 254 filename may also be given. | |
| 255 """ | |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
256 if isinstance(filenames, basestring): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
257 filenames = [filenames] |
| 11369 | 258 for filename in filenames: |
| 259 try: | |
| 260 fp = open(filename) | |
| 261 except IOError: | |
| 262 continue | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
263 self._read(fp, filename) |
|
11368
b9bbe88d9554
ConfigParser.read(): Don't mask IOError exceptions.
Fred Drake <fdrake@acm.org>
parents:
11007
diff
changeset
|
264 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
|
265 |
| 11369 | 266 def readfp(self, fp, filename=None): |
| 267 """Like read() but the argument must be a file-like object. | |
| 268 | |
| 269 The `fp' argument must have a `readline' method. Optional | |
| 270 second argument is the `filename', which if not given, is | |
| 271 taken from fp.name. If fp has no `name' attribute, `<???>' is | |
| 272 used. | |
| 273 | |
| 274 """ | |
| 275 if filename is None: | |
| 276 try: | |
| 277 filename = fp.name | |
| 278 except AttributeError: | |
| 279 filename = '<???>' | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
280 self._read(fp, filename) |
|
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 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
282 def get(self, section, option): |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
283 opt = self.optionxform(option) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
284 if section not in self._sections: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
285 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
286 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
287 if opt in self._defaults: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
288 return self._defaults[opt] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
289 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
290 raise NoOptionError(option, section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
291 elif opt in self._sections[section]: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
292 return self._sections[section][opt] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
293 elif opt in self._defaults: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
294 return self._defaults[opt] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
295 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
296 raise NoOptionError(option, 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
|
297 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
298 def items(self, section): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
299 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
300 d2 = 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
|
301 except KeyError: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
302 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
|
303 raise NoSectionError(section) |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
304 d2 = {} |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
305 d = self._defaults.copy() |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
306 d.update(d2) |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
307 if "__name__" in d: |
|
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
308 del d["__name__"] |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
309 return d.items() |
|
25461
09551c6da299
items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents:
25459
diff
changeset
|
310 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
311 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
|
312 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
|
313 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 def getint(self, section, option): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
315 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
|
316 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 def getfloat(self, section, option): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
318 return self._get(section, float, 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
|
319 |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
320 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
321 '0': False, 'no': False, 'false': False, 'off': False} |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
322 |
|
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
|
323 def getboolean(self, section, option): |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20261
diff
changeset
|
324 v = self.get(section, option) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
325 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
|
326 raise ValueError, 'Not a boolean: %s' % v |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
327 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
|
328 |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
329 def optionxform(self, optionstr): |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
330 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
|
331 |
|
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
|
332 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
|
333 """Check for the existence of a given option in a given section.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
334 if not section or section == DEFAULTSECT: |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
335 option = self.optionxform(option) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
336 return option in self._defaults |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
337 elif section not in self._sections: |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
338 return False |
|
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
|
339 else: |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
340 option = self.optionxform(option) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
341 return (option in self._sections[section] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
342 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
|
343 |
|
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
|
344 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
|
345 """Set an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
346 if not section or section == DEFAULTSECT: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
347 sectdict = 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
|
348 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
|
349 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
350 sectdict = self._sections[section] |
|
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
|
351 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
|
352 raise NoSectionError(section) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
353 sectdict[self.optionxform(option)] = 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
|
354 |
|
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
|
355 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
|
356 """Write an .ini-format representation of the configuration state.""" |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
357 if self._defaults: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
358 fp.write("[%s]\n" % DEFAULTSECT) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
359 for (key, value) in self._defaults.items(): |
|
22294
c6c1a5fbb6c1
I presume this should go here.
Michael W. Hudson <mwh@python.net>
parents:
20547
diff
changeset
|
360 fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) |
|
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 fp.write("\n") |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
362 for section in self._sections: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
363 fp.write("[%s]\n" % section) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
364 for (key, value) in self._sections[section].items(): |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
365 if key != "__name__": |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
366 fp.write("%s = %s\n" % |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
367 (key, str(value).replace('\n', '\n\t'))) |
|
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
|
368 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
|
369 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
370 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
|
371 """Remove an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
372 if not section or section == DEFAULTSECT: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
373 sectdict = self._defaults |
|
13600
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 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
376 sectdict = self._sections[section] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
377 except KeyError: |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
378 raise NoSectionError(section) |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
379 option = self.optionxform(option) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23611
diff
changeset
|
380 existed = option in sectdict |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
381 if existed: |
|
15479
e98964bc2681
remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents:
14899
diff
changeset
|
382 del sectdict[option] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
383 return existed |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
384 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
385 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
|
386 """Remove a file section.""" |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
387 existed = section in self._sections |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
388 if existed: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
389 del self._sections[section] |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
390 return existed |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
391 |
| 8614 | 392 # |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
393 # Regular expressions for parsing section headers and options. |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
394 # |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
395 SECTCRE = re.compile( |
| 8614 | 396 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
|
397 r'(?P<header>[^]]+)' # very permissive! |
| 8614 | 398 r'\]' # ] |
| 399 ) | |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
400 OPTCRE = re.compile( |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
401 r'(?P<option>[^:=\s][^:=]*)' # very permissive! |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
402 r'\s*(?P<vi>[:=])\s*' # any number of space/tab, |
| 8614 | 403 # followed by separator |
| 404 # (either : or =), followed | |
| 405 # by any # space/tab | |
| 406 r'(?P<value>.*)$' # everything up to eol | |
| 407 ) | |
| 408 | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
409 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
|
410 """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
|
411 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
412 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
|
413 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
|
414 options lines, indicated by `name: value' format lines. |
| 26078 | 415 Continuations are represented by an embedded newline then |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
416 leading whitespace. Blank lines, lines beginning with a '#', |
| 26078 | 417 and just about everything else are ignored. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
418 """ |
| 8614 | 419 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
|
420 optname = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
421 lineno = 0 |
| 8614 | 422 e = None # None, or an exception |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
423 while True: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
424 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
425 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
426 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
427 lineno = lineno + 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
428 # comment or blank line? |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
429 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
|
430 continue |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
431 if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR": |
|
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
432 # 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
|
433 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
434 # continuation line? |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
435 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
|
436 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
|
437 if value: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
438 cursect[optname] = "%s\n%s" % (cursect[optname], value) |
| 8614 | 439 # a section header or option header? |
| 440 else: | |
| 441 # 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
|
442 mo = self.SECTCRE.match(line) |
| 8614 | 443 if mo: |
| 444 sectname = mo.group('header') | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
445 if sectname in self._sections: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
446 cursect = self._sections[sectname] |
| 8614 | 447 elif sectname == DEFAULTSECT: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
448 cursect = self._defaults |
| 8614 | 449 else: |
|
8853
bbe9d72bccda
Time machine experiment. Use '__name__' as the special key (always
Barry Warsaw <barry@python.org>
parents:
8614
diff
changeset
|
450 cursect = {'__name__': sectname} |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
451 self._sections[sectname] = cursect |
| 8614 | 452 # So sections can't start with a continuation line |
| 453 optname = None | |
| 454 # no section header in the file? | |
| 455 elif cursect is None: | |
| 11369 | 456 raise MissingSectionHeaderError(fpname, lineno, `line`) |
| 8614 | 457 # 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
|
458 else: |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
459 mo = self.OPTCRE.match(line) |
| 8614 | 460 if mo: |
|
11873
7ed104c40db3
(Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents:
11381
diff
changeset
|
461 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
|
462 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
|
463 # ';' 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
|
464 # a spacing character |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
465 pos = optval.find(';') |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
466 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
|
467 optval = optval[:pos] |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
468 optval = optval.strip() |
| 8614 | 469 # allow empty values |
| 470 if optval == '""': | |
| 471 optval = '' | |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
472 optname = self.optionxform(optname.rstrip()) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
473 cursect[optname] = optval |
| 8614 | 474 else: |
| 475 # a non-fatal parsing error occurred. set up the | |
| 476 # exception but keep going. the exception will be | |
| 477 # raised at the end of the file and will contain a | |
| 478 # list of all bogus lines | |
| 479 if not e: | |
| 11369 | 480 e = ParsingError(fpname) |
| 8614 | 481 e.append(lineno, `line`) |
| 482 # if any parsing errors occurred, raise an exception | |
| 483 if e: | |
| 484 raise e | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
485 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
486 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
487 class ConfigParser(RawConfigParser): |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
488 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
489 def get(self, section, option, raw=False, vars=None): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
490 """Get an option value for a given section. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
491 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
492 All % interpolations are expanded in the return values, based on the |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
493 defaults passed into the constructor, unless the optional argument |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
494 `raw' is true. Additional substitutions may be provided using the |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
495 `vars' argument, which must be a dictionary whose contents overrides |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
496 any pre-existing defaults. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
497 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
498 The section DEFAULT is special. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
499 """ |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
500 d = self._defaults.copy() |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
501 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
502 d.update(self._sections[section]) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
503 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
504 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
505 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
506 # Update with the entry specific variables |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
507 if vars is not None: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
508 d.update(vars) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
509 option = self.optionxform(option) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
510 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
511 value = d[option] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
512 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
513 raise NoOptionError(option, section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
514 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
515 if raw: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
516 return value |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
517 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
518 return self._interpolate(section, option, value, d) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
519 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
520 def items(self, section, raw=False, vars=None): |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
521 """Return a list of tuples with (name, value) for each option |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
522 in the section. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
523 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
524 All % interpolations are expanded in the return values, based on the |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
525 defaults passed into the constructor, unless the optional argument |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
526 `raw' is true. Additional substitutions may be provided using the |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
527 `vars' argument, which must be a dictionary whose contents overrides |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
528 any pre-existing defaults. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
529 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
530 The section DEFAULT is special. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
531 """ |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
532 d = self._defaults.copy() |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
533 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
534 d.update(self._sections[section]) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
535 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
536 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
537 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
538 # Update with the entry specific variables |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
539 if vars: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
540 d.update(vars) |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
541 options = d.keys() |
|
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
542 if "__name__" in options: |
|
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
543 options.remove("__name__") |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
544 if raw: |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
545 for option in options: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
546 yield (option, d[option]) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
547 else: |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
548 for option in options: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
549 yield (option, |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
550 self._interpolate(section, option, d[option], d)) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
551 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
552 def _interpolate(self, section, option, rawval, vars): |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
553 # do the string interpolation |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
554 value = rawval |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26078
diff
changeset
|
555 depth = MAX_INTERPOLATION_DEPTH |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
556 while depth: # Loop through this until it's done |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
557 depth -= 1 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
558 if value.find("%(") != -1: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
559 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
560 value = value % vars |
|
26927
33dcf4fd0ff9
ConfigParser._interpolate(): Pass the missing key to the
Fred Drake <fdrake@acm.org>
parents:
26909
diff
changeset
|
561 except KeyError, e: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
562 raise InterpolationMissingOptionError( |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
563 option, section, rawval, e[0]) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
564 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
565 break |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
566 if value.find("%(") != -1: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
567 raise InterpolationDepthError(option, section, rawval) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
568 return value |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
569 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
570 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
571 class SafeConfigParser(ConfigParser): |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
572 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
573 def _interpolate(self, section, option, rawval, vars): |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
574 # do the string interpolation |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
575 L = [] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
576 self._interpolate_some(option, L, rawval, section, vars, 1) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
577 return ''.join(L) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
578 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
579 _interpvar_match = re.compile(r"%\(([^)]+)\)s").match |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
580 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
581 def _interpolate_some(self, option, accum, rest, section, map, depth): |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
582 if depth > MAX_INTERPOLATION_DEPTH: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
583 raise InterpolationDepthError(option, section, rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
584 while rest: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
585 p = rest.find("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
586 if p < 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
587 accum.append(rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
588 return |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
589 if p > 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
590 accum.append(rest[:p]) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
591 rest = rest[p:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
592 # p is no longer used |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
593 c = rest[1:2] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
594 if c == "%": |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
595 accum.append("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
596 rest = rest[2:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
597 elif c == "(": |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
598 m = self._interpvar_match(rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
599 if m is None: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
600 raise InterpolationSyntaxError( |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
601 "bad interpolation variable reference", rest) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
602 var = m.group(1) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
603 rest = rest[m.end():] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
604 try: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
605 v = map[var] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
606 except KeyError: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
607 raise InterpolationMissingOptionError( |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
608 option, section, rest, var) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
609 if "%" in v: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
610 self._interpolate_some(option, accum, v, |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
611 section, map, depth + 1) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
612 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
613 accum.append(v) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
614 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
615 raise InterpolationSyntaxError( |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
616 option, section, rest, |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
617 "'%' must be followed by '%' or '(', found: " + `rest`) |
