Mercurial > cpython-lru_cache-weakref
annotate Lib/ConfigParser.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 8e091e36fc80 |
| children |
| rev | line source |
|---|---|
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Configuration file parser. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 A setup file consists of sections, lead by a "[section]" header, |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 and followed by "name: value" entries, with continuations and such in |
| 8614 | 5 the style of RFC 822. |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
| 8614 | 7 The option values can contain format strings which refer to other values in |
| 8 the same section, or values in a special [DEFAULT] section. | |
| 9 | |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 For example: |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 something: %(dir)s/whatever |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 would resolve the "%(dir)s" to the value of dir. All reference |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 expansions are done late, on demand. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 Intrinsic defaults can be specified by passing them into the |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 ConfigParser constructor as a dictionary. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 class: |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
| 30723 | 22 ConfigParser -- responsible for parsing a list of |
|
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
|
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; |
|
35276
4adbd331d335
Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents:
33663
diff
changeset
|
31 its 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 | |
| 32117 | 48 are ignored. Return list of successfully read files. |
| 11369 | 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 |
|
51964
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
90 try: |
|
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
91 from collections import OrderedDict as _default_dict |
|
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
92 except ImportError: |
|
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
93 # fallback for setup.py which hasn't yet built _collections |
|
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
94 _default_dict = dict |
|
6b1b2e7a06e9
Backport 70111: Let configparser use ordered dicts by default.
Raymond Hettinger <python@rcn.com>
parents:
48135
diff
changeset
|
95 |
| 8614 | 96 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
|
97 |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
98 __all__ = ["NoSectionError", "DuplicateSectionError", "NoOptionError", |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
99 "InterpolationError", "InterpolationDepthError", |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
100 "InterpolationSyntaxError", "ParsingError", |
|
33663
5412096d0f95
SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents:
33662
diff
changeset
|
101 "MissingSectionHeaderError", |
|
5412096d0f95
SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents:
33662
diff
changeset
|
102 "ConfigParser", "SafeConfigParser", "RawConfigParser", |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
103 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15867
diff
changeset
|
104 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 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
|
106 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
107 MAX_INTERPOLATION_DEPTH = 10 |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
108 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
110 |
|
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
|
111 # exception classes |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
112 class Error(Exception): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
113 """Base class for ConfigParser exceptions.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
114 |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
115 def _get_message(self): |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
116 """Getter for 'message'; needed only to override deprecation in |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
117 BaseException.""" |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
118 return self.__message |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
119 |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
120 def _set_message(self, value): |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
121 """Setter for 'message'; needed only to override deprecation in |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
122 BaseException.""" |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
123 self.__message = value |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
124 |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
125 # BaseException.message has been deprecated since Python 2.6. To prevent |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
126 # DeprecationWarning from popping up over this pre-existing attribute, use |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
127 # a new property that takes lookup precedence. |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
128 message = property(_get_message, _set_message) |
|
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41157
diff
changeset
|
129 |
|
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 def __init__(self, msg=''): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
131 self.message = msg |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
132 Exception.__init__(self, msg) |
|
26909
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 __repr__(self): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
135 return self.message |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
136 |
|
15501
55f4a6e8b5b3
Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents:
15479
diff
changeset
|
137 __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
|
138 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 class NoSectionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
140 """Raised when no section matches a requested option.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
141 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 def __init__(self, section): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
143 Error.__init__(self, 'No section: %r' % (section,)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
144 self.section = section |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
145 self.args = (section, ) |
|
74545
5ecf650ede7c
Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
71789
diff
changeset
|
146 |
|
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 class DuplicateSectionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
148 """Raised when a section is multiply-created.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
149 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 def __init__(self, section): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
151 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
|
152 self.section = section |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
153 self.args = (section, ) |
|
74545
5ecf650ede7c
Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
71789
diff
changeset
|
154 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 class NoOptionError(Error): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
156 """A requested option was not found.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
157 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 def __init__(self, option, section): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
159 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
|
160 (option, section)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
161 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
162 self.section = section |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
163 self.args = (option, section) |
|
74545
5ecf650ede7c
Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
71789
diff
changeset
|
164 |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 class InterpolationError(Error): |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
166 """Base class for interpolation-related exceptions.""" |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
167 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
168 def __init__(self, option, section, msg): |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
169 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
|
170 self.option = option |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
171 self.section = section |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
172 self.args = (option, section, msg) |
|
74545
5ecf650ede7c
Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
71789
diff
changeset
|
173 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
174 class InterpolationMissingOptionError(InterpolationError): |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
175 """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
|
176 |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
177 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
|
178 msg = ("Bad value substitution:\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
179 "\tsection: [%s]\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
180 "\toption : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
181 "\tkey : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
182 "\trawval : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
183 % (section, option, reference, rawval)) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
184 InterpolationError.__init__(self, option, section, msg) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
185 self.reference = reference |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
186 self.args = (option, section, rawval, reference) |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
187 |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
188 class InterpolationSyntaxError(InterpolationError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
189 """Raised when the source text into which substitutions are made |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
190 does not conform to the required syntax.""" |
|
26907
ccba3b66d676
Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26685
diff
changeset
|
191 |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
192 class InterpolationDepthError(InterpolationError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
193 """Raised when substitutions are nested too deeply.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
194 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
195 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
|
196 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
|
197 "\tsection: [%s]\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
198 "\toption : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
199 "\trawval : %s\n" |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
200 % (section, option, rawval)) |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
201 InterpolationError.__init__(self, option, section, msg) |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
202 self.args = (option, section, rawval) |
| 8614 | 203 |
| 204 class ParsingError(Error): | |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
205 """Raised when a configuration file does not follow legal syntax.""" |
|
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
206 |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
207 def __init__(self, filename): |
| 8614 | 208 Error.__init__(self, 'File contains parsing errors: %s' % filename) |
| 209 self.filename = filename | |
| 210 self.errors = [] | |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
211 self.args = (filename, ) |
| 8614 | 212 |
| 213 def append(self, lineno, line): | |
| 214 self.errors.append((lineno, line)) | |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
215 self.message += '\n\t[line %2d]: %s' % (lineno, line) |
| 8614 | 216 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
217 class MissingSectionHeaderError(ParsingError): |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
218 """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
|
219 |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
220 def __init__(self, filename, lineno, line): |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
221 Error.__init__( |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
222 self, |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
223 'File contains no section headers.\nfile: %s, line: %d\n%r' % |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
224 (filename, lineno, line)) |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
225 self.filename = filename |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
226 self.lineno = lineno |
|
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
227 self.line = line |
|
74581
8e091e36fc80
Fixes #4686. Reverts redundant picklability code from r74544.
Łukasz Langa <lukasz@langa.pl>
parents:
74545
diff
changeset
|
228 self.args = (filename, lineno, line) |
|
74545
5ecf650ede7c
Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
71789
diff
changeset
|
229 |
|
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 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
231 class RawConfigParser: |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
232 def __init__(self, defaults=None, dict_type=_default_dict, |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
233 allow_no_value=False): |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
234 self._dict = dict_type |
|
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
235 self._sections = self._dict() |
|
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
236 self._defaults = self._dict() |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
237 if allow_no_value: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
238 self._optcre = self.OPTCRE_NV |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
239 else: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
240 self._optcre = self.OPTCRE |
|
33662
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
241 if defaults: |
|
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
242 for key, value in defaults.items(): |
|
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
243 self._defaults[self.optionxform(key)] = value |
|
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
|
244 |
|
5b24cbb1f99b
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 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
|
246 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
|
247 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 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
|
249 """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
|
250 # 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
|
251 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
|
252 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 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
|
254 """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
|
255 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
256 Raise DuplicateSectionError if a section by the specified name |
|
45445
dda92a097e8c
Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents:
41617
diff
changeset
|
257 already exists. Raise ValueError if name is DEFAULT or any of it's |
|
dda92a097e8c
Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents:
41617
diff
changeset
|
258 case-insensitive variants. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
259 """ |
|
45445
dda92a097e8c
Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents:
41617
diff
changeset
|
260 if section.lower() == "default": |
|
dda92a097e8c
Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents:
41617
diff
changeset
|
261 raise ValueError, 'Invalid section name: %s' % section |
|
dda92a097e8c
Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents:
41617
diff
changeset
|
262 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
263 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
|
264 raise DuplicateSectionError(section) |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
265 self._sections[section] = self._dict() |
|
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
|
266 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 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
|
268 """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
|
269 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
270 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
|
271 """ |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
272 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
|
273 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 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
|
275 """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
|
276 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
277 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
|
278 except KeyError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
279 raise NoSectionError(section) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
280 opts.update(self._defaults) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23611
diff
changeset
|
281 if '__name__' in opts: |
|
14899
606a80834a86
Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents:
14840
diff
changeset
|
282 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
|
283 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
|
284 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 def read(self, filenames): |
| 11369 | 286 """Read and parse a filename or a list of filenames. |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15501
diff
changeset
|
287 |
| 11369 | 288 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
|
289 designed so that you can specify a list of potential |
| 11369 | 290 configuration file locations (e.g. current directory, user's |
| 291 home directory, systemwide directory), and all existing | |
| 292 configuration files in the list will be read. A single | |
| 293 filename may also be given. | |
| 32117 | 294 |
| 295 Return list of successfully read files. | |
| 11369 | 296 """ |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23620
diff
changeset
|
297 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
|
298 filenames = [filenames] |
| 32117 | 299 read_ok = [] |
| 11369 | 300 for filename in filenames: |
| 301 try: | |
| 302 fp = open(filename) | |
| 303 except IOError: | |
| 304 continue | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
305 self._read(fp, filename) |
|
11368
b9bbe88d9554
ConfigParser.read(): Don't mask IOError exceptions.
Fred Drake <fdrake@acm.org>
parents:
11007
diff
changeset
|
306 fp.close() |
| 32117 | 307 read_ok.append(filename) |
| 308 return read_ok | |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 |
| 11369 | 310 def readfp(self, fp, filename=None): |
| 311 """Like read() but the argument must be a file-like object. | |
| 312 | |
| 313 The `fp' argument must have a `readline' method. Optional | |
| 314 second argument is the `filename', which if not given, is | |
| 315 taken from fp.name. If fp has no `name' attribute, `<???>' is | |
| 316 used. | |
| 317 | |
| 318 """ | |
| 319 if filename is None: | |
| 320 try: | |
| 321 filename = fp.name | |
| 322 except AttributeError: | |
| 323 filename = '<???>' | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
324 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
|
325 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
330 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
331 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
|
332 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
|
333 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
340 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
|
341 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
342 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
|
343 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
344 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
|
345 except KeyError: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
346 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
|
347 raise NoSectionError(section) |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
348 d2 = self._dict() |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
349 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
|
350 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
|
351 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
|
352 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
|
353 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
|
354 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
355 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
|
356 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
|
357 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 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
|
359 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
|
360 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 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
|
362 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
|
363 |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
364 _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
|
365 '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
|
366 |
|
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
|
367 def getboolean(self, section, option): |
|
20547
a12ccdbce8da
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20261
diff
changeset
|
368 v = self.get(section, option) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
369 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
|
370 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
|
371 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
|
372 |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
373 def optionxform(self, optionstr): |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
374 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
|
375 |
|
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
|
376 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
|
377 """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
|
378 if not section or section == DEFAULTSECT: |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
379 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
|
380 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
|
381 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
|
382 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
|
383 else: |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
384 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
|
385 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
|
386 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
|
387 |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
388 def set(self, section, option, value=None): |
|
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
|
389 """Set an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
390 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
|
391 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
|
392 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
|
393 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
394 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
|
395 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
|
396 raise NoSectionError(section) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
397 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
|
398 |
|
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
|
399 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
|
400 """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
|
401 if self._defaults: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
402 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
|
403 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
|
404 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
|
405 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
|
406 for section in self._sections: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
407 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
|
408 for (key, value) in self._sections[section].items(): |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
409 if key == "__name__": |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
410 continue |
|
64385
16cefdfed682
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63910
diff
changeset
|
411 if (value is not None) or (self._optcre == self.OPTCRE): |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
412 key = " = ".join((key, str(value).replace('\n', '\n\t'))) |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
413 fp.write("%s\n" % (key)) |
|
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
|
414 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
|
415 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
416 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
|
417 """Remove an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
418 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
|
419 sectdict = self._defaults |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
420 else: |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
421 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
422 sectdict = self._sections[section] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
423 except KeyError: |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
424 raise NoSectionError(section) |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
425 option = self.optionxform(option) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23611
diff
changeset
|
426 existed = option in sectdict |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
427 if existed: |
|
15479
e98964bc2681
remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents:
14899
diff
changeset
|
428 del sectdict[option] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
429 return existed |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
430 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
431 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
|
432 """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
|
433 existed = section in self._sections |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
434 if existed: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
435 del self._sections[section] |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
436 return existed |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
437 |
| 8614 | 438 # |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
439 # 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
|
440 # |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
441 SECTCRE = re.compile( |
| 8614 | 442 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
|
443 r'(?P<header>[^]]+)' # very permissive! |
| 8614 | 444 r'\]' # ] |
| 445 ) | |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
446 OPTCRE = re.compile( |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
447 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
|
448 r'\s*(?P<vi>[:=])\s*' # any number of space/tab, |
| 8614 | 449 # followed by separator |
| 450 # (either : or =), followed | |
| 451 # by any # space/tab | |
| 452 r'(?P<value>.*)$' # everything up to eol | |
| 453 ) | |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
454 OPTCRE_NV = re.compile( |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
455 r'(?P<option>[^:=\s][^:=]*)' # very permissive! |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
456 r'\s*(?:' # any number of space/tab, |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
457 r'(?P<vi>[:=])\s*' # optionally followed by |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
458 # separator (either : or |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
459 # =), followed by any # |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
460 # space/tab |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
461 r'(?P<value>.*))?$' # everything up to eol |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
462 ) |
| 8614 | 463 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
464 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
|
465 """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
|
466 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
467 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
|
468 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
|
469 options lines, indicated by `name: value' format lines. |
| 26078 | 470 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
|
471 leading whitespace. Blank lines, lines beginning with a '#', |
| 26078 | 472 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
|
473 """ |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
474 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
|
475 optname = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
476 lineno = 0 |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
477 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
|
478 while True: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
479 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
480 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
481 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
482 lineno = lineno + 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
483 # comment or blank line? |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
484 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
|
485 continue |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
486 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
|
487 # 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
|
488 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
489 # continuation line? |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
490 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
|
491 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
|
492 if value: |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
493 cursect[optname].append(value) |
| 8614 | 494 # a section header or option header? |
| 495 else: | |
| 496 # 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
|
497 mo = self.SECTCRE.match(line) |
| 8614 | 498 if mo: |
| 499 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
|
500 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
|
501 cursect = self._sections[sectname] |
| 8614 | 502 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
|
503 cursect = self._defaults |
| 8614 | 504 else: |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
505 cursect = self._dict() |
|
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
35276
diff
changeset
|
506 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
|
507 self._sections[sectname] = cursect |
| 8614 | 508 # So sections can't start with a continuation line |
| 509 optname = None | |
| 510 # no section header in the file? | |
| 511 elif cursect is None: | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
512 raise MissingSectionHeaderError(fpname, lineno, line) |
| 8614 | 513 # 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
|
514 else: |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
515 mo = self._optcre.match(line) |
| 8614 | 516 if mo: |
|
11873
7ed104c40db3
(Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents:
11381
diff
changeset
|
517 optname, vi, optval = mo.group('option', 'vi', 'value') |
|
25462
16e661970930
Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents:
25461
diff
changeset
|
518 optname = self.optionxform(optname.rstrip()) |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
519 # This check is fine because the OPTCRE cannot |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
520 # match if it would set optval to None |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
521 if optval is not None: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
522 if vi in ('=', ':') and ';' in optval: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
523 # ';' is a comment delimiter only if it follows |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
524 # a spacing character |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
525 pos = optval.find(';') |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
526 if pos != -1 and optval[pos-1].isspace(): |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
527 optval = optval[:pos] |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
528 optval = optval.strip() |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
529 # allow empty values |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
530 if optval == '""': |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
531 optval = '' |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
532 cursect[optname] = [optval] |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
533 else: |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
534 # valueless option handling |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
535 cursect[optname] = optval |
| 8614 | 536 else: |
| 537 # a non-fatal parsing error occurred. set up the | |
| 538 # exception but keep going. the exception will be | |
| 539 # raised at the end of the file and will contain a | |
| 540 # list of all bogus lines | |
| 541 if not e: | |
| 11369 | 542 e = ParsingError(fpname) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
543 e.append(lineno, repr(line)) |
| 8614 | 544 # if any parsing errors occurred, raise an exception |
| 545 if e: | |
| 546 raise e | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
547 |
|
63221
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
548 # join the multi-line values collected while reading |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
549 all_sections = [self._defaults] |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
550 all_sections.extend(self._sections.values()) |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
551 for options in all_sections: |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
552 for name, val in options.items(): |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
553 if isinstance(val, list): |
|
eecc018aac3e
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
58948
diff
changeset
|
554 options[name] = '\n'.join(val) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
555 |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
556 import UserDict as _UserDict |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
557 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
558 class _Chainmap(_UserDict.DictMixin): |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
559 """Combine multiple mappings for successive lookups. |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
560 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
561 For example, to emulate Python's normal lookup sequence: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
562 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
563 import __builtin__ |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
564 pylookup = _Chainmap(locals(), globals(), vars(__builtin__)) |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
565 """ |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
566 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
567 def __init__(self, *maps): |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
568 self._maps = maps |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
569 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
570 def __getitem__(self, key): |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
571 for mapping in self._maps: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
572 try: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
573 return mapping[key] |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
574 except KeyError: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
575 pass |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
576 raise KeyError(key) |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
577 |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
578 def keys(self): |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
579 result = [] |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
580 seen = set() |
|
71789
7d5a37ce42d5
Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents:
68256
diff
changeset
|
581 for mapping in self._maps: |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
582 for key in mapping: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
583 if key not in seen: |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
584 result.append(key) |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
585 seen.add(key) |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
586 return result |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
587 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
588 class ConfigParser(RawConfigParser): |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
589 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
590 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
|
591 """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
|
592 |
|
63503
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
593 If `vars' is provided, it must be a dictionary. The option is looked up |
|
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
594 in `vars' (if provided), `section', and in `defaults' in that order. |
|
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
595 |
|
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
596 All % interpolations are expanded in the return values, unless the |
|
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
597 optional argument `raw' is true. Values for interpolation keys are |
|
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53131
diff
changeset
|
598 looked up in the same manner as the option. |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
599 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
600 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
|
601 """ |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
602 sectiondict = {} |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
603 try: |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
604 sectiondict = self._sections[section] |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
605 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
606 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
607 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
608 # Update with the entry specific variables |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
609 vardict = {} |
|
33662
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
610 if vars: |
|
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
611 for key, value in vars.items(): |
|
67844
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
612 vardict[self.optionxform(key)] = value |
|
aa873004cea8
Issue 11089: Fix performance bug in ConfigParser that impaired its
Raymond Hettinger <python@rcn.com>
parents:
64385
diff
changeset
|
613 d = _Chainmap(vardict, sectiondict, self._defaults) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
614 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
|
615 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
616 value = d[option] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
617 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
618 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
|
619 |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
620 if raw or value is None: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
621 return value |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
622 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
623 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
|
624 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
625 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
|
626 """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
|
627 in the section. |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
628 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
629 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
|
630 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
|
631 `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
|
632 `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
|
633 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
|
634 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
635 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
|
636 """ |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
637 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
|
638 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
639 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
|
640 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
641 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
642 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
643 # 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
|
644 if vars: |
|
33662
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
645 for key, value in vars.items(): |
|
cc4684051c46
SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents:
32117
diff
changeset
|
646 d[self.optionxform(key)] = value |
|
25916
8bb6003f7f54
According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents:
25908
diff
changeset
|
647 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
|
648 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
|
649 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
|
650 if raw: |
|
30734
916f782ed6d5
Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents:
30723
diff
changeset
|
651 return [(option, d[option]) |
|
916f782ed6d5
Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents:
30723
diff
changeset
|
652 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
|
653 else: |
|
30734
916f782ed6d5
Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents:
30723
diff
changeset
|
654 return [(option, self._interpolate(section, option, d[option], d)) |
|
916f782ed6d5
Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents:
30723
diff
changeset
|
655 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
|
656 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
657 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
|
658 # 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
|
659 value = rawval |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26078
diff
changeset
|
660 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
|
661 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
|
662 depth -= 1 |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
663 if value and "%(" in value: |
| 32114 | 664 value = self._KEYCRE.sub(self._interpolation_replace, value) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
665 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
666 value = value % vars |
|
26927
33dcf4fd0ff9
ConfigParser._interpolate(): Pass the missing key to the
Fred Drake <fdrake@acm.org>
parents:
26909
diff
changeset
|
667 except KeyError, e: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
668 raise InterpolationMissingOptionError( |
|
48135
a7bf503c857e
Remove a __getitem__() removal on an exception to silence a warning triggered
Brett Cannon <bcannon@gmail.com>
parents:
47252
diff
changeset
|
669 option, section, rawval, e.args[0]) |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
670 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
671 break |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
672 if value and "%(" in value: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
673 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
|
674 return value |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
675 |
| 32114 | 676 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.") |
| 677 | |
| 678 def _interpolation_replace(self, match): | |
| 679 s = match.group(1) | |
| 680 if s is None: | |
| 681 return match.group() | |
| 682 else: | |
| 683 return "%%(%s)s" % self.optionxform(s) | |
| 684 | |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
685 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
686 class SafeConfigParser(ConfigParser): |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
687 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
688 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
|
689 # do the string interpolation |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
690 L = [] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
691 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
|
692 return ''.join(L) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
693 |
|
41157
53ee2795287b
Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents:
40418
diff
changeset
|
694 _interpvar_re = re.compile(r"%\(([^)]+)\)s") |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
695 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
696 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
|
697 if depth > MAX_INTERPOLATION_DEPTH: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
698 raise InterpolationDepthError(option, section, rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
699 while rest: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
700 p = rest.find("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
701 if p < 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
702 accum.append(rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
703 return |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
704 if p > 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
705 accum.append(rest[:p]) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
706 rest = rest[p:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
707 # p is no longer used |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
708 c = rest[1:2] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
709 if c == "%": |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
710 accum.append("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
711 rest = rest[2:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
712 elif c == "(": |
|
41157
53ee2795287b
Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents:
40418
diff
changeset
|
713 m = self._interpvar_re.match(rest) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
714 if m is None: |
|
29661
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
715 raise InterpolationSyntaxError(option, section, |
|
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
716 "bad interpolation variable reference %r" % rest) |
| 32114 | 717 var = self.optionxform(m.group(1)) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
718 rest = rest[m.end():] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
719 try: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
720 v = map[var] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
721 except KeyError: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
722 raise InterpolationMissingOptionError( |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
723 option, section, rest, var) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
724 if "%" in v: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
725 self._interpolate_some(option, accum, v, |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
726 section, map, depth + 1) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
727 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
728 accum.append(v) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
729 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
730 raise InterpolationSyntaxError( |
|
29661
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
731 option, section, |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
732 "'%%' must be followed by '%%' or '(', found: %r" % (rest,)) |
|
33663
5412096d0f95
SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents:
33662
diff
changeset
|
733 |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
734 def set(self, section, option, value=None): |
|
33663
5412096d0f95
SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents:
33662
diff
changeset
|
735 """Set an option. Extend ConfigParser.set: check for string values.""" |
|
58948
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
736 # The only legal non-string value if we allow valueless |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
737 # options is None, so we need to check if the value is a |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
738 # string if: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
739 # - we do not allow valueless options, or |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
740 # - we allow valueless options but the value is not None |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
741 if self._optcre is self.OPTCRE or value: |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
742 if not isinstance(value, basestring): |
|
6e74d3e51d33
- apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents:
53129
diff
changeset
|
743 raise TypeError("option values must be strings") |
|
63910
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
744 if value is not None: |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
745 # check for bad percent signs: |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
746 # first, replace all "good" interpolations |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
747 tmp_value = value.replace('%%', '') |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
748 tmp_value = self._interpvar_re.sub('', tmp_value) |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
749 # then, check if there's a lone percent sign left |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
750 if '%' in tmp_value: |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
751 raise ValueError("invalid interpolation syntax in %r at " |
|
4f76ce69aed0
Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents:
63468
diff
changeset
|
752 "position %d" % (value, tmp_value.find('%'))) |
|
33663
5412096d0f95
SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents:
33662
diff
changeset
|
753 ConfigParser.set(self, section, option, value) |
