comparison src/bridge/bridge_constructor/constructors/embedded/embedded_template.py @ 2087:159250d66407

bridge (constructor): embedded bridge generator: "embedded" is used to have backend and frontend together in the same process (frontend call backend as a module).
author Goffi <goffi@goffi.org>
date Wed, 05 Oct 2016 22:07:51 +0200
parents
children f413bfc24458
comparison
equal deleted inserted replaced
2086:4633cfcbcccb 2087:159250d66407
1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*-
3
4 # SàT: a XMPP client
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.log import getLogger
21 log = getLogger(__name__)
22 from sat.core import exceptions
23
24
25 class _Bridge(object):
26 def __init__(self):
27 log.info(u"Init embedded bridge...")
28 self._methods_cbs = {}
29 self._signals_cbs = {
30 "core": {},
31 "plugin": {}
32 }
33
34 def register_method(self, name, callback):
35 log.debug(u"registering embedded bridge method [{}]".format(name))
36 if name in self._methods_cbs:
37 raise exceptions.ConflictError(u"method {} is already regitered".format(name))
38 self._methods_cbs[name] = callback
39
40 def register_signal(self, functionName, handler, iface="core"):
41 iface_dict = self._signals_cbs[iface]
42 if functionName in iface_dict:
43 raise exceptions.ConflictError(u"signal {name} is already regitered for interface {iface}".format(name=functionName, iface=iface))
44 iface_dict[functionName] = handler
45
46 def call_method(self, name, out_sign, async_, args, kwargs):
47 callback = kwargs.pop("callback", None)
48 errback = kwargs.pop("errback", None)
49 if async_:
50 d = self._methods_cbs[name](*args, **kwargs)
51 if callback is not None:
52 d.addCallback(callback if out_sign else lambda dummy: callback())
53 if errback is None:
54 d.addErrback(lambda failure_: log.error(failure_))
55 else:
56 d.addErrback(errback)
57 return d
58 else:
59 try:
60 ret = self._methods_cbs[name](*args, **kwargs)
61 except Exception as e:
62 if errback is not None:
63 errback(e)
64 else:
65 raise e
66 else:
67 if callback is None:
68 return ret
69 else:
70 if out_sign:
71 callback(ret)
72 else:
73 callback()
74
75 def send_signal(self, name, args, kwargs):
76 try:
77 cb = self._signals_cbs["plugin"][name]
78 except KeyError:
79 log.warning(u"ignoring signal {}: no callback registered".format(name))
80 else:
81 cb(*args, **kwargs)
82
83 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
84 #FIXME: doc parameter is kept only temporary, the time to remove it from calls
85 log.debug("Adding method [{}] to embedded bridge".format(name))
86 self.register_method(name, method)
87 setattr(self.__class__, name, lambda self_, *args, **kwargs: self.call_method(name, out_sign, async, args, kwargs))
88
89 def addSignal(self, name, int_suffix, signature, doc={}):
90 setattr(self.__class__, name, lambda self_, *args, **kwargs: self.send_signal(name, args, kwargs))
91
92 ## signals ##
93
94 ##SIGNALS_PART##
95 ## methods ##
96
97 ##METHODS_PART##
98
99 # we want the same instance for both core and frontend
100 bridge = None
101 def Bridge():
102 global bridge
103 if bridge is None:
104 bridge = _Bridge()
105 return bridge