eric6/Helpviewer/OpenSearch/OpenSearchWriter.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 writer for open search engine descriptions.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QXmlStreamWriter, QIODevice
13
14
15 class OpenSearchWriter(QXmlStreamWriter):
16 """
17 Class implementing a writer for open search engine descriptions.
18 """
19 def __init__(self):
20 """
21 Constructor
22 """
23 super(OpenSearchWriter, self).__init__()
24
25 self.setAutoFormatting(True)
26
27 def write(self, device, engine):
28 """
29 Public method to write the description of an engine.
30
31 @param device reference to the device to write to (QIODevice)
32 @param engine reference to the engine (OpenSearchEngine)
33 @return flag indicating success (boolean)
34 """
35 if engine is None:
36 return False
37
38 if not device.isOpen():
39 if not device.open(QIODevice.WriteOnly):
40 return False
41
42 self.setDevice(device)
43 self.__write(engine)
44 return True
45
46 def __write(self, engine):
47 """
48 Private method to write the description of an engine.
49
50 @param engine reference to the engine (OpenSearchEngine)
51 """
52 self.writeStartDocument()
53 self.writeStartElement("OpenSearchDescription")
54 self.writeDefaultNamespace("http://a9.com/-/spec/opensearch/1.1/")
55
56 if engine.name():
57 self.writeTextElement("ShortName", engine.name())
58
59 if engine.description():
60 self.writeTextElement("Description", engine.description())
61
62 if engine.searchUrlTemplate():
63 self.writeStartElement("Url")
64 self.writeAttribute("method", engine.searchMethod())
65 self.writeAttribute("type", "text/html")
66 self.writeAttribute("template", engine.searchUrlTemplate())
67
68 if len(engine.searchParameters()) > 0:
69 self.writeNamespace(
70 "http://a9.com/-/spec/opensearch/extensions/"
71 "parameters/1.0/", "p")
72 for parameter in engine.searchParameters():
73 self.writeStartElement("p:Parameter")
74 self.writeAttribute("name", parameter[0])
75 self.writeAttribute("value", parameter[1])
76
77 self.writeEndElement()
78
79 if engine.suggestionsUrlTemplate():
80 self.writeStartElement("Url")
81 self.writeAttribute("method", engine.suggestionsMethod())
82 self.writeAttribute("type", "application/x-suggestions+json")
83 self.writeAttribute("template", engine.suggestionsUrlTemplate())
84
85 if len(engine.suggestionsParameters()) > 0:
86 self.writeNamespace(
87 "http://a9.com/-/spec/opensearch/extensions/"
88 "parameters/1.0/", "p")
89 for parameter in engine.suggestionsParameters():
90 self.writeStartElement("p:Parameter")
91 self.writeAttribute("name", parameter[0])
92 self.writeAttribute("value", parameter[1])
93
94 self.writeEndElement()
95
96 if engine.imageUrl():
97 self.writeTextElement("Image", engine.imageUrl())
98
99 self.writeEndElement()
100 self.writeEndDocument()

eric ide

mercurial