annotate Lib/configparser.py @ 65989:458b05abf987

Output served URL when running ssl_servers
author Antoine Pitrou <solipsis@pitrou.net>
date Fri, 05 Nov 2010 20:26:59 +0000
parents d5001d6c7736
children e546f7605cf1
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
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
7 The option values can contain format strings which refer to other values in
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
8 the same section, or values in a special [DEFAULT] section.
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
9
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 For example:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 something: %(dir)s/whatever
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 would resolve the "%(dir)s" to the value of dir. All reference
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 expansions are done late, on demand.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Intrinsic defaults can be specified by passing them into the
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 ConfigParser constructor as a dictionary.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 class:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 30275
diff changeset
22 ConfigParser -- responsible for parsing a list of
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 configuration files, and managing the parsed database.
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 methods:
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
27 __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
28 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
29 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
30 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
31 dictionary or intrinsic defaults. The keys must be strings, the values
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
32 must be appropriate for %()s string interpolation. Note that `__name__'
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
33 is always an intrinsic default; its value is the section's 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
34
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
35 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
36 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
37 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
38
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples 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 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
40 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
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 `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
43 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
44
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
45 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
46 duplicates while reading from a single source (file, string or
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
47 dictionary). Default is False.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
48
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 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
50 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
51 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
52
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
53 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
54 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
55
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
56 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
57 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
58
11370
41990bce023a Added has_option(); fix bug in get() which botched interpolation if
Guido van Rossum <guido@python.org>
parents: 11369
diff changeset
59 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
60 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
61
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
62 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
63 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
64
9961
879e3491b661 Re-format the module docstring and document the new get() argument.
Barry Warsaw <barry@python.org>
parents: 9959
diff changeset
65 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
66 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
67
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
68 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
69 Read and parse the list of named configuration files, given by
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
70 name. A single filename is also allowed. Non-existing files
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
71 are ignored. Return list of successfully read files.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
72
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
73 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
74 Read and parse one configuration file, given as a file object.
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
75 The filename defaults to f.name; it is only used in error
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
76 messages (if f has no `name' attribute, the string `<???>' is used).
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
77
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
78 read_string(string)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
79 Read configuration from a given string.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
80
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
81 read_dict(dictionary)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
82 Read configuration from a dictionary. Keys are section names,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
83 values are dictionaries with keys and values that should be present
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
84 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
85 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
86 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
87
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
88 get(section, option, raw=False, vars=None, default=_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 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
90 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
91 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
92 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
93 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
94 `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
95
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
96 getint(section, options, raw=False, vars=None, default=_UNSET)
63264
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
97 Like get(), but convert value to 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
98
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
99 getfloat(section, options, raw=False, vars=None, default=_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
100 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
101
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
102 getboolean(section, options, raw=False, vars=None, default=_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
103 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
104 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
105 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
106
26685
a20cb5abab6b Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26126
diff changeset
107 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
108 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
109 in the section.
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
110
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
111 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
112 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
113
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
114 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
115 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
116
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
117 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
118 Set the given option.
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
119
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 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
121 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
122 `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
123 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
124 """
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
51960
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
126 try:
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
127 from collections import OrderedDict as _default_dict
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
128 except ImportError:
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
129 # fallback for setup.py which hasn't yet built _collections
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
130 _default_dict = dict
03832356c096 Fix bootstrapping problem where setup.py was using configparser
Raymond Hettinger <python@rcn.com>
parents: 51955
diff changeset
131
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
132 import io
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
133 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
134 import sys
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
135 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
136
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
137 __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
138 "NoOptionError", "InterpolationError", "InterpolationDepthError",
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
139 "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
140 "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
141 "ConfigParser", "SafeConfigParser", "RawConfigParser",
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
142 "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
143
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 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
145
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
146 MAX_INTERPOLATION_DEPTH = 10
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
147
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
148
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
149
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # exception classes
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
151 class Error(Exception):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
152 """Base class for ConfigParser exceptions."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
153
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
154 def _get_message(self):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
155 """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
156 BaseException.
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
157 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
158 return self.__message
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
159
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
160 def _set_message(self, value):
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
161 """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
162 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
163 """
41716
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
164 self.__message = value
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
165
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
166 # BaseException.message has been deprecated since Python 2.6. To prevent
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
167 # DeprecationWarning from popping up over this pre-existing attribute, use
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
168 # 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
169 message = property(_get_message, _set_message)
00c02c4488ce Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
170
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
171 def __init__(self, msg=''):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
172 self.message = msg
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
173 Exception.__init__(self, msg)
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
174
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 def __repr__(self):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
176 return self.message
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
177
15501
55f4a6e8b5b3 Make ConfigParser.Error inherit from Exception.
Fred Drake <fdrake@acm.org>
parents: 15479
diff changeset
178 __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
179
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
180
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 class NoSectionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
182 """Raised when no section matches a requested option."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
183
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
184 def __init__(self, section):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
185 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
186 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
187 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
188
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
189
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
190 class DuplicateSectionError(Error):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
191 """Raised when a section is repeated in an input source.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
192
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
193 Possible repetitions that raise this exception are: multiple creation
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
194 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
195 in a single input file, string or dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
196 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
197
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
198 def __init__(self, section, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
199 msg = [repr(section), " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
200 if source is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
201 message = ["While reading from ", source]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
202 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
203 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
204 message.append(": section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
205 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
206 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
207 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
208 msg.insert(0, "Section ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
209 Error.__init__(self, "".join(msg))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
210 self.section = section
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
211 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
212 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
213 self.args = (section, source, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
214
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
215
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
216 class DuplicateOptionError(Error):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
217 """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
218
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
219 Current implementation raises this exception only when an option is found
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
220 more than once in a single file, string or dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
221 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
222
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
223 def __init__(self, section, option, source=None, lineno=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
224 msg = [repr(option), " in section ", repr(section),
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
225 " already exists"]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
226 if source is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
227 message = ["While reading from ", source]
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
228 if lineno is not None:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
229 message.append(" [line {0:2d}]".format(lineno))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
230 message.append(": option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
231 message.extend(msg)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
232 msg = message
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
233 else:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
234 msg.insert(0, "Option ")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
235 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
236 self.section = section
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
237 self.option = option
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
238 self.source = source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
239 self.lineno = lineno
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
240 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
241
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
242
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
243 class NoOptionError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
244 """A requested option was not found."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
245
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 def __init__(self, option, section):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
247 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
248 (option, section))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
249 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
250 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
251 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
252
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
253
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
254 class InterpolationError(Error):
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
255 """Base class for interpolation-related exceptions."""
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
256
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
257 def __init__(self, option, section, msg):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
258 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
259 self.option = option
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
260 self.section = section
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
261 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
262
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
263
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
264 class InterpolationMissingOptionError(InterpolationError):
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
265 """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
266
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
267 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
268 msg = ("Bad value substitution:\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
269 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
270 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
271 "\tkey : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
272 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
273 % (section, option, reference, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
274 InterpolationError.__init__(self, option, section, msg)
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
275 self.reference = reference
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
276 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
277
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
278
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
279 class InterpolationSyntaxError(InterpolationError):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
280 """Raised when the source text contains invalid syntax.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
281
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
282 Current implementation raises this exception only for SafeConfigParser
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
283 instances when the source text into which substitutions are made
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
284 does not conform to the required syntax.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
285 """
26907
ccba3b66d676 Add missing InterpolationSyntaxError.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26685
diff changeset
286
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
287
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
288 class InterpolationDepthError(InterpolationError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
289 """Raised when substitutions are nested too deeply."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
290
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
291 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
292 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
293 "\tsection: [%s]\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
294 "\toption : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
295 "\trawval : %s\n"
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
296 % (section, option, rawval))
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
297 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
298 self.args = (option, section, rawval)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
299
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
300
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
301 class ParsingError(Error):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
302 """Raised when a configuration file does not follow legal syntax."""
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
303
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
304 def __init__(self, source=None, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
305 # Exactly one of `source'/`filename' arguments has to be given.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
306 # `filename' kept for compatibility.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
307 if filename and source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
308 raise ValueError("Cannot specify both `filename' and `source'. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
309 "Use `source'.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
310 elif not filename and not source:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
311 raise ValueError("Required argument `source' not given.")
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
312 elif filename:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
313 source = filename
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
314 Error.__init__(self, 'Source contains parsing errors: %s' % source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
315 self.source = source
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
316 self.errors = []
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
317 self.args = (source, )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
318
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
319 @property
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
320 def filename(self):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
321 """Deprecated, use `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
322 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
323 "This 'filename' attribute will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
324 "Use 'source' instead.",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
325 PendingDeprecationWarning, stacklevel=2
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
326 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
327 return self.source
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
328
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
329 @filename.setter
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
330 def filename(self, value):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
331 """Deprecated, user `source'."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
332 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
333 "The 'filename' attribute will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
334 "Use 'source' instead.",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
335 PendingDeprecationWarning, stacklevel=2
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
336 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
337 self.source = value
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
338
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
339 def append(self, lineno, line):
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
340 self.errors.append((lineno, line))
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
341 self.message += '\n\t[line %2d]: %s' % (lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
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
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
344 class MissingSectionHeaderError(ParsingError):
26909
074919715355 - added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents: 26907
diff changeset
345 """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
346
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
347 def __init__(self, filename, lineno, line):
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
348 Error.__init__(
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
349 self,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
350 '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
351 (filename, lineno, line))
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
352 self.source = filename
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
353 self.lineno = lineno
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
354 self.line = line
63216
9ecbdefc9849 Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 58949
diff changeset
355 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
356
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
357
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
358 # 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
359 # 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
360 # inline)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
361 _COMPATIBLE = object()
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
362
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
363 # 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
364 # 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
365 # 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
366 _UNSET = object()
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
367
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
368
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
369 class RawConfigParser:
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
370 """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
371
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
372 # 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
373 _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
374 \[ # [
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
375 (?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
376 \] # ]
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
377 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
378 _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
379 (?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
380 \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
381 # 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
382 # 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
383 # 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
384 (?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
385 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
386 _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
387 (?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
388 \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
389 (?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
390 # 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
391 # 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
392 # 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
393 (?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
394 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
395
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
396 # 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
397 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
398 # 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
399 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
400 # 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
401 # 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
402 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
403 # 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
404 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
405 # 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
406 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
407 '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
408
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
409 def __init__(self, defaults=None, dict_type=_default_dict,
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
410 allow_no_value=False, *, delimiters=('=', ':'),
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
411 comment_prefixes=_COMPATIBLE, strict=False,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
412 empty_lines_in_values=True):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
413 self._dict = dict_type
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
414 self._sections = self._dict()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
415 self._defaults = self._dict()
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
416 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
417 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
418 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
419 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
420 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
421 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
422 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
423 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
424 if allow_no_value:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
425 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
426 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
427 else:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
428 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
429 re.VERBOSE)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
430 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
431 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
432 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
433 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
434 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
435 self._comment_prefixes = tuple(comment_prefixes or ())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
436 self._strict = strict
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
437 self._empty_lines_in_values = empty_lines_in_values
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
438
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 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
440 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
441
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 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
443 """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
444 # 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
445 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
446
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 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
448 """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
449
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
450 Raise DuplicateSectionError if a section by the specified name
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
451 already exists. Raise ValueError if name is DEFAULT or any of it's
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
452 case-insensitive variants.
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
453 """
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
454 if section.lower() == "default":
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
455 raise ValueError('Invalid section name: %s' % section)
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
456
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
457 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
458 raise DuplicateSectionError(section)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
459 self._sections[section] = self._dict()
6795
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 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
462 """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
463
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
464 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
465 """
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
466 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
467
5b24cbb1f99b Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 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
469 """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
470 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
471 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
472 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
473 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
474 opts.update(self._defaults)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23611
diff changeset
475 if '__name__' in opts:
14899
606a80834a86 Allow spaces in section names.
Fred Drake <fdrake@acm.org>
parents: 14840
diff changeset
476 del opts['__name__']
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
477 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
478
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
479 def read(self, filenames, encoding=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
480 """Read and parse a filename or a list of filenames.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15501
diff changeset
481
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
482 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
483 designed so that you can specify a list of potential
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
484 configuration file locations (e.g. current directory, user's
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
485 home directory, systemwide directory), and all existing
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
486 configuration files in the list will be read. A single
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
487 filename may also be given.
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
488
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
489 Return list of successfully read files.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
490 """
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
491 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
492 filenames = [filenames]
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
493 read_ok = []
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
494 for filename in filenames:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
495 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
496 with open(filename, encoding=encoding) as fp:
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
497 self._read(fp, filename)
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
498 except IOError:
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
499 continue
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
500 read_ok.append(filename)
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
501 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
502
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
503 def read_file(self, f, source=None):
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
504 """Like read() but the argument must be a file-like object.
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
505
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
506 The `f' argument must have a `readline' method. Optional second
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
507 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
508 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
509 `<???>' is used.
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
510 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
511 if source is None:
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
512 try:
64897
d5001d6c7736 Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64424
diff changeset
513 source = f.name
11369
29ce956fa7b7 Urmpfh!
Guido van Rossum <guido@python.org>
parents: 11368
diff changeset
514 except AttributeError:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
515 source = '<???>'
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
516 self._read(f, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
517
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
518 def read_string(self, string, source='<string>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
519 """Read configuration from a given string."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
520 sfile = io.StringIO(string)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
521 self.read_file(sfile, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
522
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
523 def read_dict(self, dictionary, source='<dict>'):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
524 """Read configuration from a dictionary.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
525
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
526 Keys are section names, values are dictionaries with keys and values
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
527 that should be present in the section. If the used dictionary type
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
528 preserves order, sections and their keys will be added in order.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
529
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
530 Optional second argument is the `source' specifying the name of the
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
531 dictionary being read.
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
532 """
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
533 elements_added = set()
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
534 for section, keys in dictionary.items():
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
535 try:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
536 self.add_section(section)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
537 except DuplicateSectionError:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
538 if self._strict and section in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
539 raise
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
540 elements_added.add(section)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
541 for key, value in keys.items():
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
542 key = self.optionxform(key)
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
543 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
544 value = str(value)
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
545 if self._strict and (section, key) in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
546 raise DuplicateOptionError(section, key, source)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
547 elements_added.add((section, key))
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
548 self.set(section, key, value)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
549
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
550 def readfp(self, fp, filename=None):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
551 """Deprecated, use read_file instead."""
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
552 warnings.warn(
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
553 "This method will be removed in future versions. "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
554 "Use 'parser.read_file()' instead.",
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
555 PendingDeprecationWarning, stacklevel=2
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
556 )
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
557 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
558
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
559 def get(self, section, option, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
560 """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
561
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
562 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
563 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
564 If the key is not found and `default' is provided, it is used as
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
565 a fallback value. `None' can be provided as a `default' value.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
566 """
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
567 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
568 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
569 except NoSectionError:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
570 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
571 raise
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
572 else:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
573 return default
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
574 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
575 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
576 return d[option]
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
577 except KeyError:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
578 if default is _UNSET:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
579 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
580 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
581 return 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
582
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
583 def items(self, section):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
584 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
585 d2 = self._sections[section]
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
586 except KeyError:
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
587 if section != DEFAULTSECT:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
588 raise NoSectionError(section)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
589 d2 = self._dict()
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
590 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
591 d.update(d2)
25916
8bb6003f7f54 According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents: 25908
diff changeset
592 if "__name__" in d:
8bb6003f7f54 According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents: 25908
diff changeset
593 del d["__name__"]
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
594 return d.items()
25461
09551c6da299 items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
Fred Drake <fdrake@acm.org>
parents: 25459
diff changeset
595
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
596 def _get(self, section, conv, option, *args, **kwargs):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
597 return conv(self.get(section, option, *args, **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
598
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
599 def getint(self, section, option, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
600 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
601 return self._get(section, int, option, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
602 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
603 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
604 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
605 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
606 return 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
607
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
608 def getfloat(self, section, option, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
609 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
610 return self._get(section, float, option, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
611 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
612 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
613 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
614 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
615 return default
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
616
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
617 def getboolean(self, section, option, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
618 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
619 return self._get(section, self._convert_to_boolean, option, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
620 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
621 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
622 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
623 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
624 return 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
625
11007
b9f55cc10ab3 Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents: 10145
diff changeset
626 def optionxform(self, optionstr):
16480
e97217f5b323 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
627 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
628
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
629 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
630 """Check for the existence of a given option in a given section."""
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
631 if not section or section == DEFAULTSECT:
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
632 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
633 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
634 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
635 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
636 else:
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
637 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
638 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
639 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
640
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
641 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
642 """Set an option."""
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
643 if not section or section == DEFAULTSECT:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
644 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
645 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
646 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
647 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
648 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
649 raise NoSectionError(section)
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
650 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
651
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
652 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
653 """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
654
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
655 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
656 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
657 """
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
658 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
659 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
660 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
661 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
662 if self._defaults:
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
663 self._write_section(fp, DEFAULTSECT, 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
664 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
665 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
666 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
667
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
668 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
669 """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
670 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
671 for key, value in section_items:
bb9eef8d1ac6 #1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents: 63220
diff changeset
672 if key == "__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
673 continue
64386
16d8a4e94dd0 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63875
diff changeset
674 if (value is not None) or (self._optcre == self.OPTCRE):
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
675 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
676 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
677 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
678 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
679 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
680
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
681 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
682 """Remove an option."""
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
683 if not section or section == DEFAULTSECT:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
684 sectdict = self._defaults
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
685 else:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
686 try:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
687 sectdict = self._sections[section]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
688 except KeyError:
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
689 raise NoSectionError(section)
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16594
diff changeset
690 option = self.optionxform(option)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23611
diff changeset
691 existed = option in sectdict
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
692 if existed:
15479
e98964bc2681 remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents: 14899
diff changeset
693 del sectdict[option]
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
694 return existed
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
695
13650
3d6d218b673d Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents: 13600
diff changeset
696 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
697 """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
698 existed = section in self._sections
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
699 if existed:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
700 del self._sections[section]
25459
d07f8db3db41 has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
701 return existed
13600
39f54b9a7f9c ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents: 13521
diff changeset
702
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
703 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
704 """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
705
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
706 Each section in a configuration file contains a header, indicated by
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
707 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
708 `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
709 default).
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
710
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
711 Values can span multiple lines, as long as they are indented deeper
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
712 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
713 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
714
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
715 Configuration files may include comments, prefixed by specific
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
716 characters (`#' and `;' by default). Comments may appear on their own
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
717 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
718 section names.
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
719 """
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
720 elements_added = set()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
721 cursect = None # None, or a dictionary
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
722 sectname = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
723 optname = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
724 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
725 indent_level = 0
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
726 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
727 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
728 # 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
729 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
730 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
731 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
732 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
733 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
734 # 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
735 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
736 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
737 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
738 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
739 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
740 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
741 if not value:
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
742 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
743 # 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
744 # comment on the line
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
745 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
746 cursect is not None and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
747 optname and
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
748 cursect[optname] is not None):
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
749 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
750 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
751 # 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
752 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
753 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7165
diff changeset
754 # 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
755 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
756 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
757 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
758 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
759 cursect[optname].append(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
760 # a section header or option header?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
761 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
762 indent_level = cur_indent_level
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
763 # 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
764 mo = self.SECTCRE.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
765 if mo:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
766 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
767 if sectname in self._sections:
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
768 if self._strict and sectname in elements_added:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
769 raise DuplicateSectionError(sectname, fpname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
770 lineno)
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
771 cursect = self._sections[sectname]
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
772 elements_added.add(sectname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
773 elif sectname == DEFAULTSECT:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
774 cursect = self._defaults
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
775 else:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
776 cursect = self._dict()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 35276
diff changeset
777 cursect['__name__'] = sectname
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
778 self._sections[sectname] = cursect
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
779 elements_added.add(sectname)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
780 # So sections can't start with a continuation line
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
781 optname = None
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
782 # no section header in the file?
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
783 elif cursect is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30734
diff changeset
784 raise MissingSectionHeaderError(fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
785 # 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
786 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
787 mo = self._optcre.match(value)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
788 if mo:
11873
7ed104c40db3 (Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents: 11381
diff changeset
789 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
790 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
791 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
792 optname = self.optionxform(optname.rstrip())
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
793 if (self._strict and
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
794 (sectname, optname) in elements_added):
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
795 raise DuplicateOptionError(sectname, optname,
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
796 fpname, lineno)
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
797 elements_added.add((sectname, optname))
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
798 # This check is fine because the OPTCRE cannot
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
799 # match if it would set optval to None
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
800 if optval is not None:
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
801 optval = optval.strip()
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
802 # allow empty values
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
803 if optval == '""':
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
804 optval = ''
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
805 cursect[optname] = [optval]
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
806 else:
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
807 # valueless option handling
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
808 cursect[optname] = optval
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
809 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
810 # a non-fatal parsing error occurred. set up the
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
811 # exception but keep going. the exception will be
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
812 # raised at the end of the file and will contain a
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
813 # 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
814 e = self._handle_error(e, fpname, lineno, line)
8614
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
815 # if any parsing errors occurred, raise an exception
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
816 if e:
64f0c3188e70 Several changes:
Barry Warsaw <barry@python.org>
parents: 7755
diff changeset
817 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
818 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
819
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
820 def _join_multiline_values(self):
63220
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
821 all_sections = [self._defaults]
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
822 all_sections.extend(self._sections.values())
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
823 for options in all_sections:
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
824 for name, val in options.items():
fdb71d6c1822 Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 63216
diff changeset
825 if isinstance(val, list):
63280
e2e32a869810 Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents: 63264
diff changeset
826 options[name] = '\n'.join(val).rstrip()
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
827
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
828 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
829 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
830 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
831 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
832 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
833
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
834 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
835 """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
836 `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
837 take precendence.
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
838 """
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
839 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
840 try:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
841 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
842 except KeyError:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
843 if section != DEFAULTSECT:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
844 raise NoSectionError(section)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
845 # 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
846 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
847 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
848 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
849 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
850 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
851 return d
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
852
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
853 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
854 """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
855 """
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
856 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
857 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
858 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
859
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
860
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
861 class ConfigParser(RawConfigParser):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
862 """ConfigParser implementing interpolation."""
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
863
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
864 def get(self, section, option, raw=False, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
865 """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
866
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
867 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
868 in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
869 If the key is not found and `default' is provided, it is used as
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
870 a fallback value. `None' can be provided as a `default' value.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
871
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
872 All % interpolations are expanded in the return values, unless the
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
873 optional argument `raw' is true. Values for interpolation keys are
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
874 looked up in the same manner as the option.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
875
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
876 The section DEFAULT is special.
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
877 """
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
878 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
879 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
880 except NoSectionError:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
881 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
882 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
883 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
884 return default
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
885 option = self.optionxform(option)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
886 try:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
887 value = d[option]
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
888 except KeyError:
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
889 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
890 raise NoOptionError(option, section)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
891 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
892 return default
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
893
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
894 if raw or value is None:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
895 return value
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
896 else:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
897 return self._interpolate(section, option, value, d)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
898
64424
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
899 def getint(self, section, option, raw=False, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
900 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
901 return self._get(section, int, option, raw, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
902 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
903 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
904 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
905 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
906 return default
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
907
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
908 def getfloat(self, section, option, raw=False, vars=None, default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
909 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
910 return self._get(section, float, option, raw, vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
911 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
912 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
913 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
914 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
915 return default
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
916
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
917 def getboolean(self, section, option, raw=False, vars=None,
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
918 default=_UNSET):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
919 try:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
920 return self._get(section, self._convert_to_boolean, option, raw,
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
921 vars)
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
922 except (NoSectionError, NoOptionError):
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
923 if default is _UNSET:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
924 raise
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
925 else:
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
926 return default
b2b262c5daa8 add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents: 64386
diff changeset
927
26685
a20cb5abab6b Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26126
diff changeset
928 def items(self, section, raw=False, vars=None):
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
929 """Return a list of (name, value) tuples for each option in a section.
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
930
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
931 All % interpolations are expanded in the return values, based on the
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
932 defaults passed into the constructor, unless the optional argument
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
933 `raw' is true. Additional substitutions may be provided using the
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
934 `vars' argument, which must be a dictionary whose contents overrides
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
935 any pre-existing defaults.
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
936
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
937 The section DEFAULT is special.
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
938 """
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
939 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
940 try:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
941 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
942 except KeyError:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
943 if section != DEFAULTSECT:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
944 raise NoSectionError(section)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
945 # Update with the entry specific variables
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
946 if vars:
33662
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
947 for key, value in vars.items():
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
948 d[self.optionxform(key)] = value
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
949 options = list(d.keys())
25916
8bb6003f7f54 According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents: 25908
diff changeset
950 if "__name__" in options:
8bb6003f7f54 According to the docs, __name__ is not exposed via the API except
Fred Drake <fdrake@acm.org>
parents: 25908
diff changeset
951 options.remove("__name__")
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
952 if raw:
30734
916f782ed6d5 Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents: 30723
diff changeset
953 return [(option, d[option])
916f782ed6d5 Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents: 30723
diff changeset
954 for option in options]
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
955 else:
30734
916f782ed6d5 Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents: 30723
diff changeset
956 return [(option, self._interpolate(section, option, d[option], d))
916f782ed6d5 Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents: 30723
diff changeset
957 for option in options]
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
958
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
959 def _interpolate(self, section, option, rawval, vars):
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
960 # do the string interpolation
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
961 value = rawval
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26078
diff changeset
962 depth = MAX_INTERPOLATION_DEPTH
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
963 while depth: # Loop through this until it's done
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
964 depth -= 1
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
965 if value and "%(" in value:
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
966 value = self._KEYCRE.sub(self._interpolation_replace, value)
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
967 try:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
968 value = value % vars
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
969 except KeyError as e:
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
970 raise InterpolationMissingOptionError(
41274
bfc4e8046102 When removing indexing/slicing on exceptions some places were changed
Brett Cannon <bcannon@gmail.com>
parents: 40959
diff changeset
971 option, section, rawval, e.args[0])
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
972 else:
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
973 break
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
974 if value and "%(" in value:
25908
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
975 raise InterpolationDepthError(option, section, rawval)
df055a9eff9c Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
976 return value
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
977
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
978 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
979
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
980 def _interpolation_replace(self, match):
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
981 s = match.group(1)
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
982 if s is None:
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
983 return match.group()
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
984 else:
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
985 return "%%(%s)s" % self.optionxform(s)
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
986
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
987
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
988 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
989 """ConfigParser implementing sane interpolation."""
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
990
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
991 def _interpolate(self, section, option, rawval, vars):
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
992 # do the string interpolation
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
993 L = []
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
994 self._interpolate_some(option, L, rawval, section, vars, 1)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
995 return ''.join(L)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
996
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41274
diff changeset
997 _interpvar_re = re.compile(r"%\(([^)]+)\)s")
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
998
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
999 def _interpolate_some(self, option, accum, rest, section, map, depth):
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1000 if depth > MAX_INTERPOLATION_DEPTH:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1001 raise InterpolationDepthError(option, section, rest)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1002 while rest:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1003 p = rest.find("%")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1004 if p < 0:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1005 accum.append(rest)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1006 return
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1007 if p > 0:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1008 accum.append(rest[:p])
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1009 rest = rest[p:]
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1010 # p is no longer used
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1011 c = rest[1:2]
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1012 if c == "%":
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1013 accum.append("%")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1014 rest = rest[2:]
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1015 elif c == "(":
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41274
diff changeset
1016 m = self._interpvar_re.match(rest)
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1017 if m is None:
29661
79303de9a05a Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents: 26969
diff changeset
1018 raise InterpolationSyntaxError(option, section,
79303de9a05a Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents: 26969
diff changeset
1019 "bad interpolation variable reference %r" % rest)
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 31967
diff changeset
1020 var = self.optionxform(m.group(1))
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1021 rest = rest[m.end():]
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1022 try:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1023 v = map[var]
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1024 except KeyError:
26969
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
1025 raise InterpolationMissingOptionError(
776f270a6ea9 Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents: 26927
diff changeset
1026 option, section, rest, var)
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1027 if "%" in v:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1028 self._interpolate_some(option, accum, v,
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1029 section, map, depth + 1)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1030 else:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1031 accum.append(v)
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1032 else:
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25916
diff changeset
1033 raise InterpolationSyntaxError(
29661
79303de9a05a Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents: 26969
diff changeset
1034 option, section,
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1035 "'%%' must be followed by '%%' or '(', "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1036 "found: %r" % (rest,))
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
1037
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1038 def set(self, section, option, value=None):
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
1039 """Set an option. Extend ConfigParser.set: check for string values."""
58949
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1040 # The only legal non-string value if we allow valueless
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1041 # options is None, so we need to check if the value is a
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1042 # string if:
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1043 # - we do not allow valueless options, or
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1044 # - we allow valueless options but the value is not None
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1045 if self._optcre is self.OPTCRE or value:
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1046 if not isinstance(value, str):
4fc8b6fe4af8 Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 53525
diff changeset
1047 raise TypeError("option values must be strings")
63875
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1048 # check for bad percent signs
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1049 if value:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1050 tmp_value = value.replace('%%', '') # escaped percent signs
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1051 tmp_value = self._interpvar_re.sub('', tmp_value) # valid syntax
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1052 if '%' in tmp_value:
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1053 raise ValueError("invalid interpolation syntax in %r at "
8ba09cdb6fc4 issue #9452:
Fred Drake <fdrake@acm.org>
parents: 63287
diff changeset
1054 "position %d" % (value, tmp_value.find('%')))
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
1055 ConfigParser.set(self, section, option, value)