eric6/Globals/E5ConfigParser.py

Sat, 31 Aug 2019 12:58:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Aug 2019 12:58:11 +0200
branch
without_py2_and_pyqt4
changeset 7192
a22eee00b052
parent 6942
2602857055c5
permissions
-rw-r--r--

Started removing runtime support for Python2 and PyQt4.

5255
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
5255
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a ConfigParser wrapper for Python 2 to provide the
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 dictionary like interface of the Python 3 variant.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from __future__ import unicode_literals
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 try:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from configparser import ConfigParser as E5ConfigParser
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 except ImportError:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 # Py2 part with the compatibility wrapper class
5294
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
17 try:
5616
adcffadf4962 Reworked some __IGNORE_WARNING__ comments to be more specific.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
18 from collections import OrderedDict as _default_dict
adcffadf4962 Reworked some __IGNORE_WARNING__ comments to be more specific.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
19 # __IGNORE_WARNING_N813__
5294
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
20 except ImportError:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
21 # fallback for setup.py which hasn't yet built _collections
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
22 _default_dict = dict
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
23
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
24 import re
5255
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 import itertools
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 from ConfigParser import SafeConfigParser, DEFAULTSECT
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 class E5ConfigParser(SafeConfigParser):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Class implementing a wrapper of the ConfigParser class implementing
5294
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
31 dictionary like special methods and some enhancements from Python 3.
5255
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
5294
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
33 _OPT_TMPL = r"""
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
34 (?P<option>.*?) # very permissive!
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
35 \s*(?P<vi>{delim})\s* # any number of space/tab,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
36 # followed by any of the
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
37 # allowed delimiters,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
38 # followed by any space/tab
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
39 (?P<value>.*)$ # everything up to eol
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
40 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
41 _OPT_NV_TMPL = r"""
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
42 (?P<option>.*?) # very permissive!
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
43 \s*(?: # any number of space/tab,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
44 (?P<vi>{delim})\s* # optionally followed by
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
45 # any of the allowed
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
46 # delimiters, followed by any
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
47 # space/tab
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
48 (?P<value>.*))?$ # everything up to eol
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
49 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
50 # Compiled regular expression for matching options with typical
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
51 # separators
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
52 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
53 # Compiled regular expression for matching options with optional
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
54 # values delimited using typical separators
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
55 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
56
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
57 def __init__(self, defaults=None, dict_type=_default_dict,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
58 allow_no_value=False, delimiters=('=', ':')):
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
59 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
60 Constructor
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
61 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
62 SafeConfigParser.__init__(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
63 self,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
64 defaults=defaults, dict_type=dict_type,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
65 allow_no_value=allow_no_value)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
66
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
67 if delimiters == ('=', ':'):
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
68 self._optcre = \
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
69 self.OPTCRE_NV if allow_no_value else self.OPTCRE
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
70 else:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
71 d = "|".join(re.escape(d) for d in delimiters)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
72 if allow_no_value:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
73 self._optcre = re.compile(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
74 self._OPT_NV_TMPL.format(delim=d), re.VERBOSE)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
75 else:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
76 self._optcre = re.compile(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
77 self._OPT_TMPL.format(delim=d), re.VERBOSE)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
78
5255
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 def __getitem__(self, key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 Special method to get a section.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @param key name of the section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @type str
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 @return section for the given key
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 @rtype dict
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 @exception KeyError raised if a non-existent key is given
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 if key == DEFAULTSECT:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 return self._defaults
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 elif self.has_section(key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 return self._sections[key]
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 else:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 raise KeyError(key)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 def __setitem__(self, key, values):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 Special method to set the values of a section.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @param key name of the section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @type str
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 @param values value for the section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 @type dict
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 # To conform with the mapping protocol, overwrites existing values
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 # in the section.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 if key == DEFAULTSECT:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self._defaults.clear()
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 elif self.has_section(key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self._sections[key].clear()
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 else:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self.add_section(key)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 for subkey, value in values.items():
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 subkey = self.optionxform(str(subkey))
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 if value is not None:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 value = str(value)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 self.set(key, subkey, value)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 def __delitem__(self, key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 Special method to delete a section.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 @param key name of the section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 @type str
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 @exception ValueError raised to indicate non-removal of the
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 default section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 @exception KeyError raised to indicate a non-existent section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 if key == DEFAULTSECT:
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 raise ValueError("Cannot remove the default section.")
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 if not self.has_section(key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 raise KeyError(key)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 self.remove_section(key)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 def __contains__(self, key):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 Special method to test, if a section is contained in the config.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @param key name of the section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 @type str
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 @return flag indicating containment
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @rtype bool
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 return key == DEFAULTSECT or self.has_section(key)
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 def __len__(self):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 Special method get the number of sections of the config.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 @return number of sections
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 @rtype int
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 return len(self._sections) + 1 # the default section
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 def __iter__(self):
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 Special method to return an iterator of the section names starting
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 with the default section.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 @return iterator of the section names contained in the config
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 @rtype iterator of str
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 """
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 return itertools.chain((DEFAULTSECT,), self._sections.keys())
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 if __name__ == "__main__":
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 # This is some test code.
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 import sys
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 c = E5ConfigParser()
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 c["DEFAULT"] = {'ServerAliveInterval': '45',
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 'Compression': 'yes',
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 'CompressionLevel': '9'}
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 c['bitbucket.org'] = {}
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 c['bitbucket.org']['User'] = 'hg'
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 c['topsecret.server.com'] = {}
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 topsecret = c['topsecret.server.com']
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 topsecret['Port'] = '50022'
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 topsecret['ForwardX11'] = 'no'
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 c['DEFAULT']['ForwardX11'] = 'yes'
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181
5fc76ccd369e Added a config parser class to implement the dictionary interface for the Python 2 config parser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 c.write(sys.stdout)

eric ide

mercurial