Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_frontend_template.py @ 1062:95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
author | souliane <souliane@mailoo.org> |
---|---|
date | Sat, 07 Jun 2014 15:20:39 +0200 |
parents | 301b342c697a |
children | d123d61976c8 |
rev | line source |
---|---|
267 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
4 # SAT communication bridge |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) |
267 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (at your option) any later version. |
267 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # GNU Affero General Public License for more details. |
267 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
595
diff
changeset
|
17 # 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
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
267 | 19 |
771 | 20 from sat.core.i18n import _ |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
21 from bridge_frontend import BridgeFrontend, BridgeException |
535
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
22 import dbus |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
23 from sat.core.log import getLogger |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
24 log = getLogger(__name__) |
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 |
267 | 26 |
535
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
27 from dbus.mainloop.glib import DBusGMainLoop |
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
28 DBusGMainLoop(set_as_default=True) |
790be337cc41
bridge: fixed D-Bus warning in frontend side of bridge
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
29 |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
30 const_INT_PREFIX = "org.goffi.SAT" # Interface prefix |
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
31 const_ERROR_PREFIX = const_INT_PREFIX + ".error" |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
32 const_OBJ_PATH = '/org/goffi/SAT/bridge' |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
33 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
|
34 const_PLUGIN_SUFFIX = ".plugin" |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
35 |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
36 |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
37 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
|
38 """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
|
39 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
40 @param dbus_e (DBusException) |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
41 @return: BridgeException |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
42 """ |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
43 name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:] |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
44 return BridgeException(name, dbus_e.get_dbus_message()) |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
45 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
46 |
267 | 47 class DBusBridgeFrontend(BridgeFrontend): |
48 def __init__(self): | |
49 try: | |
50 self.sessions_bus = dbus.SessionBus() | |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 dbus_interface=const_INT_PREFIX + const_PLUGIN_SUFFIX) |
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
57 except dbus.exceptions.DBusException, e: |
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
58 if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown': |
267 | 59 raise 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
|
60 elif e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
61 log.error(_(u"D-Bus is not launched, please see README to see instructions on how to launch it")) |
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
|
62 raise BridgeInitError |
267 | 63 else: |
64 raise e | |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
65 #props = self.db_core_iface.getProperties() |
267 | 66 |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
67 def register(self, functionName, handler, iface="core"): |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 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
|
72 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
73 log.error(_('Unknown interface')) |
267 | 74 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
75 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
|
76 """ 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
|
77 try: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
78 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
|
79 except AttributeError: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
80 # 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
|
81 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
82 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
|
83 # 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
|
84 # - 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
|
85 # - 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
|
86 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
87 async = False |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
88 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
89 if kwargs: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
90 if 'callback' in kwargs and 'errback' in kwargs: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
91 async = True |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
92 _callback = kwargs.pop('callback') |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
93 _errback = kwargs.pop('errback') |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
94 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]): |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
95 async = True |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
96 args = list(args) |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
97 _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
|
98 _callback = args.pop() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
99 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
100 method = getattr(self.db_plugin_iface, name) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
101 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
102 if async: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
103 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
|
104 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
|
105 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
106 return method(*args, **kwargs) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
107 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
108 return getPluginMethod |
723 | 109 ##METHODS_PART## |