annotate libervia/backend/bridge/bridge_constructor/constructors/embedded/embedded_template.py @ 4310:d27228b3c704

test (unit): add test for email gateway: rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
3480
7550ae9cfbac Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
4 # Libervia: an XMPP client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2087
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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
21
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = getLogger(__name__)
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core import exceptions
2087
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
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class _Bridge(object):
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 def __init__(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
28 log.debug("Init embedded bridge...")
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self._methods_cbs = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
30 self._signals_cbs = {"core": {}, "plugin": {}}
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
32 def bridge_connect(self, callback, errback):
2091
f413bfc24458 bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents: 2087
diff changeset
33 callback()
f413bfc24458 bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents: 2087
diff changeset
34
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def register_method(self, name, callback):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
36 log.debug("registering embedded bridge method [{}]".format(name))
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 if name in self._methods_cbs:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
38 raise exceptions.ConflictError("method {} is already regitered".format(name))
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 self._methods_cbs[name] = callback
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def register_signal(self, functionName, handler, iface="core"):
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 iface_dict = self._signals_cbs[iface]
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 if functionName in iface_dict:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 raise exceptions.ConflictError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 "signal {name} is already regitered for interface {iface}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 name=functionName, iface=iface
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 )
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 iface_dict[functionName] = handler
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def call_method(self, name, out_sign, async_, args, kwargs):
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 callback = kwargs.pop("callback", None)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 errback = kwargs.pop("errback", None)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if async_:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 d = self._methods_cbs[name](*args, **kwargs)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if callback is not None:
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
57 d.addCallback(callback if out_sign else lambda __: callback())
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 if errback is None:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 d.addErrback(lambda failure_: log.error(failure_))
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 d.addErrback(errback)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return d
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 try:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 ret = self._methods_cbs[name](*args, **kwargs)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 except Exception as e:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 if errback is not None:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 errback(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 raise e
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if callback is None:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return ret
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if out_sign:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 callback(ret)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 callback()
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def send_signal(self, name, args, kwargs):
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 cb = self._signals_cbs["plugin"][name]
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
84 log.debug("ignoring signal {}: no callback registered".format(name))
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 else:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 cb(*args, **kwargs)
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
88 def add_method(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
89 self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 ):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 # FIXME: doc parameter is kept only temporary, the time to remove it from calls
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 log.debug("Adding method [{}] to embedded bridge".format(name))
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self.register_method(name, method)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 setattr(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 self.__class__,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 lambda self_, *args, **kwargs: self.call_method(
3074
c2625920e05a bridge (embedded): typo
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
98 name, out_sign, async_, args, kwargs
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 )
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
102 def add_signal(self, name, int_suffix, signature, doc={}):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 setattr(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 self.__class__,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 lambda self_, *args, **kwargs: self.send_signal(name, args, kwargs),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 )
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 ## signals ##
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 ##SIGNALS_PART##
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 ## methods ##
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 ##METHODS_PART##
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 # we want the same instance for both core and frontend
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 bridge = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
121 def bridge():
2087
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 global bridge
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if bridge is None:
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 bridge = _Bridge()
159250d66407 bridge (constructor): embedded bridge generator:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return bridge