Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_frontend_template.py @ 1167:fe102b4cf275
core (xmpp): fixes SatPresenceProtocol.available to prevent an eventual code skipping
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 04 Sep 2014 11:26:22 +0200 |
parents | f094583732de |
children | bf2927e6a0f5 |
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 |
1073
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
30 import ast |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
31 |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
32 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
|
33 const_ERROR_PREFIX = const_INT_PREFIX + ".error" |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 const_TIMEOUT = 120 |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
38 |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
39 |
1062
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
40 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
|
41 """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
|
42 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
43 @param dbus_e (DBusException) |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
44 @return: BridgeException |
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 name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:] |
1073
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
47 # 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
|
48 # 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
|
49 # 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
|
50 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
|
51 try: |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
52 message, condition = ast.literal_eval(message) |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
53 except (SyntaxError, ValueError, TypeError): |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
54 condition = '' |
f094583732de
bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents:
1072
diff
changeset
|
55 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
|
56 |
95758ef3faa8
bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
57 |
267 | 58 class DBusBridgeFrontend(BridgeFrontend): |
59 def __init__(self): | |
60 try: | |
61 self.sessions_bus = dbus.SessionBus() | |
359
eb9d33ba4e36
bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents:
337
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 except dbus.exceptions.DBusException, e: |
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
69 if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown': |
267 | 70 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
|
71 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
|
72 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
|
73 raise BridgeInitError |
267 | 74 else: |
75 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
|
76 #props = self.db_core_iface.getProperties() |
267 | 77 |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
363
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
84 log.error(_('Unknown interface')) |
267 | 85 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
86 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
|
87 """ 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
|
88 try: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
89 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
|
90 except AttributeError: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
91 # 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
|
92 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
93 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
|
94 # 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
|
95 # - 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
|
96 # - 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
|
97 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
98 async = False |
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 if kwargs: |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
101 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
|
102 async = True |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
103 _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
|
104 _errback = kwargs.pop('errback') |
595
1f160467f5de
Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
105 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
|
106 async = True |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
107 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
|
108 _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
|
109 _callback = args.pop() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
110 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
111 method = getattr(self.db_plugin_iface, name) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
112 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 |
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
118 return method(*args, **kwargs) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
119 |
568
239abc5484c9
bridge: generic plugin methods handling for frontend side in D-Bus Bridge \o/
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
120 return getPluginMethod |
723 | 121 ##METHODS_PART## |