comparison 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
comparison
equal deleted inserted replaced
9:63ab3d8058f4 10:14d7861ca59e
25 import dbus.service 25 import dbus.service
26 import dbus.mainloop.glib 26 import dbus.mainloop.glib
27 import pdb 27 import pdb
28 from logging import debug, info, error 28 from logging import debug, info, error
29 29
30 CONST_INT_PREFIX = "org.goffi.SAT" #Interface prefix 30 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
31 31
32 class DbusObject(dbus.service.Object): 32 class DbusObject(dbus.service.Object):
33 33
34 def __init__(self, bus, path): 34 def __init__(self, bus, path):
35 dbus.service.Object.__init__(self, bus, path) 35 dbus.service.Object.__init__(self, bus, path)
39 def register(self, name, cb): 39 def register(self, name, cb):
40 self.cb[name]=cb 40 self.cb[name]=cb
41 41
42 ### signals ### 42 ### signals ###
43 43
44 @dbus.service.signal(CONST_INT_PREFIX+".communication", 44 @dbus.service.signal(const_INT_PREFIX+".communication",
45 signature='sa{ss}as') 45 signature='sa{ss}as')
46 def newContact(self, contact, attributes, groups): 46 def newContact(self, contact, attributes, groups):
47 debug("new contact signal (%s) sended", contact) 47 debug("new contact signal (%s) sended", contact)
48 48
49 @dbus.service.signal(CONST_INT_PREFIX+".communication", 49 @dbus.service.signal(const_INT_PREFIX+".communication",
50 signature='ssss') 50 signature='ssss')
51 def newMessage(self, from_jid, msg, type='chat', to=''): 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) 52 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to)
53 53
54 @dbus.service.signal(CONST_INT_PREFIX+".communication", 54 @dbus.service.signal(const_INT_PREFIX+".communication",
55 signature='ssssi') 55 signature='ssssi')
56 def presenceUpdate(self, jid, type, show, status, priority): 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) 57 debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority)
58 58
59 @dbus.service.signal(CONST_INT_PREFIX+".communication", 59 @dbus.service.signal(const_INT_PREFIX+".communication",
60 signature='sss') 60 signature='sss')
61 def paramUpdate(self, name, value, namespace): 61 def paramUpdate(self, name, value, namespace):
62 debug("param update signal: %s=%s in namespace %s", name, value, namespace) 62 debug("param update signal: %s=%s in namespace %s", name, value, namespace)
63 63
64 @dbus.service.signal(CONST_INT_PREFIX+".communication", 64 @dbus.service.signal(const_INT_PREFIX+".communication",
65 signature='s') 65 signature='s')
66 def contactDeleted(self, jid): 66 def contactDeleted(self, jid):
67 debug("contact deleted signal: %s", jid) 67 debug("contact deleted signal: %s", jid)
68 68
69 @dbus.service.signal(CONST_INT_PREFIX+".request", 69 @dbus.service.signal(const_INT_PREFIX+".request",
70 signature='ssa{ss}') 70 signature='ssa{ss}')
71 def askConfirmation(self, type, id, data): 71 def askConfirmation(self, type, id, data):
72 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) 72 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data)
73 73
74 74
75 75
76 ### methods ### 76 ### methods ###
77 77
78 @dbus.service.method(CONST_INT_PREFIX+".communication", 78 @dbus.service.method(const_INT_PREFIX+".communication",
79 in_signature='', out_signature='') 79 in_signature='', out_signature='')
80 def connect(self): 80 def connect(self):
81 info ("Connection asked") 81 info ("Connection asked")
82 return self.cb["connect"]() 82 return self.cb["connect"]()
83 83
84 @dbus.service.method(CONST_INT_PREFIX+".communication", 84 @dbus.service.method(const_INT_PREFIX+".communication",
85 in_signature='', out_signature='') 85 in_signature='', out_signature='')
86 def disconnect(self): 86 def disconnect(self):
87 info ("Disconnection asked") 87 info ("Disconnection asked")
88 return self.cb["disconnect"]() 88 return self.cb["disconnect"]()
89 89
90 @dbus.service.method(CONST_INT_PREFIX+".communication", 90 @dbus.service.method(const_INT_PREFIX+".communication",
91 in_signature='', out_signature='a(sa{ss}as)') 91 in_signature='', out_signature='a(sa{ss}as)')
92 def getContacts(self): 92 def getContacts(self):
93 debug("getContacts...") 93 debug("getContacts...")
94 return self.cb["getContacts"]() 94 return self.cb["getContacts"]()
95 95
96 @dbus.service.method(CONST_INT_PREFIX+".communication", 96 @dbus.service.method(const_INT_PREFIX+".communication",
97 in_signature='', out_signature='a(ssssi)') 97 in_signature='', out_signature='a(ssssi)')
98 def getPresenceStatus(self): 98 def getPresenceStatus(self):
99 debug("getPresenceStatus...") 99 debug("getPresenceStatus...")
100 return self.cb["getPresenceStatus"]() 100 return self.cb["getPresenceStatus"]()
101 101
102 @dbus.service.method(CONST_INT_PREFIX+".communication", 102 @dbus.service.method(const_INT_PREFIX+".communication",
103 in_signature='ss', out_signature='') 103 in_signature='ss', out_signature='')
104 def sendMessage(self, to, message): 104 def sendMessage(self, to, message):
105 debug("sendMessage...") 105 debug("sendMessage...")
106 self.cb["sendMessage"](to, message) 106 self.cb["sendMessage"](to, message)
107 107
108 @dbus.service.method(CONST_INT_PREFIX+".communication", 108 @dbus.service.method(const_INT_PREFIX+".communication",
109 in_signature='ssssi', out_signature='') 109 in_signature='ssssi', out_signature='')
110 def setPresence(self, to="", type="", show="", status="", priority=0): 110 def setPresence(self, to="", type="", show="", status="", priority=0):
111 self.cb["setPresence"](to, type, show, status, priority) 111 self.cb["setPresence"](to, type, show, status, priority)
112 112
113 @dbus.service.method(CONST_INT_PREFIX+".communication", 113 @dbus.service.method(const_INT_PREFIX+".communication",
114 in_signature='sss', out_signature='') 114 in_signature='sss', out_signature='')
115 def setParam(self, name, value, namespace="default"): 115 def setParam(self, name, value, namespace="default"):
116 self.cb["setParam"](name, str(value), namespace) 116 self.cb["setParam"](name, str(value), namespace)
117 117
118 @dbus.service.method(CONST_INT_PREFIX+".communication", 118 @dbus.service.method(const_INT_PREFIX+".communication",
119 in_signature='ss', out_signature='(ss)') 119 in_signature='ss', out_signature='(ss)')
120 def getParam(self, name, namespace="default"): 120 def getParam(self, name, namespace="default"):
121 return self.cb["getParam"](name, namespace) 121 return self.cb["getParam"](name, namespace)
122 122
123 @dbus.service.method(CONST_INT_PREFIX+".communication", 123 @dbus.service.method(const_INT_PREFIX+".communication",
124 in_signature='s', out_signature='a(sss)') 124 in_signature='s', out_signature='a(sss)')
125 def getParams(self, namespace): 125 def getParams(self, namespace):
126 return self.cb["getParams"](namespace) 126 return self.cb["getParams"](namespace)
127 127
128 @dbus.service.method(CONST_INT_PREFIX+".communication", 128 @dbus.service.method(const_INT_PREFIX+".communication",
129 in_signature='', out_signature='as') 129 in_signature='', out_signature='as')
130 def getParamsCategories(self): 130 def getParamsCategories(self):
131 return self.cb["getParamsCategories"]() 131 return self.cb["getParamsCategories"]()
132 132
133 @dbus.service.method(CONST_INT_PREFIX+".communication", 133 @dbus.service.method(const_INT_PREFIX+".communication",
134 in_signature='ssi', out_signature='a{i(ss)}') 134 in_signature='ssi', out_signature='a{i(ss)}')
135 def getHistory(self, from_jid, to_jid, size): 135 def getHistory(self, from_jid, to_jid, size):
136 debug("History asked for %s", to_jid) 136 debug("History asked for %s", to_jid)
137 return self.cb["getHistory"](from_jid, to_jid, size) 137 return self.cb["getHistory"](from_jid, to_jid, size)
138 138
139 @dbus.service.method(CONST_INT_PREFIX+".communication", 139 @dbus.service.method(const_INT_PREFIX+".communication",
140 in_signature='s', out_signature='') 140 in_signature='s', out_signature='')
141 def addContact(self, jid): 141 def addContact(self, jid):
142 debug("Subscription asked for %s", jid) 142 debug("Subscription asked for %s", jid)
143 return self.cb["addContact"](jid) 143 return self.cb["addContact"](jid)
144 144
145 @dbus.service.method(CONST_INT_PREFIX+".communication", 145 @dbus.service.method(const_INT_PREFIX+".communication",
146 in_signature='s', out_signature='') 146 in_signature='s', out_signature='')
147 def delContact(self, jid): 147 def delContact(self, jid):
148 debug("Unsubscription asked for %s", jid) 148 debug("Unsubscription asked for %s", jid)
149 return self.cb["delContact"](jid) 149 return self.cb["delContact"](jid)
150 150
151 @dbus.service.method(CONST_INT_PREFIX+".communication", 151 @dbus.service.method(const_INT_PREFIX+".communication",
152 in_signature='', out_signature='b') 152 in_signature='', out_signature='b')
153 def isConnected(self): 153 def isConnected(self):
154 debug("Connection status requested") 154 debug("Connection status requested")
155 return self.cb["isConnected"]() 155 return self.cb["isConnected"]()
156 156
157 @dbus.service.method(CONST_INT_PREFIX+".request", 157 @dbus.service.method(const_INT_PREFIX+".request",
158 in_signature='sba{ss}', out_signature='') 158 in_signature='sba{ss}', out_signature='')
159 def confirmationAnswer(self, id, accepted, data): 159 def confirmationAnswer(self, id, accepted, data):
160 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") 160 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused")
161 return self.cb["confirmationAnswer"](id, accepted, data) 161 return self.cb["confirmationAnswer"](id, accepted, data)
162 162
163 @dbus.service.method(CONST_INT_PREFIX+".request", 163 @dbus.service.method(const_INT_PREFIX+".request",
164 in_signature='s', out_signature='a{ss}') 164 in_signature='s', out_signature='a{ss}')
165 def getProgress(self, id): 165 def getProgress(self, id):
166 #debug("Progress asked for %s", id) 166 #debug("Progress asked for %s", id)
167 return self.cb["getProgress"](id) 167 return self.cb["getProgress"](id)
168 168
196 196
197 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') 197 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
198 exec (code) 198 exec (code)
199 method = locals()[name] 199 method = locals()[name]
200 setattr(DbusObject, name, dbus.service.method( 200 setattr(DbusObject, name, dbus.service.method(
201 CONST_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) 201 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method))
202 202
203 class DBusBridge(Bridge): 203 class DBusBridge(Bridge):
204 def __init__(self): 204 def __init__(self):
205 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 205 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
206 Bridge.__init__(self) 206 Bridge.__init__(self)
207 info ("Init DBus...") 207 info ("Init DBus...")
208 self.session_bus = dbus.SessionBus() 208 self.session_bus = dbus.SessionBus()
209 self.dbus_name = dbus.service.BusName(CONST_INT_PREFIX, self.session_bus) 209 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
210 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') 210 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')
211 211
212 def newContact(self, contact, attributes, groups): 212 def newContact(self, contact, attributes, groups):
213 self.dbus_bridge.newContact(contact, attributes, groups) 213 self.dbus_bridge.newContact(contact, attributes, groups)
214 214