annotate Lib/configparser.py @ 74006:88198b93ff2f

Issue #13560: Add PyUnicode_EncodeLocale() * Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not available * Document my last changes in Misc/NEWS
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 17 Dec 2011 04:13:41 +0100
parents bfbe144986d7
children 5257bb466d18
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)
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
102 If section is given, return a list of tuples with (section_name,
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
103 section_proxy) for each section, including DEFAULTSECT. Otherwise,
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
104 return a list of tuples with (name, value) for each option
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
105 in the section.
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
106
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
107 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
108 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
109
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
110 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
111 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
112
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
113 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
114 Set the given option.
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
115
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
116 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
117 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
118 `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
119 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
120 """
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
68112
7f8da10bfcef Fix imports from collections.abc
Raymond Hettinger <python@rcn.com>
parents: 68081
diff changeset
122 from collections.abc import MutableMapping
68164
73c5abb41296 Issue #11297: Add collections.ChainMap()
Raymond Hettinger <python@rcn.com>
parents: 68112
diff changeset
123 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
124 import functools
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
125 import io
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
126 import itertools
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
127 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
128 import sys
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
129 import warnings
51960
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
130
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
131 __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
132 "NoOptionError", "InterpolationError", "InterpolationDepthError",
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
133 "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
134 "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
135 "ConfigParser", "SafeConfigParser", "RawConfigParser",
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
136 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
137
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 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
139
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
140 MAX_INTERPOLATION_DEPTH = 10
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
141
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
143
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
144 # exception classes
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
145 class Error(Exception):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
146 """Base class for ConfigParser exceptions."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
147
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
148 def _get_message(self):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
149 """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
150 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
151 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
152 return self.__message
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
153
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
154 def _set_message(self, value):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
155 """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
156 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
157 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
158 self.__message = value
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
159
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
160 # 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
161 # 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
162 # 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
163 message = property(_get_message, _set_message)
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
164
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def __init__(self, msg=''):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
166 self.message = msg
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
167 Exception.__init__(self, msg)
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
168
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
169 def __repr__(self):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
170 return self.message
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
171
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
172 __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
173
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
174
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 class NoSectionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
176 """Raised when no section matches a requested option."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
177
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 def __init__(self, section):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
179 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
180 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
181 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
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
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 class DuplicateSectionError(Error):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
185 """Raised when a section is repeated in an input source.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
186
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
187 Possible repetitions that raise this exception are: multiple creation
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
188 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
189 in a single input file, string or dictionary.
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
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
192 def __init__(self, section, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
193 msg = [repr(section), " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
194 if source is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
195 message = ["While reading from ", source]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
196 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
197 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
198 message.append(": section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
199 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
200 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
201 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
202 msg.insert(0, "Section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
203 Error.__init__(self, "".join(msg))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
204 self.section = section
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
205 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
206 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
207 self.args = (section, source, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
208
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
209
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
210 class DuplicateOptionError(Error):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
211 """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
212
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
213 Current implementation raises this exception only when an option is found
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
214 more than once in a single file, string or dictionary.
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
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
217 def __init__(self, section, option, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
218 msg = [repr(option), " in section ", repr(section),
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
219 " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
220 if source is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
221 message = ["While reading from ", source]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
222 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
223 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
224 message.append(": option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
225 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
226 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
227 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
228 msg.insert(0, "Option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
229 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
230 self.section = section
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
231 self.option = option
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
232 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
233 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
234 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
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
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 class NoOptionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
238 """A requested option was not found."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
239
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
240 def __init__(self, option, section):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
241 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
242 (option, section))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
243 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
244 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
245 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
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
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 class InterpolationError(Error):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
249 """Base class for interpolation-related exceptions."""
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
250
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
251 def __init__(self, option, section, msg):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
252 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
253 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
254 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
255 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
256
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
258 class InterpolationMissingOptionError(InterpolationError):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
259 """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
260
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
261 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
262 msg = ("Bad value substitution:\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
263 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
264 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
265 "\tkey : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
266 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
267 % (section, option, reference, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
268 InterpolationError.__init__(self, option, section, msg)
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
269 self.reference = reference
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
270 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
271
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
272
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
273 class InterpolationSyntaxError(InterpolationError):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
274 """Raised when the source text contains invalid syntax.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
275
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
276 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
277 which substitutions are made does not conform to the required syntax.
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
278 """
26907
ccba3b66d676 Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26685
diff changeset
279
ccba3b66d676 Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26685
diff changeset
280
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
281 class InterpolationDepthError(InterpolationError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
282 """Raised when substitutions are nested too deeply."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
283
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
284 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
285 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
286 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
287 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
288 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
289 % (section, option, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
290 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
291 self.args = (option, section, rawval)
8614
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
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
294 class ParsingError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
295 """Raised when a configuration file does not follow legal syntax."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
296
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
297 def __init__(self, source=None, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
298 # Exactly one of `source'/`filename' arguments has to be given.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
299 # `filename' kept for compatibility.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
300 if filename and source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
301 raise ValueError("Cannot specify both `filename' and `source'. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
302 "Use `source'.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
303 elif not filename and not source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
304 raise ValueError("Required argument `source' not given.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
305 elif filename:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
306 source = filename
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
307 Error.__init__(self, 'Source contains parsing errors: %s' % source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
308 self.source = source
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
309 self.errors = []
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
310 self.args = (source, )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
311
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
312 @property
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
313 def filename(self):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
314 """Deprecated, use `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
315 warnings.warn(
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
316 "The 'filename' attribute will be removed in future versions. "
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
317 "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
318 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
319 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
320 return self.source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
321
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
322 @filename.setter
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
323 def filename(self, value):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
324 """Deprecated, user `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
325 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
326 "The 'filename' attribute will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
327 "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
328 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
329 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
330 self.source = value
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
331
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
332 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
333 self.errors.append((lineno, line))
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
334 self.message += '\n\t[line %2d]: %s' % (lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
335
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
336
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
337 class MissingSectionHeaderError(ParsingError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
338 """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
339
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
340 def __init__(self, filename, lineno, line):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
341 Error.__init__(
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
342 self,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
343 '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
344 (filename, lineno, line))
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
345 self.source = filename
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
346 self.lineno = lineno
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
347 self.line = line
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
348 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
349
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
350
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
351 # 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
352 # 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
353 # 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
354 _UNSET = object()
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
355
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
356
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
357 class Interpolation:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
358 """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
359
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
360 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
361 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
362
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
363 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
364 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
365
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
366 def before_read(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
367 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
368
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
369 def before_write(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
370 return value
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
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
373 class BasicInterpolation(Interpolation):
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
374 """Interpolation as implemented in the classic ConfigParser.
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
375
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
376 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
377 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
378
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
379 For example:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
380
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
381 something: %(dir)s/whatever
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
382
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
383 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
384 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
385 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
386 is considered a user error and raises `InterpolationSyntaxError'."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
387
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
388 _KEYCRE = re.compile(r"%\(([^)]+)\)s")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
389
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
390 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
391 L = []
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
392 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
393 return ''.join(L)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
394
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
395 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
396 tmp_value = value.replace('%%', '') # escaped percent signs
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
397 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
398 if '%' in tmp_value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
399 raise ValueError("invalid interpolation syntax in %r at "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
400 "position %d" % (value, tmp_value.find('%')))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
401 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
402
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
403 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
404 depth):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
405 if depth > MAX_INTERPOLATION_DEPTH:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
406 raise InterpolationDepthError(option, section, rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
407 while rest:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
408 p = rest.find("%")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
409 if p < 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
410 accum.append(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
411 return
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
412 if p > 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
413 accum.append(rest[:p])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
414 rest = rest[p:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
415 # p is no longer used
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
416 c = rest[1:2]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
417 if c == "%":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
418 accum.append("%")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
419 rest = rest[2:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
420 elif c == "(":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
421 m = self._KEYCRE.match(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
422 if m is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
423 raise InterpolationSyntaxError(option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
424 "bad interpolation variable reference %r" % rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
425 var = parser.optionxform(m.group(1))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
426 rest = rest[m.end():]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
427 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
428 v = map[var]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
429 except KeyError:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
430 raise InterpolationMissingOptionError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
431 option, section, rest, var)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
432 if "%" in v:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
433 self._interpolate_some(parser, option, accum, v,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
434 section, map, depth + 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
435 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
436 accum.append(v)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
437 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
438 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
439 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
440 "'%%' must be followed by '%%' or '(', "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
441 "found: %r" % (rest,))
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
442
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
443
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
444 class ExtendedInterpolation(Interpolation):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
445 """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
446 `zc.buildout'. Enables interpolation between sections."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
447
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
448 _KEYCRE = re.compile(r"\$\{([^}]+)\}")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
449
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
450 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
451 L = []
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
452 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
453 return ''.join(L)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
454
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
455 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
456 tmp_value = value.replace('$$', '') # escaped dollar signs
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
457 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
458 if '$' in tmp_value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
459 raise ValueError("invalid interpolation syntax in %r at "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
460 "position %d" % (value, tmp_value.find('%')))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
461 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
462
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
463 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
464 depth):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
465 if depth > MAX_INTERPOLATION_DEPTH:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
466 raise InterpolationDepthError(option, section, rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
467 while rest:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
468 p = rest.find("$")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
469 if p < 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
470 accum.append(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
471 return
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
472 if p > 0:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
473 accum.append(rest[:p])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
474 rest = rest[p:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
475 # p is no longer used
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
476 c = rest[1:2]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
477 if c == "$":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
478 accum.append("$")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
479 rest = rest[2:]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
480 elif c == "{":
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
481 m = self._KEYCRE.match(rest)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
482 if m is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
483 raise InterpolationSyntaxError(option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
484 "bad interpolation variable reference %r" % rest)
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
485 path = m.group(1).split(':')
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
486 rest = rest[m.end():]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
487 sect = section
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
488 opt = option
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
489 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
490 if len(path) == 1:
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
491 opt = parser.optionxform(path[0])
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
492 v = map[opt]
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
493 elif len(path) == 2:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
494 sect = path[0]
69657
57c076ab4bbd Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents: 69631
diff changeset
495 opt = parser.optionxform(path[1])
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
496 v = parser.get(sect, opt, raw=True)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
497 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
498 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
499 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
500 "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
501 except (KeyError, NoSectionError, NoOptionError):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
502 raise InterpolationMissingOptionError(
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
503 option, section, rest, ":".join(path))
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
504 if "$" in v:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
505 self._interpolate_some(parser, opt, accum, v, sect,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
506 dict(parser.items(sect, raw=True)),
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
507 depth + 1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
508 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
509 accum.append(v)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
510 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
511 raise InterpolationSyntaxError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
512 option, section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
513 "'$' must be followed by '$' or '{', "
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
514 "found: %r" % (rest,))
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
515
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
516
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
517 class LegacyInterpolation(Interpolation):
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
518 """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
519 Use BasicInterpolation or ExtendedInterpolation instead."""
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
520
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
521 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
522
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
523 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
524 rawval = value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
525 depth = MAX_INTERPOLATION_DEPTH
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
526 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
527 depth -= 1
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
528 if value and "%(" in value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
529 replace = functools.partial(self._interpolation_replace,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
530 parser=parser)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
531 value = self._KEYCRE.sub(replace, value)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
532 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
533 value = value % vars
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
534 except KeyError as e:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
535 raise InterpolationMissingOptionError(
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
536 option, section, rawval, e.args[0])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
537 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
538 break
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
539 if value and "%(" in value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
540 raise InterpolationDepthError(option, section, rawval)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
541 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
542
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
543 def before_set(self, parser, section, option, value):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
544 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
545
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
546 @staticmethod
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
547 def _interpolation_replace(match, parser):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
548 s = match.group(1)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
549 if s is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
550 return match.group()
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
551 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
552 return "%%(%s)s" % parser.optionxform(s)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
553
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
554
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
555 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
556 """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
557
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 # 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
559 _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
560 \[ # [
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 (?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
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 """
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 _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
565 (?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
566 \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
567 # 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
568 # 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
569 # 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
570 (?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
571 """
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 _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
573 (?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
574 \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
575 (?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
576 # 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
577 # 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
578 # 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
579 (?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
580 """
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
581 # 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
582 _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
583 # 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
584 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
585 # 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
586 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
587 # 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
588 # 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
589 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
590 # 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
591 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
592 # 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
593 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
594 '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
595
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
596 def __init__(self, defaults=None, dict_type=_default_dict,
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
597 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
598 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
599 strict=True, empty_lines_in_values=True,
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
600 default_section=DEFAULTSECT,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
601 interpolation=_UNSET):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
602
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
603 self._dict = dict_type
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
604 self._sections = self._dict()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
605 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
606 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
607 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
608 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
609 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
610 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
611 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
612 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
613 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
614 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
615 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
616 if allow_no_value:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
617 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
618 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
619 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
620 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
621 re.VERBOSE)
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
622 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
623 self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
624 self._strict = strict
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
625 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
626 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
627 self.default_section=default_section
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
628 self._interpolation = interpolation
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
629 if self._interpolation is _UNSET:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
630 self._interpolation = self._DEFAULT_INTERPOLATION
69668
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
631 if self._interpolation is None:
32031e33d793 Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents: 69657
diff changeset
632 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
633
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 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
635 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
636
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 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
638 """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
639 # 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
640 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
641
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 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
643 """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
644
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
645 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
646 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
647 """
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
648 if section == self.default_section:
66665
1486416c86d9 %s -> %r correction after review by Éric Araujo
Łukasz Langa <lukasz@langa.pl>
parents: 66647
diff changeset
649 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
650
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
651 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
652 raise DuplicateSectionError(section)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
653 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
654 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
655
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 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
657 """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
658
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
659 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
660 """
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
661 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
662
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 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
664 """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
665 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
666 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
667 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
668 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
669 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
670 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
671
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
672 def read(self, filenames, encoding=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
673 """Read and parse a filename or a list of filenames.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
674
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
675 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
676 designed so that you can specify a list of potential
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
677 configuration file locations (e.g. current directory, user's
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
678 home directory, systemwide directory), and all existing
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
679 configuration files in the list will be read. A single
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
680 filename may also be given.
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
681
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
682 Return list of successfully read files.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
683 """
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
684 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
685 filenames = [filenames]
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
686 read_ok = []
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
687 for filename in filenames:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
688 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
689 with open(filename, encoding=encoding) as fp:
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
690 self._read(fp, filename)
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
691 except IOError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
692 continue
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
693 read_ok.append(filename)
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
694 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
695
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
696 def read_file(self, f, source=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
697 """Like read() but the argument must be a file-like object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
698
69631
6f937d6369b6 Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents: 68257
diff changeset
699 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
700 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
701 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
702 `name' attribute, `<???>' is used.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
703 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
704 if source is None:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
705 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
706 source = f.name
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
707 except AttributeError:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
708 source = '<???>'
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
709 self._read(f, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
710
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
711 def read_string(self, string, source='<string>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
712 """Read configuration from a given string."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
713 sfile = io.StringIO(string)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
714 self.read_file(sfile, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
715
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
716 def read_dict(self, dictionary, source='<dict>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
717 """Read configuration from a dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
718
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
719 Keys are section names, values are dictionaries with keys and values
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
720 that should be present in the section. If the used dictionary type
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
721 preserves order, sections and their keys will be added in order.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
722
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
723 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
724 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
725
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
726 Optional second argument is the `source' specifying the name of the
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
727 dictionary being read.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
728 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
729 elements_added = set()
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
730 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
731 section = str(section)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
732 try:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
733 self.add_section(section)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
734 except (DuplicateSectionError, ValueError):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
735 if self._strict and section in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
736 raise
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
737 elements_added.add(section)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
738 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
739 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
740 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
741 value = str(value)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
742 if self._strict and (section, key) in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
743 raise DuplicateOptionError(section, key, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
744 elements_added.add((section, key))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
745 self.set(section, key, value)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
746
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
747 def readfp(self, fp, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
748 """Deprecated, use read_file instead."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
749 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
750 "This method will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
751 "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
752 DeprecationWarning, stacklevel=2
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
753 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
754 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
755
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
756 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
757 """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
758
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
759 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
760 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
761 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
762 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
763
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
764 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
765 all interpolations are expanded in the return values.
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
766
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
767 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
768
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
769 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
770 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
771 try:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
772 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
773 except NoSectionError:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
774 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
775 raise
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
776 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
777 return fallback
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
778 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
779 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
780 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
781 except KeyError:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
782 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
783 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
784 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
785 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
786
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
787 if raw or value is None:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
788 return value
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
789 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
790 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
791 d)
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
792
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
793 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
794 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
795
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
796 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
797 fallback=_UNSET):
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
798 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
799 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
800 except (NoSectionError, NoOptionError):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
801 if fallback is _UNSET:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
802 raise
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
803 else:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
804 return fallback
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
805
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
806 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
807 fallback=_UNSET):
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
808 try:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
809 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
810 except (NoSectionError, NoOptionError):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
811 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
812 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
813 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
814 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
815
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
816 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
817 fallback=_UNSET):
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
818 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
819 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
820 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
821 except (NoSectionError, NoOptionError):
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
822 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
823 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
824 else:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
825 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
826
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
827 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
828 """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
829
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
830 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
831 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
832 `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
833 `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
834 any pre-existing defaults.
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
835
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
836 The section DEFAULT is special.
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
837 """
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
838 if section is _UNSET:
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
839 return super().items()
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
840 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
841 try:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
842 d.update(self._sections[section])
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
843 except KeyError:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
844 if section != self.default_section:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
845 raise NoSectionError(section)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
846 # Update with the entry specific variables
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
847 if vars:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
848 for key, value in vars.items():
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
849 d[self.optionxform(key)] = value
66694
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
850 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
851 section, option, d[option], d)
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
852 if raw:
66694
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
853 value_getter = lambda option: d[option]
48450f05970b configparser: minute refactoring of RawConfigParser.items()
Łukasz Langa <lukasz@langa.pl>
parents: 66665
diff changeset
854 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
855
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
856 def optionxform(self, optionstr):
16480
e97217f5b323 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
857 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
858
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
859 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
860 """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
861 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
862 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
863 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
864 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
865 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
866 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
867 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
868 else:
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
869 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
870 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
871 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
872
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
873 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
874 """Set an option."""
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
875 if value:
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
876 value = self._interpolation.before_set(self, section, option,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
877 value)
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
878 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
879 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
880 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
881 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
882 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
883 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
884 raise NoSectionError(section)
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
885 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
886
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
887 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
888 """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
889
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
890 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
891 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
892 """
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
893 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
894 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
895 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
896 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
897 if self._defaults:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
898 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
899 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
900 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
901 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
902 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
903
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 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
905 """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
906 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
907 for key, value in section_items:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
908 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
909 value)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
910 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
911 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
912 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
913 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
914 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
915 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
916
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
917 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
918 """Remove an option."""
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
919 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
920 sectdict = self._defaults
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
921 else:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
922 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
923 sectdict = self._sections[section]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
924 except KeyError:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
925 raise NoSectionError(section)
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
926 option = self.optionxform(option)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23611
diff changeset
927 existed = option in sectdict
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
928 if existed:
15479
e98964bc2681 remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents: 14899
diff changeset
929 del sectdict[option]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
930 return existed
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
931
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
932 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
933 """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
934 existed = section in self._sections
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
935 if existed:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
936 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
937 del self._proxies[section]
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
938 return existed
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
939
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
940 def __getitem__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
941 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
942 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
943 return self._proxies[key]
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
944
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
945 def __setitem__(self, key, value):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
946 # 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
947 # the section.
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
948
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
949 # 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
950 # no update method in configparser is atomic in this implementation.
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
951 self.remove_section(key)
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
952 self.read_dict({key: value})
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
953
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
954 def __delitem__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
955 if key == self.default_section:
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
956 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
957 if not self.has_section(key):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
958 raise KeyError(key)
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
959 self.remove_section(key)
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 def __contains__(self, key):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
962 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
963
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
964 def __len__(self):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
965 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
966
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
967 def __iter__(self):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
968 # 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
969 return itertools.chain((self.default_section,), self._sections.keys())
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
970
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
971 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
972 """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
973
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
974 Each section in a configuration file contains a header, indicated by
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
975 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
976 `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
977 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
978
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
979 Values can span multiple lines, as long as they are indented deeper
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
980 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
981 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
982
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
983 Configuration files may include comments, prefixed by specific
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
984 characters (`#' and `;' by default). Comments may appear on their own
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
985 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
986 section names.
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
987 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
988 elements_added = set()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
989 cursect = None # None, or a dictionary
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
990 sectname = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
991 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
992 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
993 indent_level = 0
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
994 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
995 for lineno, line in enumerate(fp, start=1):
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
996 comment_start = None
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
997 # strip inline comments
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
998 for prefix in self._inline_comment_prefixes:
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
999 index = line.find(prefix)
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
1000 if index == 0 or (index > 0 and line[index-1].isspace()):
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 comment_start = index
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
1002 break
66981
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1003 # strip full line comments
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1004 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
1005 if line.strip().startswith(prefix):
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1006 comment_start = 0
074c21714a02 configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents: 66953
diff changeset
1007 break
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 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
1009 if not value:
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1010 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
1011 # 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
1012 # comment on the line
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1013 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
1014 cursect is not None and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1015 optname and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1016 cursect[optname] is not None):
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
1017 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
1018 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
1019 # 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
1020 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
1021 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
1022 # 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
1023 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
1024 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
1025 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
1026 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
1027 cursect[optname].append(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1028 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1029 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
1030 indent_level = cur_indent_level
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1031 # 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
1032 mo = self.SECTCRE.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1033 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1034 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
1035 if sectname in self._sections:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1036 if self._strict and sectname in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1037 raise DuplicateSectionError(sectname, fpname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1038 lineno)
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1039 cursect = self._sections[sectname]
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1040 elements_added.add(sectname)
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1041 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
1042 cursect = self._defaults
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1043 else:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
1044 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
1045 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
1046 self._proxies[sectname] = SectionProxy(self, sectname)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1047 elements_added.add(sectname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1048 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1049 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1050 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1051 elif cursect is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
1052 raise MissingSectionHeaderError(fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1053 # 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
1054 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
1055 mo = self._optcre.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1056 if mo:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
1057 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
1058 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
1059 e = self._handle_error(e, fpname, lineno, line)
25462
16e661970930 Allow internal whitespace in keys.
Fred Drake <fdrake@acm.org>
parents: 25461
diff changeset
1060 optname = self.optionxform(optname.rstrip())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1061 if (self._strict and
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1062 (sectname, optname) in elements_added):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1063 raise DuplicateOptionError(sectname, optname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1064 fpname, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1065 elements_added.add((sectname, optname))
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1066 # This check is fine because the OPTCRE cannot
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1067 # match if it would set optval to None
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1068 if optval is not None:
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1069 optval = optval.strip()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1070 cursect[optname] = [optval]
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1071 else:
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1072 # valueless option handling
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1073 cursect[optname] = None
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1074 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
1075 # a non-fatal parsing error occurred. set up the
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1076 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1077 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1078 # 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
1079 e = self._handle_error(e, fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1080 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1081 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
1082 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
1083 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
1084
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
1085 def _join_multiline_values(self):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1086 defaults = self.default_section, self._defaults
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1087 all_sections = itertools.chain((defaults,),
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1088 self._sections.items())
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1089 for section, options in all_sections:
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1090 for name, val in options.items():
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
1091 if isinstance(val, list):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1092 val = '\n'.join(val).rstrip()
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1093 options[name] = self._interpolation.before_read(self,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1094 section,
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1095 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
1096
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
1097 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
1098 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
1099 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
1100 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
1101 return exc
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1102
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1103 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
1104 """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
1105 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
1106
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1107 """
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1108 sectiondict = {}
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1109 try:
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1110 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
1111 except KeyError:
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1112 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
1113 raise NoSectionError(section)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
1114 # 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
1115 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
1116 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
1117 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
1118 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
1119 value = str(value)
67849
8e9969418dbe Issue #11089: Fix performance issue limiting the use of ConfigParser()
Raymond Hettinger <python@rcn.com>
parents: 63467
diff changeset
1120 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
1121 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
1122
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1123 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
1124 """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
1125 """
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1126 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
1127 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
1128 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
1129
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1130 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
1131 """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
1132
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1133 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
1134 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
1135 string if:
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1136 - 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
1137 - 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
1138
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1139 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
1140 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
1141 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
1142 """
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1143 if not isinstance(section, str):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1144 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
1145 if not isinstance(option, str):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1146 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
1147 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
1148 if not isinstance(value, str):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1149 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
1150
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1151
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1152 class ConfigParser(RawConfigParser):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
1153 """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
1154
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1155 _DEFAULT_INTERPOLATION = BasicInterpolation()
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1156
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1157 def set(self, section, option, value=None):
66647
5fda519834df Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents: 66350
diff changeset
1158 """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
1159 interpolation syntax on the value."""
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1160 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
1161 super().set(section, option, value)
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
1162
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1163 def add_section(self, section):
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1164 """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
1165 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
1166 a string."""
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1167 self._validate_value_types(section=section)
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1168 super().add_section(section)
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
1169
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1170
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1171 class SafeConfigParser(ConfigParser):
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1172 """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
1173
66953
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1174 def __init__(self, *args, **kwargs):
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1175 super().__init__(*args, **kwargs)
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1176 warnings.warn(
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1177 "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
1178 "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
1179 " Use ConfigParser directly instead.",
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1180 DeprecationWarning, stacklevel=2
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1181 )
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1182
3de6cd902624 Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents: 66722
diff changeset
1183
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1184 class SectionProxy(MutableMapping):
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1185 """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
1186
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
1187 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
1188 """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
1189 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
1190 self._name = name
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1191
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1192 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
1193 return '<Section: {}>'.format(self._name)
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1194
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1195 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
1196 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
1197 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
1198 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
1199
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1200 def __setitem__(self, key, value):
66702
ddc6c47bf825 configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents: 66694
diff changeset
1201 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
1202 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
1203
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1204 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
1205 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
1206 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
1207 raise KeyError(key)
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1208
66143
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1209 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
1210 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
1211
e546f7605cf1 Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents: 64897
diff changeset
1212 def __len__(self):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1213 return len(self._options())
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 __iter__(self):
66997
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1216 return self._options().__iter__()
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1217
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1218 def _options(self):
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1219 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
1220 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
1221 else:
892236137db9 100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents: 66981
diff changeset
1222 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
1223
66722
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1224 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
1225 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
1226 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
1227
66722
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1228 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
1229 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
1230 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1231
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1232 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
1233 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
1234 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1235
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1236 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
1237 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
1238 fallback=fallback)
3fee88e6442a configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents: 66702
diff changeset
1239
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
1240 @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
1241 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
1242 # 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
1243 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
1244
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
1245 @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
1246 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
1247 # 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
1248 return self._name