Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_core_template.py @ 423:6c20c76abdcc
backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
- getParams, getParamsForCategory and getParamsUI are now asynchronous
primitivus: management of asynchronous getParamsUI
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 07 Nov 2011 00:09:22 +0100 |
parents | 6c167a2e04b8 |
children | 72c13313b6d6 |
rev | line source |
---|---|
267 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT: a jabber client | |
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
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 | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
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 | |
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
|
27 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
|
28 from twisted.internet.defer import Deferred |
267 | 29 |
30 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
|
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:
359
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:
359
diff
changeset
|
34 const_PLUGIN_SUFFIX = ".plugin" |
267 | 35 |
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
|
36 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
|
37 _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
|
38 |
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
|
39 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
|
40 _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
|
41 |
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 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
|
43 _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
|
44 |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
45 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
|
46 def __init__(self, twisted_error): |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
47 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
|
48 mess = twisted_error.getErrorMessage() |
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
|
49 self._dbus_error_name = const_ERROR_PREFIX+"."+ (mess or str(err.__class__)) |
419
6c167a2e04b8
bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents:
371
diff
changeset
|
50 |
267 | 51 class DbusObject(dbus.service.Object): |
52 | |
53 def __init__(self, bus, path): | |
54 dbus.service.Object.__init__(self, bus, path) | |
55 debug("Init DbusObject...") | |
56 self.cb={} | |
57 | |
58 def register(self, name, cb): | |
59 self.cb[name]=cb | |
60 | |
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
|
61 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
|
62 """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
|
63 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
|
64 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
|
65 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
|
66 |
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
|
67 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
|
68 #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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 if not isinstance(result, Deferred): |
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 error("Asynchrone method [%s] does not return a Deferred." % 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
|
81 raise 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
|
82 result.addCallback(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
|
83 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
|
84 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
|
85 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
|
86 |
267 | 87 ### signals ### |
88 | |
371
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
89 @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
|
90 signature='') |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
91 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
|
92 #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
|
93 # 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
|
94 # further investigations |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
95 pass |
3ea41a199b36
bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents:
359
diff
changeset
|
96 |
267 | 97 ##SIGNALS_PART## |
98 | |
99 ### methods ### | |
100 | |
101 ##METHODS_PART## | |
102 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
103 def __attributes(self, in_sign): |
267 | 104 """Return arguments to user given a in_sign |
105 @param in_sign: in_sign in the short form (using s,a,i,b etc) | |
106 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" | |
107 i=0 | |
108 idx=0 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
109 attr=[] |
267 | 110 while i<len(in_sign): |
111 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: | |
112 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) | |
113 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
114 attr.append("arg_%i" % idx) |
267 | 115 idx+=1 |
116 | |
117 if in_sign[i] == 'a': | |
118 i+=1 | |
119 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays | |
120 i+=1 | |
121 continue #we have a simple type for the array | |
122 opening_car = in_sign[i] | |
123 assert(opening_car in ['{','(']) | |
124 closing_car = '}' if opening_car == '{' else ')' | |
125 opening_count = 1 | |
126 while (True): #we have a dict or a list of tuples | |
127 i+=1 | |
128 if i>=len(in_sign): | |
129 raise ParseError("missing }") | |
130 if in_sign[i] == opening_car: | |
131 opening_count+=1 | |
132 if in_sign[i] == closing_car: | |
133 opening_count-=1 | |
134 if opening_count == 0: | |
135 break | |
136 i+=1 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
137 return attr |
267 | 138 |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
139 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}): |
267 | 140 """Dynamically add a method to Dbus Bridge""" |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
141 _attributes = self.__attributes(in_sign) |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
142 |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
143 if async: |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
144 _attributes.extend(['callback','errback']) |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
145 |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
146 attributes = ', '.join(_attributes) |
267 | 147 |
148 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') | |
149 exec (code) | |
150 method = locals()[name] | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
151 async_callbacks = ('callback', 'errback') if async else None |
267 | 152 setattr(DbusObject, name, dbus.service.method( |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
153 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
|
154 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
|
155 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
156 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
|
157 func_table[function.__name__] = function #Needed for introspection |
267 | 158 |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
159 def addSignal(self, name, int_suffix, signature, doc={}): |
267 | 160 """Dynamically add a signal to Dbus Bridge""" |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
161 attributes = ', '.join(self.__attributes(signature)) |
267 | 162 |
337
4402ac630712
bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents:
299
diff
changeset
|
163 #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
|
164 code = compile ('def '+name+' (self,'+attributes+'): pass', '<DBus bridge>','exec') |
267 | 165 exec (code) |
166 signal = locals()[name] | |
167 setattr(DbusObject, name, dbus.service.signal( | |
168 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
|
169 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
170 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
|
171 func_table[function.__name__] = function #Needed for introspection |
267 | 172 |
173 class DBusBridge(Bridge): | |
174 def __init__(self): | |
175 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
176 Bridge.__init__(self) | |
177 info ("Init DBus...") | |
178 self.session_bus = dbus.SessionBus() | |
179 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
|
180 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) |
267 | 181 |
182 ##DIRECT_CALLS## | |
183 | |
184 def register(self, name, callback): | |
185 debug("registering DBus bridge method [%s]", name) | |
186 self.dbus_bridge.register(name, callback) | |
187 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
188 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
267 | 189 """Dynamically add a method to Dbus Bridge""" |
190 print ("Adding method [%s] to DBus bridge" % name) | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
191 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc) |
267 | 192 self.register(name, method) |
193 | |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
194 def addSignal(self, name, int_suffix, signature, doc={}): |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
195 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |
267 | 196 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) |
197 |