Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_exp_pubsub_admin.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
35 C.PI_PROTOCOLS: [], | 35 C.PI_PROTOCOLS: [], |
36 C.PI_DEPENDENCIES: [], | 36 C.PI_DEPENDENCIES: [], |
37 C.PI_RECOMMENDATIONS: [], | 37 C.PI_RECOMMENDATIONS: [], |
38 C.PI_MAIN: "PubsubAdmin", | 38 C.PI_MAIN: "PubsubAdmin", |
39 C.PI_HANDLER: "no", | 39 C.PI_HANDLER: "no", |
40 C.PI_DESCRIPTION: _("""\Implementation of Pubsub Administrator | 40 C.PI_DESCRIPTION: _( |
41 """\Implementation of Pubsub Administrator | |
41 This allows a pubsub administrator to overwrite completly items, including publisher. | 42 This allows a pubsub administrator to overwrite completly items, including publisher. |
42 Specially useful when importing a node."""), | 43 Specially useful when importing a node.""" |
44 ), | |
43 } | 45 } |
44 | 46 |
45 NS_PUBSUB_ADMIN = "https://salut-a-toi.org/spec/pubsub_admin:0" | 47 NS_PUBSUB_ADMIN = "https://salut-a-toi.org/spec/pubsub_admin:0" |
46 | 48 |
47 | 49 |
56 out_sign="as", | 58 out_sign="as", |
57 method=self._publish, | 59 method=self._publish, |
58 async_=True, | 60 async_=True, |
59 ) | 61 ) |
60 | 62 |
61 def _publish(self, service, nodeIdentifier, items, extra=None, | 63 def _publish( |
62 profile_key=C.PROF_KEY_NONE): | 64 self, service, nodeIdentifier, items, extra=None, profile_key=C.PROF_KEY_NONE |
65 ): | |
63 client = self.host.get_client(profile_key) | 66 client = self.host.get_client(profile_key) |
64 service = None if not service else jid.JID(service) | 67 service = None if not service else jid.JID(service) |
65 extra = data_format.deserialise(extra) | 68 extra = data_format.deserialise(extra) |
66 items = [generic.parseXml(i.encode('utf-8')) for i in items] | 69 items = [generic.parseXml(i.encode("utf-8")) for i in items] |
67 return self.publish( | 70 return self.publish(client, service, nodeIdentifier, items, extra) |
68 client, service, nodeIdentifier, items, extra | |
69 ) | |
70 | 71 |
71 def _send_cb(self, iq_result): | 72 def _send_cb(self, iq_result): |
72 publish_elt = iq_result.admin.pubsub.publish | 73 publish_elt = iq_result.admin.pubsub.publish |
73 ids = [] | 74 ids = [] |
74 for item_elt in publish_elt.elements(pubsub.NS_PUBSUB, 'item'): | 75 for item_elt in publish_elt.elements(pubsub.NS_PUBSUB, "item"): |
75 ids.append(item_elt['id']) | 76 ids.append(item_elt["id"]) |
76 return ids | 77 return ids |
77 | 78 |
78 def publish(self, client, service, nodeIdentifier, items, extra=None): | 79 def publish(self, client, service, nodeIdentifier, items, extra=None): |
79 for item in items: | 80 for item in items: |
80 if item.name != 'item' or item.uri != pubsub.NS_PUBSUB: | 81 if item.name != "item" or item.uri != pubsub.NS_PUBSUB: |
81 raise exceptions.DataError( | 82 raise exceptions.DataError( |
82 'Invalid element, a pubsub item is expected: {xml}'.format( | 83 "Invalid element, a pubsub item is expected: {xml}".format( |
83 xml=item.toXml())) | 84 xml=item.toXml() |
85 ) | |
86 ) | |
84 iq_elt = client.IQ() | 87 iq_elt = client.IQ() |
85 iq_elt['to'] = service.full() if service else client.jid.userhost() | 88 iq_elt["to"] = service.full() if service else client.jid.userhost() |
86 admin_elt = iq_elt.addElement((NS_PUBSUB_ADMIN, 'admin')) | 89 admin_elt = iq_elt.addElement((NS_PUBSUB_ADMIN, "admin")) |
87 pubsub_elt = admin_elt.addElement((pubsub.NS_PUBSUB, 'pubsub')) | 90 pubsub_elt = admin_elt.addElement((pubsub.NS_PUBSUB, "pubsub")) |
88 publish_elt = pubsub_elt.addElement('publish') | 91 publish_elt = pubsub_elt.addElement("publish") |
89 publish_elt['node'] = nodeIdentifier | 92 publish_elt["node"] = nodeIdentifier |
90 for item in items: | 93 for item in items: |
91 publish_elt.addChild(item) | 94 publish_elt.addChild(item) |
92 d = iq_elt.send() | 95 d = iq_elt.send() |
93 d.addCallback(self._send_cb) | 96 d.addCallback(self._send_cb) |
94 return d | 97 return d |