Mercurial > cpython-lru_cache-weakref
annotate Lib/configparser.py @ 64772:1debff42ea46
Add entries to whatsnew
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Wed, 15 Sep 2010 15:09:40 +0000 |
| parents | b2b262c5daa8 |
| children | d5001d6c7736 |
| 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 | 5 the style of RFC 822. |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
| 8614 | 7 The option values can contain format strings which refer to other values in |
| 8 the same section, or values in a special [DEFAULT] section. | |
| 9 | |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 For example: |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 something: %(dir)s/whatever |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 would resolve the "%(dir)s" to the value of dir. All reference |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 expansions are done late, on demand. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 Intrinsic defaults can be specified by passing them into the |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 ConfigParser constructor as a dictionary. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 class: |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
| 30723 | 22 ConfigParser -- responsible for parsing a list of |
|
6795
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 configuration files, and managing the parsed database. |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 methods: |
|
5b24cbb1f99b
Checking in ConfigParser.py -- I don't see a reason why this can't be
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
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 | 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 | 45 When `strict` is True, the parser won't allow for any section or option |
| 46 duplicates while reading from a single source (file, string or | |
| 47 dictionary). Default is False. | |
| 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 | 70 name. A single filename is also allowed. Non-existing files |
| 32117 | 71 are ignored. Return list of successfully read files. |
| 11369 | 72 |
| 63875 | 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 | 75 The filename defaults to f.name; it is only used in error |
| 76 messages (if f has no `name' attribute, the string `<???>' is used). | |
| 77 | |
| 78 read_string(string) | |
| 79 Read configuration from a given string. | |
| 80 | |
| 81 read_dict(dictionary) | |
| 82 Read configuration from a dictionary. Keys are section names, | |
| 83 values are dictionaries with keys and values that should be present | |
| 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 | 132 import io |
| 8614 | 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 | 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 | 137 __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError", |
| 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 | 167 # DeprecationWarning from popping up over this pre-existing attribute, use |
| 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 | 191 """Raised when a section is repeated in an input source. |
| 192 | |
| 193 Possible repetitions that raise this exception are: multiple creation | |
| 194 using the API or in strict parsers when a section is found more than once | |
| 195 in a single input file, string or dictionary. | |
| 196 """ | |
| 197 | |
| 198 def __init__(self, section, source=None, lineno=None): | |
| 199 msg = [repr(section), " already exists"] | |
| 200 if source is not None: | |
| 201 message = ["While reading from ", source] | |
| 202 if lineno is not None: | |
| 203 message.append(" [line {0:2d}]".format(lineno)) | |
| 204 message.append(": section ") | |
| 205 message.extend(msg) | |
| 206 msg = message | |
| 207 else: | |
| 208 msg.insert(0, "Section ") | |
| 209 Error.__init__(self, "".join(msg)) | |
| 210 self.section = section | |
| 211 self.source = source | |
| 212 self.lineno = lineno | |
| 213 self.args = (section, source, lineno) | |
| 214 | |
|
26909
074919715355
- added InterpolationSyntaxError to __all__
Fred Drake <fdrake@acm.org>
parents:
26907
diff
changeset
|
215 |
| 63875 | 216 class DuplicateOptionError(Error): |
| 217 """Raised by strict parsers when an option is repeated in an input source. | |
| 218 | |
| 219 Current implementation raises this exception only when an option is found | |
| 220 more than once in a single file, string or dictionary. | |
| 221 """ | |
| 222 | |
| 223 def __init__(self, section, option, source=None, lineno=None): | |
| 224 msg = [repr(option), " in section ", repr(section), | |
| 225 " already exists"] | |
| 226 if source is not None: | |
| 227 message = ["While reading from ", source] | |
| 228 if lineno is not None: | |
| 229 message.append(" [line {0:2d}]".format(lineno)) | |
| 230 message.append(": option ") | |
| 231 message.extend(msg) | |
| 232 msg = message | |
| 233 else: | |
| 234 msg.insert(0, "Option ") | |
| 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 | 237 self.option = option |
| 238 self.source = source | |
| 239 self.lineno = lineno | |
| 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 | 280 """Raised when the source text contains invalid syntax. |
| 281 | |
| 282 Current implementation raises this exception only for SafeConfigParser | |
| 283 instances when the source text into which substitutions are made | |
| 284 does not conform to the required syntax. | |
| 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 | 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 | 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 | 304 def __init__(self, source=None, filename=None): |
| 305 # Exactly one of `source'/`filename' arguments has to be given. | |
| 306 # `filename' kept for compatibility. | |
| 307 if filename and source: | |
| 308 raise ValueError("Cannot specify both `filename' and `source'. " | |
| 309 "Use `source'.") | |
| 310 elif not filename and not source: | |
| 311 raise ValueError("Required argument `source' not given.") | |
| 312 elif filename: | |
| 313 source = filename | |
| 314 Error.__init__(self, 'Source contains parsing errors: %s' % source) | |
| 315 self.source = source | |
| 8614 | 316 self.errors = [] |
| 63875 | 317 self.args = (source, ) |
| 318 | |
| 319 @property | |
| 320 def filename(self): | |
| 321 """Deprecated, use `source'.""" | |
| 322 warnings.warn( | |
| 323 "This 'filename' attribute will be removed in future versions. " | |
| 324 "Use 'source' instead.", | |
| 325 PendingDeprecationWarning, stacklevel=2 | |
| 326 ) | |
| 327 return self.source | |
| 328 | |
| 329 @filename.setter | |
| 330 def filename(self, value): | |
| 331 """Deprecated, user `source'.""" | |
| 332 warnings.warn( | |
| 333 "The 'filename' attribute will be removed in future versions. " | |
| 334 "Use 'source' instead.", | |
| 335 PendingDeprecationWarning, stacklevel=2 | |
| 336 ) | |
| 337 self.source = value | |
| 8614 | 338 |
| 339 def append(self, lineno, line): | |
| 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 | 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 | 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 | 410 allow_no_value=False, *, delimiters=('=', ':'), |
| 411 comment_prefixes=_COMPATIBLE, strict=False, | |
| 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 | 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 | 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 | 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 | 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 | 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 | 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 | 484 configuration file locations (e.g. current directory, user's |
| 485 home directory, systemwide directory), and all existing | |
| 486 configuration files in the list will be read. A single | |
| 487 filename may also be given. | |
| 32117 | 488 |
| 489 Return list of successfully read files. | |
| 11369 | 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 | 493 read_ok = [] |
| 11369 | 494 for filename in filenames: |
| 495 try: | |
|
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
|
496 fp = open(filename, encoding=encoding) |
| 11369 | 497 except IOError: |
| 498 continue | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
499 self._read(fp, filename) |
|
11368
b9bbe88d9554
ConfigParser.read(): Don't mask IOError exceptions.
Fred Drake <fdrake@acm.org>
parents:
11007
diff
changeset
|
500 fp.close() |
| 32117 | 501 read_ok.append(filename) |
| 502 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
|
503 |
| 63875 | 504 def read_file(self, f, source=None): |
| 11369 | 505 """Like read() but the argument must be a file-like object. |
| 506 | |
| 63875 | 507 The `f' argument must have a `readline' method. Optional second |
| 508 argument is the `source' specifying the name of the file being read. If | |
| 509 not given, it is taken from f.name. If `f' has no `name' attribute, | |
| 510 `<???>' is used. | |
| 11369 | 511 """ |
| 63875 | 512 if source is None: |
| 11369 | 513 try: |
| 63875 | 514 srouce = f.name |
| 11369 | 515 except AttributeError: |
| 63875 | 516 source = '<???>' |
| 517 self._read(f, source) | |
| 518 | |
| 519 def read_string(self, string, source='<string>'): | |
| 520 """Read configuration from a given string.""" | |
| 521 sfile = io.StringIO(string) | |
| 522 self.read_file(sfile, source) | |
| 523 | |
| 524 def read_dict(self, dictionary, source='<dict>'): | |
| 525 """Read configuration from a dictionary. | |
| 526 | |
| 527 Keys are section names, values are dictionaries with keys and values | |
| 528 that should be present in the section. If the used dictionary type | |
| 529 preserves order, sections and their keys will be added in order. | |
| 530 | |
| 531 Optional second argument is the `source' specifying the name of the | |
| 532 dictionary being read. | |
| 533 """ | |
| 534 elements_added = set() | |
| 535 for section, keys in dictionary.items(): | |
| 536 try: | |
| 537 self.add_section(section) | |
| 538 except DuplicateSectionError: | |
| 539 if self._strict and section in elements_added: | |
| 540 raise | |
| 541 elements_added.add(section) | |
| 542 for key, value in keys.items(): | |
| 543 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
|
544 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
|
545 value = str(value) |
| 63875 | 546 if self._strict and (section, key) in elements_added: |
| 547 raise DuplicateOptionError(section, key, source) | |
| 548 elements_added.add((section, key)) | |
| 549 self.set(section, key, value) | |
| 550 | |
| 551 def readfp(self, fp, filename=None): | |
| 552 """Deprecated, use read_file instead.""" | |
| 553 warnings.warn( | |
| 554 "This method will be removed in future versions. " | |
| 555 "Use 'parser.read_file()' instead.", | |
| 556 PendingDeprecationWarning, stacklevel=2 | |
| 557 ) | |
| 558 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
|
559 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
560 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
|
561 """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
|
562 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
563 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
|
564 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
|
565 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
|
566 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
|
567 """ |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
568 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
569 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
|
570 except NoSectionError: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
571 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
|
572 raise |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
573 else: |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
574 return default |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
575 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
|
576 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
577 return d[option] |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
578 except KeyError: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
579 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
|
580 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
|
581 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
582 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
|
583 |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
584 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
|
585 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
586 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
|
587 except KeyError: |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
588 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
|
589 raise NoSectionError(section) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35276
diff
changeset
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 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
|
596 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
597 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
|
598 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
|
599 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
600 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
|
601 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
602 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
|
603 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
604 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
|
605 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
606 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
607 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
|
608 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
609 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
|
610 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
611 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
|
612 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
613 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
|
614 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
615 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
616 return default |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
617 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
618 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
|
619 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
620 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
|
621 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
622 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
|
623 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
624 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
625 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
|
626 |
|
11007
b9f55cc10ab3
Patch suggested (and partially provided) by Lars Damerow: instead of
Guido van Rossum <guido@python.org>
parents:
10145
diff
changeset
|
627 def optionxform(self, optionstr): |
|
16480
e97217f5b323
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
628 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
|
629 |
|
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
|
630 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
|
631 """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
|
632 if not section or section == DEFAULTSECT: |
|
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
633 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
|
634 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
|
635 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
|
636 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
|
637 else: |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
638 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
|
639 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
|
640 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
|
641 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
642 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
|
643 """Set an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
644 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
|
645 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
|
646 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
|
647 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
648 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
|
649 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
|
650 raise NoSectionError(section) |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
651 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
|
652 |
|
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
|
653 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
|
654 """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
|
655 |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples 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 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
|
657 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
|
658 """ |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples 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 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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 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
|
665 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
|
666 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
|
667 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
|
668 |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples 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 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
|
670 """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
|
671 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
|
672 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
|
673 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
|
674 continue |
|
64386
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
675 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
|
676 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
|
677 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
|
678 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".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
|
680 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
|
681 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
682 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
|
683 """Remove an option.""" |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
684 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
|
685 sectdict = self._defaults |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
686 else: |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
687 try: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
688 sectdict = self._sections[section] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
689 except KeyError: |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
690 raise NoSectionError(section) |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16594
diff
changeset
|
691 option = self.optionxform(option) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23611
diff
changeset
|
692 existed = option in sectdict |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
693 if existed: |
|
15479
e98964bc2681
remove_option(): Use the right variable name for the option name!
Fred Drake <fdrake@acm.org>
parents:
14899
diff
changeset
|
694 del sectdict[option] |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
695 return existed |
|
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
696 |
|
13650
3d6d218b673d
Small fixes by Petru Paler (patch #100946) checked in with esr's approval.
Thomas Wouters <thomas@python.org>
parents:
13600
diff
changeset
|
697 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
|
698 """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
|
699 existed = section in self._sections |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
700 if existed: |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
701 del self._sections[section] |
|
25459
d07f8db3db41
has_option(): Use the option name transform consistently.
Fred Drake <fdrake@acm.org>
parents:
23645
diff
changeset
|
702 return existed |
|
13600
39f54b9a7f9c
ConfigParser enhancements to edit existing configs, part 2
Eric S. Raymond <esr@thyrsus.com>
parents:
13521
diff
changeset
|
703 |
|
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
|
704 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
|
705 """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
|
706 |
| 63875 | 707 Each section in a configuration file contains a header, indicated by |
| 708 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
|
709 `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
|
710 default). |
| 8614 | 711 |
| 63875 | 712 Values can span multiple lines, as long as they are indented deeper |
| 713 than the first line of the value. Depending on the parser's mode, blank | |
| 714 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
|
715 |
|
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
|
716 Configuration files may include comments, prefixed by specific |
| 63875 | 717 characters (`#' and `;' by default). Comments may appear on their own |
| 718 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
|
719 section names. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
720 """ |
| 63875 | 721 elements_added = set() |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
722 cursect = None # None, or a dictionary |
| 63875 | 723 sectname = None |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
724 optname = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
725 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
|
726 indent_level = 0 |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
727 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
|
728 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
|
729 # 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
|
730 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
|
731 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
|
732 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
|
733 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
|
734 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
|
735 # 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
|
736 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
|
737 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
|
738 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
|
739 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
|
740 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
|
741 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
|
742 if not value: |
|
63280
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
743 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
|
744 # 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
|
745 # comment on the line |
|
63280
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
746 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
|
747 cursect is not None and |
|
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
748 optname and |
|
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
749 cursect[optname] is not None): |
|
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
750 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
|
751 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
|
752 # 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
|
753 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
|
754 continue |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7165
diff
changeset
|
755 # 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
|
756 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
|
757 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
|
758 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
|
759 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
|
760 cursect[optname].append(value) |
| 8614 | 761 # a section header or option header? |
| 762 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
|
763 indent_level = cur_indent_level |
| 8614 | 764 # 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
|
765 mo = self.SECTCRE.match(value) |
| 8614 | 766 if mo: |
| 767 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
|
768 if sectname in self._sections: |
| 63875 | 769 if self._strict and sectname in elements_added: |
| 770 raise DuplicateSectionError(sectname, fpname, | |
| 771 lineno) | |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
772 cursect = self._sections[sectname] |
| 63875 | 773 elements_added.add(sectname) |
| 8614 | 774 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
|
775 cursect = self._defaults |
| 8614 | 776 else: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35276
diff
changeset
|
777 cursect = self._dict() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35276
diff
changeset
|
778 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
|
779 self._sections[sectname] = cursect |
| 63875 | 780 elements_added.add(sectname) |
| 8614 | 781 # So sections can't start with a continuation line |
| 782 optname = None | |
| 783 # no section header in the file? | |
| 784 elif cursect is None: | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30734
diff
changeset
|
785 raise MissingSectionHeaderError(fpname, lineno, line) |
| 8614 | 786 # 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
|
787 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
|
788 mo = self._optcre.match(value) |
| 8614 | 789 if mo: |
|
11873
7ed104c40db3
(Finally!) Changes related to the ConfigParser/INI-file topics
Fred Drake <fdrake@acm.org>
parents:
11381
diff
changeset
|
790 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
|
791 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
|
792 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
|
793 optname = self.optionxform(optname.rstrip()) |
| 63875 | 794 if (self._strict and |
| 795 (sectname, optname) in elements_added): | |
| 796 raise DuplicateOptionError(sectname, optname, | |
| 797 fpname, lineno) | |
| 798 elements_added.add((sectname, optname)) | |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
799 # This check is fine because the OPTCRE cannot |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
800 # match if it would set optval to None |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
801 if optval is not None: |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
802 optval = optval.strip() |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
803 # allow empty values |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
804 if optval == '""': |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
805 optval = '' |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
806 cursect[optname] = [optval] |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
807 else: |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
808 # valueless option handling |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
809 cursect[optname] = optval |
| 8614 | 810 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
|
811 # a non-fatal parsing error occurred. set up the |
| 8614 | 812 # exception but keep going. the exception will be |
| 813 # raised at the end of the file and will contain a | |
| 814 # 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
|
815 e = self._handle_error(e, fpname, lineno, line) |
| 8614 | 816 # if any parsing errors occurred, raise an exception |
| 817 if e: | |
| 818 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
|
819 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
|
820 |
|
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
|
821 def _join_multiline_values(self): |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
822 all_sections = [self._defaults] |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
823 all_sections.extend(self._sections.values()) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
824 for options in all_sections: |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
825 for name, val in options.items(): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
826 if isinstance(val, list): |
|
63280
e2e32a869810
Fix for r83202: improve the handling of empty lines.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
827 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
|
828 |
|
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
|
829 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
|
830 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
|
831 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
|
832 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
|
833 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
|
834 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
835 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
|
836 """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
|
837 `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
|
838 take precendence. |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
839 """ |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
840 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
|
841 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
842 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
|
843 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
844 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
845 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
846 # 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
|
847 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
|
848 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
|
849 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
|
850 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
|
851 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
|
852 return d |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
853 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
854 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
|
855 """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
|
856 """ |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
857 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
|
858 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
|
859 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
|
860 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
861 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
862 class ConfigParser(RawConfigParser): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
863 """ConfigParser implementing interpolation.""" |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
864 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
865 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
|
866 """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
|
867 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
868 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
|
869 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
|
870 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
|
871 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
|
872 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
873 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
|
874 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
|
875 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
|
876 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
877 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
|
878 """ |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
879 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
880 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
|
881 except NoSectionError: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
882 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
|
883 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
884 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
885 return default |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
886 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
|
887 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
888 value = d[option] |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
889 except KeyError: |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
890 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
|
891 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
|
892 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
893 return default |
|
25908
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
894 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
895 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
|
896 return value |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
897 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
898 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
|
899 |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
900 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
|
901 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
902 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
|
903 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
904 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
|
905 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
906 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
907 return default |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
908 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
909 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
|
910 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
911 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
|
912 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
913 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
|
914 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
915 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
916 return default |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
917 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
918 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
|
919 default=_UNSET): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
920 try: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
921 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
|
922 vars) |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
923 except (NoSectionError, NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
924 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
|
925 raise |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
926 else: |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
927 return default |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
928 |
|
26685
a20cb5abab6b
Update doc for getboolean() to match code (ie, returning True/False)
Neal Norwitz <nnorwitz@gmail.com>
parents:
26126
diff
changeset
|
929 def items(self, section, raw=False, vars=None): |
| 63875 | 930 """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
|
931 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
932 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
|
933 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
|
934 `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
|
935 `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
|
936 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
|
937 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
938 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
|
939 """ |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
940 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
|
941 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
942 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
|
943 except KeyError: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
944 if section != DEFAULTSECT: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
945 raise NoSectionError(section) |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
946 # 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
|
947 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
|
948 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
|
949 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
|
950 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
|
951 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
|
952 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
|
953 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
|
954 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
|
955 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
|
956 else: |
|
30734
916f782ed6d5
Make both items() methods return lists; one had changed to return an
Fred Drake <fdrake@acm.org>
parents:
30723
diff
changeset
|
957 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
|
958 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
|
959 |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
960 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
|
961 # 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
|
962 value = rawval |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26078
diff
changeset
|
963 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
|
964 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
|
965 depth -= 1 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
966 if value and "%(" in value: |
| 32114 | 967 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
|
968 try: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
969 value = value % vars |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
970 except KeyError as e: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
971 raise InterpolationMissingOptionError( |
|
41274
bfc4e8046102
When removing indexing/slicing on exceptions some places were changed
Brett Cannon <bcannon@gmail.com>
parents:
40959
diff
changeset
|
972 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
|
973 else: |
|
df055a9eff9c
Re-factor: Use a RawConfigParser base class and make ConfigParser a
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
974 break |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
975 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
|
976 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
|
977 return value |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
978 |
| 32114 | 979 _KEYCRE = re.compile(r"%\(([^)]*)\)s|.") |
| 980 | |
| 981 def _interpolation_replace(self, match): | |
| 982 s = match.group(1) | |
| 983 if s is None: | |
| 984 return match.group() | |
| 985 else: | |
| 986 return "%%(%s)s" % self.optionxform(s) | |
| 987 | |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
988 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
989 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
|
990 """ConfigParser implementing sane interpolation.""" |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
991 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
992 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
|
993 # do the string interpolation |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
994 L = [] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
995 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
|
996 return ''.join(L) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
997 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41274
diff
changeset
|
998 _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
|
999 |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1000 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
|
1001 if depth > MAX_INTERPOLATION_DEPTH: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1002 raise InterpolationDepthError(option, section, rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1003 while rest: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1004 p = rest.find("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1005 if p < 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1006 accum.append(rest) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1007 return |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1008 if p > 0: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1009 accum.append(rest[:p]) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1010 rest = rest[p:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1011 # p is no longer used |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1012 c = rest[1:2] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1013 if c == "%": |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1014 accum.append("%") |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1015 rest = rest[2:] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1016 elif c == "(": |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41274
diff
changeset
|
1017 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
|
1018 if m is None: |
|
29661
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
1019 raise InterpolationSyntaxError(option, section, |
|
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
1020 "bad interpolation variable reference %r" % rest) |
| 32114 | 1021 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
|
1022 rest = rest[m.end():] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1023 try: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1024 v = map[var] |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1025 except KeyError: |
|
26969
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
1026 raise InterpolationMissingOptionError( |
|
776f270a6ea9
Further cleanup of exceptions. All interpolation-related exceptions
Fred Drake <fdrake@acm.org>
parents:
26927
diff
changeset
|
1027 option, section, rest, var) |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1028 if "%" in v: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1029 self._interpolate_some(option, accum, v, |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1030 section, map, depth + 1) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1031 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1032 accum.append(v) |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1033 else: |
|
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25916
diff
changeset
|
1034 raise InterpolationSyntaxError( |
|
29661
79303de9a05a
Fix arguments for instantiating InterpolationSyntaxError
Neal Norwitz <nnorwitz@gmail.com>
parents:
26969
diff
changeset
|
1035 option, section, |
| 63875 | 1036 "'%%' must be followed by '%%' or '(', " |
| 1037 "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
|
1038 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
1039 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
|
1040 """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
|
1041 # 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
|
1042 # 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
|
1043 # string if: |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
1044 # - we do not allow valueless options, or |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
1045 # - 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
|
1046 if self._optcre is self.OPTCRE or value: |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
1047 if not isinstance(value, str): |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
53525
diff
changeset
|
1048 raise TypeError("option values must be strings") |
| 63875 | 1049 # check for bad percent signs |
| 1050 if value: | |
| 1051 tmp_value = value.replace('%%', '') # escaped percent signs | |
| 1052 tmp_value = self._interpvar_re.sub('', tmp_value) # valid syntax | |
| 1053 if '%' in tmp_value: | |
| 1054 raise ValueError("invalid interpolation syntax in %r at " | |
| 1055 "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
|
1056 ConfigParser.set(self, section, option, value) |
