Globals/E5ConfigParser.py

changeset 5294
d70147155302
parent 5255
5fc76ccd369e
child 5389
9b1c800daff3
equal deleted inserted replaced
5293:26f1e696a2a5 5294:d70147155302
12 12
13 try: 13 try:
14 from configparser import ConfigParser as E5ConfigParser 14 from configparser import ConfigParser as E5ConfigParser
15 except ImportError: 15 except ImportError:
16 # Py2 part with the compatibility wrapper class 16 # Py2 part with the compatibility wrapper class
17 try:
18 from collections import OrderedDict as _default_dict # __IGNORE_WARNING__
19 except ImportError:
20 # fallback for setup.py which hasn't yet built _collections
21 _default_dict = dict
22
23 import re
17 import itertools 24 import itertools
18 from ConfigParser import SafeConfigParser, DEFAULTSECT 25 from ConfigParser import SafeConfigParser, DEFAULTSECT
19 26
20 class E5ConfigParser(SafeConfigParser): 27 class E5ConfigParser(SafeConfigParser):
21 """ 28 """
22 Class implementing a wrapper of the ConfigParser class implementing 29 Class implementing a wrapper of the ConfigParser class implementing
23 dictionary like special methods. 30 dictionary like special methods and some enhancements from Python 3.
24 """ 31 """
32 _OPT_TMPL = r"""
33 (?P<option>.*?) # very permissive!
34 \s*(?P<vi>{delim})\s* # any number of space/tab,
35 # followed by any of the
36 # allowed delimiters,
37 # followed by any space/tab
38 (?P<value>.*)$ # everything up to eol
39 """
40 _OPT_NV_TMPL = r"""
41 (?P<option>.*?) # very permissive!
42 \s*(?: # any number of space/tab,
43 (?P<vi>{delim})\s* # optionally followed by
44 # any of the allowed
45 # delimiters, followed by any
46 # space/tab
47 (?P<value>.*))?$ # everything up to eol
48 """
49 # Compiled regular expression for matching options with typical
50 # separators
51 OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
52 # Compiled regular expression for matching options with optional
53 # values delimited using typical separators
54 OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
55
56 def __init__(self, defaults=None, dict_type=_default_dict,
57 allow_no_value=False, delimiters=('=', ':')):
58 """
59 Constructor
60 """
61 SafeConfigParser.__init__(
62 self,
63 defaults=defaults, dict_type=dict_type,
64 allow_no_value=allow_no_value)
65
66 if delimiters == ('=', ':'):
67 self._optcre = \
68 self.OPTCRE_NV if allow_no_value else self.OPTCRE
69 else:
70 d = "|".join(re.escape(d) for d in delimiters)
71 if allow_no_value:
72 self._optcre = re.compile(
73 self._OPT_NV_TMPL.format(delim=d), re.VERBOSE)
74 else:
75 self._optcre = re.compile(
76 self._OPT_TMPL.format(delim=d), re.VERBOSE)
77
25 def __getitem__(self, key): 78 def __getitem__(self, key):
26 """ 79 """
27 Special method to get a section. 80 Special method to get a section.
28 81
29 @param key name of the section 82 @param key name of the section

eric ide

mercurial