Migrated user project properties handler to user project properties reader/writer.

Mon, 13 Sep 2010 09:06:38 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 13 Sep 2010 09:06:38 +0200
changeset 606
cc26d1c27da5
parent 605
9c7527ecbf6e
child 607
d95d60ba16aa

Migrated user project properties handler to user project properties reader/writer.

Documentation/Source/eric5.E5XML.UserProjectHandler.html file | annotate | diff | comparison | revisions
E5XML/UserProjectHandler.py file | annotate | diff | comparison | revisions
E5XML/UserProjectReader.py file | annotate | diff | comparison | revisions
E5XML/UserProjectWriter.py file | annotate | diff | comparison | revisions
Project/Project.py file | annotate | diff | comparison | revisions
eric5.e4p file | annotate | diff | comparison | revisions
--- a/Documentation/Source/eric5.E5XML.UserProjectHandler.html	Mon Sep 13 08:32:36 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
-'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
-<html><head>
-<title>eric5.E5XML.UserProjectHandler</title>
-<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>eric5.E5XML.UserProjectHandler</h1>
-<p>
-Module implementing the handler class for reading an XML user project properties file.
-</p>
-<h3>Global Attributes</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<h3>Classes</h3>
-<table>
-<tr>
-<td><a href="#UserProjectHandler">UserProjectHandler</a></td>
-<td>Class implementing a sax handler to read an XML user project properties file.</td>
-</tr>
-</table>
-<h3>Functions</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<hr /><hr />
-<a NAME="UserProjectHandler" ID="UserProjectHandler"></a>
-<h2>UserProjectHandler</h2>
-<p>
-    Class implementing a sax handler to read an XML user project properties file.
-</p>
-<h3>Derived from</h3>
-XMLHandlerBase
-<h3>Class Attributes</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<h3>Methods</h3>
-<table>
-<tr>
-<td><a href="#UserProjectHandler.__init__">UserProjectHandler</a></td>
-<td>Constructor</td>
-</tr><tr>
-<td><a href="#UserProjectHandler.endVcsType">endVcsType</a></td>
-<td>Handler method for the "VcsType" end tag.</td>
-</tr><tr>
-<td><a href="#UserProjectHandler.getVersion">getVersion</a></td>
-<td>Public method to retrieve the version of the user project file.</td>
-</tr><tr>
-<td><a href="#UserProjectHandler.startDocumentProject">startDocumentProject</a></td>
-<td>Handler called, when the document parsing is started.</td>
-</tr><tr>
-<td><a href="#UserProjectHandler.startUserProject">startUserProject</a></td>
-<td>Handler method for the "UserProject" start tag.</td>
-</tr><tr>
-<td><a href="#UserProjectHandler.startVcsStatusMonitorInterval">startVcsStatusMonitorInterval</a></td>
-<td>Handler method for the "VcsStatusMonitorInterval" start tag.</td>
-</tr>
-</table>
-<a NAME="UserProjectHandler.__init__" ID="UserProjectHandler.__init__"></a>
-<h4>UserProjectHandler (Constructor)</h4>
-<b>UserProjectHandler</b>(<i>project</i>)
-<p>
-        Constructor
-</p><dl>
-<dt><i>project</i></dt>
-<dd>
-Reference to the project object to store the
-                information into.
-</dd>
-</dl><a NAME="UserProjectHandler.endVcsType" ID="UserProjectHandler.endVcsType"></a>
-<h4>UserProjectHandler.endVcsType</h4>
-<b>endVcsType</b>(<i></i>)
-<p>
-        Handler method for the "VcsType" end tag.
-</p><a NAME="UserProjectHandler.getVersion" ID="UserProjectHandler.getVersion"></a>
-<h4>UserProjectHandler.getVersion</h4>
-<b>getVersion</b>(<i></i>)
-<p>
-        Public method to retrieve the version of the user project file.
-</p><dl>
-<dt>Returns:</dt>
-<dd>
-String containing the version number.
-</dd>
-</dl><a NAME="UserProjectHandler.startDocumentProject" ID="UserProjectHandler.startDocumentProject"></a>
-<h4>UserProjectHandler.startDocumentProject</h4>
-<b>startDocumentProject</b>(<i></i>)
-<p>
-        Handler called, when the document parsing is started.
-</p><a NAME="UserProjectHandler.startUserProject" ID="UserProjectHandler.startUserProject"></a>
-<h4>UserProjectHandler.startUserProject</h4>
-<b>startUserProject</b>(<i>attrs</i>)
-<p>
-        Handler method for the "UserProject" start tag.
-</p><dl>
-<dt><i>attrs</i></dt>
-<dd>
-list of tag attributes
-</dd>
-</dl><a NAME="UserProjectHandler.startVcsStatusMonitorInterval" ID="UserProjectHandler.startVcsStatusMonitorInterval"></a>
-<h4>UserProjectHandler.startVcsStatusMonitorInterval</h4>
-<b>startVcsStatusMonitorInterval</b>(<i>attrs</i>)
-<p>
-        Handler method for the "VcsStatusMonitorInterval" start tag.
-</p><dl>
-<dt><i>attrs</i></dt>
-<dd>
-list of tag attributes
-</dd>
-</dl>
-<div align="right"><a href="#top">Up</a></div>
-<hr />
-</body></html>
\ No newline at end of file
--- a/E5XML/UserProjectHandler.py	Mon Sep 13 08:32:36 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# Copyright (c) 2006 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
-#
-
-"""
-Module implementing the handler class for reading an XML user project properties file.
-"""
-
-from .Config import userProjectFileFormatVersion
-from .XMLHandlerBase import XMLHandlerBase
-
-import Preferences
-
-class UserProjectHandler(XMLHandlerBase):
-    """
-    Class implementing a sax handler to read an XML user project properties file.
-    """
-    def __init__(self, project):
-        """
-        Constructor
-        
-        @param project Reference to the project object to store the
-                information into.
-        """
-        XMLHandlerBase.__init__(self)
-        
-        self.startDocumentSpecific = self.startDocumentProject
-        
-        self.elements.update({
-            'UserProject' : (self.startUserProject, self.defaultEndElement),
-            'VcsType' : (self.defaultStartElement, self.endVcsType),
-            'VcsStatusMonitorInterval' : (self.startVcsStatusMonitorInterval, 
-                                          self.defaultEndElement),
-        })
-    
-        self.project = project
-        
-    def startDocumentProject(self):
-        """
-        Handler called, when the document parsing is started.
-        """
-        self.version = ''
-        
-    ###################################################
-    ## below follow the individual handler functions
-    ###################################################
-    
-    def endVcsType(self):
-        """
-        Handler method for the "VcsType" end tag.
-        """
-        self.project.pudata["VCSOVERRIDE"] = [self.buffer]
-        
-    def startVcsStatusMonitorInterval(self, attrs):
-        """
-        Handler method for the "VcsStatusMonitorInterval" start tag.
-        
-        @param attrs list of tag attributes
-        """
-        interval = int(attrs.get("value", Preferences.getVCS("StatusMonitorInterval")))
-        self.project.pudata["VCSSTATUSMONITORINTERVAL"] = [interval]
-        
-    def startUserProject(self, attrs):
-        """
-        Handler method for the "UserProject" start tag.
-        
-        @param attrs list of tag attributes
-        """
-        self.version = attrs.get('version', userProjectFileFormatVersion)
-        
-    def getVersion(self):
-        """
-        Public method to retrieve the version of the user project file.
-        
-        @return String containing the version number.
-        """
-        return self.version
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/E5XML/UserProjectReader.py	Mon Sep 13 09:06:38 2010 +0200
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a class for reading an XML user project properties file.
+"""
+
+from .Config import userProjectFileFormatVersion
+from .XMLStreamReaderBase import XMLStreamReaderBase
+
+import Preferences
+
+class UserProjectReader(XMLStreamReaderBase):
+    """
+    Class for reading an XML user project properties file.
+    """
+    supportedVersions = ["4.0"]
+    
+    def __init__(self, device, project):
+        """
+        Constructor
+        
+        @param device reference to the I/O device to read from (QIODevice)
+        @param project Reference to the project object to store the
+                information into.
+        """
+        XMLStreamReaderBase.__init__(self, device)
+    
+        self.project = project
+        
+        self.version = ""
+    
+    def readXML(self):
+        """
+        Public method to read and parse the XML document.
+        """
+        while not self.atEnd():
+            self.readNext()
+            if self.isStartElement():
+                if self.name() == "UserProject":
+                    self.version = self.attribute("version", userProjectFileFormatVersion)
+                    if self.version not in self.supportedVersions:
+                        self.raiseUnsupportedFormatVersion(self.version)
+                elif self.name() == "VcsType":
+                    self.project.pudata["VCSOVERRIDE"] = [self.readElementText()]
+                elif self.name() == "VcsStatusMonitorInterval":
+                    interval = int(self.attribute("value", 
+                        Preferences.getVCS("StatusMonitorInterval")))
+                    self.project.pudata["VCSSTATUSMONITORINTERVAL"] = [interval]
+                else:
+                    self.raiseUnexpectedStartTag(self.name())
+        
+        self.showErrorMessage()
--- a/E5XML/UserProjectWriter.py	Mon Sep 13 08:32:36 2010 +0200
+++ b/E5XML/UserProjectWriter.py	Mon Sep 13 09:06:38 2010 +0200
@@ -11,23 +11,23 @@
 
 from E5Gui.E5Application import e5App
 
-from .XMLWriterBase import XMLWriterBase
+from .XMLStreamWriterBase import XMLStreamWriterBase
 from .Config import userProjectFileFormatVersion
 
 import Preferences
 
-class UserProjectWriter(XMLWriterBase):
+class UserProjectWriter(XMLStreamWriterBase):
     """
     Class implementing the writer class for writing an XML user project properties  file.
     """
-    def __init__(self, file, projectName):
+    def __init__(self, device, projectName):
         """
         Constructor
         
-        @param file open file (like) object for writing
+        @param device reference to the I/O device to write to (QIODevice)
         @param projectName name of the project (string)
         """
-        XMLWriterBase.__init__(self, file)
+        XMLStreamWriterBase.__init__(self, device)
         
         self.pudata = e5App().getObject("Project").pudata
         self.pdata = e5App().getObject("Project").pdata
@@ -37,28 +37,28 @@
         """
         Public method to write the XML to the file.
         """
-        XMLWriterBase.writeXML(self)
+        XMLStreamWriterBase.writeXML(self)
         
-        self._write('<!DOCTYPE UserProject SYSTEM "UserProject-{0}.dtd">'.format(
+        self.writeDTD('<!DOCTYPE UserProject SYSTEM "UserProject-{0}.dtd">'.format(
             userProjectFileFormatVersion))
         
         # add some generation comments
-        self._write("<!-- eric5 user project file for project {0} -->".format(self.name))
+        self.writeComment(" eric5 user project file for project {0} ".format(self.name))
         if Preferences.getProject("XMLTimestamp"):
-            self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
-            self._write("<!-- Copyright (C) {0} {1}, {2} -->".format(
-                    time.strftime('%Y'), 
-                    self.escape(self.pdata["AUTHOR"][0]), 
-                    self.escape(self.pdata["EMAIL"][0])))
+            self.writeComment(" Saved: {0} ".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
+            self.writeComment(" Copyright (C) {0} {1}, {2} ".format(
+                time.strftime('%Y'), self.pdata["AUTHOR"][0], self.pdata["EMAIL"][0]))
         
         # add the main tag
-        self._write('<UserProject version="{0}">'.format(userProjectFileFormatVersion))
+        self.writeStartElement("UserProject")
+        self.writeAttribute("version", userProjectFileFormatVersion)
         
         # do the vcs override stuff
         if self.pudata["VCSOVERRIDE"]:
-            self._write("  <VcsType>{0}</VcsType>".format(self.pudata["VCSOVERRIDE"][0]))
+            self.writeTextElement("VcsType", self.pudata["VCSOVERRIDE"][0])
         if self.pudata["VCSSTATUSMONITORINTERVAL"]:
-            self._write('  <VcsStatusMonitorInterval value="{0:d}" />'.format(
-                self.pudata["VCSSTATUSMONITORINTERVAL"][0]))
+            self.writeEmptyElement("VcsStatusMonitorInterval")
+            self.writeAttribute("value", str(self.pudata["VCSSTATUSMONITORINTERVAL"][0]))
         
-        self._write("</UserProject>", newline = False)
+        self.writeEndElement()
+        self.writeEndDocument()
--- a/Project/Project.py	Mon Sep 13 08:32:36 2010 +0200
+++ b/Project/Project.py	Mon Sep 13 09:06:38 2010 +0200
@@ -43,7 +43,7 @@
 
 from E5XML.ProjectHandler import ProjectHandler
 from E5XML.ProjectWriter import ProjectWriter
-from E5XML.UserProjectHandler import UserProjectHandler
+from E5XML.UserProjectReader import UserProjectReader
 from E5XML.UserProjectWriter import UserProjectWriter
 from E5XML.SessionReader import SessionReader
 from E5XML.SessionWriter import SessionWriter
@@ -823,40 +823,18 @@
             return
         
         fn, ext = os.path.splitext(os.path.basename(self.pfile))
-        
         fn = os.path.join(self.getProjectManagementDir(), '{0}.e4q'.format(fn))
         if os.path.exists(fn):
-            try:
-                f = open(fn, "r", encoding = "utf-8")
-                
-                parser = make_parser(True)
-                handler = UserProjectHandler(self)
-                er = XMLEntityResolver()
-                eh = XMLErrorHandler()
-                
-                parser.setContentHandler(handler)
-                parser.setEntityResolver(er)
-                parser.setErrorHandler(eh)
-                
-                try:
-                    try:
-                        parser.parse(f)
-                    except UnicodeEncodeError:
-                        f.seek(0)
-                        buf = io.StringIO(f.read())
-                        parser.parse(buf)
-                finally:
-                    f.close()
-            except IOError:
+            f = QFile(fn)
+            if f.open(QIODevice.ReadOnly):
+                reader = UserProjectReader(f, self)
+                reader.readXML()
+                f.close()
+            else:
                 E5MessageBox.critical(self.ui,
                     self.trUtf8("Read user project properties"),
                     self.trUtf8("<p>The user specific project properties file <b>{0}</b>"
                         " could not be read.</p>").format(fn))
-                return
-            except XMLFatalParseError:
-                pass
-            
-            eh.showParseMessages()
         
     def __writeUserProperties(self):
         """
@@ -866,16 +844,13 @@
             return
         
         fn, ext = os.path.splitext(os.path.basename(self.pfile))
-        
         fn = os.path.join(self.getProjectManagementDir(), '{0}.e4q'.format(fn))
         
-        try:
-            f = open(fn, "w", encoding = "utf-8")
-            
+        f = QFile(fn)
+        if f.open(QIODevice.WriteOnly):
             UserProjectWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML()
-            
             f.close()
-        except IOError:
+        else:
             E5MessageBox.critical(self.ui,
                 self.trUtf8("Save user project properties"),
                 self.trUtf8("<p>The user specific project properties file <b>{0}</b>"
--- a/eric5.e4p	Mon Sep 13 08:32:36 2010 +0200
+++ b/eric5.e4p	Mon Sep 13 09:06:38 2010 +0200
@@ -719,7 +719,6 @@
     <Source>E5XML/__init__.py</Source>
     <Source>E5XML/TasksWriter.py</Source>
     <Source>E5XML/UserProjectWriter.py</Source>
-    <Source>E5XML/UserProjectHandler.py</Source>
     <Source>E5XML/MultiProjectWriter.py</Source>
     <Source>E5XML/HighlightingStylesWriter.py</Source>
     <Source>E5XML/XMLUtilities.py</Source>
@@ -821,6 +820,7 @@
     <Source>E5XML/ShortcutsReader.py</Source>
     <Source>E5XML/SessionReader.py</Source>
     <Source>E5XML/MultiProjectReader.py</Source>
+    <Source>E5XML/UserProjectReader.py</Source>
   </Sources>
   <Forms>
     <Form>PyUnit/UnittestDialog.ui</Form>

eric ide

mercurial