annotate src/bridge/bridge_constructor/constructors/embedded/embedded_template.py @ 2312:eaff25529c53

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