Globals/E5ConfigParser.py

Sat, 19 Nov 2016 12:51:02 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 19 Nov 2016 12:51:02 +0100
branch
maintenance
changeset 5335
112840bac20c
parent 5294
d70147155302
child 5389
9b1c800daff3
permissions
-rw-r--r--

Prepared release 16.11.1

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
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
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
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:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
18 from collections import OrderedDict as _default_dict # __IGNORE_WARNING__
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
19 except ImportError:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
20 # 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
21 _default_dict = dict
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
22
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
23 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
24 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
25 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
26
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 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
28 """
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 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
30 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
31 """
5294
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
32 _OPT_TMPL = r"""
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
33 (?P<option>.*?) # very permissive!
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
34 \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
35 # followed by any of the
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
36 # allowed delimiters,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
37 # followed by any space/tab
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
38 (?P<value>.*)$ # everything up to eol
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
39 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
40 _OPT_NV_TMPL = r"""
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
41 (?P<option>.*?) # very permissive!
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
42 \s*(?: # any number of space/tab,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
43 (?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
44 # any of the allowed
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
45 # delimiters, followed by any
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
46 # space/tab
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
47 (?P<value>.*))?$ # everything up to eol
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
48 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
49 # 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
50 # separators
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
51 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
52 # 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
53 # values delimited using typical separators
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
54 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
55
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
56 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
57 allow_no_value=False, delimiters=('=', ':')):
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
58 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
59 Constructor
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
60 """
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
61 SafeConfigParser.__init__(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
62 self,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
63 defaults=defaults, dict_type=dict_type,
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
64 allow_no_value=allow_no_value)
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
65
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
66 if delimiters == ('=', ':'):
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
67 self._optcre = \
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
68 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
69 else:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
70 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
71 if allow_no_value:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
72 self._optcre = re.compile(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
73 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
74 else:
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
75 self._optcre = re.compile(
d70147155302 Fixed issues with the E5ConfigParser with Python2.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5255
diff changeset
76 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
77
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
78 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
79 """
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 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
81
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 @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
83 @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
84 @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
85 @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
86 @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
87 """
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 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
89 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
90 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
91 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
92 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
93 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
94
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 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
96 """
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 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
98
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 @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
100 @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
101 @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
102 @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
103 """
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 # 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
105 # 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117
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 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
119 """
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 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
121
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 @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
123 @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
124 @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
125 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
126 @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
127 """
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 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
129 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
130 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
131 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
132 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
133
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 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
135 """
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 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
137
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 @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
139 @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
140 @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
141 @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
142 """
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 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
144
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 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
146 """
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 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
148
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 @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
150 @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
151 """
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 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
153
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 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
155 """
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 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
157 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
158
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 @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
160 @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
161 """
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 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
163
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 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
166 # 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
167 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
168
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 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
170 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
171 '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
172 '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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180
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 c.write(sys.stdout)

eric ide

mercurial