Mercurial > libervia-backend
annotate libervia/backend/bridge/bridge_constructor/constructors/dbus/constructor.py @ 4280:4cf98f506269
bridge(constructor): fix template parsing, which was broken following `black` reformating.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 12 Jul 2024 17:58:06 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2085 | 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 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2085 | 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 | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
20 from libervia.backend.bridge.bridge_constructor import base_constructor |
2085 | 21 |
22 | |
23 class DbusConstructor(base_constructor.Constructor): | |
24 NAME = "dbus" | |
25 CORE_TEMPLATE = "dbus_core_template.py" | |
2086 | 26 CORE_DEST = "dbus_bridge.py" |
2085 | 27 CORE_FORMATS = { |
3539
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
28 "methods_declarations": """\ |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
29 Method('{name}', arguments='{sig_in}', returns='{sig_out}'),""", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
30 "methods": """\ |
3539
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
31 def dbus_{name}(self, {args}): |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
32 {debug}return self._callback("{name}", {args_no_default})\n""", |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
33 "signals_declarations": """\ |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
34 Signal('{name}', '{sig_in}'),""", |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
35 "signals": """\ |
2085 | 36 def {name}(self, {args}): |
3539
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
37 self._obj.emitSignal("{name}", {args})\n""", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 } |
2085 | 39 |
40 FRONTEND_TEMPLATE = "dbus_frontend_template.py" | |
41 FRONTEND_DEST = CORE_DEST | |
42 FRONTEND_FORMATS = { | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
43 "methods": """\ |
2085 | 44 def {name}(self, {args}{async_comma}{async_args}): |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
45 {error_handler}{blocking_call}{debug}return {result}\n""", |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
46 "async_methods": """\ |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
47 def {name}(self{async_comma}{args}): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
48 loop = asyncio.get_running_loop() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
49 fut = loop.create_future() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
50 reply_handler = lambda ret=None: loop.call_soon_threadsafe(fut.set_result, ret) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
51 error_handler = lambda err: loop.call_soon_threadsafe(fut.set_exception, dbus_to_bridge_exception(err)) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
52 self.db_{category}_iface.{name}({args_result}{async_comma}timeout=const_TIMEOUT, reply_handler=reply_handler, error_handler=error_handler) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
53 {debug}return fut\n""", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 } |
2085 | 55 |
56 def core_completion_signal(self, completion, function, default, arg_doc, async_): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 completion["category"] = completion["category"].upper() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 completion["body"] = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 "pass" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 if not self.args.debug |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 else 'log.debug ("{}")'.format(completion["name"]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 ) |
2085 | 63 |
64 def core_completion_method(self, completion, function, default, arg_doc, async_): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 completion.update( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 { |
3539
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
67 "debug": ( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
68 "" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
69 if not self.args.debug |
3539
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
70 else f'log.debug ("{completion["name"]}")\n{8 * " "}' |
60d3861e5996
bridge (dbus): use Tx DBus for backend part of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
71 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 ) |
2085 | 74 |
75 def frontend_completion_method(self, completion, function, default, arg_doc, async_): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 completion.update( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 # XXX: we can manage blocking call in the same way as async one: if callback is None the call will be blocking |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
79 "debug": ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
80 "" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
81 if not self.args.debug |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
82 else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " ") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
83 ), |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3539
diff
changeset
|
84 "args_result": self.get_arguments(function["sig_in"], name=arg_doc), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 "async_args": "callback=None, errback=None", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 "async_comma": ", " if function["sig_in"] else "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 "error_handler": """if callback is None: |
2085 | 88 error_handler = None |
89 else: | |
90 if errback is None: | |
91 errback = log.error | |
92 error_handler = lambda err:errback(dbus_to_bridge_exception(err)) | |
93 """, | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 } |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 ) |
2085 | 96 if async_: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 completion["blocking_call"] = "" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
98 completion["async_args_result"] = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
99 "timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 ) |
2085 | 101 else: |
102 # XXX: To have a blocking call, we must have not reply_handler, so we test if callback exists, and add reply_handler only in this case | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 completion[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 "blocking_call" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 ] = """kwargs={} |
2085 | 106 if callback is not None: |
107 kwargs['timeout'] = const_TIMEOUT | |
108 kwargs['reply_handler'] = callback | |
109 kwargs['error_handler'] = error_handler | |
110 """ | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 completion["async_args_result"] = "**kwargs" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 result = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
113 "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
114 % completion |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
115 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
116 completion["result"] = ( |
3028 | 117 "str(%s)" if self.args.unicode and function["sig_out"] == "s" else "%s" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 ) % result |