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