annotate src/bridge/bridge_constructor/dbus_core_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 f094583732de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
4 # SAT: a jabber client
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 751
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
20 from sat.core.i18n import _
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from bridge import Bridge
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import dbus
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import dbus.service
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import dbus.mainloop.glib
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
25 import inspect
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
26 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
27 log = getLogger(__name__)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
28 from twisted.internet.defer import Deferred
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
29 from sat.core.exceptions import BridgeInitError
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
31 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
32 const_ERROR_PREFIX = const_INT_PREFIX + ".error"
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
33 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: 359
diff changeset
34 const_CORE_SUFFIX = ".core"
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
35 const_PLUGIN_SUFFIX = ".plugin"
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
37
477
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
38 class ParseError(Exception):
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
39 pass
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
40
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
41
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
42 class MethodNotRegistered(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
43 _dbus_error_name = const_ERROR_PREFIX + ".MethodNotRegistered"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
44
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
45
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
46 class InternalError(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
47 _dbus_error_name = const_ERROR_PREFIX + ".InternalError"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
48
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
49
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
50 class AsyncNotDeferred(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
51 _dbus_error_name = const_ERROR_PREFIX + ".AsyncNotDeferred"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
52
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
53
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
54 class DeferredNotAsync(dbus.DBusException):
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
55 _dbus_error_name = const_ERROR_PREFIX + ".DeferredNotAsync"
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
56
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
57
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
58 class GenericException(dbus.DBusException):
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
59 def __init__(self, twisted_error):
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
60 super(GenericException, self).__init__()
751
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
61 try:
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
62 # twisted_error.value is a class
1062
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
63 class_ = twisted_error.value().__class__
751
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
64 except TypeError:
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
65 # twisted_error.value is an instance
1062
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
66 class_ = twisted_error.value.__class__
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
67 self.args = (twisted_error.getErrorMessage(),)
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
68 self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
69
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
70
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 class DbusObject(dbus.service.Object):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __init__(self, bus, path):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 dbus.service.Object.__init__(self, bus, path)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
75 log.debug("Init DbusObject...")
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
76 self.cb = {}
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def register(self, name, cb):
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
79 self.cb[name] = cb
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
81 def _callback(self, name, *args, **kwargs):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
82 """call the callback if it exists, raise an exception else
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
83 if the callback return a deferred, use async methods"""
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
84 if not name in self.cb:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
85 raise MethodNotRegistered
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
86
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
87 if "callback" in kwargs:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
88 #we must have errback too
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
89 if not "errback" in kwargs:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
90 log.error("errback is missing in method call [%s]" % name)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
91 raise InternalError
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
92 callback = kwargs.pop("callback")
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
93 errback = kwargs.pop("errback")
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
94 async = True
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
95 else:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
96 async = False
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
97 result = self.cb[name](*args, **kwargs)
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
98 if async:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
99 if not isinstance(result, Deferred):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
100 log.error("Asynchronous method [%s] does not return a Deferred." % name)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
101 raise AsyncNotDeferred
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
102 result.addCallback(lambda result: callback() if result is None else callback(result))
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
103 result.addErrback(lambda err: errback(GenericException(err)))
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
104 else:
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
105 if isinstance(result, Deferred):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
106 log.error("Synchronous method [%s] return a Deferred." % name)
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
107 raise DeferredNotAsync
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
108 return result
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
109 ### signals ###
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
111 @dbus.service.signal(const_INT_PREFIX + const_PLUGIN_SUFFIX,
371
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
112 signature='')
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
113 def dummySignal(self):
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
114 #FIXME: workaround for addSignal (doesn't work if one method doensn't
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
115 # already exist for plugins), probably missing some initialisation, need
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
116 # further investigations
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
117 pass
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
118
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 ##SIGNALS_PART##
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
120 ### methods ###
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
121
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 ##METHODS_PART##
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
123 def __attributes(self, in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 """Return arguments to user given a in_sign
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 @param in_sign: in_sign in the short form (using s,a,i,b etc)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
126 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")"""
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
127 i = 0
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
128 idx = 0
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
129 attr = []
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
130 while i < len(in_sign):
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
131 if in_sign[i] not in ['b', 'y', 'n', 'i', 'x', 'q', 'u', 't', 'd', 's', 'a']:
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
134 attr.append("arg_%i" % idx)
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
135 idx += 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if in_sign[i] == 'a':
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
138 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
139 if in_sign[i] != '{' and in_sign[i] != '(': # FIXME: must manage tuples out of arrays
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
140 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
141 continue # we have a simple type for the array
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 opening_car = in_sign[i]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
143 assert(opening_car in ['{', '('])
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 closing_car = '}' if opening_car == '{' else ')'
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 opening_count = 1
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
146 while (True): # we have a dict or a list of tuples
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
147 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
148 if i >= len(in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 raise ParseError("missing }")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if in_sign[i] == opening_car:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
151 opening_count += 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if in_sign[i] == closing_car:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
153 opening_count -= 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 if opening_count == 0:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 break
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
156 i += 1
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
157 return attr
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
159 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 """Dynamically add a method to Dbus Bridge"""
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
161 inspect_args = inspect.getargspec(method)
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
162
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
163 _arguments = inspect_args.args
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
164 _defaults = list(inspect_args.defaults or [])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
165
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
166 if inspect.ismethod(method):
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
167 #if we have a method, we don't want the first argument (usually 'self')
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
168 del(_arguments[0])
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
169
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
170 #first arguments are for the _callback method
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
171 arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback', 'errback=errback']) if async else _arguments))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
172
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
173 if async:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
174 _arguments.extend(['callback', 'errback'])
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
175 _defaults.extend([None, None])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
176
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
177 #now we create a second list with default values
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
178 for i in range(1, len(_defaults) + 1):
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
179 _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i]))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
181 arguments_defaults = ', '.join(_arguments)
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
182
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
183 code = compile('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' %
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
184 {'name': name, 'arguments_defaults': arguments_defaults, 'arguments_callback': arguments_callback}, '<DBus bridge>', 'exec')
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
185 exec (code) # FIXME: to the same thing in a cleaner way, without compile/exec
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 method = locals()[name]
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
187 async_callbacks = ('callback', 'errback') if async else None
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 setattr(DbusObject, name, dbus.service.method(
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
189 const_INT_PREFIX + int_suffix, in_signature=in_sign, out_signature=out_sign,
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
190 async_callbacks=async_callbacks)(method))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
191 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
192 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
193 func_table[function.__name__] = function # Needed for introspection
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
194
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
195 def addSignal(self, name, int_suffix, signature, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 """Dynamically add a signal to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
197 attributes = ', '.join(self.__attributes(signature))
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
198 #TODO: use doc parameter to name attributes
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
200 #code = compile ('def '+name+' (self,'+attributes+'): log.debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the log.debug is too annoying with xmllog
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
201 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec')
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 signal = locals()[name]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 setattr(DbusObject, name, dbus.service.signal(
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
205 const_INT_PREFIX + int_suffix, signature=signature)(signal))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
206 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
207 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
208 func_table[function.__name__] = function # Needed for introspection
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
209
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 class DBusBridge(Bridge):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 def __init__(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 Bridge.__init__(self)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
215 log.info("Init DBus...")
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
216 try:
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
217 self.session_bus = dbus.SessionBus()
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
218 except dbus.DBusException as e:
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
219 if 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
220 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
221 raise BridgeInitError
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
223 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 ##DIRECT_CALLS##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 def register(self, name, callback):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
227 log.debug("registering DBus bridge method [%s]" % name)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 self.dbus_bridge.register(name, callback)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
229
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
230 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 """Dynamically add a method to Dbus Bridge"""
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
232 #FIXME: doc parameter is kept only temporary, the time to remove it from calls
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
233 log.debug("Adding method [%s] to DBus bridge" % name)
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
234 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 self.register(name, method)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
236
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
237 def addSignal(self, name, int_suffix, signature, doc={}):
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
238 self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 setattr(DBusBridge, name, getattr(self.dbus_bridge, name))