|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module to read the plug-in repository contents file. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from .Config import pluginRepositoryFileFormatVersion |
|
13 from .XMLStreamReaderBase import XMLStreamReaderBase |
|
14 |
|
15 import Preferences |
|
16 |
|
17 |
|
18 class PluginRepositoryReader(XMLStreamReaderBase): |
|
19 """ |
|
20 Class to read the plug-in repository contents file. |
|
21 """ |
|
22 supportedVersions = ["4.1", "4.2"] |
|
23 |
|
24 def __init__(self, device, entryCallback): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param device reference to the I/O device to read from (QIODevice) |
|
29 @param entryCallback reference to a function to be called once the |
|
30 data for a plug-in has been read (function) |
|
31 """ |
|
32 XMLStreamReaderBase.__init__(self, device) |
|
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() == "Plugins": |
|
46 self.version = self.attribute( |
|
47 "version", |
|
48 pluginRepositoryFileFormatVersion) |
|
49 if self.version not in self.supportedVersions: |
|
50 self.raiseUnsupportedFormatVersion(self.version) |
|
51 elif self.name() == "RepositoryUrl": |
|
52 url = self.readElementText() |
|
53 Preferences.setUI("PluginRepositoryUrl6", url) |
|
54 elif self.name() == "Plugin": |
|
55 self.__readPlugin() |
|
56 else: |
|
57 self._skipUnknownElement() |
|
58 |
|
59 self.showErrorMessage() |
|
60 |
|
61 def __readPlugin(self): |
|
62 """ |
|
63 Private method to read the plug-in info. |
|
64 """ |
|
65 pluginInfo = {"name": "", |
|
66 "short": "", |
|
67 "description": "", |
|
68 "url": "", |
|
69 "author": "", |
|
70 "version": "", |
|
71 "filename": "", |
|
72 } |
|
73 pluginInfo["status"] = self.attribute("status", "unknown") |
|
74 |
|
75 while not self.atEnd(): |
|
76 self.readNext() |
|
77 if self.isEndElement() and self.name() == "Plugin": |
|
78 self.__entryCallback( |
|
79 pluginInfo["name"], pluginInfo["short"], |
|
80 pluginInfo["description"], pluginInfo["url"], |
|
81 pluginInfo["author"], pluginInfo["version"], |
|
82 pluginInfo["filename"], pluginInfo["status"]) |
|
83 break |
|
84 |
|
85 if self.isStartElement(): |
|
86 if self.name() == "Name": |
|
87 pluginInfo["name"] = self.readElementText() |
|
88 elif self.name() == "Short": |
|
89 pluginInfo["short"] = self.readElementText() |
|
90 elif self.name() == "Description": |
|
91 txt = self.readElementText() |
|
92 pluginInfo["description"] = \ |
|
93 [line.strip() for line in txt.splitlines()] |
|
94 elif self.name() == "Url": |
|
95 pluginInfo["url"] = self.readElementText() |
|
96 elif self.name() == "Author": |
|
97 pluginInfo["author"] = self.readElementText() |
|
98 elif self.name() == "Version": |
|
99 pluginInfo["version"] = self.readElementText() |
|
100 elif self.name() == "Filename": |
|
101 pluginInfo["filename"] = self.readElementText() |
|
102 else: |
|
103 self.raiseUnexpectedStartTag(self.name()) |