comparison sat/plugins/plugin_xep_0315.py @ 2772:30d95dc3c22d

plugin XEP-0315: implementation of Data Forms XML Element
author Goffi <goffi@goffi.org>
date Sat, 12 Jan 2019 15:01:20 +0100
parents
children 95321f233387
comparison
equal deleted inserted replaced
2771:003b8b4b56a7 2772:30d95dc3c22d
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin to implement XEP-0315 (Data Forms XML Element)
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 from wokkel import data_form
24 from twisted.words.xish import domish
25
26 log = getLogger(__name__)
27
28 PLUGIN_INFO = {
29 C.PI_NAME: "Data Forms XML Element",
30 C.PI_IMPORT_NAME: "XEP-0315",
31 C.PI_TYPE: "XEP",
32 C.PI_PROTOCOLS: ["XEP-0315"],
33 C.PI_DEPENDENCIES: [],
34 C.PI_MAIN: "XEP_0315",
35 C.PI_HANDLER: "no",
36 C.PI_DESCRIPTION: _("""Implementation of Data Forms XML Element"""),
37 }
38
39 NS_XML_ELEMENT = "urn:xmpp:xml-element"
40
41
42 class Field(data_form.Field):
43 """Field class adding implementation for XML Element"""
44
45
46 def __init__(self, fieldType='text-single', var=None, value=None,
47 values=None, options=None, label=None, desc=None,
48 required=False, ext_type=None):
49 """
50 @ext_type(None, unicode): extended type, can be:
51 - xml: field is an XML Element
52 - None: field is a base (XEP-0004) type
53 """
54
55 super(Field, self).__init__(fieldType, var, value, values, options, label, desc,
56 required)
57 self.ext_type = ext_type
58
59 def typeCheck(self):
60 if self.ext_type is not None:
61 # we use an extended type
62 if self.fieldType not in (None, 'hidden'):
63 raise ValueError('Invalid main type for a extended type field')
64 if len(self.values) != 1:
65 raise ValueError('extended type must have one and only one value')
66 if self.options:
67 raise ValueError('options must not be set for extented type')
68 if self.ext_type == u'xml':
69 if not isinstance(self.value, domish.Element):
70 raise ValueError('invalid value, a domish.Element is expected')
71 else:
72 raise ValueError(
73 'Unknown extended type: {ext_type}'.format(ext_type=self.ext_type))
74 return super(Field, self).typeCheck()
75
76 def toElement(self, asForm=False):
77 if self.ext_type is None:
78 return super(Field, self).toElement(asForm)
79 else:
80 self.typeCheck()
81 field_elt = domish.Element((data_form.NS_X_DATA, 'field'))
82 if self.fieldType:
83 field_elt['type'] = self.fieldType
84
85 if self.var is not None:
86 field_elt['var'] = self.var
87
88 wrapper_elt = field_elt.addElement((NS_XML_ELEMENT, u'wrapper'))
89 wrapper_elt.addChild(self.value)
90
91 if asForm:
92 if self.label is not None:
93 field_elt['label'] = self.label
94
95 if self.desc is not None:
96 field_elt.addElement('desc', content=self.desc)
97
98 if self.required:
99 field_elt.addElement('required')
100
101 return field_elt
102
103 @staticmethod
104 def fromElement(element):
105 field = super(Field, Field).fromElement(element)
106 if element.wrapper and element.wrapper.uri == NS_XML_ELEMENT:
107 field.ext_type = u"xml"
108 field.value = element.wrapper.firstChildElement()
109 return field
110
111
112 class XEP_0315(object):
113
114 def __init__(self, host):
115 self.host = host
116 if data_form.Field is not Field:
117 log.debug(u"installing data_form data extension")
118 data_form.Field = Field