annotate sat_pubsub/schema.py @ 417:412d26a9b2c2

import sat/wokkel modules only when making service: when importing some sat/wokkel module, a reactor is installed. This can cause trouble when using sat_pubsub along with other twisted plugin installing a specific reactor (which is the case for SàT backend and Libervia). This patch works around it by importing those modules only when making the service.
author Goffi <goffi@goffi.org>
date Tue, 08 Oct 2019 12:04:13 +0200
parents ccb2a22ea0fc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
1 #!/usr/bin/env python3
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3 #
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (c) 2015 Jérôme Poisson
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 # ---
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 # This module implements node schema
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel.iwokkel import IPubSubService
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from wokkel.subprotocols import XMPPHandler, IQHandlerMixin
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from wokkel import data_form, pubsub
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
30 from zope.interface import implementer
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat_pubsub import const
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 QUERY_SCHEMA = "/pubsub[@xmlns='" + const.NS_SCHEMA + "']"
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
36 @implementer(iwokkel.IDisco)
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 class SchemaHandler(XMPPHandler, IQHandlerMixin):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 iqHandlers = {"/iq[@type='get']" + QUERY_SCHEMA: 'onSchemaGet',
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "/iq[@type='set']" + QUERY_SCHEMA: 'onSchemaSet'}
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def __init__(self):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 super(SchemaHandler, self).__init__()
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.pubsub_service = None
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def connectionInitialized(self):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 for handler in self.parent.handlers:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if IPubSubService.providedBy(handler):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.pubsub_service = handler
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 break
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.backend = self.parent.parent.getServiceNamed('backend')
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.xmlstream.addObserver("/iq[@type='get' or @type='set']" + QUERY_SCHEMA, self.handleRequest)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def _getNodeSchemaCb(self, x_elt, nodeIdentifier):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 schema_elt = domish.Element((const.NS_SCHEMA, 'schema'))
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 schema_elt['node'] = nodeIdentifier
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if x_elt is not None:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
57 assert x_elt.uri == 'jabber:x:data'
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 schema_elt.addChild(x_elt)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return schema_elt
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def onSchemaGet(self, iq_elt):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 try:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 schema_elt = next(iq_elt.pubsub.elements(const.NS_SCHEMA, 'schema'))
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 nodeIdentifier = schema_elt['node']
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 except StopIteration:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 raise pubsub.BadRequest(text='missing schema element')
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except KeyError:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 raise pubsub.BadRequest(text='missing node')
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 pep = iq_elt.delegated
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 recipient = jid.JID(iq_elt['to'])
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 d = self.backend.getNodeSchema(nodeIdentifier,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 pep,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 recipient)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 d.addCallback(self._getNodeSchemaCb, nodeIdentifier)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return d.addErrback(self.pubsub_service.resource._mapErrors)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def onSchemaSet(self, iq_elt):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 try:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 schema_elt = next(iq_elt.pubsub.elements(const.NS_SCHEMA, 'schema'))
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 nodeIdentifier = schema_elt['node']
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 except StopIteration:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 raise pubsub.BadRequest(text='missing schema element')
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except KeyError:
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 raise pubsub.BadRequest(text='missing node')
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 requestor = jid.JID(iq_elt['from'])
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 pep = iq_elt.delegated
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 recipient = jid.JID(iq_elt['to'])
361
a92f482ff14f schema: allow free FORM_TYPE as this is used for items validation, and should not be in schema namespace
Goffi <goffi@goffi.org>
parents: 352
diff changeset
88 try:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
89 x_elt = next(schema_elt.elements(data_form.NS_X_DATA, 'x'))
361
a92f482ff14f schema: allow free FORM_TYPE as this is used for items validation, and should not be in schema namespace
Goffi <goffi@goffi.org>
parents: 352
diff changeset
90 except StopIteration:
352
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 # no schema form has been found
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 x_elt = None
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 d = self.backend.setNodeSchema(nodeIdentifier,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 x_elt,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 requestor,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 pep,
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 recipient)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return d.addErrback(self.pubsub_service.resource._mapErrors)
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 def getDiscoInfo(self, requestor, service, nodeIdentifier=''):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return [disco.DiscoFeature(const.NS_SCHEMA)]
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def getDiscoItems(self, requestor, service, nodeIdentifier=''):
efbdca10f0fb schema: node schema implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return []