Added a config parser class to implement the dictionary interface for the Python 2 config parser.

Thu, 20 Oct 2016 18:55:14 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 20 Oct 2016 18:55:14 +0200
changeset 5255
5fc76ccd369e
parent 5254
c89ecaa0eda3
child 5256
4720cbf7ab52

Added a config parser class to implement the dictionary interface for the Python 2 config parser.

APIs/Python3/eric6.bas file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric6.Globals.E5ConfigParser.html file | annotate | diff | comparison | revisions
Documentation/Source/index-eric6.Globals.html file | annotate | diff | comparison | revisions
Globals/E5ConfigParser.py file | annotate | diff | comparison | revisions
eric6.e4p file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric6.bas	Tue Oct 18 18:44:27 2016 +0200
+++ b/APIs/Python3/eric6.bas	Thu Oct 20 18:55:14 2016 +0200
@@ -151,6 +151,7 @@
 E5ClickableLed E5Led
 E5ComboBox QComboBox
 E5ComboPathPicker E5PathPickerBase
+E5ConfigParser SafeConfigParser
 E5DirCompleter QCompleter
 E5DnDTabBar E5WheelTabBar
 E5ErrorMessage QErrorMessage
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Tue Oct 18 18:44:27 2016 +0200
+++ b/Documentation/Help/source.qhp	Thu Oct 20 18:55:14 2016 +0200
@@ -169,6 +169,7 @@
           </section>
           <section title="eric6.Globals" ref="index-eric6.Globals.html">
             <section title="eric6.Globals.AppInfo" ref="eric6.Globals.AppInfo.html" />
+            <section title="eric6.Globals.E5ConfigParser" ref="eric6.Globals.E5ConfigParser.html" />
             <section title="eric6.Globals.__init__" ref="eric6.Globals.__init__.html" />
             <section title="eric6.Globals.compatibility_fixes" ref="eric6.Globals.compatibility_fixes.html" />
           </section>
@@ -4073,6 +4074,14 @@
       <keyword name="E5ComboPathPicker" id="E5ComboPathPicker" ref="eric6.E5Gui.E5PathPicker.html#E5ComboPathPicker" />
       <keyword name="E5ComboPathPicker (Constructor)" id="E5ComboPathPicker (Constructor)" ref="eric6.E5Gui.E5PathPicker.html#E5ComboPathPicker.__init__" />
       <keyword name="E5Completers (Module)" id="E5Completers (Module)" ref="eric6.E5Gui.E5Completers.html" />
+      <keyword name="E5ConfigParser" id="E5ConfigParser" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser" />
+      <keyword name="E5ConfigParser (Module)" id="E5ConfigParser (Module)" ref="eric6.Globals.E5ConfigParser.html" />
+      <keyword name="E5ConfigParser.__contains__" id="E5ConfigParser.__contains__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__contains__" />
+      <keyword name="E5ConfigParser.__delitem__" id="E5ConfigParser.__delitem__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__delitem__" />
+      <keyword name="E5ConfigParser.__getitem__" id="E5ConfigParser.__getitem__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__getitem__" />
+      <keyword name="E5ConfigParser.__iter__" id="E5ConfigParser.__iter__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__iter__" />
+      <keyword name="E5ConfigParser.__len__" id="E5ConfigParser.__len__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__len__" />
+      <keyword name="E5ConfigParser.__setitem__" id="E5ConfigParser.__setitem__" ref="eric6.Globals.E5ConfigParser.html#E5ConfigParser.__setitem__" />
       <keyword name="E5DirCompleter" id="E5DirCompleter" ref="eric6.E5Gui.E5Completers.html#E5DirCompleter" />
       <keyword name="E5DirCompleter (Constructor)" id="E5DirCompleter (Constructor)" ref="eric6.E5Gui.E5Completers.html#E5DirCompleter.__init__" />
       <keyword name="E5DirCompleter.rootPath" id="E5DirCompleter.rootPath" ref="eric6.E5Gui.E5Completers.html#E5DirCompleter.rootPath" />
@@ -16813,6 +16822,7 @@
       <file>eric6.E5XML.XMLStreamReaderBase.html</file>
       <file>eric6.E5XML.XMLStreamWriterBase.html</file>
       <file>eric6.Globals.AppInfo.html</file>
+      <file>eric6.Globals.E5ConfigParser.html</file>
       <file>eric6.Globals.__init__.html</file>
       <file>eric6.Globals.compatibility_fixes.html</file>
       <file>eric6.Graphics.ApplicationDiagramBuilder.html</file>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Documentation/Source/eric6.Globals.E5ConfigParser.html	Thu Oct 20 18:55:14 2016 +0200
@@ -0,0 +1,196 @@
+<!DOCTYPE html>
+<html><head>
+<title>eric6.Globals.E5ConfigParser</title>
+<meta charset="UTF-8">
+<style>
+body {
+    background: #EDECE6;
+    margin: 0em 1em 10em 1em;
+    color: black;
+}
+
+h1 { color: white; background: #85774A; }
+h2 { color: white; background: #85774A; }
+h3 { color: white; background: #9D936E; }
+h4 { color: white; background: #9D936E; }
+    
+a { color: #BA6D36; }
+
+</style>
+</head>
+<body><a NAME="top" ID="top"></a>
+<h1>eric6.Globals.E5ConfigParser</h1>
+<p>
+Module implementing a ConfigParser wrapper for Python 2 to provide the
+dictionary like interface of the Python 3 variant.
+</p>
+<h3>Global Attributes</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<h3>Classes</h3>
+<table>
+<tr>
+<td><a href="#E5ConfigParser">E5ConfigParser</a></td>
+<td>Class implementing a wrapper of the ConfigParser class implementing dictionary like special methods.</td>
+</tr>
+</table>
+<h3>Functions</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<hr /><hr />
+<a NAME="E5ConfigParser" ID="E5ConfigParser"></a>
+<h2>E5ConfigParser</h2>
+<p>
+        Class implementing a wrapper of the ConfigParser class implementing
+        dictionary like special methods.
+</p>
+<h3>Derived from</h3>
+SafeConfigParser
+<h3>Class Attributes</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<h3>Class Methods</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<h3>Methods</h3>
+<table>
+<tr>
+<td><a href="#E5ConfigParser.__contains__">__contains__</a></td>
+<td>Special method to test, if a section is contained in the config.</td>
+</tr><tr>
+<td><a href="#E5ConfigParser.__delitem__">__delitem__</a></td>
+<td>Special method to delete a section.</td>
+</tr><tr>
+<td><a href="#E5ConfigParser.__getitem__">__getitem__</a></td>
+<td>Special method to get a section.</td>
+</tr><tr>
+<td><a href="#E5ConfigParser.__iter__">__iter__</a></td>
+<td>Special method to return an iterator of the section names starting with the default section.</td>
+</tr><tr>
+<td><a href="#E5ConfigParser.__len__">__len__</a></td>
+<td>Special method get the number of sections of the config.</td>
+</tr><tr>
+<td><a href="#E5ConfigParser.__setitem__">__setitem__</a></td>
+<td>Special method to set the values of a section.</td>
+</tr>
+</table>
+<h3>Static Methods</h3>
+<table>
+<tr><td>None</td></tr>
+</table>
+<a NAME="E5ConfigParser.__contains__" ID="E5ConfigParser.__contains__"></a>
+<h4>E5ConfigParser.__contains__</h4>
+<b>__contains__</b>(<i>key</i>)
+<p>
+            Special method to test, if a section is contained in the config.
+</p><dl>
+<dt><i>key</i> (str)</dt>
+<dd>
+name of the section
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+flag indicating containment
+</dd>
+</dl><dl>
+<dt>Return Type:</dt>
+<dd>
+bool
+</dd>
+</dl><a NAME="E5ConfigParser.__delitem__" ID="E5ConfigParser.__delitem__"></a>
+<h4>E5ConfigParser.__delitem__</h4>
+<b>__delitem__</b>(<i>key</i>)
+<p>
+            Special method to delete a section.
+</p><dl>
+<dt><i>key</i> (str)</dt>
+<dd>
+name of the section
+</dd>
+</dl><dl>
+<dt>Raises <b>KeyError</b>:</dt>
+<dd>
+raised to indicate a non-existent section
+</dd><dt>Raises <b>ValueError</b>:</dt>
+<dd>
+raised to indicate non-removal of the
+                default section
+</dd>
+</dl><a NAME="E5ConfigParser.__getitem__" ID="E5ConfigParser.__getitem__"></a>
+<h4>E5ConfigParser.__getitem__</h4>
+<b>__getitem__</b>(<i>key</i>)
+<p>
+            Special method to get a section.
+</p><dl>
+<dt><i>key</i> (str)</dt>
+<dd>
+name of the section
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+section for the given key
+</dd>
+</dl><dl>
+<dt>Return Type:</dt>
+<dd>
+dict
+</dd>
+</dl><dl>
+<dt>Raises <b>KeyError</b>:</dt>
+<dd>
+raised if a non-existent key is given
+</dd>
+</dl><a NAME="E5ConfigParser.__iter__" ID="E5ConfigParser.__iter__"></a>
+<h4>E5ConfigParser.__iter__</h4>
+<b>__iter__</b>(<i></i>)
+<p>
+            Special method to return an iterator of the section names starting
+            with the default section.
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+iterator of the section names contained in the config
+</dd>
+</dl><dl>
+<dt>Return Type:</dt>
+<dd>
+iterator of str
+</dd>
+</dl><a NAME="E5ConfigParser.__len__" ID="E5ConfigParser.__len__"></a>
+<h4>E5ConfigParser.__len__</h4>
+<b>__len__</b>(<i></i>)
+<p>
+            Special method get the number of sections of the config.
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+number of sections
+</dd>
+</dl><dl>
+<dt>Return Type:</dt>
+<dd>
+int
+</dd>
+</dl><a NAME="E5ConfigParser.__setitem__" ID="E5ConfigParser.__setitem__"></a>
+<h4>E5ConfigParser.__setitem__</h4>
+<b>__setitem__</b>(<i>key, values</i>)
+<p>
+            Special method to set the values of a section.
+</p><dl>
+<dt><i>key</i> (str)</dt>
+<dd>
+name of the section
+</dd><dt><i>values</i> (dict)</dt>
+<dd>
+value for the section
+</dd>
+</dl>
+<div align="right"><a href="#top">Up</a></div>
+<hr />
+</body></html>
\ No newline at end of file
--- a/Documentation/Source/index-eric6.Globals.html	Tue Oct 18 18:44:27 2016 +0200
+++ b/Documentation/Source/index-eric6.Globals.html	Thu Oct 20 18:55:14 2016 +0200
@@ -31,6 +31,9 @@
 <td><a href="eric6.Globals.AppInfo.html">AppInfo</a></td>
 <td>Module implementing a function to generate an application info.</td>
 </tr><tr>
+<td><a href="eric6.Globals.E5ConfigParser.html">E5ConfigParser</a></td>
+<td>Module implementing a ConfigParser wrapper for Python 2 to provide the dictionary like interface of the Python 3 variant.</td>
+</tr><tr>
 <td><a href="eric6.Globals.__init__.html">Globals</a></td>
 <td>Module defining common data to be used by all modules.</td>
 </tr><tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Globals/E5ConfigParser.py	Thu Oct 20 18:55:14 2016 +0200
@@ -0,0 +1,128 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a ConfigParser wrapper for Python 2 to provide the
+dictionary like interface of the Python 3 variant.
+"""
+
+from __future__ import unicode_literals
+
+try:
+    from configparser import ConfigParser as E5ConfigParser
+except ImportError:
+    # Py2 part with the compatibility wrapper class
+    import itertools
+    from ConfigParser import SafeConfigParser, DEFAULTSECT
+    
+    class E5ConfigParser(SafeConfigParser):
+        """
+        Class implementing a wrapper of the ConfigParser class implementing
+        dictionary like special methods.
+        """
+        def __getitem__(self, key):
+            """
+            Special method to get a section.
+            
+            @param key name of the section
+            @type str
+            @return section for the given key
+            @rtype dict
+            @exception KeyError raised if a non-existent key is given
+            """
+            if key == DEFAULTSECT:
+                return self._defaults
+            elif self.has_section(key):
+                return self._sections[key]
+            else:
+                raise KeyError(key)
+        
+        def __setitem__(self, key, values):
+            """
+            Special method to set the values of a section.
+            
+            @param key name of the section
+            @type str
+            @param values value for the section
+            @type dict
+            """
+            # To conform with the mapping protocol, overwrites existing values
+            # in the section.
+            if key == DEFAULTSECT:
+                self._defaults.clear()
+            elif self.has_section(key):
+                self._sections[key].clear()
+            else:
+                self.add_section(key)
+            for subkey, value in values.items():
+                subkey = self.optionxform(str(subkey))
+                if value is not None:
+                    value = str(value)
+                self.set(key, subkey, value)
+        
+        def __delitem__(self, key):
+            """
+            Special method to delete a section.
+            
+            @param key name of the section
+            @type str
+            @exception ValueError raised to indicate non-removal of the
+                default section
+            @exception KeyError raised to indicate a non-existent section
+            """
+            if key == DEFAULTSECT:
+                raise ValueError("Cannot remove the default section.")
+            if not self.has_section(key):
+                raise KeyError(key)
+            self.remove_section(key)
+        
+        def __contains__(self, key):
+            """
+            Special method to test, if a section is contained in the config.
+            
+            @param key name of the section
+            @type str
+            @return flag indicating containment
+            @rtype bool
+            """
+            return key == DEFAULTSECT or self.has_section(key)
+        
+        def __len__(self):
+            """
+            Special method get the number of sections of the config.
+            
+            @return number of sections
+            @rtype int
+            """
+            return len(self._sections) + 1  # the default section
+        
+        def __iter__(self):
+            """
+            Special method to return an iterator of the section names starting
+            with the default section.
+            
+            @return iterator of the section names contained in the config
+            @rtype iterator of str
+            """
+            return itertools.chain((DEFAULTSECT,), self._sections.keys())
+
+
+if __name__ == "__main__":
+    # This is some test code.
+    import sys
+    
+    c = E5ConfigParser()
+    c["DEFAULT"] = {'ServerAliveInterval': '45',
+                    'Compression': 'yes',
+                    'CompressionLevel': '9'}
+    c['bitbucket.org'] = {}
+    c['bitbucket.org']['User'] = 'hg'
+    c['topsecret.server.com'] = {}
+    topsecret = c['topsecret.server.com']
+    topsecret['Port'] = '50022'
+    topsecret['ForwardX11'] = 'no'
+    c['DEFAULT']['ForwardX11'] = 'yes'
+    
+    c.write(sys.stdout)
--- a/eric6.e4p	Tue Oct 18 18:44:27 2016 +0200
+++ b/eric6.e4p	Thu Oct 20 18:55:14 2016 +0200
@@ -195,6 +195,7 @@
     <Source>Examples/modpython_dbg.py</Source>
     <Source>Examples/rhallo.py</Source>
     <Source>Globals/AppInfo.py</Source>
+    <Source>Globals/E5ConfigParser.py</Source>
     <Source>Globals/__init__.py</Source>
     <Source>Globals/compatibility_fixes.py</Source>
     <Source>Graphics/ApplicationDiagramBuilder.py</Source>
@@ -1927,14 +1928,14 @@
   <Interfaces/>
   <Others>
     <Other>.hgignore</Other>
-    <Other>APIs/Python/zope-2.10.7.api</Other>
-    <Other>APIs/Python/zope-2.11.2.api</Other>
-    <Other>APIs/Python/zope-3.3.1.api</Other>
     <Other>APIs/Python3/PyQt4.bas</Other>
     <Other>APIs/Python3/PyQt5.bas</Other>
     <Other>APIs/Python3/QScintilla2.bas</Other>
     <Other>APIs/Python3/eric6.api</Other>
     <Other>APIs/Python3/eric6.bas</Other>
+    <Other>APIs/Python/zope-2.10.7.api</Other>
+    <Other>APIs/Python/zope-2.11.2.api</Other>
+    <Other>APIs/Python/zope-3.3.1.api</Other>
     <Other>APIs/QSS/qss.api</Other>
     <Other>APIs/Ruby/Ruby-1.8.7.api</Other>
     <Other>APIs/Ruby/Ruby-1.8.7.bas</Other>

eric ide

mercurial