annotate sat/bridge/bridge_constructor/constructors/dbus/dbus_frontend_template.py @ 3576:8876803db81b

/!\ package is being renamed to libervia-backend following global name change /!\
author Goffi <goffi@goffi.org>
date Fri, 18 Jun 2021 14:44:01 +0200
parents 7550ae9cfbac
children 71cfe9334f73
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
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3208
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 try:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
4f00fdf11445 bridge (constructor/dbus): typo
Goffi <goffi@goffi.org>
parents: 3143
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
239abc5484c9 bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents: 562
diff changeset
143 return method(*args, **kwargs)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
144
568
239abc5484c9 bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents: 562
diff changeset
145 return getPluginMethod
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1188
diff changeset
146
723
f02e2c277f0c bridge: fixed bridge_constructor
Goffi <goffi@goffi.org>
parents: 709
diff changeset
147 ##METHODS_PART##
3042
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
148
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
149 class AIOBridge(Bridge):
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
150
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
151 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
152 loop = asyncio.get_running_loop()
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
153 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
154 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
155
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
156 def __getattribute__(self, name):
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
157 """ 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
158 try:
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
159 return object.__getattribute__(self, name)
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
160 except AttributeError:
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
161 # 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
162 def getPluginMethod(*args, **kwargs):
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
163 loop = asyncio.get_running_loop()
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
164 fut = loop.create_future()
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
165 method = getattr(self.db_plugin_iface, name)
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
166 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
167 fut.set_result, ret)
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
168 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
169 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
170 method(
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
171 *args,
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
172 **kwargs,
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
173 timeout=const_TIMEOUT,
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
174 reply_handler=reply_handler,
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
175 error_handler=error_handler
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
176 )
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
177 return fut
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
178
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
179 return getPluginMethod
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
180
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
181 def bridgeConnect(self):
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
182 loop = asyncio.get_running_loop()
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
183 fut = loop.create_future()
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
184 super().bridgeConnect(
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
185 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
186 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
187 )
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
188 return fut
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
189
964abd07dc03 bridge (dbus): AsyncIO version of D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
190 ##ASYNC_METHODS_PART##