Mercurial > libervia-backend
annotate sat/bridge/bridge_constructor/constructors/dbus/dbus_frontend_template.py @ 3836:e183f1fbfa8d
doc (cli/pubsub): add doc for ``reference`` subcommand:
rel 369
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Jul 2022 16:15:09 +0200 |
parents | 71cfe9334f73 |
children | 524856bd7b19 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
267 | 2 |
3143
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
3 # SàT communication bridge |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
267 | 5 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
9 # (at your option) any later version. |
267 | 10 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
14 # GNU Affero General Public License for more details. |
267 | 15 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
267 | 18 |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
19 import asyncio |
535
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
20 import dbus |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
21 import ast |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
22 from sat.core.i18n import _ |
3143
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
23 from sat.tools import config |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
24 from sat.core.log import getLogger |
627
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
25 from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
26 from dbus.mainloop.glib import DBusGMainLoop |
3143
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
27 from .bridge_frontend import BridgeException |
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
28 |
267 | 29 |
535
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
30 DBusGMainLoop(set_as_default=True) |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
31 log = getLogger(__name__) |
535
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
32 |
1073
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
33 |
3143
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
34 # Interface prefix |
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
35 const_INT_PREFIX = config.getConfig( |
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
36 config.parseMainConf(), |
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
37 "", |
830fce0db15d
bridge (dbus): new `bridge_dbus_int_prefix` option (in `[DEFAULT]` settings) to change interface prefix
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
38 "bridge_dbus_int_prefix", |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
39 "org.libervia.Libervia") |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
40 const_ERROR_PREFIX = const_INT_PREFIX + ".error" |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
41 const_OBJ_PATH = '/org/libervia/Libervia/bridge' |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
42 const_CORE_SUFFIX = ".core" |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
43 const_PLUGIN_SUFFIX = ".plugin" |
1072
d123d61976c8
bridge (D-Bus): frontend side now use a 120 s timeout for async calls
Goffi <goffi@goffi.org>
parents:
1062
diff
changeset
|
44 const_TIMEOUT = 120 |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
45 |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
46 |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
47 def dbus_to_bridge_exception(dbus_e): |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
48 """Convert a DBusException to a BridgeException. |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
49 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
50 @param dbus_e (DBusException) |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
51 @return: BridgeException |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
52 """ |
1188
bf2927e6a0f5
frontends (dbus): error is not truncated anymore if it's not a SàT error.
Goffi <goffi@goffi.org>
parents:
1073
diff
changeset
|
53 full_name = dbus_e.get_dbus_name() |
bf2927e6a0f5
frontends (dbus): error is not truncated anymore if it's not a SàT error.
Goffi <goffi@goffi.org>
parents:
1073
diff
changeset
|
54 if full_name.startswith(const_ERROR_PREFIX): |
bf2927e6a0f5
frontends (dbus): error is not truncated anymore if it's not a SàT error.
Goffi <goffi@goffi.org>
parents:
1073
diff
changeset
|
55 name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:] |
bf2927e6a0f5
frontends (dbus): error is not truncated anymore if it's not a SàT error.
Goffi <goffi@goffi.org>
parents:
1073
diff
changeset
|
56 else: |
bf2927e6a0f5
frontends (dbus): error is not truncated anymore if it's not a SàT error.
Goffi <goffi@goffi.org>
parents:
1073
diff
changeset
|
57 name = full_name |
1073
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
58 # XXX: dbus_e.args doesn't contain the original DBusException args, but we |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
59 # receive its serialized form in dbus_e.args[0]. From that we can rebuild |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
60 # the original arguments list thanks to ast.literal_eval (secure eval). |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
61 message = dbus_e.get_dbus_message() # similar to dbus_e.args[0] |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
62 try: |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
63 message, condition = ast.literal_eval(message) |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
64 except (SyntaxError, ValueError, TypeError): |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
65 condition = '' |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
66 return BridgeException(name, message, condition) |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
67 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
68 |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
69 class Bridge: |
2091
f413bfc24458
bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
70 |
f413bfc24458
bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
71 def bridgeConnect(self, callback, errback): |
267 | 72 try: |
73 self.sessions_bus = dbus.SessionBus() | |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
74 self.db_object = self.sessions_bus.get_object(const_INT_PREFIX, |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
75 const_OBJ_PATH) |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
76 self.db_core_iface = dbus.Interface(self.db_object, |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
77 dbus_interface=const_INT_PREFIX + const_CORE_SUFFIX) |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
78 self.db_plugin_iface = dbus.Interface(self.db_object, |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
79 dbus_interface=const_INT_PREFIX + const_PLUGIN_SUFFIX) |
3028 | 80 except dbus.exceptions.DBusException as e: |
1917
05a5a125a238
bridge (frontend): raise a BridgeExceptionNoService on org.freedesktop.DBus.Error.Spawn.ExecFailed D-Bus error
Goffi <goffi@goffi.org>
parents:
1794
diff
changeset
|
81 if e._dbus_error_name in ('org.freedesktop.DBus.Error.ServiceUnknown', |
05a5a125a238
bridge (frontend): raise a BridgeExceptionNoService on org.freedesktop.DBus.Error.Spawn.ExecFailed D-Bus error
Goffi <goffi@goffi.org>
parents:
1794
diff
changeset
|
82 'org.freedesktop.DBus.Error.Spawn.ExecFailed'): |
2091
f413bfc24458
bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
83 errback(BridgeExceptionNoService()) |
627
d207c2186519
core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
84 elif e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': |
3028 | 85 log.error(_("D-Bus is not launched, please see README to see instructions on how to launch it")) |
2091
f413bfc24458
bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
86 errback(BridgeInitError) |
267 | 87 else: |
2091
f413bfc24458
bridge, quick_frontend: preparation for async bridge
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
88 errback(e) |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
89 else: |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
90 callback() |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
91 #props = self.db_core_iface.getProperties() |
267 | 92 |
2086 | 93 def register_signal(self, functionName, handler, iface="core"): |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
94 if iface == "core": |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
95 self.db_core_iface.connect_to_signal(functionName, handler) |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
96 elif iface == "plugin": |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
97 self.db_plugin_iface.connect_to_signal(functionName, handler) |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
98 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
99 log.error(_('Unknown interface')) |
267 | 100 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
101 def __getattribute__(self, name): |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
102 """ usual __getattribute__ if the method exists, else try to find a plugin method """ |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
103 try: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
104 return object.__getattribute__(self, name) |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
105 except AttributeError: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
106 # The attribute is not found, we try the plugin proxy to find the requested method |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
107 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
108 def getPluginMethod(*args, **kwargs): |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
109 # We first check if we have an async call. We detect this in two ways: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
110 # - if we have the 'callback' and 'errback' keyword arguments |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
111 # - or if the last two arguments are callable |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
112 |
3028 | 113 async_ = False |
1794
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
114 args = list(args) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
115 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
116 if kwargs: |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
117 if 'callback' in kwargs: |
3028 | 118 async_ = True |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
119 _callback = kwargs.pop('callback') |
3028 | 120 _errback = kwargs.pop('errback', lambda failure: log.error(str(failure))) |
1794
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
121 try: |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
122 args.append(kwargs.pop('profile')) |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
123 except KeyError: |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
124 try: |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
125 args.append(kwargs.pop('profile_key')) |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
126 except KeyError: |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
127 pass |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
128 # at this point, kwargs should be empty |
b0ed4863dbc7
bridge (D-Bus): fixed handling of profile in kwargs:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
129 if kwargs: |
3208 | 130 log.warning("unexpected keyword arguments, they will be ignored: {}".format(kwargs)) |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
131 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]): |
3028 | 132 async_ = True |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
133 _errback = args.pop() |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
134 _callback = args.pop() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
135 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
136 method = getattr(self.db_plugin_iface, name) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
137 |
3028 | 138 if async_: |
1072
d123d61976c8
bridge (D-Bus): frontend side now use a 120 s timeout for async calls
Goffi <goffi@goffi.org>
parents:
1062
diff
changeset
|
139 kwargs['timeout'] = const_TIMEOUT |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
140 kwargs['reply_handler'] = _callback |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
141 kwargs['error_handler'] = lambda err: _errback(dbus_to_bridge_exception(err)) |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
142 |
3648
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
143 try: |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
144 return method(*args, **kwargs) |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
145 except ValueError as e: |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
146 if e.args[0].startswith("Unable to guess signature"): |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
147 # XXX: if frontend is started too soon after backend, the |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
148 # inspection misses methods (notably plugin dynamically added |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
149 # methods). The following hack works around that by redoing the |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
150 # cache of introspected methods signatures. |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
151 log.debug("using hack to work around inspection issue") |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
152 proxy = self.db_plugin_iface.proxy_object |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
153 IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
154 proxy._introspect_state = IN_PROGRESS |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
155 proxy._Introspect() |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
156 return self.db_plugin_iface.get_dbus_method(name)(*args, **kwargs) |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
157 raise e |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
158 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
159 return getPluginMethod |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1188
diff
changeset
|
160 |
723 | 161 ##METHODS_PART## |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
162 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
163 class AIOBridge(Bridge): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
164 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
165 def register_signal(self, functionName, handler, iface="core"): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
166 loop = asyncio.get_running_loop() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
167 async_handler = lambda *args: asyncio.run_coroutine_threadsafe(handler(*args), loop) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
168 return super().register_signal(functionName, async_handler, iface) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
169 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
170 def __getattribute__(self, name): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
171 """ usual __getattribute__ if the method exists, else try to find a plugin method """ |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
172 try: |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
173 return object.__getattribute__(self, name) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
174 except AttributeError: |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
175 # The attribute is not found, we try the plugin proxy to find the requested method |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
176 def getPluginMethod(*args, **kwargs): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
177 loop = asyncio.get_running_loop() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
178 fut = loop.create_future() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
179 method = getattr(self.db_plugin_iface, name) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
180 reply_handler = lambda ret=None: loop.call_soon_threadsafe( |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
181 fut.set_result, ret) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
182 error_handler = lambda err: loop.call_soon_threadsafe( |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
183 fut.set_exception, dbus_to_bridge_exception(err)) |
3648
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
184 try: |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
185 method( |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
186 *args, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
187 **kwargs, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
188 timeout=const_TIMEOUT, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
189 reply_handler=reply_handler, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
190 error_handler=error_handler |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
191 ) |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
192 except ValueError as e: |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
193 if e.args[0].startswith("Unable to guess signature"): |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
194 # same hack as for Bridge.__getattribute__ |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
195 log.warning("using hack to work around inspection issue") |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
196 proxy = self.db_plugin_iface.proxy_object |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
197 IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
198 proxy._introspect_state = IN_PROGRESS |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
199 proxy._Introspect() |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
200 self.db_plugin_iface.get_dbus_method(name)( |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
201 *args, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
202 **kwargs, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
203 timeout=const_TIMEOUT, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
204 reply_handler=reply_handler, |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
205 error_handler=error_handler |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
206 ) |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
207 |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
208 else: |
71cfe9334f73
bridge (dbus/frontend): reintrospect signature in case of guess error:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
209 raise e |
3042
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
210 return fut |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
211 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
212 return getPluginMethod |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
213 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
214 def bridgeConnect(self): |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
215 loop = asyncio.get_running_loop() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
216 fut = loop.create_future() |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
217 super().bridgeConnect( |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
218 callback=lambda: loop.call_soon_threadsafe(fut.set_result, None), |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
219 errback=lambda e: loop.call_soon_threadsafe(fut.set_exception, e) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
220 ) |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
221 return fut |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
222 |
964abd07dc03
bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
223 ##ASYNC_METHODS_PART## |