comparison libervia/backend/bridge/bridge_constructor/constructors/embedded/constructor.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/bridge/bridge_constructor/constructors/embedded/constructor.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # Libervia: an XMPP client
5 # Copyright (C) 2009-2021 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 libervia.backend.bridge.bridge_constructor import base_constructor
21
22 #  from textwraps import dedent
23
24
25 class EmbeddedConstructor(base_constructor.Constructor):
26 NAME = "embedded"
27 CORE_TEMPLATE = "embedded_template.py"
28 CORE_DEST = "embedded.py"
29 CORE_FORMATS = {
30 "methods": """\
31 def {name}(self, {args}{args_comma}callback=None, errback=None):
32 {ret_routine}
33 """,
34 "signals": """\
35 def {name}(self, {args}):
36 try:
37 cb = self._signals_cbs["{category}"]["{name}"]
38 except KeyError:
39 log.warning(u"ignoring signal {name}: no callback registered")
40 else:
41 cb({args_result})
42 """,
43 }
44 FRONTEND_TEMPLATE = "embedded_frontend_template.py"
45 FRONTEND_DEST = CORE_DEST
46 FRONTEND_FORMATS = {}
47
48 def core_completion_method(self, completion, function, default, arg_doc, async_):
49 completion.update(
50 {
51 "debug": ""
52 if not self.args.debug
53 else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " "),
54 "args_result": self.get_arguments(function["sig_in"], name=arg_doc),
55 "args_comma": ", " if function["sig_in"] else "",
56 }
57 )
58
59 if async_:
60 completion["cb_or_lambda"] = (
61 "callback" if function["sig_out"] else "lambda __: callback()"
62 )
63 completion[
64 "ret_routine"
65 ] = """\
66 d = self._methods_cbs["{name}"]({args_result})
67 if callback is not None:
68 d.addCallback({cb_or_lambda})
69 if errback is None:
70 d.addErrback(lambda failure_: log.error(failure_))
71 else:
72 d.addErrback(errback)
73 return d
74 """.format(
75 **completion
76 )
77 else:
78 completion["ret_or_nothing"] = "ret" if function["sig_out"] else ""
79 completion[
80 "ret_routine"
81 ] = """\
82 try:
83 ret = self._methods_cbs["{name}"]({args_result})
84 except Exception as e:
85 if errback is not None:
86 errback(e)
87 else:
88 raise e
89 else:
90 if callback is None:
91 return ret
92 else:
93 callback({ret_or_nothing})""".format(
94 **completion
95 )
96
97 def core_completion_signal(self, completion, function, default, arg_doc, async_):
98 completion.update(
99 {"args_result": self.get_arguments(function["sig_in"], name=arg_doc)}
100 )