annotate Lib/configparser.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 06e70937364b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Configuration file parser.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
3 A configuration file consists of sections, lead by a "[section]" header,
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
4 and followed by "name: value" entries, with continuations and such in
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
5 the style of RFC 822.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 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
8 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
9
5b24cbb1f99b 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 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
11
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 30275
diff changeset
12 ConfigParser -- responsible for parsing a list of
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
13 configuration files, and managing the parsed database.
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
14
5b24cbb1f99b 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 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
16
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
17 __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
18 delimiters=('=', ':'), comment_prefixes=('#', ';'),
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
19 inline_comment_prefixes=None, strict=True,
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
20 empty_lines_in_values=True):
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
21 Create the parser. When `defaults' is given, it is initialized into the
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
22 dictionary or intrinsic defaults. The keys must be strings, the values
66349
c13f8980ca7f Issue #10489: removed broken `__name__` support from configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66287
diff changeset
23 must be appropriate for %()s string interpolation.
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
24
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
25 When `dict_type' is given, it will be used to create the dictionary
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
26 objects for the list of sections, for the options within a section, and
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
27 for the default values.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
28
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
29 When `delimiters' is given, it will be used as the set of substrings
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
30 that divide keys from values.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
31
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
32 When `comment_prefixes' is given, it will be used as the set of
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
33 substrings that prefix comments in empty lines. Comments can be
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
34 indented.
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
35
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
36 When `inline_comment_prefixes' is given, it will be used as the set of
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
37 substrings that prefix comments in non-empty lines.
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
38
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
39 When `strict` is True, the parser won't allow for any section or option
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
40 duplicates while reading from a single source (file, string or
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
41 dictionary). Default is True.
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
42
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
43 When `empty_lines_in_values' is False (default: True), each empty line
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
44 marks the end of an option. Otherwise, internal empty lines of
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
45 a multiline option are kept as part of the value.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
46
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
47 When `allow_no_value' is True (default: False), options without
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
48 values are accepted; the value presented for these is None.
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
49
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
50 sections()
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
51 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
52
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
53 has_section(section)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
54 Return whether the given section exists.
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
55
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
56 has_option(section, option)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
57 Return whether the given option exists in the given section.
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
58
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
59 options(section)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
60 Return list of configuration options for the named section.
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
61
63281
e5a2f50fee64 #9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents: 63280
diff changeset
62 read(filenames, encoding=None)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
63 Read and parse the list of named configuration files, given by
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
64 name. A single filename is also allowed. Non-existing files
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
65 are ignored. Return list of successfully read files.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
66
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
67 read_file(f, filename=None)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
68 Read and parse one configuration file, given as a file object.
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
69 The filename defaults to f.name; it is only used in error
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
70 messages (if f has no `name' attribute, the string `<???>' is used).
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
71
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
72 read_string(string)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
73 Read configuration from a given string.
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
74
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
75 read_dict(dictionary)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
76 Read configuration from a dictionary. Keys are section names,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
77 values are dictionaries with keys and values that should be present
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
78 in the section. If the used dictionary type preserves order, sections
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
79 and their keys will be added in order. Values are automatically
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
80 converted to strings.
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
81
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
82 get(section, option, raw=False, vars=None, fallback=_UNSET)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
83 Return a string value for the named option. All % interpolations are
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
84 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
85 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
86 provided using the `vars' argument, which must be a dictionary whose
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
87 contents override any pre-existing defaults. If `option' is a key in
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
88 `vars', the value from `vars' 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
89
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
90 getint(section, options, raw=False, vars=None, fallback=_UNSET)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
91 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
92
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
93 getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
94 Like get(), but convert value to a float.
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
95
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
96 getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
97 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
98 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
99 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
100
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
101 items(section=_UNSET, raw=False, vars=None)
81161
850503a117a9 Fixes issue #15803: incorrect `ConfigParser.items()` docstring
Łukasz Langa <lukasz@langa.pl>
parents: 72985
diff changeset
102 If section is given, return a list of tuples with (name, value) for
850503a117a9 Fixes issue #15803: incorrect `ConfigParser.items()` docstring
Łukasz Langa <lukasz@langa.pl>
parents: 72985
diff changeset
103 each option in the section. Otherwise, return a list of tuples with
850503a117a9 Fixes issue #15803: incorrect `ConfigParser.items()` docstring
Łukasz Langa <lukasz@langa.pl>
parents: 72985
diff changeset
104 (section_name, section_proxy) for each section, including DEFAULTSECT.
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
105
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
106 remove_section(section)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
107 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
108
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
109 remove_option(section, option)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
110 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
111
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
112 set(section, option, value)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
113 Set the given option.
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
114
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
115 write(fp, space_around_delimiters=True)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
116 Write the configuration state in .ini format. If
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
117 `space_around_delimiters' is True (the default), delimiters
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
118 between keys and values are surrounded by spaces.
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
119 """
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
68112
7f8da10bfcef Fix imports from collections.abc
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
121 from collections.abc import MutableMapping
68164
73c5abb41296 Issue #11297: Add collections.ChainMap()
Raymond Hettinger <python@rcn.com>
parents: 68112
diff changeset
122 from collections import OrderedDict as _default_dict, ChainMap as _ChainMap
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
123 import functools
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
124 import io
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
125 import itertools
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
126 import re
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
127 import sys
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
128 import warnings
51960
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
129
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
130 __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
131 "NoOptionError", "InterpolationError", "InterpolationDepthError",
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
132 "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
133 "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
134 "ConfigParser", "SafeConfigParser", "RawConfigParser",
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
135 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
136
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
137 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
138
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
139 MAX_INTERPOLATION_DEPTH = 10
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
140
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
141
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
142
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
143 # exception classes
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
144 class Error(Exception):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
145 """Base class for ConfigParser exceptions."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
146
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
147 def _get_message(self):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
148 """Getter for 'message'; needed only to override deprecation in
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
149 BaseException.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
150 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
151 return self.__message
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
152
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
153 def _set_message(self, value):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
154 """Setter for 'message'; needed only to override deprecation in
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
155 BaseException.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
156 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
157 self.__message = value
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
158
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
159 # BaseException.message has been deprecated since Python 2.6. To prevent
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
160 # DeprecationWarning from popping up over this pre-existing attribute, use
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
161 # a new property that takes lookup precedence.
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
162 message = property(_get_message, _set_message)
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
163
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
164 def __init__(self, msg=''):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
165 self.message = msg
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
166 Exception.__init__(self, msg)
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
167
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def __repr__(self):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
169 return self.message
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
170
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
171 __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
172
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
173
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
174 class NoSectionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
175 """Raised when no section matches a requested option."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
176
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
177 def __init__(self, section):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
178 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
179 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
180 self.args = (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
181
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 class DuplicateSectionError(Error):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
184 """Raised when a section is repeated in an input source.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
185
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
186 Possible repetitions that raise this exception are: multiple creation
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
187 using the API or in strict parsers when a section is found more than once
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
188 in a single input file, string or dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
189 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
190
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
191 def __init__(self, section, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
192 msg = [repr(section), " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
193 if source is not None:
84279
56c1227f21f5 Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents: 83508
diff changeset
194 message = ["While reading from ", repr(source)]
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
195 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
196 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
197 message.append(": section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
198 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
199 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
200 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
201 msg.insert(0, "Section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
202 Error.__init__(self, "".join(msg))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
203 self.section = section
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
204 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
205 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
206 self.args = (section, source, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
207
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
208
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
209 class DuplicateOptionError(Error):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
210 """Raised by strict parsers when an option is repeated in an input source.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
211
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
212 Current implementation raises this exception only when an option is found
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
213 more than once in a single file, string or dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
214 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
215
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
216 def __init__(self, section, option, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
217 msg = [repr(option), " in section ", repr(section),
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
218 " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
219 if source is not None:
84279
56c1227f21f5 Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents: 83508
diff changeset
220 message = ["While reading from ", repr(source)]
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
221 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
222 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
223 message.append(": option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
224 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
225 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
226 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
227 msg.insert(0, "Option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
228 Error.__init__(self, "".join(msg))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
229 self.section = section
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
230 self.option = option
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
231 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
232 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
233 self.args = (section, option, source, lineno)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 class NoOptionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
237 """A requested option was not found."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
238
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
239 def __init__(self, option, section):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
240 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
241 (option, section))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
242 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
243 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
244 self.args = (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
245
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
5b24cbb1f99b 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 class InterpolationError(Error):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
248 """Base class for interpolation-related exceptions."""
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
249
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
250 def __init__(self, option, section, msg):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
251 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
252 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
253 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
254 self.args = (option, section, msg)
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
257 class InterpolationMissingOptionError(InterpolationError):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
258 """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
259
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
260 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
261 msg = ("Bad value substitution:\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
262 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
263 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
264 "\tkey : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
265 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
266 % (section, option, reference, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
267 InterpolationError.__init__(self, option, section, msg)
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
268 self.reference = reference
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
269 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
270
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
271
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
272 class InterpolationSyntaxError(InterpolationError):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
273 """Raised when the source text contains invalid syntax.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
274
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
275 Current implementation raises this exception when the source text into
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
276 which substitutions are made does not conform to the required syntax.
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
277 """
26907
ccba3b66d676 Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26685
diff changeset
278
ccba3b66d676 Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26685
diff changeset
279
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
280 class InterpolationDepthError(InterpolationError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
281 """Raised when substitutions are nested too deeply."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
282
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
283 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
284 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
285 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
286 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
287 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
288 % (section, option, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
289 InterpolationError.__init__(self, option, section, msg)
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
290 self.args = (option, section, rawval)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
291
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
292
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
293 class ParsingError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
294 """Raised when a configuration file does not follow legal syntax."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
295
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
296 def __init__(self, source=None, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
297 # Exactly one of `source'/`filename' arguments has to be given.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
298 # `filename' kept for compatibility.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
299 if filename and source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
300 raise ValueError("Cannot specify both `filename' and `source'. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
301 "Use `source'.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
302 elif not filename and not source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
303 raise ValueError("Required argument `source' not given.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
304 elif filename:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
305 source = filename
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
306 Error.__init__(self, 'Source contains parsing errors: %s' % source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
307 self.source = source
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
308 self.errors = []
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
309 self.args = (source, )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
310
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
311 @property
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
312 def filename(self):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
313 """Deprecated, use `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
314 warnings.warn(
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
315 "The 'filename' attribute will be removed in future versions. "
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
316 "Use 'source' instead.",
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
317 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
318 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
319 return self.source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
320
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
321 @filename.setter
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
322 def filename(self, value):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
323 """Deprecated, user `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
324 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
325 "The 'filename' attribute will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
326 "Use 'source' instead.",
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
327 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
328 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
329 self.source = value
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
330
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
331 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
332 self.errors.append((lineno, line))
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
333 self.message += '\n\t[line %2d]: %s' % (lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
334
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
335
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
336 class MissingSectionHeaderError(ParsingError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
337 """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
338
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
339 def __init__(self, filename, lineno, line):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
340 Error.__init__(
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
341 self,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
342 '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
343 (filename, lineno, line))
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
344 self.source = filename
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
345 self.lineno = lineno
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
346 self.line = line
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
347 self.args = (filename, lineno, line)
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
348
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
349
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
350 # Used in parser getters to indicate the default behaviour when a specific
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
351 # option is not found it to raise an exception. Created to enable `None' as
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
352 # a valid fallback value.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
353 _UNSET = object()
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
354
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
355
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
356 class Interpolation:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
357 """Dummy interpolation that passes the value through with no changes."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
358
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
359 def before_get(self, parser, section, option, value, defaults):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
360 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
361
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
362 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
363 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
364
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
365 def before_read(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
366 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
367
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
368 def before_write(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
369 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
370
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
371
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
372 class BasicInterpolation(Interpolation):
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
373 """Interpolation as implemented in the classic ConfigParser.
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
374
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
375 The option values can contain format strings which refer to other values in
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
376 the same section, or values in the special default section.
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
377
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
378 For example:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
379
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
380 something: %(dir)s/whatever
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
381
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
382 would resolve the "%(dir)s" to the value of dir. All reference
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
383 expansions are done late, on demand. If a user needs to use a bare % in
72985
e19ccbbe70dd Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69687
diff changeset
384 a configuration file, she can escape it by writing %%. Other % usage
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
385 is considered a user error and raises `InterpolationSyntaxError'."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
386
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
387 _KEYCRE = re.compile(r"%\(([^)]+)\)s")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
388
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
389 def before_get(self, parser, section, option, value, defaults):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
390 L = []
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
391 self._interpolate_some(parser, option, L, value, section, defaults, 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
392 return ''.join(L)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
393
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
394 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
395 tmp_value = value.replace('%%', '') # escaped percent signs
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
396 tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
397 if '%' in tmp_value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
398 raise ValueError("invalid interpolation syntax in %r at "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
399 "position %d" % (value, tmp_value.find('%')))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
400 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
401
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
402 def _interpolate_some(self, parser, option, accum, rest, section, map,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
403 depth):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
404 if depth > MAX_INTERPOLATION_DEPTH:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
405 raise InterpolationDepthError(option, section, rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
406 while rest:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
407 p = rest.find("%")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
408 if p < 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
409 accum.append(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
410 return
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
411 if p > 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
412 accum.append(rest[:p])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
413 rest = rest[p:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
414 # p is no longer used
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
415 c = rest[1:2]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
416 if c == "%":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
417 accum.append("%")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
418 rest = rest[2:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
419 elif c == "(":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
420 m = self._KEYCRE.match(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
421 if m is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
422 raise InterpolationSyntaxError(option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
423 "bad interpolation variable reference %r" % rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
424 var = parser.optionxform(m.group(1))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
425 rest = rest[m.end():]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
426 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
427 v = map[var]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
428 except KeyError:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
429 raise InterpolationMissingOptionError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
430 option, section, rest, var)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
431 if "%" in v:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
432 self._interpolate_some(parser, option, accum, v,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
433 section, map, depth + 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
434 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
435 accum.append(v)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
436 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
437 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
438 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
439 "'%%' must be followed by '%%' or '(', "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
440 "found: %r" % (rest,))
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
441
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
442
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
443 class ExtendedInterpolation(Interpolation):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
444 """Advanced variant of interpolation, supports the syntax used by
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
445 `zc.buildout'. Enables interpolation between sections."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
446
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
447 _KEYCRE = re.compile(r"\$\{([^}]+)\}")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
448
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
449 def before_get(self, parser, section, option, value, defaults):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
450 L = []
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
451 self._interpolate_some(parser, option, L, value, section, defaults, 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
452 return ''.join(L)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
453
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
454 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
455 tmp_value = value.replace('$$', '') # escaped dollar signs
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
456 tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
457 if '$' in tmp_value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
458 raise ValueError("invalid interpolation syntax in %r at "
83508
44d764238f0d fix character index in ExtendedInterpolation's exception message
Łukasz Langa <lukasz@langa.pl>
parents: 81211
diff changeset
459 "position %d" % (value, tmp_value.find('$')))
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
460 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
461
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
462 def _interpolate_some(self, parser, option, accum, rest, section, map,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
463 depth):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
464 if depth > MAX_INTERPOLATION_DEPTH:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
465 raise InterpolationDepthError(option, section, rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
466 while rest:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
467 p = rest.find("$")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
468 if p < 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
469 accum.append(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
470 return
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
471 if p > 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
472 accum.append(rest[:p])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
473 rest = rest[p:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
474 # p is no longer used
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
475 c = rest[1:2]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
476 if c == "$":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
477 accum.append("$")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
478 rest = rest[2:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
479 elif c == "{":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
480 m = self._KEYCRE.match(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
481 if m is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
482 raise InterpolationSyntaxError(option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
483 "bad interpolation variable reference %r" % rest)
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
484 path = m.group(1).split(':')
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
485 rest = rest[m.end():]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
486 sect = section
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
487 opt = option
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
488 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
489 if len(path) == 1:
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
490 opt = parser.optionxform(path[0])
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
491 v = map[opt]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
492 elif len(path) == 2:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
493 sect = path[0]
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
494 opt = parser.optionxform(path[1])
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
495 v = parser.get(sect, opt, raw=True)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
496 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
497 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
498 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
499 "More than one ':' found: %r" % (rest,))
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
500 except (KeyError, NoSectionError, NoOptionError):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
501 raise InterpolationMissingOptionError(
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
502 option, section, rest, ":".join(path))
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
503 if "$" in v:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
504 self._interpolate_some(parser, opt, accum, v, sect,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
505 dict(parser.items(sect, raw=True)),
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
506 depth + 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
507 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
508 accum.append(v)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
509 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
510 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
511 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
512 "'$' must be followed by '$' or '{', "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
513 "found: %r" % (rest,))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
514
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
515
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
516 class LegacyInterpolation(Interpolation):
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
517 """Deprecated interpolation used in old versions of ConfigParser.
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
518 Use BasicInterpolation or ExtendedInterpolation instead."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
519
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
520 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
521
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
522 def before_get(self, parser, section, option, value, vars):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
523 rawval = value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
524 depth = MAX_INTERPOLATION_DEPTH
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
525 while depth: # Loop through this until it's done
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
526 depth -= 1
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
527 if value and "%(" in value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
528 replace = functools.partial(self._interpolation_replace,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
529 parser=parser)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
530 value = self._KEYCRE.sub(replace, value)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
531 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
532 value = value % vars
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
533 except KeyError as e:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
534 raise InterpolationMissingOptionError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
535 option, section, rawval, e.args[0])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
536 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
537 break
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
538 if value and "%(" in value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
539 raise InterpolationDepthError(option, section, rawval)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
540 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
541
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
542 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
543 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
544
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
545 @staticmethod
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
546 def _interpolation_replace(match, parser):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
547 s = match.group(1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
548 if s is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
549 return match.group()
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
550 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
551 return "%%(%s)s" % parser.optionxform(s)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
552
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
553
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
554 class RawConfigParser(MutableMapping):
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
555 """ConfigParser that does not do interpolation."""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
556
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
557 # Regular expressions for parsing section headers and options
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
558 _SECT_TMPL = r"""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
559 \[ # [
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
560 (?P<header>[^]]+) # very permissive!
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
561 \] # ]
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
562 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
563 _OPT_TMPL = r"""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
564 (?P<option>.*?) # very permissive!
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
565 \s*(?P<vi>{delim})\s* # any number of space/tab,
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
566 # followed by any of the
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
567 # allowed delimiters,
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
568 # followed by any space/tab
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
569 (?P<value>.*)$ # everything up to eol
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
570 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
571 _OPT_NV_TMPL = r"""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
572 (?P<option>.*?) # very permissive!
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
573 \s*(?: # any number of space/tab,
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
574 (?P<vi>{delim})\s* # optionally followed by
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
575 # any of the allowed
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
576 # delimiters, followed by any
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
577 # space/tab
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
578 (?P<value>.*))?$ # everything up to eol
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
579 """
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
580 # Interpolation algorithm to be used if the user does not specify another
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
581 _DEFAULT_INTERPOLATION = Interpolation()
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
582 # Compiled regular expression for matching sections
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
583 SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
584 # Compiled regular expression for matching options with typical separators
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
585 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
586 # Compiled regular expression for matching options with optional values
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
587 # delimited using typical separators
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
588 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
589 # Compiled regular expression for matching leading whitespace in a line
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
590 NONSPACECRE = re.compile(r"\S")
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
591 # Possible boolean values in the configuration.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
592 BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
593 '0': False, 'no': False, 'false': False, 'off': False}
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
594
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
595 def __init__(self, defaults=None, dict_type=_default_dict,
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
596 allow_no_value=False, *, delimiters=('=', ':'),
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
597 comment_prefixes=('#', ';'), inline_comment_prefixes=None,
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
598 strict=True, empty_lines_in_values=True,
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
599 default_section=DEFAULTSECT,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
600 interpolation=_UNSET):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
601
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
602 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
603 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
604 self._defaults = self._dict()
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
605 self._proxies = self._dict()
66287
51e1cbdd8fd2 configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents: 66151
diff changeset
606 self._proxies[default_section] = SectionProxy(self, default_section)
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
607 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
608 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
609 self._defaults[self.optionxform(key)] = value
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
610 self._delimiters = tuple(delimiters)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
611 if delimiters == ('=', ':'):
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
612 self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
613 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
614 d = "|".join(re.escape(d) for d in delimiters)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
615 if allow_no_value:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
616 self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
617 re.VERBOSE)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
618 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
619 self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
620 re.VERBOSE)
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
621 self._comment_prefixes = tuple(comment_prefixes or ())
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
622 self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
623 self._strict = strict
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
624 self._allow_no_value = allow_no_value
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
625 self._empty_lines_in_values = empty_lines_in_values
69668
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
626 self.default_section=default_section
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
627 self._interpolation = interpolation
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
628 if self._interpolation is _UNSET:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
629 self._interpolation = self._DEFAULT_INTERPOLATION
69668
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
630 if self._interpolation is None:
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
631 self._interpolation = Interpolation()
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
632
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 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
634 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
635
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 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
637 """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
638 # self._sections will never have [DEFAULT] in it
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
639 return list(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
640
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 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
642 """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
643
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
644 Raise DuplicateSectionError if a section by the specified name
66287
51e1cbdd8fd2 configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents: 66151
diff changeset
645 already exists. Raise ValueError if name is DEFAULT.
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
646 """
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
647 if section == self.default_section:
66665
1486416c86d9 %s -> %r correction after review by Éric Araujo
Łukasz Langa <lukasz@langa.pl>
parents: 66647
diff changeset
648 raise ValueError('Invalid section name: %r' % section)
45446
5e0772ed20d1 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
649
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
650 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
651 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
652 self._sections[section] = self._dict()
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
653 self._proxies[section] = SectionProxy(self, 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
654
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 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
656 """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
657
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
658 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
659 """
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
660 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
661
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 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
663 """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
664 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
665 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
666 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
667 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
668 opts.update(self._defaults)
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
669 return list(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
670
63281
e5a2f50fee64 #9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents: 63280
diff changeset
671 def read(self, filenames, encoding=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
672 """Read and parse a filename or a list of filenames.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
673
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
674 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
675 designed so that you can specify a list of potential
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
676 configuration file locations (e.g. current directory, user's
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
677 home directory, systemwide directory), and all existing
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
678 configuration files in the list will be read. A single
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
679 filename may also be given.
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
680
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
681 Return list of successfully read files.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
682 """
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
683 if isinstance(filenames, str):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
684 filenames = [filenames]
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
685 read_ok = []
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
686 for filename in filenames:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
687 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
688 with open(filename, encoding=encoding) as fp:
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
689 self._read(fp, filename)
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 77980
diff changeset
690 except OSError:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
691 continue
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
692 read_ok.append(filename)
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
693 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
694
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
695 def read_file(self, f, source=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
696 """Like read() but the argument must be a file-like object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
697
69631
6f937d6369b6 Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents: 68257
diff changeset
698 The `f' argument must be iterable, returning one line at a time.
6f937d6369b6 Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents: 68257
diff changeset
699 Optional second argument is the `source' specifying the name of the
6f937d6369b6 Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents: 68257
diff changeset
700 file being read. If not given, it is taken from f.name. If `f' has no
6f937d6369b6 Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents: 68257
diff changeset
701 `name' attribute, `<???>' is used.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
702 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
703 if source is None:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
704 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
705 source = f.name
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
706 except AttributeError:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
707 source = '<???>'
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
708 self._read(f, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
709
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
710 def read_string(self, string, source='<string>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
711 """Read configuration from a given string."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
712 sfile = io.StringIO(string)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
713 self.read_file(sfile, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
714
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
715 def read_dict(self, dictionary, source='<dict>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
716 """Read configuration from a dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
717
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
718 Keys are section names, values are dictionaries with keys and values
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
719 that should be present in the section. If the used dictionary type
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
720 preserves order, sections and their keys will be added in order.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
721
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
722 All types held in the dictionary are converted to strings during
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
723 reading, including section names, option names and 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
724
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
725 Optional second argument is the `source' specifying the name of the
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
726 dictionary being read.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
727 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
728 elements_added = set()
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
729 for section, keys in dictionary.items():
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
730 section = str(section)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
731 try:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
732 self.add_section(section)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
733 except (DuplicateSectionError, ValueError):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
734 if self._strict and section in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
735 raise
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
736 elements_added.add(section)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
737 for key, value in keys.items():
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
738 key = self.optionxform(str(key))
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
739 if value is not None:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
740 value = str(value)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
741 if self._strict and (section, key) in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
742 raise DuplicateOptionError(section, key, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
743 elements_added.add((section, key))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
744 self.set(section, key, value)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
745
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
746 def readfp(self, fp, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
747 """Deprecated, use read_file instead."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
748 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
749 "This method will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
750 "Use 'parser.read_file()' instead.",
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
751 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
752 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
753 self.read_file(fp, source=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
754
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
755 def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
756 """Get an option value for a given section.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
757
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
758 If `vars' is provided, it must be a dictionary. The option is looked up
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
759 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
760 If the key is not found and `fallback' is provided, it is used as
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
761 a fallback value. `None' can be provided as a `fallback' value.
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
762
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
763 If interpolation is enabled and the optional argument `raw' is False,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
764 all interpolations are expanded in the return values.
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
765
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
766 Arguments `raw', `vars', and `fallback' are keyword only.
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
767
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
768 The section DEFAULT is special.
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
769 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
770 try:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
771 d = self._unify_values(section, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
772 except NoSectionError:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
773 if fallback is _UNSET:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
774 raise
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
775 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
776 return fallback
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
777 option = self.optionxform(option)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
778 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
779 value = d[option]
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
780 except KeyError:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
781 if fallback is _UNSET:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
782 raise NoOptionError(option, section)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
783 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
784 return fallback
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
785
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
786 if raw or value is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
787 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
788 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
789 return self._interpolation.before_get(self, section, option, value,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
790 d)
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
791
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
792 def _get(self, section, conv, option, **kwargs):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
793 return conv(self.get(section, option, **kwargs))
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
794
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
795 def getint(self, section, option, *, raw=False, vars=None,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
796 fallback=_UNSET):
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
797 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
798 return self._get(section, int, option, raw=raw, vars=vars)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
799 except (NoSectionError, NoOptionError):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
800 if fallback is _UNSET:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
801 raise
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
802 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
803 return fallback
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
804
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
805 def getfloat(self, section, option, *, raw=False, vars=None,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
806 fallback=_UNSET):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
807 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
808 return self._get(section, float, option, raw=raw, vars=vars)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
809 except (NoSectionError, NoOptionError):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
810 if fallback is _UNSET:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
811 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
812 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
813 return fallback
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
814
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
815 def getboolean(self, section, option, *, raw=False, vars=None,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
816 fallback=_UNSET):
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
817 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
818 return self._get(section, self._convert_to_boolean, option,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
819 raw=raw, vars=vars)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
820 except (NoSectionError, NoOptionError):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
821 if fallback is _UNSET:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
822 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
823 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
824 return fallback
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
825
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
826 def items(self, section=_UNSET, raw=False, vars=None):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
827 """Return a list of (name, value) tuples for each option in a 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
828
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
829 All % interpolations are expanded in the return values, based on the
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
830 defaults passed into the constructor, unless the optional argument
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
831 `raw' is true. Additional substitutions may be provided using the
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
832 `vars' argument, which must be a dictionary whose contents overrides
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
833 any pre-existing defaults.
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
834
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
835 The section DEFAULT is special.
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
836 """
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
837 if section is _UNSET:
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
838 return super().items()
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
839 d = self._defaults.copy()
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
840 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
841 d.update(self._sections[section])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
842 except KeyError:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
843 if section != self.default_section:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
844 raise NoSectionError(section)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
845 # Update with the entry specific variables
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
846 if vars:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
847 for key, value in vars.items():
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
848 d[self.optionxform(key)] = value
66694
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
849 value_getter = lambda option: self._interpolation.before_get(self,
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
850 section, option, d[option], d)
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
851 if raw:
66694
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
852 value_getter = lambda option: d[option]
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
853 return [(option, value_getter(option)) for option in d.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
854
81164
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
855 def popitem(self):
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
856 """Remove a section from the parser and return it as
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
857 a (section_name, section_proxy) tuple. If no section is present, raise
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
858 KeyError.
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
859
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
860 The section DEFAULT is never returned because it cannot be removed.
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
861 """
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
862 for key in self.sections():
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
863 value = self[key]
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
864 del self[key]
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
865 return key, value
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
866 raise KeyError
dc5adc08f1a8 Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81161
diff changeset
867
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
868 def optionxform(self, optionstr):
16480
e97217f5b323 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
869 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
870
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
871 def has_option(self, section, option):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
872 """Check for the existence of a given option in a given section.
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
873 If the specified `section' is None or an empty string, DEFAULT is
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
874 assumed. If the specified `section' does not exist, returns False."""
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
875 if not section or section == self.default_section:
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
876 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
877 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
878 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
879 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
880 else:
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
881 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
882 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
883 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
884
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
885 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
886 """Set an option."""
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
887 if value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
888 value = self._interpolation.before_set(self, section, option,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
889 value)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
890 if not section or section == self.default_section:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
891 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
892 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
893 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
894 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
895 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
896 raise NoSectionError(section)
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
897 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
898
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
899 def write(self, fp, space_around_delimiters=True):
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
900 """Write an .ini-format representation of the configuration state.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
901
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
902 If `space_around_delimiters' is True (the default), delimiters
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
903 between keys and values are surrounded by spaces.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
904 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
905 if space_around_delimiters:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
906 d = " {} ".format(self._delimiters[0])
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
907 else:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
908 d = self._delimiters[0]
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
909 if self._defaults:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
910 self._write_section(fp, self.default_section,
66287
51e1cbdd8fd2 configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents: 66151
diff changeset
911 self._defaults.items(), d)
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
912 for section in self._sections:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
913 self._write_section(fp, section,
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
914 self._sections[section].items(), d)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
915
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
916 def _write_section(self, fp, section_name, section_items, delimiter):
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
917 """Write a single section to the specified `fp'."""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
918 fp.write("[{}]\n".format(section_name))
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
919 for key, value in section_items:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
920 value = self._interpolation.before_write(self, section_name, key,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
921 value)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
922 if value is not None or not self._allow_no_value:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
923 value = delimiter + str(value).replace('\n', '\n\t')
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
924 else:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
925 value = ""
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
926 fp.write("{}{}\n".format(key, value))
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
927 fp.write("\n")
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
928
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
929 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
930 """Remove an option."""
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
931 if not section or section == self.default_section:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
932 sectdict = self._defaults
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
933 else:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
934 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
935 sectdict = self._sections[section]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
936 except KeyError:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
937 raise NoSectionError(section)
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
938 option = self.optionxform(option)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23611
diff changeset
939 existed = option in sectdict
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
940 if existed:
15479
e98964bc2681 remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents: 14899
diff changeset
941 del sectdict[option]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
942 return existed
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
943
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
944 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
945 """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
946 existed = section in self._sections
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
947 if existed:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
948 del self._sections[section]
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
949 del self._proxies[section]
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
950 return existed
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
951
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
952 def __getitem__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
953 if key != self.default_section and not self.has_section(key):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
954 raise KeyError(key)
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
955 return self._proxies[key]
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
956
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
957 def __setitem__(self, key, value):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
958 # To conform with the mapping protocol, overwrites existing values in
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
959 # the section.
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
960
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
961 # XXX this is not atomic if read_dict fails at any point. Then again,
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
962 # no update method in configparser is atomic in this implementation.
81170
459a23083b66 Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81166
diff changeset
963 if key == self.default_section:
459a23083b66 Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents: 81166
diff changeset
964 self._defaults.clear()
81205
f580342b63d8 configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents: 81170
diff changeset
965 elif key in self._sections:
81210
6f0cee62f0c6 configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents: 81164
diff changeset
966 self._sections[key].clear()
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
967 self.read_dict({key: value})
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
968
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
969 def __delitem__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
970 if key == self.default_section:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
971 raise ValueError("Cannot remove the default section.")
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
972 if not self.has_section(key):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
973 raise KeyError(key)
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
974 self.remove_section(key)
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
975
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
976 def __contains__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
977 return key == self.default_section or self.has_section(key)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
978
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
979 def __len__(self):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
980 return len(self._sections) + 1 # the default section
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
981
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
982 def __iter__(self):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
983 # XXX does it break when underlying container state changed?
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
984 return itertools.chain((self.default_section,), self._sections.keys())
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
985
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
986 def _read(self, fp, fpname):
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
987 """Parse a sectioned configuration file.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
988
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
989 Each section in a configuration file contains a header, indicated by
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
990 a name in square brackets (`[]'), plus key/value options, indicated by
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
991 `name' and `value' delimited with a specific substring (`=' or `:' by
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
992 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
993
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
994 Values can span multiple lines, as long as they are indented deeper
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
995 than the first line of the value. Depending on the parser's mode, blank
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
996 lines may be treated as parts of multiline values or ignored.
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
997
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
998 Configuration files may include comments, prefixed by specific
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
999 characters (`#' and `;' by default). Comments may appear on their own
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1000 in an otherwise empty line or may be entered in lines holding values or
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1001 section names.
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1002 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1003 elements_added = set()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1004 cursect = None # None, or a dictionary
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1005 sectname = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1006 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1007 lineno = 0
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1008 indent_level = 0
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1009 e = None # None, or an exception
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1010 for lineno, line in enumerate(fp, start=1):
77980
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1011 comment_start = sys.maxsize
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1012 # strip inline comments
77980
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1013 inline_prefixes = {p: -1 for p in self._inline_comment_prefixes}
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1014 while comment_start == sys.maxsize and inline_prefixes:
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1015 next_prefixes = {}
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1016 for prefix, index in inline_prefixes.items():
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1017 index = line.find(prefix, index+1)
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1018 if index == -1:
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1019 continue
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1020 next_prefixes[prefix] = index
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1021 if index == 0 or (index > 0 and line[index-1].isspace()):
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1022 comment_start = min(comment_start, index)
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1023 inline_prefixes = next_prefixes
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1024 # strip full line comments
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1025 for prefix in self._comment_prefixes:
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1026 if line.strip().startswith(prefix):
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1027 comment_start = 0
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1028 break
77980
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1029 if comment_start == sys.maxsize:
5257bb466d18 Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents: 72986
diff changeset
1030 comment_start = None
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1031 value = line[:comment_start].strip()
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1032 if not value:
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1033 if self._empty_lines_in_values:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1034 # add empty line to the value, but only if there was no
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1035 # comment on the line
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1036 if (comment_start is None and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1037 cursect is not None and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1038 optname and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1039 cursect[optname] is not None):
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1040 cursect[optname].append('') # newlines added at join
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1041 else:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1042 # empty line marks end of value
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1043 indent_level = sys.maxsize
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1044 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1045 # continuation line?
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1046 first_nonspace = self.NONSPACECRE.search(line)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1047 cur_indent_level = first_nonspace.start() if first_nonspace else 0
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1048 if (cursect is not None and optname and
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1049 cur_indent_level > indent_level):
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1050 cursect[optname].append(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1051 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1052 else:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1053 indent_level = cur_indent_level
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1054 # is it a section header?
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1055 mo = self.SECTCRE.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1056 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1057 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
1058 if sectname in self._sections:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1059 if self._strict and sectname in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1060 raise DuplicateSectionError(sectname, fpname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1061 lineno)
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1062 cursect = self._sections[sectname]
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1063 elements_added.add(sectname)
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1064 elif sectname == self.default_section:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1065 cursect = self._defaults
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1066 else:
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
1067 cursect = self._dict()
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1068 self._sections[sectname] = cursect
66151
5572b86967ff configparser.py: changed PendingDeprecationWarnings to DeprecationWarnings, via http://mail.python.org/pipermail/python-dev/2010-November/105391.html
Łukasz Langa <lukasz@langa.pl>
parents: 66143
diff changeset
1069 self._proxies[sectname] = SectionProxy(self, sectname)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1070 elements_added.add(sectname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1071 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1072 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1073 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1074 elif cursect is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
1075 raise MissingSectionHeaderError(fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1076 # 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
1077 else:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1078 mo = self._optcre.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1079 if mo:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
1080 optname, vi, optval = mo.group('option', 'vi', 'value')
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1081 if not optname:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1082 e = self._handle_error(e, fpname, lineno, line)
25462
16e661970930 Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents: 25461
diff changeset
1083 optname = self.optionxform(optname.rstrip())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1084 if (self._strict and
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1085 (sectname, optname) in elements_added):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1086 raise DuplicateOptionError(sectname, optname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1087 fpname, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1088 elements_added.add((sectname, optname))
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1089 # This check is fine because the OPTCRE cannot
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1090 # match if it would set optval to None
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1091 if optval is not None:
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1092 optval = optval.strip()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1093 cursect[optname] = [optval]
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1094 else:
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1095 # valueless option handling
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1096 cursect[optname] = None
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1097 else:
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1098 # a non-fatal parsing error occurred. set up the
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1099 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1100 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1101 # list of all bogus lines
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1102 e = self._handle_error(e, fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1103 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1104 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1105 raise e
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1106 self._join_multiline_values()
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1107
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1108 def _join_multiline_values(self):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1109 defaults = self.default_section, self._defaults
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1110 all_sections = itertools.chain((defaults,),
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1111 self._sections.items())
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1112 for section, options in all_sections:
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1113 for name, val in options.items():
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1114 if isinstance(val, list):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1115 val = '\n'.join(val).rstrip()
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1116 options[name] = self._interpolation.before_read(self,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1117 section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1118 name, val)
67852
2e3c5841a0a6 collections.Mapping is not available for setup.py. Remove the dependency the old-fashioned way (copy and paste).
Raymond Hettinger <python@rcn.com>
parents: 67849
diff changeset
1119
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1120 def _handle_error(self, exc, fpname, lineno, line):
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1121 if not exc:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1122 exc = ParsingError(fpname)
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1123 exc.append(lineno, repr(line))
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
1124 return exc
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1125
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1126 def _unify_values(self, section, vars):
68081
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 66997
diff changeset
1127 """Create a sequence of lookups with 'vars' taking priority over
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 66997
diff changeset
1128 the 'section' which takes priority over the DEFAULTSECT.
63467
2ca842dcbe11 Merged revisions 83226-83227,83229-83232 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53525
diff changeset
1129
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1130 """
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1131 sectiondict = {}
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1132 try:
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1133 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
1134 except KeyError:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1135 if section != self.default_section:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1136 raise NoSectionError(section)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1137 # Update with the entry specific variables
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1138 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
1139 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
1140 for key, value in vars.items():
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1141 if value is not None:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1142 value = str(value)
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1143 vardict[self.optionxform(key)] = value
68081
b17bba1c7303 Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 66997
diff changeset
1144 return _ChainMap(vardict, sectiondict, self._defaults)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1145
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1146 def _convert_to_boolean(self, value):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1147 """Return a boolean value translating from other types if necessary.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1148 """
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1149 if value.lower() not in self.BOOLEAN_STATES:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1150 raise ValueError('Not a boolean: %s' % value)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1151 return self.BOOLEAN_STATES[value.lower()]
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1152
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1153 def _validate_value_types(self, *, section="", option="", value=""):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1154 """Raises a TypeError for non-string values.
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1155
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1156 The only legal non-string value if we allow valueless
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1157 options is None, so we need to check if the value is a
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1158 string if:
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1159 - we do not allow valueless options, or
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1160 - we allow valueless options but the value is not None
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1161
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1162 For compatibility reasons this method is not used in classic set()
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1163 for RawConfigParsers. It is invoked in every case for mapping protocol
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1164 access and in ConfigParser.set().
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1165 """
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1166 if not isinstance(section, str):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1167 raise TypeError("section names must be strings")
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1168 if not isinstance(option, str):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1169 raise TypeError("option keys must be strings")
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1170 if not self._allow_no_value or value:
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1171 if not isinstance(value, str):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1172 raise TypeError("option values must be strings")
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1173
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1174
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1175 class ConfigParser(RawConfigParser):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1176 """ConfigParser implementing interpolation."""
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1177
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1178 _DEFAULT_INTERPOLATION = BasicInterpolation()
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1179
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1180 def set(self, section, option, value=None):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1181 """Set an option. Extends RawConfigParser.set by validating type and
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1182 interpolation syntax on the value."""
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1183 self._validate_value_types(option=option, value=value)
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1184 super().set(section, option, value)
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
1185
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1186 def add_section(self, section):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1187 """Create a new section in the configuration. Extends
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1188 RawConfigParser.add_section by validating if the section name is
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1189 a string."""
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1190 self._validate_value_types(section=section)
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1191 super().add_section(section)
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
1192
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1193
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1194 class SafeConfigParser(ConfigParser):
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1195 """ConfigParser alias for backwards compatibility purposes."""
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1196
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1197 def __init__(self, *args, **kwargs):
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1198 super().__init__(*args, **kwargs)
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1199 warnings.warn(
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1200 "The SafeConfigParser class has been renamed to ConfigParser "
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1201 "in Python 3.2. This alias will be removed in future versions."
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1202 " Use ConfigParser directly instead.",
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1203 DeprecationWarning, stacklevel=2
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1204 )
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1205
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1206
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1207 class SectionProxy(MutableMapping):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1208 """A proxy for a single section from a parser."""
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1209
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1210 def __init__(self, parser, name):
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1211 """Creates a view on a section of the specified `name` in `parser`."""
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1212 self._parser = parser
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1213 self._name = name
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1214
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1215 def __repr__(self):
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1216 return '<Section: {}>'.format(self._name)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1217
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1218 def __getitem__(self, key):
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1219 if not self._parser.has_option(self._name, key):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1220 raise KeyError(key)
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1221 return self._parser.get(self._name, key)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1222
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1223 def __setitem__(self, key, value):
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1224 self._parser._validate_value_types(option=key, value=value)
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1225 return self._parser.set(self._name, key, value)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1226
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1227 def __delitem__(self, key):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1228 if not (self._parser.has_option(self._name, key) and
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1229 self._parser.remove_option(self._name, key)):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1230 raise KeyError(key)
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1231
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1232 def __contains__(self, key):
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1233 return self._parser.has_option(self._name, key)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1234
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1235 def __len__(self):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1236 return len(self._options())
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1237
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1238 def __iter__(self):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1239 return self._options().__iter__()
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1240
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1241 def _options(self):
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1242 if self._name != self._parser.default_section:
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1243 return self._parser.options(self._name)
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1244 else:
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1245 return self._parser.defaults()
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1246
66722
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1247 def get(self, option, fallback=None, *, raw=False, vars=None):
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1248 return self._parser.get(self._name, option, raw=raw, vars=vars,
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1249 fallback=fallback)
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
1250
66722
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1251 def getint(self, option, fallback=None, *, raw=False, vars=None):
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1252 return self._parser.getint(self._name, option, raw=raw, vars=vars,
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1253 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1254
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1255 def getfloat(self, option, fallback=None, *, raw=False, vars=None):
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1256 return self._parser.getfloat(self._name, option, raw=raw, vars=vars,
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1257 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1258
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1259 def getboolean(self, option, fallback=None, *, raw=False, vars=None):
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1260 return self._parser.getboolean(self._name, option, raw=raw, vars=vars,
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1261 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1262
66350
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1263 @property
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1264 def parser(self):
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1265 # The parser object of the proxy is read-only.
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1266 return self._parser
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1267
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1268 @property
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1269 def name(self):
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1270 # The name of the section on a proxy is read-only.
f8ceb6afd7a9 configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents: 66349
diff changeset
1271 return self._name