forked from lspestrip/striptease
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_polview.py
115 lines (91 loc) · 2.9 KB
/
program_polview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import sys
import asyncio
from program_polview.worker import Worker
from web.rest.base import Connection
from widgets.login import LoginWidget
from config import Config
from PyQt5 import QtWidgets
from threading import Thread
import subprocess as sp
import json
class UnixProtocolR(asyncio.Protocol):
def __init__(self):
self.workers = {}
self.len = 0
self.transport = None
def set_con(self, con):
self.conn = con
def connection_made(self, transport):
self.transport = transport
print("connection made:", transport)
def connection_lost(self, exc):
print("program exiting...")
self.transport = None
sys.exit(0)
def data_received(self, data):
try:
a = json.loads(data)
if a.get("cmd"):
print("CMD:", a)
if a["cmd"] == "attach_pipe":
pol = a["pol"]
path = a["path"]
w = Worker(self.conn, pol, path)
w.start()
self.workers[pol] = w
if a["cmd"] == "detach_pipe":
print("detach")
pol = a["pol"]
self.workers[pol].stop()
del self.workers[pol]
# print('json',a)
except json.JSONDecodeError:
print("STDOUT:", str(data, encoding="utf-8"), "END")
# pass
self.len += len(data)
class UnixProtocolW(asyncio.Protocol):
def __init__(self):
self.transport = None
def connection_made(self, transport):
self.transport = transport
print("connection made:", transport)
def connection_lost(self, exc):
print("The server closed the connection")
self.transport = None
def write(self, data):
if self.transport is not None:
self.transport.write(data)
loop = None
def th_loop():
global loop
if loop:
loop.run_forever()
if __name__ == "__main__":
loop = asyncio.new_event_loop()
th = Thread(target=th_loop)
th.start()
conn = Connection()
if conn.has_login():
conn.login()
else:
app = QtWidgets.QApplication(sys.argv)
dialog = LoginWidget()
dialog.exec_()
conn.login(dialog.user, dialog.password)
conf = Config()
conf.load(conn)
gui = sp.Popen(
["program_polview/bin/program_polview", "-u", conn.user, "-p", conn.password],
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.STDOUT,
)
coro = loop.connect_read_pipe(UnixProtocolR, gui.stdout)
fut = asyncio.run_coroutine_threadsafe(coro, loop)
(_, inpipe) = fut.result()
inpipe.set_con(conn)
coro = loop.connect_write_pipe(UnixProtocolW, gui.stdin)
fut = asyncio.run_coroutine_threadsafe(coro, loop)
(_, outpipe) = fut.result()
outpipe.write(json.dumps(conf.boards).encode())
# th.join()