Implemented the plug-in repository reader.

Fri, 10 Sep 2010 08:54:33 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 10 Sep 2010 08:54:33 +0200
changeset 580
45c38566b001
parent 579
9982cd83a786
child 581
a43695c8b8da

Implemented the plug-in repository reader.

E5XML/PluginRepositoryReader.py file | annotate | diff | comparison | revisions
PluginManager/PluginRepositoryDialog.py file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/E5XML/PluginRepositoryReader.py	Fri Sep 10 08:54:33 2010 +0200
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module to read the plug-in repository contents file.
+"""
+
+from .Config import pluginRepositoryFileFormatVersion
+from .XMLStreamReaderBase import XMLStreamReaderBase
+
+import Preferences
+
+class PluginRepositoryReader(XMLStreamReaderBase):
+    """
+    Class to read the plug-in repository contents file.
+    """
+    def __init__(self, device, dlg):
+        """
+        Constructor
+        
+        @param device reference to the I/O device to read from (QIODevice)
+        @param dlg reference to the plug-in repository dialog
+        """
+        XMLStreamReaderBase.__init__(self, device)
+        
+        self.dlg = dlg
+        
+        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() == "Plugins":
+                    self.version = self.attribute("version", 
+                        pluginRepositoryFileFormatVersion)
+                elif self.name() == "RepositoryUrl":
+                    url = self.readElementText()
+                    Preferences.setUI("PluginRepositoryUrl5", url)
+                elif self.name() == "Plugin":
+                    info = {"name"         : "",
+                            "short"        : "",
+                            "description"  : "",
+                            "url"          : "",
+                            "author"       : "",
+                            "version"      : "", 
+                            "filename"     : "",
+                    }
+                    info["status"] = self.attribute("status", "unknown")
+                    self.__readPlugin(info)
+                    self.dlg.addEntry(info["name"], info["short"], 
+                                      info["description"], info["url"], 
+                                      info["author"], info["version"],
+                                      info["filename"], info["status"])
+        
+        self.showErrorMessage()
+    
+    def __readPlugin(self, pluginInfo):
+        """
+        Private method to read the plug-in info.
+        
+        @param pluginInfo reference to the dictionary to hold the result
+        @return reference to the populated dictionary
+        """
+        while not self.atEnd():
+            self.readNext()
+            if self.isEndElement() and self.name() == "Plugin":
+                return pluginInfo
+            
+            if self.isStartElement():
+                if self.name() == "Name":
+                    pluginInfo["name"] = self.readElementText()
+                elif self.name() == "Short":
+                    pluginInfo["short"] = self.readElementText()
+                elif self.name() == "Description":
+                    txt = self.readElementText()
+                    pluginInfo["description"] = \
+                        [line.strip() for line in txt.splitlines()]
+                elif self.name() == "Url":
+                    pluginInfo["url"] = self.readElementText()
+                elif self.name() == "Author":
+                    pluginInfo["author"] = self.readElementText()
+                elif self.name() == "Version":
+                    pluginInfo["version"] = self.readElementText()
+                elif self.name() == "Filename":
+                    pluginInfo["filename"] = self.readElementText()
--- a/PluginManager/PluginRepositoryDialog.py	Fri Sep 10 08:45:26 2010 +0200
+++ b/PluginManager/PluginRepositoryDialog.py	Fri Sep 10 08:54:33 2010 +0200
@@ -11,7 +11,6 @@
 import sys
 import os
 import zipfile
-import io
 
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
@@ -21,10 +20,7 @@
 
 from E5Gui import E5MessageBox
 
-from E5XML.XMLUtilities import make_parser
-from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError
-from E5XML.XMLEntityResolver import XMLEntityResolver
-from E5XML.PluginRepositoryHandler import PluginRepositoryHandler
+from E5XML.PluginRepositoryReader import PluginRepositoryReader
 
 from E5Network.E5NetworkProxyFactory import proxyAuthenticationRequired
 
@@ -259,53 +255,10 @@
         
         if os.path.exists(self.pluginRepositoryFile):
             self.__repositoryMissing = False
-            try:
-                f = open(self.pluginRepositoryFile, "r", encoding = "utf-8")
-                line = f.readline()
-                dtdLine = f.readline()
-                f.close()
-            except IOError:
-                E5MessageBox.critical(self,
-                    self.trUtf8("Read plugins repository file"),
-                    self.trUtf8("<p>The plugins repository file <b>{0}</b> "
-                                "could not be read. Select Update</p>")\
-                        .format(self.pluginRepositoryFile))
-                return
-            
-            # now read the file
-            if line.startswith('<?xml'):
-                parser = make_parser(dtdLine.startswith("<!DOCTYPE"))
-                handler = PluginRepositoryHandler(self)
-                er = XMLEntityResolver()
-                eh = XMLErrorHandler()
-                
-                parser.setContentHandler(handler)
-                parser.setEntityResolver(er)
-                parser.setErrorHandler(eh)
-                
-                try:
-                    f = open(self.pluginRepositoryFile, "r", encoding = "utf-8")
-                    try:
-                        try:
-                            parser.parse(f)
-                        except UnicodeEncodeError:
-                            f.seek(0)
-                            buf = io.StringIO(f.read())
-                            parser.parse(buf)
-                    finally:
-                        f.close()
-                except IOError:
-                    E5MessageBox.critical(self,
-                        self.trUtf8("Read plugins repository file"),
-                        self.trUtf8("<p>The plugins repository file <b>{0}</b> "
-                                    "could not be read. Select Update</p>")\
-                            .format(self.pluginRepositoryFile))
-                    return
-                except XMLFatalParseError:
-                    pass
-                
-                eh.showParseMessages()
-                
+            f = QFile(self.pluginRepositoryFile)
+            if f.open(QIODevice.ReadOnly):
+                reader = PluginRepositoryReader(f, self)
+                reader.readXML()
                 self.repositoryList.resizeColumnToContents(0)
                 self.repositoryList.resizeColumnToContents(1)
                 self.repositoryList.resizeColumnToContents(2)
@@ -314,8 +267,9 @@
                 E5MessageBox.critical(self,
                     self.trUtf8("Read plugins repository file"),
                     self.trUtf8("<p>The plugins repository file <b>{0}</b> "
-                                "has an unsupported format.</p>")\
+                                "could not be read. Select Update</p>")\
                         .format(self.pluginRepositoryFile))
+                return
         else:
             self.__repositoryMissing = True
             QTreeWidgetItem(self.repositoryList, 
@@ -600,4 +554,4 @@
                 ).format(applPath),
                 self.trUtf8('OK'))
         
-        self.close()
\ No newline at end of file
+        self.close()

eric ide

mercurial