annotate sat_tmp/wokkel/data_form.py @ 61:1245269f9aab

wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
author Goffi <goffi@goffi.org>
date Wed, 16 Jan 2019 09:17:57 +0100
parents
children 0721b6254c9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- test-case-name: wokkel.test.test_rsm -*-
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
3 #
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Series of Wokkel patches used by SàT
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2019 Jérôme Poisson (goffi@goffi.org)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
6
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
11
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
16
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
19
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from wokkel import data_form
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.xish import domish
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
22
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
23 NS_XML_ELEMENT = u"urn:xmpp:xml-element"
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
24
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class Field(data_form.Field):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
27 """Field class adding implementation for XML Element"""
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
28
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
29
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def __init__(self, fieldType='text-single', var=None, value=None,
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
31 values=None, options=None, label=None, desc=None,
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
32 required=False, ext_type=None):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
33 """
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
34 @param ext_type(None, unicode): extended type, can be:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
35 - xml: field is an XML Element
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
36 - None: field is a base (XEP-0004) type
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
38
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
39 super(Field, self).__init__(fieldType, var, value, values, options, label, desc,
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
40 required)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.ext_type = ext_type
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
42
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def typeCheck(self):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if self.ext_type is not None:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
45 # we use an extended type
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
46 if self.fieldType not in (None, 'hidden'):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
47 raise ValueError('Invalid main type for a extended type field')
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if len(self.values) != 1:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
49 raise ValueError('extended type must have one and only one value')
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if self.options:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
51 raise ValueError('options must not be set for extented type')
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
52 if self.ext_type == u'xml':
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
53 if not isinstance(self.value, domish.Element):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
54 raise ValueError('invalid value, a domish.Element is expected')
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
55 else:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
56 raise ValueError(
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
57 'Unknown extended type: {ext_type}'.format(ext_type=self.ext_type))
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return super(Field, self).typeCheck()
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
59
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def toElement(self, asForm=False):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
61 if self.ext_type is None:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return super(Field, self).toElement(asForm)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
63 else:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.typeCheck()
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
65 field_elt = domish.Element((data_form.NS_X_DATA, 'field'))
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if self.fieldType:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
67 field_elt['type'] = self.fieldType
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
68
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if self.var is not None:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
70 field_elt['var'] = self.var
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
71
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
72 wrapper_elt = field_elt.addElement((NS_XML_ELEMENT, u'wrapper'))
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
73 wrapper_elt.addChild(self.value)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
74
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if asForm:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
76 if self.label is not None:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
77 field_elt['label'] = self.label
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
78
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if self.desc is not None:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
80 field_elt.addElement('desc', content=self.desc)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
81
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if self.required:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
83 field_elt.addElement('required')
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
84
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return field_elt
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
86
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
87 @staticmethod
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def fromElement(element):
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
89 field = super(Field, Field).fromElement(element)
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if element.wrapper and element.wrapper.uri == NS_XML_ELEMENT:
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
91 field.ext_type = u"xml"
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
92 field.value = element.wrapper.firstChildElement()
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return field
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
94
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
95
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def install():
1245269f9aab wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
Goffi <goffi@goffi.org>
parents:
diff changeset
97 data_form.Field = Field