src/eric7/WebBrowser/SpellCheck/SpellCheckDictionariesReader.py

branch
eric7
changeset 10938
2a7e115e2198
parent 10595
59579e8aff98
child 11090
f5f5f5803935
equal deleted inserted replaced
10937:3478c206c787 10938:2a7e115e2198
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2017 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module to read the web browser spell check dictionaries list file.
8 """
9
10 from eric7 import Preferences
11 from eric7.XML.XMLStreamReaderBase import XMLStreamReaderBase
12
13 # version number of the web browser spell check dictionaries list file
14 dictionariesListFileFormatVersion = "1.0"
15
16
17 class SpellCheckDictionariesReader(XMLStreamReaderBase):
18 """
19 Class to read the web browser spell check dictionaries list file.
20 """
21
22 supportedVersions = ["1.0"]
23
24 def __init__(self, data, entryCallback):
25 """
26 Constructor
27
28 @param data reference to the data array to read XML from
29 @type QByteArray
30 @param entryCallback reference to a function to be called once the
31 data for a dictionary has been read
32 @type function
33 """
34 XMLStreamReaderBase.__init__(self, data)
35
36 self.__entryCallback = entryCallback
37
38 self.version = ""
39 self.baseUrl = ""
40
41 def readXML(self):
42 """
43 Public method to read and parse the XML document.
44 """
45 while not self.atEnd():
46 self.readNext()
47 if self.isStartElement():
48 if self.name() == "Dictionaries":
49 self.version = self.attribute(
50 "version", dictionariesListFileFormatVersion
51 )
52 self.baseUrl = self.attribute("baseurl", "")
53 if self.version not in self.supportedVersions:
54 self.raiseUnsupportedFormatVersion(self.version)
55 elif self.name() == "DictionariesUrl":
56 url = self.readElementText()
57 Preferences.setWebBrowser("SpellCheckDictionariesUrl", url)
58 elif self.name() == "Dictionary":
59 self.__readDictionary()
60 else:
61 self._skipUnknownElement()
62
63 self.showErrorMessage()
64
65 def __readDictionary(self):
66 """
67 Private method to read the plug-in info.
68 """
69 dictionaryInfo = {
70 "short": "",
71 "filename": "",
72 "documentation": "",
73 "locales": [],
74 }
75
76 while not self.atEnd():
77 self.readNext()
78 if self.isEndElement() and self.name() == "Dictionary":
79 self.__entryCallback(
80 dictionaryInfo["short"],
81 dictionaryInfo["filename"],
82 self.baseUrl + dictionaryInfo["filename"],
83 dictionaryInfo["documentation"],
84 dictionaryInfo["locales"],
85 )
86 break
87
88 if self.isStartElement():
89 if self.name() == "Short":
90 dictionaryInfo["short"] = self.readElementText()
91 elif self.name() == "Filename":
92 dictionaryInfo["filename"] = self.readElementText()
93 elif self.name() == "Documentation":
94 dictionaryInfo["documentation"] = self.readElementText()
95 elif self.name() == "Locales":
96 dictionaryInfo["locales"] = self.readElementText().split()
97 else:
98 self.raiseUnexpectedStartTag(self.name())

eric ide

mercurial