comparison sat/bridge/bridge_constructor/constructors/embedded/embedded_template.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/bridge/bridge_constructor/constructors/embedded/embedded_template.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*-
3
4 # SàT: a XMPP client
5 # Copyright (C) 2009-2018 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.debug(u"Init embedded bridge...")
28 self._methods_cbs = {}
29 self._signals_cbs = {
30 "core": {},
31 "plugin": {}
32 }
33
34 def bridgeConnect(self, callback, errback):
35 callback()
36
37 def register_method(self, name, callback):
38 log.debug(u"registering embedded bridge method [{}]".format(name))
39 if name in self._methods_cbs:
40 raise exceptions.ConflictError(u"method {} is already regitered".format(name))
41 self._methods_cbs[name] = callback
42
43 def register_signal(self, functionName, handler, iface="core"):
44 iface_dict = self._signals_cbs[iface]
45 if functionName in iface_dict:
46 raise exceptions.ConflictError(u"signal {name} is already regitered for interface {iface}".format(name=functionName, iface=iface))
47 iface_dict[functionName] = handler
48
49 def call_method(self, name, out_sign, async_, args, kwargs):
50 callback = kwargs.pop("callback", None)
51 errback = kwargs.pop("errback", None)
52 if async_:
53 d = self._methods_cbs[name](*args, **kwargs)
54 if callback is not None:
55 d.addCallback(callback if out_sign else lambda dummy: callback())
56 if errback is None:
57 d.addErrback(lambda failure_: log.error(failure_))
58 else:
59 d.addErrback(errback)
60 return d
61 else:
62 try:
63 ret = self._methods_cbs[name](*args, **kwargs)
64 except Exception as e:
65 if errback is not None:
66 errback(e)
67 else:
68 raise e
69 else:
70 if callback is None:
71 return ret
72 else:
73 if out_sign:
74 callback(ret)
75 else:
76 callback()
77
78 def send_signal(self, name, args, kwargs):
79 try:
80 cb = self._signals_cbs["plugin"][name]
81 except KeyError:
82 log.debug(u"ignoring signal {}: no callback registered".format(name))
83 else:
84 cb(*args, **kwargs)
85
86 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
87 #FIXME: doc parameter is kept only temporary, the time to remove it from calls
88 log.debug("Adding method [{}] to embedded bridge".format(name))
89 self.register_method(name, method)
90 setattr(self.__class__, name, lambda self_, *args, **kwargs: self.call_method(name, out_sign, async, args, kwargs))
91
92 def addSignal(self, name, int_suffix, signature, doc={}):
93 setattr(self.__class__, name, lambda self_, *args, **kwargs: self.send_signal(name, args, kwargs))
94
95 ## signals ##
96
97 ##SIGNALS_PART##
98 ## methods ##
99
100 ##METHODS_PART##
101
102 # we want the same instance for both core and frontend
103 bridge = None
104 def Bridge():
105 global bridge
106 if bridge is None:
107 bridge = _Bridge()
108 return bridge