-
Notifications
You must be signed in to change notification settings - Fork 1
/
uremi.py
145 lines (112 loc) · 3.13 KB
/
uremi.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
EVENT_MAP = {}
CONN = None
def conn():
return CONN
class Tag:
events = ""
def __init__(self, **styles):
self.children = []
self.style = styles
self.inner = ""
def html(self, s):
st = ""
for k, v in self.style.items():
st += "{}:{};".format(k, v)
s.write('<{} id="{}" class="{}" style="{}" {}>\n'.format(self.tag, id(self), self.class_, st, self.events.format(id(self))))
s.write(self.inner)
for c in self.children:
c.html(s)
s.write('</{}>'.format(self.tag))
def append(self, c):
self.children.append(c)
def on(self, event, handler):
global EVENT_MAP
EVENT_MAP[(id(self), event)] = handler
class Widget(Tag):
tag = "div"
class_ = "Widget"
def __init__(self, **styles):
super().__init__(**styles)
class Label(Tag):
tag = "p"
class_ = "Label"
def __init__(self, text, **styles):
super().__init__(**styles)
self.inner = text
def set(self, text):
CONN.write("innerHTML\t{}\t{}".format(id(self), text))
class Button(Tag):
class_ = "Button"
tag = "button"
events = 'onclick="ev(\'{0}\', \'click\')"'
def __init__(self, text, **styles):
super().__init__(**styles)
self.inner = text
def render(w, s):
s.write("""\
<html>
<head>
<link href="res/style.css" rel="stylesheet">
<script>
var ws;
function ev(id, e) {
// alert(id + " " + e);
ws.send(id + " " + e + "\\n");
}
window.onload = function() {
var wsaddr = window.location.toString().replace("http:", "ws:")
// alert("loaded: " + wsaddr);
ws = new WebSocket(wsaddr + "ws");
ws.onmessage = function(ev) {
// alert(ev.data);
var comp = ev.data.split("\t");
if (comp[0] == "innerHTML") {
var el = document.getElementById(comp[1]);
el.innerHTML = comp[2];
}
}
}
</script>
</head>
<body>
""")
w.html(s)
s.write("\n</body></html>\n")
#
# HTTP serving part
#
import server
import websocket_helper
import websocket
class WebApp:
def __init__(self, toplevel_widget):
self.w = toplevel_widget
def http_handler(self, s, req):
meth, path, proto = req.split()
if path == b"/":
server.skip_headers(s)
s.write("HTTP/1.0 200 OK\r\n\r\n")
render(self.w, s)
elif path == b"/res/style.css":
server.skip_headers(s)
s.write("HTTP/1.0 200 OK\r\n\r\n")
with open(path[1:], "rb") as f:
data = f.read()
s.write(data)
elif path == b"/ws":
websocket_helper.server_handshake(s)
s = websocket.websocket(s)
print("websock connected")
while 1:
data = s.readline()
data = data.decode("ascii").rstrip().split()
print(data)
global CONN
CONN = s
EVENT_MAP[(int(data[0]), data[1])]()
else:
s.write("HTTP/1.0 404 NAK\r\n\r\n")
def serve(self):
print(EVENT_MAP)
server.serve(self.http_handler)