Mercurial > libervia-backend
diff sat/plugins/plugin_exp_pubsub_schema.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 3e4e78de9cca |
children | 3480d4fdf83a |
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_pubsub_schema.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_exp_pubsub_schema.py Wed Jun 27 20:14:46 2018 +0200 @@ -27,6 +27,7 @@ from twisted.words.protocols.jabber.xmlstream import XMPPHandler from twisted.internet import defer from sat.core.log import getLogger + log = getLogger(__name__) from wokkel import disco, iwokkel from wokkel import data_form @@ -36,7 +37,7 @@ import copy import itertools -NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0' +NS_SCHEMA = "https://salut-a-toi/protocol/schema:0" PLUGIN_INFO = { C.PI_NAME: "PubSub Schema", @@ -46,47 +47,63 @@ C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"], C.PI_MAIN: "PubsubSchema", C.PI_HANDLER: "yes", - C.PI_DESCRIPTION: _("""Handle Pubsub data schemas""") + C.PI_DESCRIPTION: _("""Handle Pubsub data schemas"""), } class PubsubSchema(object): - def __init__(self, host): log.info(_(u"PubSub Schema initialization")) self.host = host self._p = self.host.plugins["XEP-0060"] self._i = self.host.plugins["IDENTITY"] - host.bridge.addMethod("psSchemaGet", ".plugin", - in_sign='sss', out_sign='s', - method=self._getSchema, - async=True - ) - host.bridge.addMethod("psSchemaSet", ".plugin", - in_sign='ssss', out_sign='', - method=self._setSchema, - async=True - ) - host.bridge.addMethod("psSchemaUIGet", ".plugin", - in_sign='sss', out_sign='s', - method=utils.partial(self._getUISchema, default_node=None), - async=True - ) - host.bridge.addMethod("psItemsFormGet", ".plugin", - in_sign='ssssiassa{ss}s', out_sign='(asa{ss})', - method=self._getDataFormItems, - async=True) - host.bridge.addMethod("psItemFormSend", ".plugin", - in_sign='ssa{sas}ssa{ss}s', out_sign='s', - method=self._sendDataFormItem, - async=True) + host.bridge.addMethod( + "psSchemaGet", + ".plugin", + in_sign="sss", + out_sign="s", + method=self._getSchema, + async=True, + ) + host.bridge.addMethod( + "psSchemaSet", + ".plugin", + in_sign="ssss", + out_sign="", + method=self._setSchema, + async=True, + ) + host.bridge.addMethod( + "psSchemaUIGet", + ".plugin", + in_sign="sss", + out_sign="s", + method=utils.partial(self._getUISchema, default_node=None), + async=True, + ) + host.bridge.addMethod( + "psItemsFormGet", + ".plugin", + in_sign="ssssiassa{ss}s", + out_sign="(asa{ss})", + method=self._getDataFormItems, + async=True, + ) + host.bridge.addMethod( + "psItemFormSend", + ".plugin", + in_sign="ssa{sas}ssa{ss}s", + out_sign="s", + method=self._sendDataFormItem, + async=True, + ) def getHandler(self, client): return SchemaHandler() def _getSchemaBridgeCb(self, schema_elt): if schema_elt is None: - return u'' + return u"" return schema_elt.toXml() def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE): @@ -98,11 +115,11 @@ def _getSchemaCb(self, iq_elt): try: - schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema')) + schema_elt = next(iq_elt.elements(NS_SCHEMA, "schema")) except StopIteration: - raise exceptions.DataError('missing <schema> element') + raise exceptions.DataError("missing <schema> element") try: - x_elt = next(schema_elt.elements((data_form.NS_X_DATA, 'x'))) + x_elt = next(schema_elt.elements((data_form.NS_X_DATA, "x"))) except StopIteration: # there is not schema on this node return None @@ -117,18 +134,26 @@ @return (domish.Element, None): schema (<x> element) None if not schema has been set on this node """ - iq_elt = client.IQ(u'get') + iq_elt = client.IQ(u"get") if service is not None: - iq_elt['to'] = service.full() - pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub')) - schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema')) - schema_elt['node'] = nodeIdentifier + iq_elt["to"] = service.full() + pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub")) + schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema")) + schema_elt["node"] = nodeIdentifier d = iq_elt.send() d.addCallback(self._getSchemaCb) return d @defer.inlineCallbacks - def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form', copy_form=True): + def getSchemaForm( + self, + client, + service, + nodeIdentifier, + schema=None, + form_type="form", + copy_form=True, + ): """get data form from node's schema @param service(None, jid.JID): PubSub service @@ -147,7 +172,11 @@ log.debug(_(u"unspecified schema, we need to request it")) schema = yield self.getSchema(client, service, nodeIdentifier) if schema is None: - raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form")) + raise exceptions.DataError( + _( + u"no schema specified, and this node has no schema either, we can't construct the data form" + ) + ) elif isinstance(schema, data_form.Form): if copy_form: schema = copy.deepcopy(schema) @@ -156,17 +185,18 @@ try: form = data_form.Form.fromElement(schema) except data_form.Error as e: - raise exceptions.DataError(_(u"Invalid Schema: {msg}").format( - msg = e)) + raise exceptions.DataError(_(u"Invalid Schema: {msg}").format(msg=e)) form.formType = form_type defer.returnValue(form) def schema2XMLUI(self, schema_elt): form = data_form.Form.fromElement(schema_elt) - xmlui = xml_tools.dataForm2XMLUI(form, '') + xmlui = xml_tools.dataForm2XMLUI(form, "") return xmlui - def _getUISchema(self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE): + def _getUISchema( + self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE + ): if not nodeIdentifier: if not default_node: raise ValueError(_(u"nodeIndentifier needs to be set")) @@ -185,7 +215,7 @@ def _setSchema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE): client = self.host.getClient(profile_key) service = None if not service else jid.JID(service) - schema = generic.parseXml(schema.encode('utf-8')) + schema = generic.parseXml(schema.encode("utf-8")) return self.setSchema(client, service, nodeIdentifier, schema) def setSchema(self, client, service, nodeIdentifier, schema): @@ -196,31 +226,67 @@ """ iq_elt = client.IQ() if service is not None: - iq_elt['to'] = service.full() - pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub')) - schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema')) - schema_elt['node'] = nodeIdentifier + iq_elt["to"] = service.full() + pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub")) + schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema")) + schema_elt["node"] = nodeIdentifier if schema is not None: schema_elt.addChild(schema) return iq_elt.send() - def _getDataFormItems(self, form_ns='', service='', node='', schema='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE): + def _getDataFormItems( + self, + form_ns="", + service="", + node="", + schema="", + max_items=10, + item_ids=None, + sub_id=None, + extra_dict=None, + profile_key=C.PROF_KEY_NONE, + ): client = self.host.getClient(profile_key) service = jid.JID(service) if service else None if not node: - raise exceptions.DataError(_(u'empty node is not allowed')) + raise exceptions.DataError(_(u"empty node is not allowed")) if schema: - schema = generic.parseXml(schema.encode('utf-8')) + schema = generic.parseXml(schema.encode("utf-8")) else: schema = None max_items = None if max_items == C.NO_LIMIT else max_items extra = self._p.parseExtra(extra_dict) - d = self.getDataFormItems(client, service, node, schema, max_items or None, item_ids, sub_id or None, extra.rsm_request, extra.extra, form_ns=form_ns or None) + d = self.getDataFormItems( + client, + service, + node, + schema, + max_items or None, + item_ids, + sub_id or None, + extra.rsm_request, + extra.extra, + form_ns=form_ns or None, + ) d.addCallback(self._p.serItemsData) return d @defer.inlineCallbacks - def getDataFormItems(self, client, service, nodeIdentifier, schema=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None, default_node=None, form_ns=None, filters=None): + def getDataFormItems( + self, + client, + service, + nodeIdentifier, + schema=None, + max_items=None, + item_ids=None, + sub_id=None, + rsm_request=None, + extra=None, + default_node=None, + form_ns=None, + filters=None, + ): """Get items known as being data forms, and convert them to XMLUI @param schema(domish.Element, data_form.Form, None): schema of the node if known @@ -237,15 +303,28 @@ """ if not nodeIdentifier: if not default_node: - raise ValueError(_(u"default_node must be set if nodeIdentifier is not set")) + raise ValueError( + _(u"default_node must be set if nodeIdentifier is not set") + ) nodeIdentifier = default_node # we need the initial form to get options of fields when suitable - schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result', copy_form=False) - items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra) + schema_form = yield self.getSchemaForm( + client, service, nodeIdentifier, schema, form_type="result", copy_form=False + ) + items_data = yield self._p.getItems( + client, + service, + nodeIdentifier, + max_items, + item_ids, + sub_id, + rsm_request, + extra, + ) items, metadata = items_data items_xmlui = [] for item_elt in items: - for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')): + for x_elt in item_elt.elements((data_form.NS_X_DATA, u"x")): form = data_form.Form.fromElement(x_elt) if form_ns and form.formNamespace != form_ns: continue @@ -254,28 +333,59 @@ schema_form, # FIXME: conflicts with schema (i.e. if "id" or "publisher" already exists) # are not checked - prepend = (('label', 'id'),('text', item_elt['id'], u'id'), - ('label', 'publisher'),('text', item_elt.getAttribute('publisher',''), u'publisher')), - filters = filters, - ) + prepend=( + ("label", "id"), + ("text", item_elt["id"], u"id"), + ("label", "publisher"), + ("text", item_elt.getAttribute("publisher", ""), u"publisher"), + ), + filters=filters, + ) items_xmlui.append(xmlui) break defer.returnValue((items_xmlui, metadata)) - - def _sendDataFormItem(self, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE): + def _sendDataFormItem( + self, + service, + nodeIdentifier, + values, + schema=None, + item_id=None, + extra=None, + profile_key=C.PROF_KEY_NONE, + ): client = self.host.getClient(profile_key) service = None if not service else jid.JID(service) if schema: - schema = generic.parseXml(schema.encode('utf-8')) + schema = generic.parseXml(schema.encode("utf-8")) else: schema = None - d = self.sendDataFormItem(client, service, nodeIdentifier, values, schema, item_id or None, extra, deserialise=True) - d.addCallback(lambda ret: ret or u'') + d = self.sendDataFormItem( + client, + service, + nodeIdentifier, + values, + schema, + item_id or None, + extra, + deserialise=True, + ) + d.addCallback(lambda ret: ret or u"") return d @defer.inlineCallbacks - def sendDataFormItem(self, client, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, deserialise=False): + def sendDataFormItem( + self, + client, + service, + nodeIdentifier, + values, + schema=None, + item_id=None, + extra=None, + deserialise=False, + ): """Publish an item as a dataform when we know that there is a schema @param values(dict[key(unicode), [iterable[object], object]]): values set for the form @@ -289,26 +399,34 @@ other parameters as the same as for [self._p.sendItem] @return (unicode): id of the created item """ - form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='submit') + form = yield self.getSchemaForm( + client, service, nodeIdentifier, schema, form_type="submit" + ) for name, values_list in values.iteritems(): try: field = form.fields[name] except KeyError: - log.warning(_(u"field {name} doesn't exist, ignoring it").format(name=name)) + log.warning( + _(u"field {name} doesn't exist, ignoring it").format(name=name) + ) continue - if isinstance(values_list, basestring) or not isinstance(values_list, Iterable): + if isinstance(values_list, basestring) or not isinstance( + values_list, Iterable + ): values_list = [values_list] if deserialise: - if field.fieldType == 'boolean': + if field.fieldType == "boolean": values_list = [C.bool(v) for v in values_list] - elif field.fieldType == 'text-multi': + elif field.fieldType == "text-multi": # for text-multi, lines must be put on separate values - values_list = list(itertools.chain(*[v.splitlines() for v in values_list])) + values_list = list( + itertools.chain(*[v.splitlines() for v in values_list]) + ) - elif 'jid' in field.fieldType: + elif "jid" in field.fieldType: values_list = [jid.JID(v) for v in values_list] - if 'list' in field.fieldType: + if "list" in field.fieldType: # for lists, we check that given values are allowed in form allowed_values = [o.value for o in field.options] values_list = [v for v in values_list if v in allowed_values] @@ -317,7 +435,9 @@ values_list = field.values field.values = values_list - yield self._p.sendItem(client, service, nodeIdentifier, form.toElement(), item_id, extra) + yield self._p.sendItem( + client, service, nodeIdentifier, form.toElement(), item_id, extra + ) ## filters ## # filters useful for data form to XMLUI conversion # @@ -327,7 +447,7 @@ if not args[0]: # value is not filled: we use user part of publisher (if we have it) try: - publisher = jid.JID(form_xmlui.named_widgets['publisher'].value) + publisher = jid.JID(form_xmlui.named_widgets["publisher"].value) except (KeyError, RuntimeError): pass else: @@ -339,18 +459,20 @@ main use case is using a textbox for labels """ - if widget_type != u'textbox': + if widget_type != u"textbox": return widget_type, args, kwargs - widget_type = u'list' - options = [o for o in args.pop(0).split(u'\n') if o] - kwargs = {'options': options, - 'name': kwargs.get('name'), - 'styles': (u'noselect', u'extensible', u'reducible')} + widget_type = u"list" + options = [o for o in args.pop(0).split(u"\n") if o] + kwargs = { + "options": options, + "name": kwargs.get("name"), + "styles": (u"noselect", u"extensible", u"reducible"), + } return widget_type, args, kwargs def dateFilter(self, form_xmlui, widget_type, args, kwargs): """Convert a string with a date to a unix timestamp""" - if widget_type != u'string' or not args[0]: + if widget_type != u"string" or not args[0]: return widget_type, args, kwargs # we convert XMPP date to timestamp try: @@ -377,7 +499,19 @@ return client, service, node, max_items, extra, sub_id - def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra=None, default_node=None, form_ns=None, filters=None, profile_key=C.PROF_KEY_NONE): + def _get( + self, + service="", + node="", + max_items=10, + item_ids=None, + sub_id=None, + extra=None, + default_node=None, + form_ns=None, + filters=None, + profile_key=C.PROF_KEY_NONE, + ): """Bridge method to retrieve data from node with schema this method is a helper so dependant plugins can use it directly @@ -392,11 +526,16 @@ extra = {} # XXX: Q&D way to get list for labels when displaying them, but text when we # have to modify them - if C.bool(extra.get('labels_as_list', C.BOOL_FALSE)): + if C.bool(extra.get("labels_as_list", C.BOOL_FALSE)): filters = filters.copy() - filters[u'labels'] = self.textbox2ListFilter - client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(service, node, max_items, sub_id, extra, profile_key) - d = self.getDataFormItems(client, service, node or None, + filters[u"labels"] = self.textbox2ListFilter + client, service, node, max_items, extra, sub_id = self.prepareBridgeGet( + service, node, max_items, sub_id, extra, profile_key + ) + d = self.getDataFormItems( + client, + service, + node or None, max_items=max_items, item_ids=item_ids, sub_id=sub_id, @@ -404,7 +543,8 @@ extra=extra.extra, default_node=default_node, form_ns=form_ns, - filters=filters) + filters=filters, + ) d.addCallback(self._p.serItemsData) return d @@ -416,30 +556,65 @@ client = self.host.getClient(profile_key) service = None if not service else jid.JID(service) if schema: - schema = generic.parseXml(schema.encode('utf-8')) + schema = generic.parseXml(schema.encode("utf-8")) else: schema = None - if extra and u'update' in extra: - extra[u'update'] = C.bool(extra[u'update']) + if extra and u"update" in extra: + extra[u"update"] = C.bool(extra[u"update"]) return client, service, node or None, schema, item_id or None, extra - def _set(self, service, node, values, schema=None, item_id=None, extra=None, default_node=None, form_ns=None, fill_author=True, profile_key=C.PROF_KEY_NONE): + def _set( + self, + service, + node, + values, + schema=None, + item_id=None, + extra=None, + default_node=None, + form_ns=None, + fill_author=True, + profile_key=C.PROF_KEY_NONE, + ): """Bridge method to set item in node with schema this method is a helper so dependant plugins can use it directly when adding *Set methods """ - client, service, node, schema, item_id, extra = self.prepareBridgeSet(service, node, schema, item_id, extra) - d = self.set(client, service, node, values, schema, item_id, extra, - deserialise=True, - form_ns=form_ns, - default_node=default_node, - fill_author=fill_author) - d.addCallback(lambda ret: ret or u'') + client, service, node, schema, item_id, extra = self.prepareBridgeSet( + service, node, schema, item_id, extra + ) + d = self.set( + client, + service, + node, + values, + schema, + item_id, + extra, + deserialise=True, + form_ns=form_ns, + default_node=default_node, + fill_author=fill_author, + ) + d.addCallback(lambda ret: ret or u"") return d @defer.inlineCallbacks - def set(self, client, service, node, values, schema, item_id, extra, deserialise, form_ns, default_node=None, fill_author=True): + def set( + self, + client, + service, + node, + values, + schema, + item_id, + extra, + deserialise, + form_ns, + default_node=None, + fill_author=True, + ): """Set an item in a node with a schema This method can be used directly by *Set methods added by dependant plugin @@ -463,44 +638,56 @@ node = default_node now = utils.xmpp_date() if not item_id: - values['created'] = now - elif extra.get(u'update', False): + values["created"] = now + elif extra.get(u"update", False): if item_id is None: - raise exceptions.DataError(_(u'if extra["update"] is set, item_id must be set too')) + raise exceptions.DataError( + _(u'if extra["update"] is set, item_id must be set too') + ) try: # we get previous item - items_data = yield self._p.getItems(client, service, node, item_ids=[item_id]) + items_data = yield self._p.getItems( + client, service, node, item_ids=[item_id] + ) item_elt = items_data[0][0] except Exception as e: - log.warning(_(u"Can't get previous item, update ignored: {reason}").format( - reason = e)) + log.warning( + _(u"Can't get previous item, update ignored: {reason}").format( + reason=e + ) + ) else: # and parse it form = data_form.findForm(item_elt, form_ns) if form is None: - log.warning(_(u"Can't parse previous item, update ignored: data form not found").format( - reason = e)) + log.warning( + _( + u"Can't parse previous item, update ignored: data form not found" + ).format(reason=e) + ) else: for name, field in form.fields.iteritems(): if name not in values: - values[name] = u'\n'.join(unicode(v) for v in field.values) + values[name] = u"\n".join(unicode(v) for v in field.values) - values['updated'] = now + values["updated"] = now if fill_author: - if not values.get('author'): + if not values.get("author"): identity = yield self._i.getIdentity(client, client.jid) - values['author'] = identity['nick'] - if not values.get('author_jid'): - values['author_jid'] = client.jid.full() - item_id = yield self.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise) + values["author"] = identity["nick"] + if not values.get("author_jid"): + values["author_jid"] = client.jid.full() + item_id = yield self.sendDataFormItem( + client, service, node, values, schema, item_id, extra, deserialise + ) defer.returnValue(item_id) class SchemaHandler(XMPPHandler): implements(iwokkel.IDisco) - def getDiscoInfo(self, requestor, service, nodeIdentifier=''): + def getDiscoInfo(self, requestor, service, nodeIdentifier=""): return [disco.DiscoFeature(NS_SCHEMA)] - def getDiscoItems(self, requestor, service, nodeIdentifier=''): + def getDiscoItems(self, requestor, service, nodeIdentifier=""): return []