eric6/Helpviewer/OpenSearch/OpenSearchReader.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a reader for open search engine descriptions.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QXmlStreamReader, QIODevice, QCoreApplication
13
14
15 class OpenSearchReader(QXmlStreamReader):
16 """
17 Class implementing a reader for open search engine descriptions.
18 """
19 def read(self, device):
20 """
21 Public method to read the description.
22
23 @param device device to read the description from (QIODevice)
24 @return search engine object (OpenSearchEngine)
25 """
26 self.clear()
27
28 if not device.isOpen():
29 device.open(QIODevice.ReadOnly)
30
31 self.setDevice(device)
32 return self.__read()
33
34 def __read(self):
35 """
36 Private method to read and parse the description.
37
38 @return search engine object (OpenSearchEngine)
39 """
40 from .OpenSearchEngine import OpenSearchEngine
41 engine = OpenSearchEngine()
42
43 while not self.isStartElement() and not self.atEnd():
44 self.readNext()
45
46 if self.name() != "OpenSearchDescription" or \
47 self.namespaceUri() != "http://a9.com/-/spec/opensearch/1.1/":
48 self.raiseError(QCoreApplication.translate(
49 "OpenSearchReader",
50 "The file is not an OpenSearch 1.1 file."))
51 return engine
52
53 while not self.atEnd():
54 self.readNext()
55
56 if not self.isStartElement():
57 continue
58
59 if self.name() == "ShortName":
60 engine.setName(self.readElementText())
61
62 elif self.name() == "Description":
63 engine.setDescription(self.readElementText())
64
65 elif self.name() == "Url":
66 type_ = self.attributes().value("type")
67 url = self.attributes().value("template")
68 method = self.attributes().value("method")
69
70 if type_ == "application/x-suggestions+json" and \
71 engine.suggestionsUrlTemplate():
72 continue
73
74 if (not type_ or
75 type_ == "text/html" or
76 type_ == "application/xhtml+xml") and \
77 engine.searchUrlTemplate():
78 continue
79
80 if not url:
81 continue
82
83 parameters = []
84
85 self.readNext()
86
87 while not (self.isEndElement() and self.name() == "Url"):
88 if not self.isStartElement() or \
89 (self.name() != "Param" and self.name() != "Parameter"):
90 self.readNext()
91 continue
92
93 key = self.attributes().value("name")
94 value = self.attributes().value("value")
95
96 if key and value:
97 parameters.append((key, value))
98
99 while not self.isEndElement():
100 self.readNext()
101
102 if type_ == "application/x-suggestions+json":
103 engine.setSuggestionsUrlTemplate(url)
104 engine.setSuggestionsParameters(parameters)
105 engine.setSuggestionsMethod(method)
106 elif not type_ or \
107 type_ == "text/html" or \
108 type_ == "application/xhtml+xml":
109 engine.setSearchUrlTemplate(url)
110 engine.setSearchParameters(parameters)
111 engine.setSearchMethod(method)
112
113 elif self.name() == "Image":
114 engine.setImageUrl(self.readElementText())
115
116 if engine.name() and \
117 engine.description() and \
118 engine.suggestionsUrlTemplate() and \
119 engine.searchUrlTemplate() and \
120 engine.imageUrl():
121 break
122
123 return engine

eric ide

mercurial