comparison sat_frontends/jp/cmd_pipe.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
21 21
22 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
23 import sys 23 import sys
24 from sat.core.i18n import _ 24 from sat.core.i18n import _
25 from sat_frontends.tools import jid 25 from sat_frontends.tools import jid
26 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI 26 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
27 from functools import partial 27 from functools import partial
28 import socket 28 import socket
29 import SocketServer 29 import SocketServer
30 import errno 30 import errno
31 31
32 __commands__ = ["Pipe"] 32 __commands__ = ["Pipe"]
33 33
34 START_PORT = 9999 34 START_PORT = 9999
35 35
36
36 class PipeOut(base.CommandBase): 37 class PipeOut(base.CommandBase):
37
38 def __init__(self, host): 38 def __init__(self, host):
39 super(PipeOut, self).__init__(host, 'out', help=_('send a pipe a stream')) 39 super(PipeOut, self).__init__(host, "out", help=_("send a pipe a stream"))
40 self.need_loop = True 40 self.need_loop = True
41 41
42 def add_parser_options(self): 42 def add_parser_options(self):
43 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid")) 43 self.parser.add_argument(
44 "jid", type=base.unicode_decoder, help=_("the destination jid")
45 )
44 46
45 def streamOutCb(self, port): 47 def streamOutCb(self, port):
46 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 48 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47 s.connect(('127.0.0.1', int(port))) 49 s.connect(("127.0.0.1", int(port)))
48 while True: 50 while True:
49 buf = sys.stdin.read(4096) 51 buf = sys.stdin.read(4096)
50 if not buf: 52 if not buf:
51 break 53 break
52 try: 54 try:
53 s.sendall(buf) 55 s.sendall(buf)
54 except socket.error as e: 56 except socket.error as e:
55 if e.errno == errno.EPIPE: 57 if e.errno == errno.EPIPE:
56 sys.stderr.write(str(e) + '\n') 58 sys.stderr.write(str(e) + "\n")
57 self.host.quit(1) 59 self.host.quit(1)
58 else: 60 else:
59 raise e 61 raise e
60 self.host.quit() 62 self.host.quit()
61 63
63 """ Create named pipe, and send stdin to it """ 65 """ Create named pipe, and send stdin to it """
64 self.host.bridge.streamOut( 66 self.host.bridge.streamOut(
65 self.host.get_full_jid(self.args.jid), 67 self.host.get_full_jid(self.args.jid),
66 self.profile, 68 self.profile,
67 callback=self.streamOutCb, 69 callback=self.streamOutCb,
68 errback=partial(self.errback, 70 errback=partial(
69 msg=_(u"can't start stream: {}"), 71 self.errback,
70 exit_code=C.EXIT_BRIDGE_ERRBACK)) 72 msg=_(u"can't start stream: {}"),
73 exit_code=C.EXIT_BRIDGE_ERRBACK,
74 ),
75 )
71 76
72 77
73 class StreamServer(SocketServer.BaseRequestHandler): 78 class StreamServer(SocketServer.BaseRequestHandler):
74
75 def handle(self): 79 def handle(self):
76 while True: 80 while True:
77 data = self.request.recv(4096) 81 data = self.request.recv(4096)
78 if not data: 82 if not data:
79 break 83 break
80 sys.stdout.write(data) 84 sys.stdout.write(data)
81 try: 85 try:
82 sys.stdout.flush() 86 sys.stdout.flush()
83 except IOError as e: 87 except IOError as e:
84 sys.stderr.write(str(e) + '\n') 88 sys.stderr.write(str(e) + "\n")
85 break 89 break
86 # calling shutdown will do a deadlock as we don't use separate thread 90 #  calling shutdown will do a deadlock as we don't use separate thread
87 # this is a workaround (cf. https://stackoverflow.com/a/36017741) 91 # this is a workaround (cf. https://stackoverflow.com/a/36017741)
88 self.server._BaseServer__shutdown_request = True 92 self.server._BaseServer__shutdown_request = True
89 93
90 94
91 class PipeIn(base.CommandAnswering): 95 class PipeIn(base.CommandAnswering):
92
93 def __init__(self, host): 96 def __init__(self, host):
94 super(PipeIn, self).__init__(host, 'in', help=_('receive a pipe stream')) 97 super(PipeIn, self).__init__(host, "in", help=_("receive a pipe stream"))
95 self.action_callbacks = {"STREAM": self.onStreamAction} 98 self.action_callbacks = {"STREAM": self.onStreamAction}
96 99
97 def add_parser_options(self): 100 def add_parser_options(self):
98 self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_('Jids accepted (none means "accept everything")')) 101 self.parser.add_argument(
102 "jids",
103 type=base.unicode_decoder,
104 nargs="*",
105 help=_('Jids accepted (none means "accept everything")'),
106 )
99 107
100 def getXmluiId(self, action_data): 108 def getXmluiId(self, action_data):
101 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module 109 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
102 # should be available in the future 110 # should be available in the future
103 # TODO: XMLUI module 111 # TODO: XMLUI module
104 try: 112 try:
105 xml_ui = action_data['xmlui'] 113 xml_ui = action_data["xmlui"]
106 except KeyError: 114 except KeyError:
107 self.disp(_(u"Action has no XMLUI"), 1) 115 self.disp(_(u"Action has no XMLUI"), 1)
108 else: 116 else:
109 ui = ET.fromstring(xml_ui.encode('utf-8')) 117 ui = ET.fromstring(xml_ui.encode("utf-8"))
110 xmlui_id = ui.get('submit') 118 xmlui_id = ui.get("submit")
111 if not xmlui_id: 119 if not xmlui_id:
112 self.disp(_(u"Invalid XMLUI received"), error=True) 120 self.disp(_(u"Invalid XMLUI received"), error=True)
113 return xmlui_id 121 return xmlui_id
114 122
115 def onStreamAction(self, action_data, action_id, security_limit, profile): 123 def onStreamAction(self, action_data, action_id, security_limit, profile):
116 xmlui_id = self.getXmluiId(action_data) 124 xmlui_id = self.getXmluiId(action_data)
117 if xmlui_id is None: 125 if xmlui_id is None:
118 return self.host.quitFromSignal(1) 126 return self.host.quitFromSignal(1)
119 try: 127 try:
120 from_jid = jid.JID(action_data['meta_from_jid']) 128 from_jid = jid.JID(action_data["meta_from_jid"])
121 except KeyError: 129 except KeyError:
122 self.disp(_(u"Ignoring action without from_jid data"), 1) 130 self.disp(_(u"Ignoring action without from_jid data"), 1)
123 return 131 return
124 132
125 if not self.bare_jids or from_jid.bare in self.bare_jids: 133 if not self.bare_jids or from_jid.bare in self.bare_jids:
132 port += 1 140 port += 1
133 else: 141 else:
134 raise e 142 raise e
135 else: 143 else:
136 break 144 break
137 xmlui_data = {'answer': C.BOOL_TRUE, 145 xmlui_data = {"answer": C.BOOL_TRUE, "port": unicode(port)}
138 'port': unicode(port)}
139 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile) 146 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
140 server.serve_forever() 147 server.serve_forever()
141 self.host.quitFromSignal() 148 self.host.quitFromSignal()
142 149
143 def start(self): 150 def start(self):
146 153
147 class Pipe(base.CommandBase): 154 class Pipe(base.CommandBase):
148 subcommands = (PipeOut, PipeIn) 155 subcommands = (PipeOut, PipeIn)
149 156
150 def __init__(self, host): 157 def __init__(self, host):
151 super(Pipe, self).__init__(host, 'pipe', use_profile=False, help=_('stream piping through XMPP')) 158 super(Pipe, self).__init__(
159 host, "pipe", use_profile=False, help=_("stream piping through XMPP")
160 )