Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0020.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 | |
23 log = getLogger(__name__) | 24 log = getLogger(__name__) |
24 from sat.core import exceptions | 25 from sat.core import exceptions |
25 from twisted.words.xish import domish | 26 from twisted.words.xish import domish |
26 | 27 |
27 from zope.interface import implements | 28 from zope.interface import implements |
31 except ImportError: | 32 except ImportError: |
32 from wokkel.subprotocols import XMPPHandler | 33 from wokkel.subprotocols import XMPPHandler |
33 | 34 |
34 from wokkel import disco, iwokkel, data_form | 35 from wokkel import disco, iwokkel, data_form |
35 | 36 |
36 NS_FEATURE_NEG = 'http://jabber.org/protocol/feature-neg' | 37 NS_FEATURE_NEG = "http://jabber.org/protocol/feature-neg" |
37 | 38 |
38 PLUGIN_INFO = { | 39 PLUGIN_INFO = { |
39 C.PI_NAME: "XEP 0020 Plugin", | 40 C.PI_NAME: "XEP 0020 Plugin", |
40 C.PI_IMPORT_NAME: "XEP-0020", | 41 C.PI_IMPORT_NAME: "XEP-0020", |
41 C.PI_TYPE: "XEP", | 42 C.PI_TYPE: "XEP", |
42 C.PI_PROTOCOLS: ["XEP-0020"], | 43 C.PI_PROTOCOLS: ["XEP-0020"], |
43 C.PI_MAIN: "XEP_0020", | 44 C.PI_MAIN: "XEP_0020", |
44 C.PI_HANDLER: "yes", | 45 C.PI_HANDLER: "yes", |
45 C.PI_DESCRIPTION: _("""Implementation of Feature Negotiation""") | 46 C.PI_DESCRIPTION: _("""Implementation of Feature Negotiation"""), |
46 } | 47 } |
47 | 48 |
48 | 49 |
49 class XEP_0020(object): | 50 class XEP_0020(object): |
50 | |
51 def __init__(self, host): | 51 def __init__(self, host): |
52 log.info(_("Plugin XEP_0020 initialization")) | 52 log.info(_("Plugin XEP_0020 initialization")) |
53 | 53 |
54 def getHandler(self, client): | 54 def getHandler(self, client): |
55 return XEP_0020_handler() | 55 return XEP_0020_handler() |
60 @param elt(domish.Element): parent element of the feature element | 60 @param elt(domish.Element): parent element of the feature element |
61 @return: feature elements | 61 @return: feature elements |
62 @raise exceptions.NotFound: no feature element found | 62 @raise exceptions.NotFound: no feature element found |
63 """ | 63 """ |
64 try: | 64 try: |
65 feature_elt = elt.elements(NS_FEATURE_NEG, 'feature').next() | 65 feature_elt = elt.elements(NS_FEATURE_NEG, "feature").next() |
66 except StopIteration: | 66 except StopIteration: |
67 raise exceptions.NotFound | 67 raise exceptions.NotFound |
68 return feature_elt | 68 return feature_elt |
69 | 69 |
70 def _getForm(self, elt, namespace): | 70 def _getForm(self, elt, namespace): |
98 result = {} | 98 result = {} |
99 for field in form.fields: | 99 for field in form.fields: |
100 values = form.fields[field].values | 100 values = form.fields[field].values |
101 result[field] = values[0] if values else None | 101 result[field] = values[0] if values else None |
102 if len(values) > 1: | 102 if len(values) > 1: |
103 log.warning(_(u"More than one value choosed for {}, keeping the first one").format(field)) | 103 log.warning( |
104 _( | |
105 u"More than one value choosed for {}, keeping the first one" | |
106 ).format(field) | |
107 ) | |
104 return result | 108 return result |
105 | 109 |
106 def negotiate(self, feature_elt, name, negotiable_values, namespace): | 110 def negotiate(self, feature_elt, name, negotiable_values, namespace): |
107 """Negotiate the feature options | 111 """Negotiate the feature options |
108 | 112 |
124 """Build a feature element with choosed options | 128 """Build a feature element with choosed options |
125 | 129 |
126 @param options(dict): dict with feature as key and choosed option as value | 130 @param options(dict): dict with feature as key and choosed option as value |
127 @param namespace (None, unicode): form namespace or None to ignore | 131 @param namespace (None, unicode): form namespace or None to ignore |
128 """ | 132 """ |
129 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) | 133 feature_elt = domish.Element((NS_FEATURE_NEG, "feature")) |
130 x_form = data_form.Form('submit', formNamespace=namespace) | 134 x_form = data_form.Form("submit", formNamespace=namespace) |
131 x_form.makeFields(options) | 135 x_form.makeFields(options) |
132 feature_elt.addChild(x_form.toElement()) | 136 feature_elt.addChild(x_form.toElement()) |
133 return feature_elt | 137 return feature_elt |
134 | 138 |
135 def proposeFeatures(self, options_dict, namespace): | 139 def proposeFeatures(self, options_dict, namespace): |
136 """Build a feature element with options to propose | 140 """Build a feature element with options to propose |
137 | 141 |
138 @param options_dict(dict): dict with feature as key and iterable of acceptable options as value | 142 @param options_dict(dict): dict with feature as key and iterable of acceptable options as value |
139 @param namespace(None, unicode): feature namespace | 143 @param namespace(None, unicode): feature namespace |
140 """ | 144 """ |
141 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) | 145 feature_elt = domish.Element((NS_FEATURE_NEG, "feature")) |
142 x_form = data_form.Form('form', formNamespace=namespace) | 146 x_form = data_form.Form("form", formNamespace=namespace) |
143 for field in options_dict: | 147 for field in options_dict: |
144 x_form.addField(data_form.Field('list-single', field, | 148 x_form.addField( |
145 options=[data_form.Option(option) for option in options_dict[field]])) | 149 data_form.Field( |
150 "list-single", | |
151 field, | |
152 options=[data_form.Option(option) for option in options_dict[field]], | |
153 ) | |
154 ) | |
146 feature_elt.addChild(x_form.toElement()) | 155 feature_elt.addChild(x_form.toElement()) |
147 return feature_elt | 156 return feature_elt |
148 | 157 |
149 | 158 |
150 class XEP_0020_handler(XMPPHandler): | 159 class XEP_0020_handler(XMPPHandler): |
151 implements(iwokkel.IDisco) | 160 implements(iwokkel.IDisco) |
152 | 161 |
153 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 162 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
154 return [disco.DiscoFeature(NS_FEATURE_NEG)] | 163 return [disco.DiscoFeature(NS_FEATURE_NEG)] |
155 | 164 |
156 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 165 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
157 return [] | 166 return [] |