comparison sat_frontends/bridge/pb.py @ 2579:787b15d16347

bridge: added generated pb files to repository (needed for Cagou on Android)
author Goffi <goffi@goffi.org>
date Fri, 13 Apr 2018 18:45:43 +0200
parents
children 973d4551ffae
comparison
equal deleted inserted replaced
2578:bf1b12a8f597 2579:787b15d16347
1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*-
3
4 # SAT communication bridge
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.log import getLogger
21 log = getLogger(__name__)
22 from sat.core import exceptions
23 from twisted.spread import pb
24 from twisted.internet import reactor
25
26
27 class SignalsHandler(pb.Referenceable):
28
29 def __getattr__(self, name):
30 if name.startswith("remote_"):
31 log.debug(u"calling an unregistered signal: {name}".format(
32 name = name[7:]))
33 return lambda *args, **kwargs: None
34
35 else:
36 raise AttributeError(name)
37
38 def register_signal(self, name, handler, iface="core"):
39 log.debug("registering signal {name}".format(name=name))
40 method_name = "remote_" + name
41 try:
42 self.__getattribute__(self, method_name)
43 except AttributeError:
44 pass
45 else:
46 raise exceptions.InternalError(u"{name} signal handler has been registered twice".format(
47 name = method_name))
48 setattr(self, method_name, handler)
49
50
51 class Bridge(object):
52
53 def __init__(self):
54 self.signals_handler = SignalsHandler()
55
56 def __getattr__(self, name):
57 return lambda *args, **kwargs: self.call(name, args, kwargs)
58
59 def remoteCallback(self, result, callback):
60 """call callback with argument or None
61
62 if result is not None not argument is used,
63 else result is used as argument
64 @param result: remote call result
65 @param callback(callable): method to call on result
66 """
67 if result is None:
68 callback()
69 else:
70 callback(result)
71
72 def call(self, name, args, kwargs):
73 """call a remote method
74
75 @param name(str): name of the bridge method
76 @param args(list): arguments
77 may contain callback and errback as last 2 items
78 @param kwargs(dict): keyword arguments
79 may contain callback and errback
80 """
81 callback = errback = None
82 if kwargs:
83 try:
84 callback = kwargs.pop('callback')
85 except KeyError:
86 pass
87 try:
88 errback = kwargs.pop('errback')
89 except KeyError:
90 pass
91 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]):
92 errback = args.pop()
93 callback = args.pop()
94 d = self.root.callRemote(name, *args, **kwargs)
95 if callback is not None:
96 d.addCallback(self.remoteCallback, callback)
97 if errback is not None:
98 d.addErrback(errback)
99
100 def _initBridgeEb(self, failure):
101 log.error(u"Can't init bridge: {msg}".format(msg=failure))
102
103 def _set_root(self, root):
104 """set remote root object
105
106 bridge will then be initialised
107 """
108 self.root = root
109 d = root.callRemote("initBridge", self.signals_handler)
110 d.addErrback(self._initBridgeEb)
111 return d
112
113 def _generic_errback(self, failure):
114 log.error(u"bridge failure: {}".format(failure))
115
116 def bridgeConnect(self, callback, errback):
117 factory = pb.PBClientFactory()
118 reactor.connectTCP("localhost", 8789, factory)
119 d = factory.getRootObject()
120 d.addCallback(self._set_root)
121 d.addCallback(lambda dummy: callback())
122 d.addErrback(errback)
123
124 def register_signal(self, functionName, handler, iface="core"):
125 self.signals_handler.register_signal(functionName, handler, iface)
126
127 def actionsGet(self, profile_key="@DEFAULT@", callback=None, errback=None):
128 d = self.root.callRemote("actionsGet", profile_key)
129 if callback is not None:
130 d.addCallback(callback)
131 if errback is None:
132 errback = self._generic_errback
133 d.addErrback(errback)
134
135 def addContact(self, entity_jid, profile_key="@DEFAULT@", callback=None, errback=None):
136 d = self.root.callRemote("addContact", entity_jid, profile_key)
137 if callback is not None:
138 d.addCallback(lambda dummy: callback())
139 if errback is None:
140 errback = self._generic_errback
141 d.addErrback(errback)
142
143 def asyncDeleteProfile(self, profile, callback=None, errback=None):
144 d = self.root.callRemote("asyncDeleteProfile", profile)
145 if callback is not None:
146 d.addCallback(lambda dummy: callback())
147 if errback is None:
148 errback = self._generic_errback
149 d.addErrback(errback)
150
151 def asyncGetParamA(self, name, category, attribute="value", security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
152 d = self.root.callRemote("asyncGetParamA", name, category, attribute, security_limit, profile_key)
153 if callback is not None:
154 d.addCallback(callback)
155 if errback is None:
156 errback = self._generic_errback
157 d.addErrback(errback)
158
159 def asyncGetParamsValuesFromCategory(self, category, security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
160 d = self.root.callRemote("asyncGetParamsValuesFromCategory", category, security_limit, profile_key)
161 if callback is not None:
162 d.addCallback(callback)
163 if errback is None:
164 errback = self._generic_errback
165 d.addErrback(errback)
166
167 def connect(self, profile_key="@DEFAULT@", password='', options={}, callback=None, errback=None):
168 d = self.root.callRemote("connect", profile_key, password, options)
169 if callback is not None:
170 d.addCallback(callback)
171 if errback is None:
172 errback = self._generic_errback
173 d.addErrback(errback)
174
175 def delContact(self, entity_jid, profile_key="@DEFAULT@", callback=None, errback=None):
176 d = self.root.callRemote("delContact", entity_jid, profile_key)
177 if callback is not None:
178 d.addCallback(lambda dummy: callback())
179 if errback is None:
180 errback = self._generic_errback
181 d.addErrback(errback)
182
183 def discoFindByFeatures(self, namespaces, identities, bare_jid=False, service=True, roster=True, own_jid=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
184 d = self.root.callRemote("discoFindByFeatures", namespaces, identities, bare_jid, service, roster, own_jid, profile_key)
185 if callback is not None:
186 d.addCallback(callback)
187 if errback is None:
188 errback = self._generic_errback
189 d.addErrback(errback)
190
191 def discoInfos(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
192 d = self.root.callRemote("discoInfos", entity_jid, node, use_cache, profile_key)
193 if callback is not None:
194 d.addCallback(callback)
195 if errback is None:
196 errback = self._generic_errback
197 d.addErrback(errback)
198
199 def discoItems(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
200 d = self.root.callRemote("discoItems", entity_jid, node, use_cache, profile_key)
201 if callback is not None:
202 d.addCallback(callback)
203 if errback is None:
204 errback = self._generic_errback
205 d.addErrback(errback)
206
207 def disconnect(self, profile_key="@DEFAULT@", callback=None, errback=None):
208 d = self.root.callRemote("disconnect", profile_key)
209 if callback is not None:
210 d.addCallback(lambda dummy: callback())
211 if errback is None:
212 errback = self._generic_errback
213 d.addErrback(errback)
214
215 def getConfig(self, section, name, callback=None, errback=None):
216 d = self.root.callRemote("getConfig", section, name)
217 if callback is not None:
218 d.addCallback(callback)
219 if errback is None:
220 errback = self._generic_errback
221 d.addErrback(errback)
222
223 def getContacts(self, profile_key="@DEFAULT@", callback=None, errback=None):
224 d = self.root.callRemote("getContacts", profile_key)
225 if callback is not None:
226 d.addCallback(callback)
227 if errback is None:
228 errback = self._generic_errback
229 d.addErrback(errback)
230
231 def getContactsFromGroup(self, group, profile_key="@DEFAULT@", callback=None, errback=None):
232 d = self.root.callRemote("getContactsFromGroup", group, profile_key)
233 if callback is not None:
234 d.addCallback(callback)
235 if errback is None:
236 errback = self._generic_errback
237 d.addErrback(errback)
238
239 def getEntitiesData(self, jids, keys, profile, callback=None, errback=None):
240 d = self.root.callRemote("getEntitiesData", jids, keys, profile)
241 if callback is not None:
242 d.addCallback(callback)
243 if errback is None:
244 errback = self._generic_errback
245 d.addErrback(errback)
246
247 def getEntityData(self, jid, keys, profile, callback=None, errback=None):
248 d = self.root.callRemote("getEntityData", jid, keys, profile)
249 if callback is not None:
250 d.addCallback(callback)
251 if errback is None:
252 errback = self._generic_errback
253 d.addErrback(errback)
254
255 def getFeatures(self, profile_key, callback=None, errback=None):
256 d = self.root.callRemote("getFeatures", profile_key)
257 if callback is not None:
258 d.addCallback(callback)
259 if errback is None:
260 errback = self._generic_errback
261 d.addErrback(errback)
262
263 def getMainResource(self, contact_jid, profile_key="@DEFAULT@", callback=None, errback=None):
264 d = self.root.callRemote("getMainResource", contact_jid, profile_key)
265 if callback is not None:
266 d.addCallback(callback)
267 if errback is None:
268 errback = self._generic_errback
269 d.addErrback(errback)
270
271 def getParamA(self, name, category, attribute="value", profile_key="@DEFAULT@", callback=None, errback=None):
272 d = self.root.callRemote("getParamA", name, category, attribute, profile_key)
273 if callback is not None:
274 d.addCallback(callback)
275 if errback is None:
276 errback = self._generic_errback
277 d.addErrback(errback)
278
279 def getParamsCategories(self, callback=None, errback=None):
280 d = self.root.callRemote("getParamsCategories")
281 if callback is not None:
282 d.addCallback(callback)
283 if errback is None:
284 errback = self._generic_errback
285 d.addErrback(errback)
286
287 def getParamsUI(self, security_limit=-1, app='', profile_key="@DEFAULT@", callback=None, errback=None):
288 d = self.root.callRemote("getParamsUI", security_limit, app, profile_key)
289 if callback is not None:
290 d.addCallback(callback)
291 if errback is None:
292 errback = self._generic_errback
293 d.addErrback(errback)
294
295 def getPresenceStatuses(self, profile_key="@DEFAULT@", callback=None, errback=None):
296 d = self.root.callRemote("getPresenceStatuses", profile_key)
297 if callback is not None:
298 d.addCallback(callback)
299 if errback is None:
300 errback = self._generic_errback
301 d.addErrback(errback)
302
303 def getReady(self, callback=None, errback=None):
304 d = self.root.callRemote("getReady")
305 if callback is not None:
306 d.addCallback(lambda dummy: callback())
307 if errback is None:
308 errback = self._generic_errback
309 d.addErrback(errback)
310
311 def getVersion(self, callback=None, errback=None):
312 d = self.root.callRemote("getVersion")
313 if callback is not None:
314 d.addCallback(callback)
315 if errback is None:
316 errback = self._generic_errback
317 d.addErrback(errback)
318
319 def getWaitingSub(self, profile_key="@DEFAULT@", callback=None, errback=None):
320 d = self.root.callRemote("getWaitingSub", profile_key)
321 if callback is not None:
322 d.addCallback(callback)
323 if errback is None:
324 errback = self._generic_errback
325 d.addErrback(errback)
326
327 def historyGet(self, from_jid, to_jid, limit, between=True, filters='', profile="@NONE@", callback=None, errback=None):
328 d = self.root.callRemote("historyGet", from_jid, to_jid, limit, between, filters, profile)
329 if callback is not None:
330 d.addCallback(callback)
331 if errback is None:
332 errback = self._generic_errback
333 d.addErrback(errback)
334
335 def isConnected(self, profile_key="@DEFAULT@", callback=None, errback=None):
336 d = self.root.callRemote("isConnected", profile_key)
337 if callback is not None:
338 d.addCallback(callback)
339 if errback is None:
340 errback = self._generic_errback
341 d.addErrback(errback)
342
343 def launchAction(self, callback_id, data, profile_key="@DEFAULT@", callback=None, errback=None):
344 d = self.root.callRemote("launchAction", callback_id, data, profile_key)
345 if callback is not None:
346 d.addCallback(callback)
347 if errback is None:
348 errback = self._generic_errback
349 d.addErrback(errback)
350
351 def loadParamsTemplate(self, filename, callback=None, errback=None):
352 d = self.root.callRemote("loadParamsTemplate", filename)
353 if callback is not None:
354 d.addCallback(callback)
355 if errback is None:
356 errback = self._generic_errback
357 d.addErrback(errback)
358
359 def menuHelpGet(self, menu_id, language, callback=None, errback=None):
360 d = self.root.callRemote("menuHelpGet", menu_id, language)
361 if callback is not None:
362 d.addCallback(callback)
363 if errback is None:
364 errback = self._generic_errback
365 d.addErrback(errback)
366
367 def menuLaunch(self, menu_type, path, data, security_limit, profile_key, callback=None, errback=None):
368 d = self.root.callRemote("menuLaunch", menu_type, path, data, security_limit, profile_key)
369 if callback is not None:
370 d.addCallback(callback)
371 if errback is None:
372 errback = self._generic_errback
373 d.addErrback(errback)
374
375 def menusGet(self, language, security_limit, callback=None, errback=None):
376 d = self.root.callRemote("menusGet", language, security_limit)
377 if callback is not None:
378 d.addCallback(callback)
379 if errback is None:
380 errback = self._generic_errback
381 d.addErrback(errback)
382
383 def messageSend(self, to_jid, message, subject={}, mess_type="auto", extra={}, profile_key="@NONE@", callback=None, errback=None):
384 d = self.root.callRemote("messageSend", to_jid, message, subject, mess_type, extra, profile_key)
385 if callback is not None:
386 d.addCallback(lambda dummy: callback())
387 if errback is None:
388 errback = self._generic_errback
389 d.addErrback(errback)
390
391 def namespacesGet(self, callback=None, errback=None):
392 d = self.root.callRemote("namespacesGet")
393 if callback is not None:
394 d.addCallback(callback)
395 if errback is None:
396 errback = self._generic_errback
397 d.addErrback(errback)
398
399 def paramsRegisterApp(self, xml, security_limit=-1, app='', callback=None, errback=None):
400 d = self.root.callRemote("paramsRegisterApp", xml, security_limit, app)
401 if callback is not None:
402 d.addCallback(lambda dummy: callback())
403 if errback is None:
404 errback = self._generic_errback
405 d.addErrback(errback)
406
407 def profileCreate(self, profile, password='', component='', callback=None, errback=None):
408 d = self.root.callRemote("profileCreate", profile, password, component)
409 if callback is not None:
410 d.addCallback(lambda dummy: callback())
411 if errback is None:
412 errback = self._generic_errback
413 d.addErrback(errback)
414
415 def profileIsSessionStarted(self, profile_key="@DEFAULT@", callback=None, errback=None):
416 d = self.root.callRemote("profileIsSessionStarted", profile_key)
417 if callback is not None:
418 d.addCallback(callback)
419 if errback is None:
420 errback = self._generic_errback
421 d.addErrback(errback)
422
423 def profileNameGet(self, profile_key="@DEFAULT@", callback=None, errback=None):
424 d = self.root.callRemote("profileNameGet", profile_key)
425 if callback is not None:
426 d.addCallback(callback)
427 if errback is None:
428 errback = self._generic_errback
429 d.addErrback(errback)
430
431 def profileSetDefault(self, profile, callback=None, errback=None):
432 d = self.root.callRemote("profileSetDefault", profile)
433 if callback is not None:
434 d.addCallback(lambda dummy: callback())
435 if errback is None:
436 errback = self._generic_errback
437 d.addErrback(errback)
438
439 def profileStartSession(self, password='', profile_key="@DEFAULT@", callback=None, errback=None):
440 d = self.root.callRemote("profileStartSession", password, profile_key)
441 if callback is not None:
442 d.addCallback(callback)
443 if errback is None:
444 errback = self._generic_errback
445 d.addErrback(errback)
446
447 def profilesListGet(self, clients=True, components=False, callback=None, errback=None):
448 d = self.root.callRemote("profilesListGet", clients, components)
449 if callback is not None:
450 d.addCallback(callback)
451 if errback is None:
452 errback = self._generic_errback
453 d.addErrback(errback)
454
455 def progressGet(self, id, profile, callback=None, errback=None):
456 d = self.root.callRemote("progressGet", id, profile)
457 if callback is not None:
458 d.addCallback(callback)
459 if errback is None:
460 errback = self._generic_errback
461 d.addErrback(errback)
462
463 def progressGetAll(self, profile, callback=None, errback=None):
464 d = self.root.callRemote("progressGetAll", profile)
465 if callback is not None:
466 d.addCallback(callback)
467 if errback is None:
468 errback = self._generic_errback
469 d.addErrback(errback)
470
471 def progressGetAllMetadata(self, profile, callback=None, errback=None):
472 d = self.root.callRemote("progressGetAllMetadata", profile)
473 if callback is not None:
474 d.addCallback(callback)
475 if errback is None:
476 errback = self._generic_errback
477 d.addErrback(errback)
478
479 def saveParamsTemplate(self, filename, callback=None, errback=None):
480 d = self.root.callRemote("saveParamsTemplate", filename)
481 if callback is not None:
482 d.addCallback(callback)
483 if errback is None:
484 errback = self._generic_errback
485 d.addErrback(errback)
486
487 def sessionInfosGet(self, profile_key, callback=None, errback=None):
488 d = self.root.callRemote("sessionInfosGet", profile_key)
489 if callback is not None:
490 d.addCallback(callback)
491 if errback is None:
492 errback = self._generic_errback
493 d.addErrback(errback)
494
495 def setParam(self, name, value, category, security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
496 d = self.root.callRemote("setParam", name, value, category, security_limit, profile_key)
497 if callback is not None:
498 d.addCallback(lambda dummy: callback())
499 if errback is None:
500 errback = self._generic_errback
501 d.addErrback(errback)
502
503 def setPresence(self, to_jid='', show='', statuses={}, profile_key="@DEFAULT@", callback=None, errback=None):
504 d = self.root.callRemote("setPresence", to_jid, show, statuses, profile_key)
505 if callback is not None:
506 d.addCallback(lambda dummy: callback())
507 if errback is None:
508 errback = self._generic_errback
509 d.addErrback(errback)
510
511 def subscription(self, sub_type, entity, profile_key="@DEFAULT@", callback=None, errback=None):
512 d = self.root.callRemote("subscription", sub_type, entity, profile_key)
513 if callback is not None:
514 d.addCallback(lambda dummy: callback())
515 if errback is None:
516 errback = self._generic_errback
517 d.addErrback(errback)
518
519 def updateContact(self, entity_jid, name, groups, profile_key="@DEFAULT@", callback=None, errback=None):
520 d = self.root.callRemote("updateContact", entity_jid, name, groups, profile_key)
521 if callback is not None:
522 d.addCallback(lambda dummy: callback())
523 if errback is None:
524 errback = self._generic_errback
525 d.addErrback(errback)