-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-assist.py
408 lines (303 loc) · 9.82 KB
/
demo-assist.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# -*- coding: utf-8 -*-
#
# Airbatch - Fast flight data recognition framework
# Copyright (C) 2018, 2019 by Jan Max Meyer, Phorward Software Technologies
#
import airbatch, datetime
from browser import document, window, html
class AssistantProcessor(airbatch.Processor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mode = "assistant"
self.allowedRecognizers = [
self.dateRecognizer,
self.aircraftRecognizer,
self.pilotRecognizer,
self.locationRecognizer,
self.timeRecognizer,
self.durationRecognizer
]
self.editorTimeout = None
self.editor = document["editor"]
self.editorCol = document["editor-col"]
self.editorRow = document["editor-row"]
self.editorProposal = document["editor-proposal"]
self.final = document["final"]
self.construction = document["construction"]
self.constructionRow = document["construction-row"]
self.batch = document["batch"]
self.batchSwitch = document["batch-switch"]
self.batchEditor = document["batch-editor"]
self.batchErrorRow = None
self.editorProposal.bind("click", self.selectProposal)
#editor.bind("keyup", setTimeout)
self.editor.bind("change", self.checkInput)
self.batchSwitch.bind("change", self.switchMode)
document["btn-parse"].bind("click", self.doParse)
document["btn-time"].bind("click", self.insertTime)
window.setTimeout(self.editor.focus, 500)
window.setInterval(self.updateTime, 1000)
batch = window.localStorage.getItem(datetime.datetime.now().strftime("batch-%Y-%m-%d"))
if batch:
window.alert("%d Starts aus localStorage im Batch-Modus wiederhergestellt" % (batch.count("\n") + 1))
self.batchEditor.value = batch
# --- Pure batch mode
def insertCode(self, txt):
if self.mode == "batch":
widget = "batch-editor"
else:
widget = "editor"
start = document[widget].selectionStart
txt = document[widget].value[:start] + txt + " " + document[widget].value[start:]
document[widget].value = txt
def insertObject(self, event):
opt = event.target
txt = str(opt.obj)
if " - " in txt:
self.insertCode(txt.split(" - ", 1)[0].strip())
elif " (" in txt:
self.insertCode(txt.split(" (", 1)[0].strip())
else:
self.insertCode(txt)
def makeList(self, element, items):
for i in items:
opt = document.createElement("option")
opt["value"] = i.key
opt.obj = i
opt.appendChild(document.createTextNode(str(i)))
element.appendChild(opt)
def _aircraftsAvailable(self, rec):
self.makeList(document["batch-aircraft"], rec.items)
document["batch-aircraft"].bind("dblclick", self.insertObject)
def _pilotsAvailable(self, rec):
self.makeList(document["batch-pilot"], rec.items)
document["batch-pilot"].bind("dblclick", self.insertObject)
def _locationsAvailable(self, rec):
self.makeList(document["batch-location"], rec.items)
document["batch-location"].bind("dblclick", self.insertObject)
def doParse(self, event):
self.reset()
res = self.parse(self.batchEditor.value)
lastRow = self.constructionRow
if self.batchErrorRow:
self.batchErrorRow.parent.removeChild(self.batchErrorRow)
self.batchErrorRow = None
for entry in res:
row = html.TR()
row.activity = entry
if isinstance(entry, airbatch.Activity):
for txt in [
#str(entry.row),
#entry.takeoff.strftime("%d.%m.%Y"),
str(entry.aircraft),
str(entry.pilot),
str(entry.copilot) if entry.copilot else "-",
entry.takeoff.strftime("%H:%M"),
str(entry.ltakeoff),
entry.touchdown.strftime("%H:%M"),
str(entry.ltouchdown),
str(entry.duration)]:
col = row.insertCell()
col.innerHTML = txt
row.commit = html.SPAN("✓", Class="commit")
row.commit.bind("click", self.commitRow)
row.close = html.SPAN("⨉", Class="close")
row.close.bind("click", self.removeRow)
col = row.insertCell()
col.appendChild(row.commit)
col.appendChild(row.close)
else:
self.batchErrorRow = row
row.classList.add("error")
col = row.insertCell()
col.colSpan = "10"
col.innerHTML = str(entry)
self.construction.insertBefore(row, lastRow)
# --- Interactive mode
def clearTokens(self, act):
for token in [x for x in self.editorRow.children][:-1]:
if token.result.obj in act:
token.parent.removeChild(token)
def removeRow(self, e):
row = e.target.parent.parent
print(row.parent)
print(self.construction)
print(row.parent.id)
if row.parent.id == "construction":
self.clearTokens(row.activity)
row.parent.removeChild(row)
self.updateTable()
def commitRow(self, e):
row = e.target.parent.parent
rowparent = row.parent
row.commit.style.display = "none"
self.final.appendChild(row)
acts = [row.activity]
if row.activity.link:
for x in [x for x in rowparent.children][:-1]:
if x.activity is row.activity.link:
x.commit.style.display = "none"
self.final.appendChild(x)
acts.append(x.activity)
break
for act in acts:
self.clearTokens(act)
self.updateTable()
def updateTable(self):
print("updateTable")
while True:
changes = False
children = [x for x in self.final.children]
for row, nextRow in zip(children, children[1:]):
if nextRow.activity.takeoff < row.activity.takeoff:
self.final.insertBefore(nextRow, row)
changes = True
break
if changes:
continue
break
batch = "\n".join([repr(c.activity) for c in children])
window.localStorage.setItem(datetime.datetime.now().strftime("batch-%Y-%m-%d"), batch)
if children:
document["btn-download"].style.display = ""
else:
document["btn-download"].style.display = "none"
def rebuildBatch(self):
for c in [x for x in self.construction.children][:-1]:
self.construction.removeChild(c)
self.reset(False)
for token in [x for x in self.editorRow.children][:-1]:
self.extend(token.result)
lastRow = self.constructionRow
res = self.commit()
for entry in res:
row = html.TR()
row.activity = entry
if isinstance(entry, airbatch.Activity):
for txt in [
#entry.takeoff.strftime("%d.%m.%Y"),
str(entry.aircraft),
str(entry.pilot),
str(entry.copilot) if entry.copilot else "-",
entry.takeoff.strftime("%H:%M"),
str(entry.ltakeoff),
entry.touchdown.strftime("%H:%M"),
str(entry.ltouchdown),
str(entry.duration)]:
col = row.insertCell()
col.innerHTML = txt
row.commit = html.SPAN("✓", Class="commit")
row.commit.bind("click", self.commitRow)
row.close = html.SPAN("⨉", Class="close")
row.close.bind("click", self.removeRow)
col = row.insertCell()
col.appendChild(row.commit)
col.appendChild(row.close)
else:
row.classList.add("error")
col = row.insertCell()
col.colSpan = "10"
col.innerHTML = str(entry)
self.construction.insertBefore(row, lastRow)
#lastRow = row
def createMatchLi(self, result):
def doClose(e):
e.target.parent.parent.removeChild(e.target.parent)
print("REMOVED", [str(x.result) for x in [x for x in self.editorRow.children][:-1]])
self.rebuildBatch()
token = html.SPAN(result.token, Class="token")
label = html.SPAN(str(result.obj), Class="label")
close = html.SPAN("⨉", Class="close")
close.bind("click", doClose)
close.style.display = "none"
li = html.LI([token, label, close])
li.result = result
li.close = close
return li
def extendLine(self, result):
if isinstance(result, html.LI):
li = result
else:
li = self.createMatchLi(result)
li.close.style.display = "initial"
self.editorRow.insertBefore(li, self.editorCol)
print([str(x.result) for x in [x for x in self.editorRow.children][:-1]])
self.editor.value = self.editor.value[li.result.count:].strip()
self.editor.focus()
if self.editor.value:
self.checkInput()
else:
self.rebuildBatch()
def clearProposals(self):
while self.editorProposal.firstChild:
self.editorProposal.removeChild(self.editorProposal.firstChild)
self.editorProposal.style.display = "none"
def checkInput(self, *args, **kwargs):
s = self.editor.value.strip()
self.editor.classList.remove("unknown")
self.clearProposals()
if not s:
return
matches = []
self.editor.disabled = True
for r in self.allowedRecognizers:
res = r.propose(s)
if res:
if isinstance(res, list):
matches.extend(res)
else:
matches.append(res)
if len(matches) == 1 and res.token == s:
break
print(matches)
'''
for res in matches[:]:
print(res)
if isinstance(res.obj, (airbatch.Aircraft, airbatch.Pilot)):
print(self.editorRow.children)
for c in self.editorRow.children:
if c is self.editorRow.children[-1]:
break
if c.result.obj is res.obj:
matches.remove(res)
'''
if matches:
if len(matches) > 1:
for res in matches:
self.editorProposal.appendChild(self.createMatchLi(res))
self.editorProposal.style.display = "block"
else:
self.extendLine(matches[0])
else:
self.editor.classList.add("unknown")
self.editor.disabled = False
def selectProposal(self, e):
elem = e.target
while not isinstance(elem, html.LI):
elem = elem.parent
self.extendLine(elem)
self.clearProposals()
def setTimeout(self, e):
e.preventDefault()
e.stopPropagation()
if self.editorTimeout:
window.clearTimeout(self.editorTimeout)
self.editorTimeout = None
self.editorTimeout = window.setTimeout(self.checkInput, 1500)
def switchMode(self, e):
e.preventDefault()
e.stopPropagation()
if self.batch.style.display == "none":
self.batch.style.display = ""
self.constructionRow.style.display = "none"
self.mode = "batch"
else:
self.batch.style.display = "none"
self.constructionRow.style.display = ""
self.mode = "assistant"
def updateTime(self):
document["btn-time"].innerHTML = datetime.datetime.utcnow().strftime("%H:%M")
def insertTime(self, e):
self.insertCode(datetime.datetime.utcnow().strftime("%H%M"))
AssistantProcessor()
print("AIRBATCH-ASSISTANT LOADED")