Mercurial > libervia-backend
annotate sat_bridge/DBus.py @ 10:14d7861ca59e
refactoring: CONST replaced by const
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 26 Oct 2009 18:26:21 +0100 |
parents | c14a3a7018a5 |
children | 0a024d5e0cd0 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT: a jabber client | |
6 Copyright (C) 2009 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 | |
27 import pdb | |
28 from logging import debug, info, error | |
29 | |
10 | 30 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
31 |
0 | 32 class DbusObject(dbus.service.Object): |
33 | |
34 def __init__(self, bus, path): | |
35 dbus.service.Object.__init__(self, bus, path) | |
36 debug("Init DbusObject...") | |
37 self.cb={} | |
38 | |
39 def register(self, name, cb): | |
40 self.cb[name]=cb | |
41 | |
42 ### signals ### | |
43 | |
10 | 44 @dbus.service.signal(const_INT_PREFIX+".communication", |
0 | 45 signature='sa{ss}as') |
46 def newContact(self, contact, attributes, groups): | |
47 debug("new contact signal (%s) sended", contact) | |
48 | |
10 | 49 @dbus.service.signal(const_INT_PREFIX+".communication", |
0 | 50 signature='ssss') |
51 def newMessage(self, from_jid, msg, type='chat', to=''): | |
52 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) | |
53 | |
10 | 54 @dbus.service.signal(const_INT_PREFIX+".communication", |
0 | 55 signature='ssssi') |
56 def presenceUpdate(self, jid, type, show, status, priority): | |
57 debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) | |
58 | |
10 | 59 @dbus.service.signal(const_INT_PREFIX+".communication", |
0 | 60 signature='sss') |
61 def paramUpdate(self, name, value, namespace): | |
62 debug("param update signal: %s=%s in namespace %s", name, value, namespace) | |
63 | |
10 | 64 @dbus.service.signal(const_INT_PREFIX+".communication", |
0 | 65 signature='s') |
66 def contactDeleted(self, jid): | |
67 debug("contact deleted signal: %s", jid) | |
68 | |
10 | 69 @dbus.service.signal(const_INT_PREFIX+".request", |
0 | 70 signature='ssa{ss}') |
71 def askConfirmation(self, type, id, data): | |
72 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) | |
73 | |
74 | |
75 | |
76 ### methods ### | |
77 | |
10 | 78 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 79 in_signature='', out_signature='') |
80 def connect(self): | |
81 info ("Connection asked") | |
82 return self.cb["connect"]() | |
83 | |
10 | 84 @dbus.service.method(const_INT_PREFIX+".communication", |
1 | 85 in_signature='', out_signature='') |
86 def disconnect(self): | |
87 info ("Disconnection asked") | |
88 return self.cb["disconnect"]() | |
89 | |
10 | 90 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 91 in_signature='', out_signature='a(sa{ss}as)') |
92 def getContacts(self): | |
93 debug("getContacts...") | |
94 return self.cb["getContacts"]() | |
95 | |
10 | 96 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 97 in_signature='', out_signature='a(ssssi)') |
98 def getPresenceStatus(self): | |
99 debug("getPresenceStatus...") | |
100 return self.cb["getPresenceStatus"]() | |
101 | |
10 | 102 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 103 in_signature='ss', out_signature='') |
104 def sendMessage(self, to, message): | |
105 debug("sendMessage...") | |
106 self.cb["sendMessage"](to, message) | |
107 | |
10 | 108 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 109 in_signature='ssssi', out_signature='') |
110 def setPresence(self, to="", type="", show="", status="", priority=0): | |
111 self.cb["setPresence"](to, type, show, status, priority) | |
112 | |
10 | 113 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 114 in_signature='sss', out_signature='') |
115 def setParam(self, name, value, namespace="default"): | |
116 self.cb["setParam"](name, str(value), namespace) | |
117 | |
10 | 118 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 119 in_signature='ss', out_signature='(ss)') |
120 def getParam(self, name, namespace="default"): | |
121 return self.cb["getParam"](name, namespace) | |
122 | |
10 | 123 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 124 in_signature='s', out_signature='a(sss)') |
125 def getParams(self, namespace): | |
126 return self.cb["getParams"](namespace) | |
127 | |
10 | 128 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 129 in_signature='', out_signature='as') |
130 def getParamsCategories(self): | |
131 return self.cb["getParamsCategories"]() | |
132 | |
10 | 133 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 134 in_signature='ssi', out_signature='a{i(ss)}') |
135 def getHistory(self, from_jid, to_jid, size): | |
136 debug("History asked for %s", to_jid) | |
137 return self.cb["getHistory"](from_jid, to_jid, size) | |
138 | |
10 | 139 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 140 in_signature='s', out_signature='') |
141 def addContact(self, jid): | |
142 debug("Subscription asked for %s", jid) | |
143 return self.cb["addContact"](jid) | |
144 | |
10 | 145 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 146 in_signature='s', out_signature='') |
147 def delContact(self, jid): | |
148 debug("Unsubscription asked for %s", jid) | |
149 return self.cb["delContact"](jid) | |
150 | |
10 | 151 @dbus.service.method(const_INT_PREFIX+".communication", |
0 | 152 in_signature='', out_signature='b') |
153 def isConnected(self): | |
154 debug("Connection status requested") | |
155 return self.cb["isConnected"]() | |
156 | |
10 | 157 @dbus.service.method(const_INT_PREFIX+".request", |
0 | 158 in_signature='sba{ss}', out_signature='') |
159 def confirmationAnswer(self, id, accepted, data): | |
160 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") | |
161 return self.cb["confirmationAnswer"](id, accepted, data) | |
162 | |
10 | 163 @dbus.service.method(const_INT_PREFIX+".request", |
0 | 164 in_signature='s', out_signature='a{ss}') |
165 def getProgress(self, id): | |
166 #debug("Progress asked for %s", id) | |
167 return self.cb["getProgress"](id) | |
168 | |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
169 def _attribute_string(self, in_sign): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
170 i=0 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
171 idx=0 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
172 attr_string="" |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
173 while i<len(in_sign): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
174 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
175 raise Exception #FIXME: create an exception here (unmanaged attribute type) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
176 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
177 attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
178 idx+=1 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
179 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
180 if in_sign[i] == 'a': |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
181 while (True): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
182 i+=1 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
183 if i>=len(in_sign): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
184 raise Exception #FIXME: create an exception here (the '}' is not presend) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
185 if in_sign[i] == '}': |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
186 break |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
187 i+=1 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
188 return attr_string |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
189 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
190 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
191 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
192 def addMethod(self, name, int_suffix, in_sign, out_sign): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
193 """Dynamically add a method to Dbus Bridge""" |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
194 #FIXME: Better way ??? |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
195 attributes = self._attribute_string(in_sign) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
196 |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
197 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
198 exec (code) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
199 method = locals()[name] |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
200 setattr(DbusObject, name, dbus.service.method( |
10 | 201 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
202 |
0 | 203 class DBusBridge(Bridge): |
204 def __init__(self): | |
205 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
206 Bridge.__init__(self) | |
207 info ("Init DBus...") | |
208 self.session_bus = dbus.SessionBus() | |
10 | 209 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) |
0 | 210 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') |
211 | |
212 def newContact(self, contact, attributes, groups): | |
213 self.dbus_bridge.newContact(contact, attributes, groups) | |
214 | |
215 def newMessage(self,from_jid,msg,type='chat', to=''): | |
216 debug("sending message...") | |
217 self.dbus_bridge.newMessage(from_jid, msg, type, to) | |
218 | |
219 def presenceUpdate(self, jid, type, show, status, priority): | |
220 debug("updating presence for %s",jid) | |
221 self.dbus_bridge.presenceUpdate(jid, type, show, status, priority) | |
222 | |
223 def paramUpdate(self, name, value, namespace): | |
224 debug("updating param [%s] %s ", namespace, name) | |
225 self.dbus_bridge.paramUpdate(name, value, namespace) | |
226 | |
227 def contactDeleted(self, jid): | |
228 debug("sending contact deleted signal %s ", jid) | |
229 self.dbus_bridge.contactDeleted(jid) | |
230 | |
231 def askConfirmation(self, type, id, data): | |
232 self.dbus_bridge.askConfirmation(type, id, data) | |
233 | |
234 def register(self, name, callback): | |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
235 debug("registering DBus bridge method [%s]",name) |
0 | 236 self.dbus_bridge.register(name, callback) |
237 | |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
238 def addMethod(self, name, int_suffix, in_sign, out_sign, method): |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
239 """Dynamically add a method to Dbus Bridge""" |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
240 print ("Adding method [%s] to DBus bridge" % name) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
241 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
242 self.register(name, method) |
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
243 |