annotate Lib/configparser.py @ 66807:d28126ebd60d

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