E5XML/SpellCheckDictionariesReader.py

changeset 5868
c1a98c164cd3
child 5870
82e04c70f969
diff -r 099008539886 -r c1a98c164cd3 E5XML/SpellCheckDictionariesReader.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/E5XML/SpellCheckDictionariesReader.py	Sat Sep 02 20:00:57 2017 +0200
@@ -0,0 +1,82 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module to read the web browser spell check dictionaries list file.
+"""
+
+from __future__ import unicode_literals
+
+from .Config import dictionariesListFileFormatVersion
+from .XMLStreamReaderBase import XMLStreamReaderBase
+
+import Preferences
+
+
+class SpellCheckDictionariesReader(XMLStreamReaderBase):
+    """
+    Class to read the web browser spell check dictionaries list file.
+    """
+    supportedVersions = ["1.0",]
+    
+    def __init__(self, data, entryCallback):
+        """
+        Constructor
+        
+        @param data reference to the data array to read XML from (QByteArray)
+        @param entryCallback reference to a function to be called once the
+            data for a dictionary has been read (function)
+        """
+        XMLStreamReaderBase.__init__(self, data)
+        
+        self.__entryCallback = entryCallback
+        
+        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() == "Dictionaries":
+                    self.version = self.attribute(
+                        "version",
+                        dictionariesListFileFormatVersion)
+                    if self.version not in self.supportedVersions:
+                        self.raiseUnsupportedFormatVersion(self.version)
+                elif self.name() == "DictionariesUrl":
+                    url = self.readElementText()
+                    Preferences.setWebBrowser("SpellCheckDictionariesUrl", url)
+                elif self.name() == "Dictionary":
+                    self.__readDictionary()
+                else:
+                    self._skipUnknownElement()
+        
+        self.showErrorMessage()
+    
+    def __readDictionary(self):
+        """
+        Private method to read the plug-in info.
+        """
+        dictionaryInfo = {"short": "",
+                      "filename": "",
+                      }
+        
+        while not self.atEnd():
+            self.readNext()
+            if self.isEndElement() and self.name() == "Dictionary":
+                self.__entryCallback(
+                    dictionaryInfo["short"], dictionaryInfo["filename"])
+                break
+            
+            if self.isStartElement():
+                if self.name() == "Short":
+                    dictionaryInfo["short"] = self.readElementText()
+                elif self.name() == "Filename":
+                    dictionaryInfo["filename"] = self.readElementText()
+                else:
+                    self.raiseUnexpectedStartTag(self.name())

eric ide

mercurial