src/eric7/WebBrowser/OpenSearch/OpenSearchReader.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
12 12
13 class OpenSearchReader(QXmlStreamReader): 13 class OpenSearchReader(QXmlStreamReader):
14 """ 14 """
15 Class implementing a reader for open search engine descriptions. 15 Class implementing a reader for open search engine descriptions.
16 """ 16 """
17
17 def read(self, device): 18 def read(self, device):
18 """ 19 """
19 Public method to read the description. 20 Public method to read the description.
20 21
21 @param device device to read the description from (QIODevice) 22 @param device device to read the description from (QIODevice)
22 @return search engine object (OpenSearchEngine) 23 @return search engine object (OpenSearchEngine)
23 """ 24 """
24 self.clear() 25 self.clear()
25 26
26 if not device.isOpen(): 27 if not device.isOpen():
27 device.open(QIODevice.OpenModeFlag.ReadOnly) 28 device.open(QIODevice.OpenModeFlag.ReadOnly)
28 29
29 self.setDevice(device) 30 self.setDevice(device)
30 return self.__read() 31 return self.__read()
31 32
32 def __read(self): 33 def __read(self):
33 """ 34 """
34 Private method to read and parse the description. 35 Private method to read and parse the description.
35 36
36 @return search engine object (OpenSearchEngine) 37 @return search engine object (OpenSearchEngine)
37 """ 38 """
38 from .OpenSearchEngine import OpenSearchEngine 39 from .OpenSearchEngine import OpenSearchEngine
40
39 engine = OpenSearchEngine() 41 engine = OpenSearchEngine()
40 42
41 while not self.isStartElement() and not self.atEnd(): 43 while not self.isStartElement() and not self.atEnd():
42 self.readNext() 44 self.readNext()
43 45
44 if ( 46 if (
45 self.name() != "OpenSearchDescription" or 47 self.name() != "OpenSearchDescription"
46 self.namespaceUri() != "http://a9.com/-/spec/opensearch/1.1/" 48 or self.namespaceUri() != "http://a9.com/-/spec/opensearch/1.1/"
47 ): 49 ):
48 self.raiseError(QCoreApplication.translate( 50 self.raiseError(
49 "OpenSearchReader", 51 QCoreApplication.translate(
50 "The file is not an OpenSearch 1.1 file.")) 52 "OpenSearchReader", "The file is not an OpenSearch 1.1 file."
53 )
54 )
51 return engine 55 return engine
52 56
53 while not self.atEnd(): 57 while not self.atEnd():
54 self.readNext() 58 self.readNext()
55 59
56 if not self.isStartElement(): 60 if not self.isStartElement():
57 continue 61 continue
58 62
59 if self.name() == "ShortName": 63 if self.name() == "ShortName":
60 engine.setName(self.readElementText()) 64 engine.setName(self.readElementText())
61 65
62 elif self.name() == "Description": 66 elif self.name() == "Description":
63 engine.setDescription(self.readElementText()) 67 engine.setDescription(self.readElementText())
64 68
65 elif self.name() == "Url": 69 elif self.name() == "Url":
66 type_ = self.attributes().value("type") 70 type_ = self.attributes().value("type")
67 url = self.attributes().value("template") 71 url = self.attributes().value("template")
68 method = self.attributes().value("method") 72 method = self.attributes().value("method")
69 73
70 if ( 74 if (
71 type_ == "application/x-suggestions+json" and 75 type_ == "application/x-suggestions+json"
72 engine.suggestionsUrlTemplate() 76 and engine.suggestionsUrlTemplate()
73 ): 77 ):
74 continue 78 continue
75 79
76 if ( 80 if (
77 (not type_ or 81 not type_ or type_ in ("text/html", "application/xhtml+xml")
78 type_ in ("text/html", "application/xhtml+xml")) and 82 ) and engine.searchUrlTemplate():
79 engine.searchUrlTemplate()
80 ):
81 continue 83 continue
82 84
83 if not url: 85 if not url:
84 continue 86 continue
85 87
86 parameters = [] 88 parameters = []
87 89
88 self.readNext() 90 self.readNext()
89 91
90 while not (self.isEndElement() and self.name() == "Url"): 92 while not (self.isEndElement() and self.name() == "Url"):
91 if ( 93 if not self.isStartElement() or (
92 not self.isStartElement() or 94 self.name() != "Param" and self.name() != "Parameter"
93 (self.name() != "Param" and
94 self.name() != "Parameter")
95 ): 95 ):
96 self.readNext() 96 self.readNext()
97 continue 97 continue
98 98
99 key = self.attributes().value("name") 99 key = self.attributes().value("name")
100 value = self.attributes().value("value") 100 value = self.attributes().value("value")
101 101
102 if key and value: 102 if key and value:
103 parameters.append((key, value)) 103 parameters.append((key, value))
104 104
105 while not self.isEndElement(): 105 while not self.isEndElement():
106 self.readNext() 106 self.readNext()
107 107
108 if type_ == "application/x-suggestions+json": 108 if type_ == "application/x-suggestions+json":
109 engine.setSuggestionsUrlTemplate(url) 109 engine.setSuggestionsUrlTemplate(url)
110 engine.setSuggestionsParameters(parameters) 110 engine.setSuggestionsParameters(parameters)
111 engine.setSuggestionsMethod(method) 111 engine.setSuggestionsMethod(method)
112 elif ( 112 elif not type_ or type_ in ("text/html", "application/xhtml+xml"):
113 not type_ or
114 type_ in ("text/html", "application/xhtml+xml")
115 ):
116 engine.setSearchUrlTemplate(url) 113 engine.setSearchUrlTemplate(url)
117 engine.setSearchParameters(parameters) 114 engine.setSearchParameters(parameters)
118 engine.setSearchMethod(method) 115 engine.setSearchMethod(method)
119 116
120 elif self.name() == "Image": 117 elif self.name() == "Image":
121 engine.setImageUrl(self.readElementText()) 118 engine.setImageUrl(self.readElementText())
122 119
123 if ( 120 if (
124 engine.name() and 121 engine.name()
125 engine.description() and 122 and engine.description()
126 engine.suggestionsUrlTemplate() and 123 and engine.suggestionsUrlTemplate()
127 engine.searchUrlTemplate() and 124 and engine.searchUrlTemplate()
128 engine.imageUrl() 125 and engine.imageUrl()
129 ): 126 ):
130 break 127 break
131 128
132 return engine 129 return engine

eric ide

mercurial