1
2
3 __docformat__ = 'epytext en'
4 __all__ = ['OpenSearchDescription']
5
6 from email.utils import parseaddr, formataddr
7
8 import amara
9
10 from amplee.ext.opensearch import OPENSEARCH_NS, OPENSEARCH_PREFIX
11 from amplee.utils import qname
12
15 self._short_name = None
16 self._description = None
17 self.urls = []
18 self._contact = None
19 self._tags = None
20 self._long_name = None
21 self.images = []
22 self.queries = []
23 self._developer = None
24 self._attribution = None
25 self._syndication_right = None
26 self.adult_content = False
27 self.languages = []
28 self.input_encodings = []
29 self.output_encodings = []
30
31
32
33
35 return self._short_name
36
38 if len(value) > 16:
39 raise ValueError("Short name must have a length inferior to 16 characters")
40
41 self._short_name = value
42
43 short_name = property(_get_short_name, _set_short_name,
44 doc="Gets or sets the short name of the description document")
45
47 return self._long_name
48
50 if len(value) > 48:
51 raise ValueError("Long name must have a length inferior to 48 characters")
52
53 self._long_name = value
54
55 long_name = property(_get_long_name, _set_long_name,
56 doc="Gets or sets the long name of the description document")
57
59 return self._description
60
62 if len(value) > 1024:
63 raise ValueError("Description must have a length inferior to 1024 characters")
64
65 self._description = value
66
67 description = property(_get_description, _set_description,
68 doc="Gets or sets the description content of the description document")
69
72
80
81 contact = property(_get_contact, _set_contact,
82 doc="Gets or sets the contact of the description document")
83
86
95
96 tags = property(_get_tags, _set_tags,
97 doc="Gets or sets the tags of the description document")
98
100 return self._developer
101
103 if len(value) > 64:
104 raise ValueError("Developer must have a length inferior to 64 characters")
105
106 self._developer = value
107
108 developer = property(_get_developer, _set_developer,
109 doc="Gets or sets the developer of the description document")
110
112 return self._attribution
113
115 if len(value) > 256:
116 raise ValueError("Attribution must have a length inferior to 256 characters")
117
118 self._attribution = value
119
120 attribution = property(_get_attribution, _set_attribution,
121 doc="Gets or sets the attribution of the description document")
122
124 return self._syndication_right
125
127 value = value.lower().strip()
128 if value not in [u'open', u'limited', u'private', u'closed']:
129 raise ValueError("Syndication right must be one of 'open', 'limited', 'private' or 'closed'")
130
131 self._syndication_right = value
132
133 syndication_right = property(_get_syndication_right, _set_syndication_right,
134 doc="Gets or sets the syndication right of the description document")
135
136
137
138
140 d = amara.create_document(prefixes={OPENSEARCH_PREFIX: OPENSEARCH_NS})
141
142 osd = d.xml_append(d.xml_create_element(qname(u"OpenSearchDescription",
143 OPENSEARCH_PREFIX),
144 ns=OPENSEARCH_NS))
145
146 if not self.short_name:
147 raise ValueError('Short name must be set')
148
149 osd.xml_append(d.xml_create_element(qname(u"ShortName", OPENSEARCH_PREFIX),
150 ns=OPENSEARCH_NS, content=self.short_name))
151
152 if self.long_name:
153 osd.xml_append(d.xml_create_element(qname(u"LongName", OPENSEARCH_PREFIX),
154 ns=OPENSEARCH_NS, content=self.long_name))
155
156
157 if not self.description:
158 raise ValueError('Description must be set')
159
160 osd.xml_append(d.xml_create_element(qname(u"Description", OPENSEARCH_PREFIX),
161 ns=OPENSEARCH_NS, content=self.description))
162
163 if len(self.urls) == 0:
164 raise ValueError('There must be at least one url set')
165
166 for url in self.urls:
167 url.add_to_element(osd)
168
169 if self.contact:
170 contact = formataddr(self.contact)
171 osd.xml_append(d.xml_create_element(qname(u"Contact", OPENSEARCH_PREFIX),
172 ns=OPENSEARCH_NS, content=contact))
173
174 if self.tags:
175 for tags in self.tags:
176 osd.xml_append(d.xml_create_element(qname(u"Tags", OPENSEARCH_PREFIX),
177 ns=OPENSEARCH_NS, content=tags))
178
179 if self.developer:
180 osd.xml_append(d.xml_create_element(qname(u"Developer", OPENSEARCH_PREFIX),
181 ns=OPENSEARCH_NS, content=self.developer))
182
183 if self.attribution:
184 osd.xml_append(d.xml_create_element(qname(u"Attribution", OPENSEARCH_PREFIX),
185 ns=OPENSEARCH_NS, content=self.attribution))
186
187 if self.syndication_right:
188 osd.xml_append(d.xml_create_element(qname(u"SyndicationRight", OPENSEARCH_PREFIX),
189 ns=OPENSEARCH_NS, content=self.syndication_right))
190
191 if self.adult_content == False:
192 osd.xml_append(d.xml_create_element(qname(u"AdultContent", OPENSEARCH_PREFIX),
193 ns=OPENSEARCH_NS, content=u'false'))
194 elif self.adult_content == True:
195 osd.xml_append(d.xml_create_element(qname(u"AdultContent", OPENSEARCH_PREFIX),
196 ns=OPENSEARCH_NS, content=u'true'))
197
198 for language in self.languages:
199 osd.xml_append(d.xml_create_element(qname(u"Language", OPENSEARCH_PREFIX),
200 ns=OPENSEARCH_NS, content=language))
201
202 for encoding in self.input_encodings:
203 osd.xml_append(d.xml_create_element(qname(u"InputEncoding", OPENSEARCH_PREFIX),
204 ns=OPENSEARCH_NS, content=encoding))
205
206 for encoding in self.output_encodings:
207 osd.xml_append(d.xml_create_element(qname(u"OutputEncoding", OPENSEARCH_PREFIX),
208 ns=OPENSEARCH_NS, content=encoding))
209
210 for image in self.images:
211 image.add_to_element(osd)
212
213 for query in self.queries:
214 query.add_to_element(osd)
215
216 return d
217
218 if __name__ == '__main__':
219 from amplee.ext.opensearch.url import OpenSearchURL
220 from amplee.ext.opensearch.image import OpenSearchImage
221 from amplee.ext.opensearch.query import OpenSearchQuery
222
223 osd = OpenSearchDescription()
224 osd.short_name = u'search me'
225 osd.description = u'some search'
226 osd.urls.append(OpenSearchURL(u'http://blah.com', mimetype=u'text/plain'))
227 osd.contact = u'sylvain <sh@defuze.org>'
228 osd.tags = u'atom atompub soa'
229 osd.developer = u'Sylvain Hellegouarch'
230 osd.syndication_right = u'open'
231 osd.languages.append(u'en-US')
232 osd.images.append(OpenSearchImage(u'http://myimage.png', mimetype=u'image/png'))
233 osd.queries.append(OpenSearchQuery(u'example'))
234 print osd.to_document().xml(indent=True)
235