comparison sat/bridge/pb.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 787b15d16347
children 779351da2c13
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT: a jabber client 4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 6
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22
22 log = getLogger(__name__) 23 log = getLogger(__name__)
23 from twisted.spread import jelly, pb 24 from twisted.spread import jelly, pb
24 from twisted.internet import reactor 25 from twisted.internet import reactor
25 26
26 27
27 ## jelly hack 28 ## jelly hack
28 # we monkey patch jelly to handle namedtuple 29 # we monkey patch jelly to handle namedtuple
29 ori_jelly = jelly._Jellier.jelly 30 ori_jelly = jelly._Jellier.jelly
30 31
32
31 def fixed_jelly(self, obj): 33 def fixed_jelly(self, obj):
32 """this method fix handling of namedtuple""" 34 """this method fix handling of namedtuple"""
33 if isinstance(obj, tuple) and not obj is tuple: 35 if isinstance(obj, tuple) and not obj is tuple:
34 obj = tuple(obj) 36 obj = tuple(obj)
35 return ori_jelly(self, obj) 37 return ori_jelly(self, obj)
36 38
39
37 jelly._Jellier.jelly = fixed_jelly 40 jelly._Jellier.jelly = fixed_jelly
38 41
39 42
40 class PBRoot(pb.Root): 43 class PBRoot(pb.Root):
41
42 def __init__(self): 44 def __init__(self):
43 self.signals_handlers = [] 45 self.signals_handlers = []
44 46
45 def remote_initBridge(self, signals_handler): 47 def remote_initBridge(self, signals_handler):
46 self.signals_handlers.append(signals_handler) 48 self.signals_handlers.append(signals_handler)
47 log.info(u"registered signal handler") 49 log.info(u"registered signal handler")
48 50
49 def sendSignalEb(self, failure, signal_name): 51 def sendSignalEb(self, failure, signal_name):
50 log.error(u"Error while sending signal {name}: {msg}".format( 52 log.error(
51 name = signal_name, 53 u"Error while sending signal {name}: {msg}".format(
52 msg = failure, 54 name=signal_name, msg=failure
53 )) 55 )
56 )
54 57
55 def sendSignal(self, name, args, kwargs): 58 def sendSignal(self, name, args, kwargs):
56 to_remove = [] 59 to_remove = []
57 for handler in self.signals_handlers: 60 for handler in self.signals_handlers:
58 try: 61 try:
64 if to_remove: 67 if to_remove:
65 for handler in to_remove: 68 for handler in to_remove:
66 log.debug(u"Removing signal handler for dead frontend") 69 log.debug(u"Removing signal handler for dead frontend")
67 self.signals_handlers.remove(handler) 70 self.signals_handlers.remove(handler)
68 71
72
69 ##METHODS_PART## 73 ##METHODS_PART##
70 74
71 75
72 class Bridge(object): 76 class Bridge(object):
73
74 def __init__(self): 77 def __init__(self):
75 log.info("Init Perspective Broker...") 78 log.info("Init Perspective Broker...")
76 self.root = PBRoot() 79 self.root = PBRoot()
77 reactor.listenTCP(8789, pb.PBServerFactory(self.root)) 80 reactor.listenTCP(8789, pb.PBServerFactory(self.root))
78 81
83 self.signals_handlers.append(signals_handler) 86 self.signals_handlers.append(signals_handler)
84 log.info(u"registered signal handler") 87 log.info(u"registered signal handler")
85 88
86 def register_method(self, name, callback): 89 def register_method(self, name, callback):
87 log.debug("registering PB bridge method [%s]" % name) 90 log.debug("registering PB bridge method [%s]" % name)
88 setattr(self.root, "remote_"+name, callback) 91 setattr(self.root, "remote_" + name, callback)
89 # self.root.register_method(name, callback) 92 #  self.root.register_method(name, callback)
90 93
91 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): 94 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
92 """Dynamically add a method to PB Bridge""" 95 """Dynamically add a method to PB Bridge"""
93 #FIXME: doc parameter is kept only temporary, the time to remove it from calls 96 # FIXME: doc parameter is kept only temporary, the time to remove it from calls
94 log.debug("Adding method {name} to PB bridge".format(name=name)) 97 log.debug("Adding method {name} to PB bridge".format(name=name))
95 self.register_method(name, method) 98 self.register_method(name, method)
96 99
97 def addSignal(self, name, int_suffix, signature, doc={}): 100 def addSignal(self, name, int_suffix, signature, doc={}):
98 log.debug("Adding signal {name} to PB bridge".format(name=name)) 101 log.debug("Adding signal {name} to PB bridge".format(name=name))
99 setattr(self, name, lambda *args, **kwargs: self.sendSignal(name, *args, **kwargs)) 102 setattr(
103 self, name, lambda *args, **kwargs: self.sendSignal(name, *args, **kwargs)
104 )
100 105
101 def actionNew(self, action_data, id, security_limit, profile): 106 def actionNew(self, action_data, id, security_limit, profile):
102 self.sendSignal("actionNew", action_data, id, security_limit, profile) 107 self.sendSignal("actionNew", action_data, id, security_limit, profile)
103 108
104 def connected(self, profile, jid_s): 109 def connected(self, profile, jid_s):
111 self.sendSignal("disconnected", profile) 116 self.sendSignal("disconnected", profile)
112 117
113 def entityDataUpdated(self, jid, name, value, profile): 118 def entityDataUpdated(self, jid, name, value, profile):
114 self.sendSignal("entityDataUpdated", jid, name, value, profile) 119 self.sendSignal("entityDataUpdated", jid, name, value, profile)
115 120
116 def messageNew(self, uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile): 121 def messageNew(
117 self.sendSignal("messageNew", uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile) 122 self,
123 uid,
124 timestamp,
125 from_jid,
126 to_jid,
127 message,
128 subject,
129 mess_type,
130 extra,
131 profile,
132 ):
133 self.sendSignal(
134 "messageNew",
135 uid,
136 timestamp,
137 from_jid,
138 to_jid,
139 message,
140 subject,
141 mess_type,
142 extra,
143 profile,
144 )
118 145
119 def newContact(self, contact_jid, attributes, groups, profile): 146 def newContact(self, contact_jid, attributes, groups, profile):
120 self.sendSignal("newContact", contact_jid, attributes, groups, profile) 147 self.sendSignal("newContact", contact_jid, attributes, groups, profile)
121 148
122 def paramUpdate(self, name, value, category, profile): 149 def paramUpdate(self, name, value, category, profile):