annotate src/bridge/bridge_constructor/dbus_core_template.py @ 425:e4e9187e3b5b

backend, bridge: asynchronous history quick_frontend: use of asynchronous history
author Goffi <goffi@goffi.org>
date Tue, 08 Nov 2011 01:08:11 +0100
parents 72c13313b6d6
children cf005701624b
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT: a jabber client
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
12
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from bridge import Bridge
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import dbus
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import dbus.service
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
45 class DeferredNotAsync(dbus.DBusException):
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
46 _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
47
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
48 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
49 def __init__(self, twisted_error):
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
50 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
51 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
52 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
53
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 class DbusObject(dbus.service.Object):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def __init__(self, bus, path):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 dbus.service.Object.__init__(self, bus, path)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 debug("Init DbusObject...")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.cb={}
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def register(self, name, cb):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.cb[name]=cb
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
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
64 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
65 """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
66 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
67 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
68 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
69
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 "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
71 #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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 if not isinstance(result, Deferred):
425
e4e9187e3b5b backend, bridge: asynchronous history
Goffi <goffi@goffi.org>
parents: 424
diff changeset
83 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
84 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
85 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
86 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
87 else:
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
88 if isinstance(result, Deferred):
425
e4e9187e3b5b backend, bridge: asynchronous history
Goffi <goffi@goffi.org>
parents: 424
diff changeset
89 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
90 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
91 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
92
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 ### signals ###
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
371
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
95 @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
96 signature='')
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
97 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
98 #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
99 # 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
100 # further investigations
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
101 pass
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
102
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 ##SIGNALS_PART##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 ### methods ###
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 ##METHODS_PART##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
109 def __attributes(self, in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """Return arguments to user given a in_sign
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @param in_sign: in_sign in the short form (using s,a,i,b etc)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")"""
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 i=0
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 idx=0
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
115 attr=[]
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 while i<len(in_sign):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
120 attr.append("arg_%i" % idx)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 idx+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if in_sign[i] == 'a':
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 continue #we have a simple type for the array
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 opening_car = in_sign[i]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 assert(opening_car in ['{','('])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 closing_car = '}' if opening_car == '{' else ')'
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 opening_count = 1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 while (True): #we have a dict or a list of tuples
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if i>=len(in_sign):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 raise ParseError("missing }")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 if in_sign[i] == opening_car:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 opening_count+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if in_sign[i] == closing_car:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 opening_count-=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if opening_count == 0:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 break
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 i+=1
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
143 return attr
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
145 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 """Dynamically add a method to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
147 _attributes = self.__attributes(in_sign)
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
148
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
149 if async:
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
150 _attributes.extend(['callback','errback'])
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
151
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
152 attributes = ', '.join(_attributes)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 method = locals()[name]
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
157 async_callbacks = ('callback', 'errback') if async else None
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 setattr(DbusObject, name, dbus.service.method(
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
159 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
160 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
161 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
162 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
163 func_table[function.__name__] = function #Needed for introspection
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
165 def addSignal(self, name, int_suffix, signature, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """Dynamically add a signal to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
167 attributes = ', '.join(self.__attributes(signature))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
168
337
4402ac630712 bridge: async callback managed in bridge_constructor + misc
Goffi <goffi@goffi.org>
parents: 299
diff changeset
169 #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
170 code = compile ('def '+name+' (self,'+attributes+'): pass', '<DBus bridge>','exec')
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 signal = locals()[name]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 setattr(DbusObject, name, dbus.service.signal(
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 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
175 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
176 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
177 func_table[function.__name__] = function #Needed for introspection
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 class DBusBridge(Bridge):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def __init__(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 Bridge.__init__(self)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 info ("Init DBus...")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 self.session_bus = dbus.SessionBus()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 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
186 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
187
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 ##DIRECT_CALLS##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 def register(self, name, callback):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 debug("registering DBus bridge method [%s]", name)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.dbus_bridge.register(name, callback)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
194 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
195 """Dynamically add a method to Dbus Bridge"""
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 print ("Adding method [%s] to DBus bridge" % name)
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
197 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self.register(name, method)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
200 def addSignal(self, name, int_suffix, signature, doc={}):
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
201 self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 setattr(DBusBridge, name, getattr(self.dbus_bridge, name))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
203