-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathzeroframe.py
121 lines (99 loc) · 2.77 KB
/
zeroframe.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
_cache = {}
def module(io):
if io["site"].address in _cache:
return _cache[io["site"].address]
import sys
import gevent
from User import UserManager
from Ui import UiWebsocket
# Create a fake WebSocket
class WS(object):
def send(self, *args, **kwargs):
pass
ws = WS()
# Create a fake UiRequest
class FakeUiRequest(object):
def __init__(self):
self.env = {
"REMOTE_ADDR": "0.0.0.0"
}
def getWrapperNonce(self):
return ""
ui_request = FakeUiRequest()
# Create fake UiWebsocket
waiting_ids = {}
class FakeUiWebsocket(UiWebsocket):
# Responses
def response(self, to, result):
if to in waiting_ids:
waiting_ids[to].set_result(result)
# Callbacks
def cmd(self, cmd, params={}, cb=None):
attr_name = "on" + cmd[0].upper() + cmd[1:]
if hasattr(zeroframe, attr_name):
getattr(zeroframe, attr_name)(params)
site = io["site"]
user = UserManager.user_manager.get()
# Create a fake UiWebsocket
ui_server = sys.modules["main"].ui_server
ui_websocket = FakeUiWebsocket(ws, site, ui_server, user, ui_request)
site.websockets.append(ui_websocket) # Add to site websockets to allow notify on events
last_req_id = [0]
class ZeroFrame(object):
def cmd(self, cmd, *args, **kwargs):
wait = kwargs.pop("wait", False)
# Check params
if len(args) == 0:
params = kwargs
elif len(kwargs) == 0:
params = list(args)
else:
raise TypeError("ZeroFrame.cmd() accepts either *vararg or **kwarg, not both")
# Generate ID
req_id = last_req_id[0]
last_req_id[0] += 1
if wait:
# Set callback
waiting_ids[req_id] = gevent.event.AsyncResult()
# Send
ui_websocket.handleRequest({
"cmd": cmd,
"params": params,
"id": req_id
})
if wait:
# Wait
result = waiting_ids[req_id].get()
# Reply
del waiting_ids[req_id]
if isinstance(result, dict) and "error" in result:
raise ValueError(result["error"])
else:
return result
# Same as settings .on... attribute, but can set several handlers
def on(self, event_name, func):
attr_name = "on" + event_name[0].upper() + event_name[1:]
old_handler = getattr(self, attr_name, None)
def handler(*args, **kwargs):
if old_handler is not None:
try:
old_handler(*args, **kwargs)
except:
pass
func(*args, **kwargs)
setattr(self, attr_name, handler)
# A simplier way to call API, e.g.:
# ZeroFrame.fileGet("content.json")
def __getattr__(self, name):
def call(*args, **kwargs):
return self.cmd(name, *args, **kwargs)
return call
zeroframe = ZeroFrame()
_cache[io["site"].address] = zeroframe
return zeroframe
def close(io):
zeroframe = _cache[io["site"].address]
for key in list(zeroframe.__dict__.keys()):
if key.startswith("on"):
delattr(zeroframe, key)
module.close = close