Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_core_template.py @ 313:cc8ffbfe938c
QnD workaround for presence broadcast openfire bug
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 16 Apr 2011 18:06:22 +0200 |
parents | e044d1dc37d1 |
children | 4402ac630712 |
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 | |
272
1d2e0dfe7114
bridge: core & frontend sides of bridge are now generated
Goffi <goffi@goffi.org>
parents:
267
diff
changeset
|
27 from logging import debug, info |
267 | 28 |
29 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix | |
30 const_COMM_SUFFIX = ".communication" | |
31 const_REQ_SUFFIX = ".request" | |
32 | |
33 class DbusObject(dbus.service.Object): | |
34 | |
35 def __init__(self, bus, path): | |
36 dbus.service.Object.__init__(self, bus, path) | |
37 debug("Init DbusObject...") | |
38 self.cb={} | |
39 | |
40 def register(self, name, cb): | |
41 self.cb[name]=cb | |
42 | |
43 ### signals ### | |
44 | |
45 ##SIGNALS_PART## | |
46 | |
47 ### methods ### | |
48 | |
49 ##METHODS_PART## | |
50 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
51 def __attributes(self, in_sign): |
267 | 52 """Return arguments to user given a in_sign |
53 @param in_sign: in_sign in the short form (using s,a,i,b etc) | |
54 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" | |
55 i=0 | |
56 idx=0 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
57 attr=[] |
267 | 58 while i<len(in_sign): |
59 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: | |
60 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) | |
61 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
62 attr.append("arg_%i" % idx) |
267 | 63 idx+=1 |
64 | |
65 if in_sign[i] == 'a': | |
66 i+=1 | |
67 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays | |
68 i+=1 | |
69 continue #we have a simple type for the array | |
70 opening_car = in_sign[i] | |
71 assert(opening_car in ['{','(']) | |
72 closing_car = '}' if opening_car == '{' else ')' | |
73 opening_count = 1 | |
74 while (True): #we have a dict or a list of tuples | |
75 i+=1 | |
76 if i>=len(in_sign): | |
77 raise ParseError("missing }") | |
78 if in_sign[i] == opening_car: | |
79 opening_count+=1 | |
80 if in_sign[i] == closing_car: | |
81 opening_count-=1 | |
82 if opening_count == 0: | |
83 break | |
84 i+=1 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
85 return attr |
267 | 86 |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
87 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}): |
267 | 88 """Dynamically add a method to Dbus Bridge""" |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
89 _attributes = self.__attributes(in_sign) |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
90 |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
91 if async: |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
92 _attributes.extend(['callback','errback']) |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
93 |
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
94 attributes = ', '.join(_attributes) |
267 | 95 |
96 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') | |
97 exec (code) | |
98 method = locals()[name] | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
99 async_callbacks = ('callback', 'errback') if async else None |
267 | 100 setattr(DbusObject, name, dbus.service.method( |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
101 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
|
102 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
|
103 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
104 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
|
105 func_table[function.__name__] = function #Needed for introspection |
267 | 106 |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
107 def addSignal(self, name, int_suffix, signature, doc={}): |
267 | 108 """Dynamically add a signal to Dbus Bridge""" |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
109 attributes = ', '.join(self.__attributes(signature)) |
267 | 110 |
111 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') | |
112 exec (code) | |
113 signal = locals()[name] | |
114 setattr(DbusObject, name, dbus.service.signal( | |
115 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
|
116 function = getattr(self, name) |
c25371424090
dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents:
272
diff
changeset
|
117 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
|
118 func_table[function.__name__] = function #Needed for introspection |
267 | 119 |
120 class DBusBridge(Bridge): | |
121 def __init__(self): | |
122 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
123 Bridge.__init__(self) | |
124 info ("Init DBus...") | |
125 self.session_bus = dbus.SessionBus() | |
126 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) | |
127 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') | |
128 | |
129 ##DIRECT_CALLS## | |
130 | |
131 def register(self, name, callback): | |
132 debug("registering DBus bridge method [%s]", name) | |
133 self.dbus_bridge.register(name, callback) | |
134 | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
135 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
267 | 136 """Dynamically add a method to Dbus Bridge""" |
137 print ("Adding method [%s] to DBus bridge" % name) | |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
138 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc) |
267 | 139 self.register(name, method) |
140 | |
298
15c8916317d0
dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents:
284
diff
changeset
|
141 def addSignal(self, name, int_suffix, signature, doc={}): |
299
e044d1dc37d1
dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents:
298
diff
changeset
|
142 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |
267 | 143 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) |
144 |