forked from MarxMustermann/OfMiceAndMechs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executeYou.py
85 lines (59 loc) · 1.95 KB
/
executeYou.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
#!/usr/bin/env python3
import socket
HOST = input("enter server adress") # The server's hostname or IP address
PORT = 65440 # The port used by the server
import time
import json
import urwid
header = urwid.Text(u"")
main = urwid.Text(u"")
footer = urwid.Text(u"")
main.set_layout("left", "clip")
frame = urwid.Frame(urwid.Filler(main, "top"), header=header, footer=footer)
def draw(raw):
def deserializeUrwid(inData):
outData = []
for item in inData:
if item[0] == "tuple":
outData.append(
(urwid.AttrSpec(item[1][0], item[1][1]), deserializeUrwid(item[2]))
)
if item[0] == "list":
outData.append(deserializeUrwid(item[1]))
if item[0] == "str":
outData.append(item[1])
return outData
# header.set_text((urwid.AttrSpec("default","default"),deserializeUrwid(raw["head"])))
main.set_text((urwid.AttrSpec("default", "default"), deserializeUrwid(raw["main"])))
footer.set_text(
(urwid.AttrSpec("default", "default"), deserializeUrwid(raw["footer"]))
)
def getData(request=b"ignore"):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(request)
data = b""
while True:
part = s.recv(1024)
data += part
if not part:
break
return data.decode("utf-8")
commands = []
def keyboardListener(key):
header.set_text(str(key))
if not isinstance(key, str):
return
commands.append(key)
loop = urwid.MainLoop(frame, unhandled_input=keyboardListener)
def tmp3(loop, user_data):
if not commands:
data = getData(request=b"redraw")
else:
data = getData(request=json.dumps(commands).encode("utf-8"))
commands.clear()
raw = json.loads(data)
draw(raw)
loop.set_alarm_in(0.01, tmp3)
loop.set_alarm_in(0.1, tmp3)
loop.run()