Globals/E5ConfigParser.py

changeset 5294
d70147155302
parent 5255
5fc76ccd369e
child 5389
9b1c800daff3
diff -r 26f1e696a2a5 -r d70147155302 Globals/E5ConfigParser.py
--- a/Globals/E5ConfigParser.py	Sun Nov 06 17:27:02 2016 +0100
+++ b/Globals/E5ConfigParser.py	Sun Nov 06 18:04:19 2016 +0100
@@ -14,14 +14,67 @@
     from configparser import ConfigParser as E5ConfigParser
 except ImportError:
     # Py2 part with the compatibility wrapper class
+    try:
+        from collections import OrderedDict as _default_dict    # __IGNORE_WARNING__
+    except ImportError:
+        # fallback for setup.py which hasn't yet built _collections
+        _default_dict = dict
+
+    import re
     import itertools
     from ConfigParser import SafeConfigParser, DEFAULTSECT
     
     class E5ConfigParser(SafeConfigParser):
         """
         Class implementing a wrapper of the ConfigParser class implementing
-        dictionary like special methods.
+        dictionary like special methods and some enhancements from Python 3.
         """
+        _OPT_TMPL = r"""
+            (?P<option>.*?)                    # very permissive!
+            \s*(?P<vi>{delim})\s*              # any number of space/tab,
+                                               # followed by any of the
+                                               # allowed delimiters,
+                                               # followed by any space/tab
+            (?P<value>.*)$                     # everything up to eol
+            """
+        _OPT_NV_TMPL = r"""
+            (?P<option>.*?)                    # very permissive!
+            \s*(?:                             # any number of space/tab,
+            (?P<vi>{delim})\s*                 # optionally followed by
+                                               # any of the allowed
+                                               # delimiters, followed by any
+                                               # space/tab
+            (?P<value>.*))?$                   # everything up to eol
+            """
+        # Compiled regular expression for matching options with typical
+        # separators
+        OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
+        # Compiled regular expression for matching options with optional
+        # values delimited using typical separators
+        OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
+        
+        def __init__(self, defaults=None, dict_type=_default_dict,
+                     allow_no_value=False, delimiters=('=', ':')):
+            """
+            Constructor
+            """
+            SafeConfigParser.__init__(
+                self,
+                defaults=defaults, dict_type=dict_type,
+                allow_no_value=allow_no_value)
+            
+            if delimiters == ('=', ':'):
+                self._optcre = \
+                    self.OPTCRE_NV if allow_no_value else self.OPTCRE
+            else:
+                d = "|".join(re.escape(d) for d in delimiters)
+                if allow_no_value:
+                    self._optcre = re.compile(
+                        self._OPT_NV_TMPL.format(delim=d), re.VERBOSE)
+                else:
+                    self._optcre = re.compile(
+                        self._OPT_TMPL.format(delim=d), re.VERBOSE)
+        
         def __getitem__(self, key):
             """
             Special method to get a section.

eric ide

mercurial