|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 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 __future__ import unicode_literals |
|
11 |
|
12 from .Config import dictionariesListFileFormatVersion |
|
13 from .XMLStreamReaderBase import XMLStreamReaderBase |
|
14 |
|
15 import Preferences |
|
16 |
|
17 |
|
18 class SpellCheckDictionariesReader(XMLStreamReaderBase): |
|
19 """ |
|
20 Class to read the web browser spell check dictionaries list file. |
|
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 (QByteArray) |
|
29 @param entryCallback reference to a function to be called once the |
|
30 data for a dictionary has been read (function) |
|
31 """ |
|
32 XMLStreamReaderBase.__init__(self, data) |
|
33 |
|
34 self.__entryCallback = entryCallback |
|
35 |
|
36 self.version = "" |
|
37 |
|
38 def readXML(self): |
|
39 """ |
|
40 Public method to read and parse the XML document. |
|
41 """ |
|
42 while not self.atEnd(): |
|
43 self.readNext() |
|
44 if self.isStartElement(): |
|
45 if self.name() == "Dictionaries": |
|
46 self.version = self.attribute( |
|
47 "version", |
|
48 dictionariesListFileFormatVersion) |
|
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 } |
|
68 |
|
69 while not self.atEnd(): |
|
70 self.readNext() |
|
71 if self.isEndElement() and self.name() == "Dictionary": |
|
72 self.__entryCallback( |
|
73 dictionaryInfo["short"], dictionaryInfo["filename"]) |
|
74 break |
|
75 |
|
76 if self.isStartElement(): |
|
77 if self.name() == "Short": |
|
78 dictionaryInfo["short"] = self.readElementText() |
|
79 elif self.name() == "Filename": |
|
80 dictionaryInfo["filename"] = self.readElementText() |
|
81 else: |
|
82 self.raiseUnexpectedStartTag(self.name()) |