Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_core_template.py @ 480:2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 01 Aug 2012 22:53:02 +0200 |
parents | 031b0e0aaab8 |
children | ca13633d3b6b |
rev | line source |
---|---|
267 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT: a jabber client | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
267 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
477
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
267 | 10 the Free Software Foundation, either version 3 of the License, or |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
477
diff
changeset
|
16 GNU Affero General Public License for more details. |
267 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
477
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
267 | 19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 """ | |
21 | |
22 | |
23 from bridge import Bridge | |
24 import dbus | |
25 import dbus.service | |
26 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
|
27 import inspect |
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 logging import debug, info, error |
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
|
29 from twisted.internet.defer import Deferred |
267 | 30 |
31 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix | |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
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 | 36 |
477
031b0e0aaab8
plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
37 class ParseError(Exception): |
031b0e0aaab8
plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
38 pass |
031b0e0aaab8
plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
39 |
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
|
40 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
|
41 _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
|
42 |
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 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
|
44 _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
|
45 |
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 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
|
47 _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
|
48 |
424
72c13313b6d6
bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents:
423
diff
changeset
|
49 class DeferredNotAsync(dbus.DBusException): |
72c13313b6d6
bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents:
423
diff
changeset
|
50 _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
|
51 |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
52 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
|
53 def __init__(self, twisted_error): |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
54 super(GenericException,self).__init__() |
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
|
55 mess = twisted_error.getErrorMessage() |
477
031b0e0aaab8
plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents:
469
diff
changeset
|
56 self._dbus_error_name = const_ERROR_PREFIX+"."+ (mess or str(twisted_error.__class__)) |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
57 |
267 | 58 class DbusObject(dbus.service.Object): |
59 | |
60 def __init__(self, bus, path): | |
61 dbus.service.Object.__init__(self, bus, path) | |
62 debug("Init DbusObject...") | |
63 self.cb={} | |
64 | |
65 def register(self, name, cb): | |
66 self.cb[name]=cb | |
67 | |
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
|
68 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
|
69 """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
|
70 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
|
71 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
|
72 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
|
73 |
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
|
74 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
|
75 #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
|
76 if not "errback" 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
|
77 error("errback is missing in method call [%s]" % name) |
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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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 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
|
84 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
|
85 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
|
86 if not isinstance(result, Deferred): |
425
e4e9187e3b5b
backend, bridge: asynchronous history
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
87 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
|
88 raise AsyncNotDeferred |
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
|
89 result.addCallback(lambda result: callback() if result==None else callback(result)) |
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
|
90 result.addErrback(lambda err:errback(GenericException(err))) |
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 else: |
424
72c13313b6d6
bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents:
423
diff
changeset
|
92 if isinstance(result, Deferred): |
425
e4e9187e3b5b
backend, bridge: asynchronous history
Goffi <goffi@goffi.org>
parents:
424
diff
changeset
|
93 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
|
94 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
|
95 return result |
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 |
267 | 97 ### signals ### |
98 | |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
99 @dbus.service.signal(const_INT_PREFIX+const_PLUGIN_SUFFIX, |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
100 signature='') |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
101 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
|
102 #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
|
103 # 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
|
104 # further investigations |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
105 pass |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
106 |
267 | 107 ##SIGNALS_PART## |
108 | |
109 ### methods ### | |
110 | |
111 ##METHODS_PART## | |
112 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
113 def __attributes(self, in_sign): |
267 | 114 """Return arguments to user given a in_sign |
115 @param in_sign: in_sign in the short form (using s,a,i,b etc) | |
116 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" | |
117 i=0 | |
118 idx=0 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
119 attr=[] |
267 | 120 while i<len(in_sign): |
121 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: | |
122 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) | |
123 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
124 attr.append("arg_%i" % idx) |
267 | 125 idx+=1 |
126 | |
127 if in_sign[i] == 'a': | |
128 i+=1 | |
129 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays | |
130 i+=1 | |
131 continue #we have a simple type for the array | |
132 opening_car = in_sign[i] | |
133 assert(opening_car in ['{','(']) | |
134 closing_car = '}' if opening_car == '{' else ')' | |
135 opening_count = 1 | |
136 while (True): #we have a dict or a list of tuples | |
137 i+=1 | |
138 if i>=len(in_sign): | |
139 raise ParseError("missing }") | |
140 if in_sign[i] == opening_car: | |
141 opening_count+=1 | |
142 if in_sign[i] == closing_car: | |
143 opening_count-=1 | |
144 if opening_count == 0: | |
145 break | |
146 i+=1 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
147 return attr |
267 | 148 |
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
|
149 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): |
267 | 150 """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
|
151 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
|
152 |
469
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
153 _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
|
154 _defaults = list(inspect_args.defaults or []) |
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
|
155 |
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
|
156 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
|
157 #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
|
158 del(_arguments[0]) |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
159 |
469
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
160 #first arguments are for the _callback method |
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
161 arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback','errback=errback']) if async else _arguments)) |
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
162 |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
163 if async: |
469
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
164 _arguments.extend(['callback','errback']) |
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
165 _defaults.extend([None, None]) |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
166 |
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
|
167 |
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
|
168 #now we create a second list with default values |
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
|
169 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
|
170 _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i])) |
267 | 171 |
469
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
172 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
|
173 |
469
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
174 code = compile ('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' % |
db4c2b82bab6
D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
468
diff
changeset
|
175 {'name':name, 'arguments_defaults':arguments_defaults, 'arguments_callback':arguments_callback}, '<DBus bridge>','exec') |
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
|
176 exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec |
267 | 177 method = locals()[name] |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
178 async_callbacks = ('callback', 'errback') if async else None |
267 | 179 setattr(DbusObject, name, dbus.service.method( |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
180 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign, |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
181 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
|
182 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
183 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface] |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
184 func_table[function.__name__] = function #Needed for introspection |
267 | 185 |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
186 def addSignal(self, name, int_suffix, signature, doc={}): |
267 | 187 """Dynamically add a signal to Dbus Bridge""" |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
188 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
|
189 #TODO: use doc parameter to name attributes |
267 | 190 |
337
4402ac630712
bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents:
299
diff
changeset
|
191 #code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the debug is too annoying with xmllog |
4402ac630712
bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents:
299
diff
changeset
|
192 code = compile ('def '+name+' (self,'+attributes+'): pass', '<DBus bridge>','exec') |
267 | 193 exec (code) |
194 signal = locals()[name] | |
195 setattr(DbusObject, name, dbus.service.signal( | |
196 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
|
197 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
198 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface] |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
199 func_table[function.__name__] = function #Needed for introspection |
267 | 200 |
201 class DBusBridge(Bridge): | |
202 def __init__(self): | |
203 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
204 Bridge.__init__(self) | |
205 info ("Init DBus...") | |
206 self.session_bus = dbus.SessionBus() | |
207 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
|
208 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) |
267 | 209 |
210 ##DIRECT_CALLS## | |
211 | |
212 def register(self, name, callback): | |
213 debug("registering DBus bridge method [%s]", name) | |
214 self.dbus_bridge.register(name, callback) | |
215 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
216 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
267 | 217 """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
|
218 #FIXME: doc parameter is kept only temporary, the time to remove it from calls |
267 | 219 print ("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
|
220 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) |
267 | 221 self.register(name, method) |
222 | |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
223 def addSignal(self, name, int_suffix, signature, doc={}): |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
224 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |
267 | 225 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) |
226 |