-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
256 lines (222 loc) · 7.6 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from flexx import flx
from widgets import MyWidget, MyPrompt, MyConsole
from collections import deque
import asyncio, sys, threading, time
from pynput.keyboard import Key, Controller # For document focus on creation
from _cpcp.utils._dict import Dict
from _cpcp.utils._fifo import MyFIFO
class MyApp_JS(MyWidget):
ready = flx.BoolProp(False)
def init(self):
super().init()
with flx.VBox():
with flx.HBox():
self.wtitle = flx.Label(flex=1)
self.wversion = flx.Label(text='v0.0.0')
self.wprompt = MyPrompt()
self.wconsole = MyConsole(flex=1)
self._init_focus()
self._init_ready()
return
async def _init_focus(self):
while self.document.hasFocus()==False:
await self.sleep(50)
self.wprompt.set_focus()
return
async def _init_ready(self):
while not self.wtitle or not self.wprompt or not self.wconsole:
await self.sleep(50)
self._mutate_ready(True)
return
@flx.emitter
def emit_option(self, event):
return event
@flx.emitter
def emit_text(self, event):
return event
@flx.emitter
def emit_escape(self, event):
return event
@flx.emitter
def emit_interrupt(self, event):
return event
@flx.emitter
def emit_version(self, event):
return event
@flx.reaction('wprompt.emit_option', mode='greedy')
def listen_option(self, *events):
return self.emit_option(events[-1])
@flx.reaction('wprompt.emit_text', mode='greedy')
def listen_text(self, *events):
return self.emit_text(events[-1])
@flx.reaction('wprompt.emit_escape', mode='greedy')
def listen_escape(self, *events):
return self.emit_escape(events[-1])
@flx.reaction('wprompt.emit_interrupt', mode='greedy')
def listen_interrupt(self, *events):
return self.emit_interrupt(events[-1])
@flx.reaction('wversion.pointer_click', mode='greedy')
def listen_version(self, *events):
return self.emit_version(events[-1])
@flx.action
def set_focus(self):
self.wprompt.set_focus()
@flx.action
def set_output(self, text):
self.wconsole.set_output(text)
@flx.action
def append_output(self, text):
self.wconsole.append_output(text)
@flx.action
def clear_output(self, text):
self.wconsole.clear_output(text)
@flx.action
def set_properties(self, kwargs):
self.wprompt.set_properties(kwargs)
@flx.action
def set_title(self, text):
if self.ready:
self.wtitle.set_text(text)
@flx.action
def set_version(self, text):
if self.ready:
self.wversion.set_text(text)
class MyApp(flx.PyComponent):
def init(self):
self.ready = False
self.js = MyApp_JS()
self._interrupt = threading.Event()
self._version = threading.Event()
self.term = flx.Dict(fifo=MyFIFO(), size=0, max_size=10**5)
self.prompts = flx.Dict(fifo=MyFIFO(), ans=MyFIFO(), results={})
self.actions = MyFIFO()
asyncio.ensure_future(self._post_init())
return
async def _post_init(self):
while not self.js.ready:
await asyncio.sleep(50e-3)
keyboard = Controller()
keyboard.press(Key.tab) # Focus document
keyboard.release(Key.tab)
asyncio.ensure_future(self._writer())
asyncio.ensure_future(self._prompter())
asyncio.ensure_future(self._actioner())
def wait_ready(self):
while not self.js.ready:
time.sleep(50e-3)
@flx.action
def _set_title(self, title):
self.js.set_title(title)
def set_title(self, title):
self.actions.push_fn(self._set_title, title)
@flx.action
def _set_version(self, text):
self.js.set_version(text)
def set_version(self, text):
self.actions.push_fn(self._set_version, text)
@flx.reaction('js.emit_escape', mode='greedy')
def listen_escape(self, *events):
self.prompts.ans.push(None)
@flx.reaction('js.emit_text', 'js.emit_option', mode='greedy')
def listen_prompt(self, *events):
self.prompts.ans.push(events[-1].value)
@flx.reaction('js.emit_interrupt', mode='greedy')
def listen_interrupt(self, *events):
self._interrupt.set()
@flx.reaction('js.emit_version', mode='greedy')
def listen_version(self, *events):
self._version.set()
def print(self, *args, end='\n', flush=None):
self.write(' '.join(str(x) for x in args))
self.write(end or '')
if flush or (flush==None and end=='\n'):
self.flush()
def clear(self):
self.term.fifo.clear()
self.term.fifo.push(None)
def write(self, data):
self.term.fifo.push(data)
def flush(self):
self.term.fifo.wait_flush()
async def _writer(self):
while 1:
data = await self.term.fifo.pop()
if data==None:
self.term.size = 0
self.js.clear_output()
else:
self.term.size += len(data)
if self.term.size > self.term.max_size:
div = '...\n---automatic crop. too much previous output---\n'
self.js.set_output(div)
data = data[-self.term.max_size:]
self.term.size = len(data)
self.js.append_output(data)
return
async def _prompter(self):
prev = None
while 1:
tab, ref, sync = await self.prompts.fifo.pop()
curr = str(tab)
if curr!=prev:
self.js.set_properties(tab)
self.js.set_focus()
prev = curr
self.prompts.ans.clear()
ans = await self.prompts.ans.pop()
self.prompts.results[ref] = ans
sync.set()
return
async def _actioner(self):
while 1:
action, args, kwargs = await self.actions.pop()
action(*args, **kwargs)
return
def choice(self, message, options,
placeholder='', prefill='', n_options=5):
tab = dict(
options=options,
above=message,
prefill=prefill,
placeholder=placeholder,
n_options=n_options,
)
self.wait_ready()
ref = time.time()
sync = threading.Event()
self.prompts.fifo.push((tab, ref, sync))
sync.wait()
ans = self.prompts.results.pop(ref)
return ans
def alert(self, message):
self.choice(message=message, options=['OK'])
return
def confirm(self, message, default=True):
options = ['Yes', 'No'] if default==True else ['No', 'Yes']
ans = self.choice(message=message, options=options)
return {'Yes':True, 'No':False}.get(ans)
def prompt(self, message, validator=None, **kwargs):
error = ':)'
while error:
ans = self.choice(
message=message,
options=[], **kwargs,
)
error = validator(ans) if ans!=None and validator else None
if error:
self.print(error)
return ans
def wait_interrupt(self):
self._interrupt.wait()
self._interrupt.clear()
if __name__=='__main__':
_, *args = sys.argv
app = flx.App(MyGUI_PY)
title = 'Amigo CPCP'
icon = 'icon.ico'
if args and args[0].endswith('browser'):
app.launch(args[0], title=title, icon=icon)
else:
app.launch('app', title=title, icon=icon)
#app.export('example.html', link=0)
flx.run()