forked from LmeSzinc/AzurLaneAutoScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
988 lines (876 loc) · 37.8 KB
/
gui.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
import argparse
import logging
import queue
import time
from datetime import datetime
from multiprocessing import Manager, Process
from multiprocessing.managers import SyncManager
from typing import Dict, Generator, List
from filelock import FileLock
from pywebio import config as webconfig
from pywebio.exceptions import SessionClosedException, SessionNotFoundException
from pywebio.output import (clear, close_popup, output, popup, put_button,
put_buttons, put_collapse, put_column, put_error,
put_html, put_loading, put_markdown, put_row,
put_text, toast)
from pywebio.pin import pin, pin_wait_change
from pywebio.session import go_app, info, register_thread, run_js, set_env
# This must be the first to import
from module.logger import logger # Change folder
import module.webui.lang as lang
import module.config.server as server
from module.config.config import AzurLaneConfig, Function
from module.config.config_updater import ConfigUpdater
from module.config.utils import (alas_instance, deep_get, deep_iter, deep_set,
dict_to_kv, filepath_args, filepath_config,
read_file, write_file)
from module.webui.base import Frame
from module.webui.lang import _t, t
from module.webui.pin import put_input, put_select
from module.webui.translate import translate
from module.webui.utils import (Icon, QueueHandler, Task, Thread, add_css,
filepath_css, get_output,
get_window_visibility_state, login,
parse_pin_value, re_fullmatch)
from module.webui.widgets import ScrollableCode, put_group, put_icon_buttons
config_updater = ConfigUpdater()
class AlasManager:
sync_manager: SyncManager
all_alas: Dict[str, "AlasManager"] = {}
def __init__(self, config_name: str = 'alas') -> None:
self.config_name = config_name
self.log_queue = self.sync_manager.Queue()
self.log = []
self.log_max_length = 500
self.log_reduce_length = 100
self._process = Process()
self.thd_log_queue_handler = Thread()
def start(self, func: str = 'Alas') -> None:
if not self.alive:
self._process = Process(
target=AlasManager.run_alas,
args=(self.config_name, self.log_queue, func))
self._process.start()
self.thd_log_queue_handler = Thread(
target=self._thread_log_queue_handler)
register_thread(self.thd_log_queue_handler)
self.thd_log_queue_handler.start()
else:
toast(t("Gui.Toast.AlasIsRunning"), position='right', color='warn')
def stop(self) -> None:
lock = FileLock(f"{filepath_config(self.config_name)}.lock")
with lock:
if self.alive:
self._process.terminate()
self.log.append("Scheduler stopped.\n")
if self.thd_log_queue_handler.is_alive():
self.thd_log_queue_handler.stop()
def _thread_log_queue_handler(self) -> None:
while self.alive:
log = self.log_queue.get()
self.log.append(log)
if len(self.log) > self.log_max_length:
self.log = self.log[self.log_reduce_length:]
@property
def alive(self) -> bool:
return self._process.is_alive()
@classmethod
def get_alas(cls, config_name: str) -> "AlasManager":
"""
Create a new alas if not exists.
"""
if config_name not in cls.all_alas:
cls.all_alas[config_name] = AlasManager(config_name)
return cls.all_alas[config_name]
@staticmethod
def run_alas(config_name, q: queue.Queue, func: str) -> None:
# Setup logger
qh = QueueHandler(q)
formatter = logging.Formatter(
fmt='%(asctime)s.%(msecs)03d | %(levelname)s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
webconsole = logging.StreamHandler(stream=qh)
webconsole.setFormatter(formatter)
logging.getLogger('alas').addHandler(webconsole)
# Set server before loading any buttons.
config = AzurLaneConfig(config_name=config_name)
server.server = deep_get(config.data, keys='Alas.Emulator.Server', default='cn')
# Run alas
if func == 'Alas':
from alas import AzurLaneAutoScript
AzurLaneAutoScript(config_name=config_name).loop()
elif func == 'Daemon':
from module.daemon.daemon import AzurLaneDaemon
AzurLaneDaemon(config=config_name, task='Daemon').run()
elif func == 'OpsiDaemon':
from module.daemon.os_daemon import AzurLaneDaemon
AzurLaneDaemon(config=config_name, task='OpsiDaemon').run()
elif func == 'AzurLaneUncensored':
from module.daemon.uncensored import AzurLaneUncensored
AzurLaneUncensored(config=config_name, task='AzurLaneUncensored').run()
q.put("Scheduler stopped.\n") # Prevent status turns to warning
elif func == 'Benchmark':
from module.daemon.benchmark import Benchmark
Benchmark(config=config_name, task='Benchmark').run()
q.put("Scheduler stopped.\n") # Prevent status turns to warning
else:
logger.critical("No function matched")
class AlasGUI(Frame):
ALAS_MENU: Dict[str, Dict[str, List[str]]]
ALAS_ARGS: Dict[str, Dict[str, Dict[str, Dict[str, str]]]]
path_to_idx: Dict[str, str] = {}
idx_to_path: Dict[str, str] = {}
theme = 'default'
@classmethod
def shorten_path(cls, prefix='a') -> None:
"""
Reduce pin_wait_change() command content-length
Using full path name will transfer ~16KB per command,
may lag when remote control or in bad internet condition.
Use ~4KB after doing this.
Args:
prefix: all idx need to be a valid html, so a random character here
"""
cls.ALAS_MENU = read_file(filepath_args('menu'))
cls.ALAS_ARGS = read_file(filepath_args('args'))
i = 0
for list_path, _ in deep_iter(cls.ALAS_ARGS, depth=3):
cls.path_to_idx['.'.join(list_path)] = f'{prefix}{i}'
cls.idx_to_path[f'{prefix}{i}'] = '.'.join(list_path)
i += 1
def __init__(self) -> None:
super().__init__()
# modified keys, return values of pin_wait_change()
self.modified_config_queue = queue.Queue()
# alas config name
self.alas_name = ''
self.alas_config = AzurLaneConfig('template')
self.alas_logs = ScrollableCode()
self.alas_running = output().style("container-overview-task")
self.alas_pending = output().style("container-overview-task")
self.alas_waiting = output().style("container-overview-task")
def set_aside(self) -> None:
# note: value doesn't matters if onclick is a list, button binds to functions in order.
# when onclick isn't a list, value will pass to function.
self.aside.reset(
put_icon_buttons(Icon.DEVELOP, buttons=[{"label": t(
"Gui.Aside.Develop"), "value": "Develop", "color": "aside"}], onclick=[self.ui_develop]),
*[put_icon_buttons(Icon.RUN,
buttons=[{"label": name, "value": name, "color": "aside"}],
onclick=self.ui_alas)
for name in alas_instance()],
put_icon_buttons(Icon.ADD, buttons=[{"label": t(
"Gui.Aside.AddAlas"), "value": "AddAlas", "color": "aside"}], onclick=[self.ui_add_alas]),
)
self.aside_setting.reset(
put_icon_buttons(
Icon.SETTING,
buttons=[
{"label": t("Gui.Aside.Setting"),
"value": "setting", "color": "aside"}],
onclick=[self.ui_setting],
)
)
def set_status(self, status: int) -> None:
"""
Args:
status (int):
1 (running),
2 (not running),
-1 (warning, stop unexpectedly),
0 (hide)
"""
if not self.visible:
return
if self._status == status:
return
self._status = status
if status == 1:
s = put_row([
put_loading(color='success').style(
"width:1.5rem;height:1.5rem;border:.2em solid currentColor;border-right-color:transparent;"),
None,
put_text(t("Gui.Status.Running"))
], size='auto 2px 1fr')
elif status == 2:
s = put_row([
put_loading(color='secondary').style("width:1.5rem;height:1.5rem;border:.2em solid currentColor;"),
None,
put_text(t("Gui.Status.Inactive"))
], size='auto 2px 1fr')
elif status == -1:
s = put_row([
put_loading(shape='grow', color='warning').style("width:1.5rem;height:1.5rem;"),
None,
put_text(t("Gui.Status.Warning"))
], size='auto 2px 1fr')
else:
s = ''
self.status.reset(s)
# Alas
def alas_set_menu(self) -> None:
"""
Set menu for alas
"""
self.menu.append(
put_buttons([
{"label": t("Gui.MenuAlas.Overview"),
"value": "Overview", "color": "menu"}
], onclick=[self.alas_overview]).style(f'--menu-Overview--'),
)
for key, tasks in deep_iter(self.ALAS_MENU, depth=2):
# path = '.'.join(key)
menu = key[1]
if menu == 'Tool':
_onclick = self.alas_daemon_overview
else:
_onclick = self.alas_set_group
task_btn_list = []
for task in tasks:
task_btn_list.append(
put_buttons([
{"label": t(f'Task.{task}.name'), "value": task, "color": "menu"}
], onclick=_onclick).style(f'--menu-{task}--')
)
self.menu.append(
put_collapse(
title=t(f"Menu.{menu}.name"),
content=task_btn_list
)
)
self.alas_overview()
def alas_set_group(self, task: str) -> None:
"""
Set arg groups from dict
"""
self.init_menu(name=task)
self.title.reset(f"{t(f'Task.{task}.name')}")
group_area = output()
navigator = output()
if self.is_mobile:
content_alas = group_area
else:
content_alas = put_row([
None,
group_area,
navigator,
], size="1fr minmax(25rem, 5fr) 2fr")
self.content.append(content_alas)
config = config_updater.update_config(self.alas_name)
for group, arg_dict in deep_iter(self.ALAS_ARGS[task], depth=1):
group = group[0]
group_help = t(f"{group}._info.help")
if group_help == "" or not group_help:
group_help = None
arg_group = put_group(t(f"{group}._info.name"), group_help)
list_arg = []
for arg, d in deep_iter(arg_dict, depth=1):
arg = arg[0]
arg_type = d['type']
if arg_type == 'disable':
continue
value = deep_get(config, f'{task}.{group}.{arg}', d['value'])
value = str(value) if isinstance(value, datetime) else value
# Option
options = deep_get(d, 'option', None)
if options:
option = []
for opt in options:
o = {"label": t(f"{group}.{arg}.{opt}"), "value": opt}
if value == opt:
o["selected"] = True
option.append(o)
else:
option = None
# Help
arg_help = t(f"{group}.{arg}.help")
if arg_help == "" or not arg_help:
arg_help = None
# Invalid feedback
invalid_feedback = t("Gui.Text.InvalidFeedBack").format(d['value'])
list_arg.append(get_output(
arg_type=arg_type,
name=self.path_to_idx[f"{task}.{group}.{arg}"],
title=t(f"{group}.{arg}.name"),
arg_help=arg_help,
value=value,
options=option,
invalid_feedback=invalid_feedback,
))
if list_arg:
group_area.append(arg_group)
arg_group.append(*list_arg)
def alas_overview(self) -> None:
self.init_menu(name="Overview")
self.title.reset(f"{t(f'Gui.MenuAlas.Overview')}")
scheduler = put_row([
put_text(t("Gui.Overview.Scheduler")).style(
"font-size: 1.25rem; margin: auto .5rem auto;"),
None,
put_buttons(
buttons=[
{"label": t("Gui.Button.Start"),
"value": "Start", "color": "scheduler-on"},
{"label": t("Gui.Button.Stop"),
"value": "Stop", "color": "scheduler-off"},
],
onclick=[
lambda: self.alas.start('Alas'),
self.alas.stop,
]
)
], size="auto 1fr auto").style("container-overview-group")
log = put_row([
put_text(t("Gui.Overview.Log")).style(
"font-size: 1.25rem; margin: auto .5rem auto;"),
None,
put_buttons(
buttons=[
{"label": t("Gui.Button.ClearLog"),
"value": "ClearLog", "color": "scheduler-on"},
{"label": t("Gui.Button.ScrollON"),
"value": "ScrollON", "color": "scheduler-on"},
{"label": t("Gui.Button.ScrollOFF"),
"value": "ScrollOFF", "color": "scheduler-on"},
],
onclick=[
self.alas_logs.reset,
lambda: self.alas_logs.set_scroll(True),
lambda: self.alas_logs.set_scroll(False),
],
)
], size="auto 1fr auto").style("container-overview-group")
running = put_column([
put_text(t("Gui.Overview.Running")).style("group-overview-title"),
put_html('<hr class="hr-group">'),
self.alas_running,
], size="auto auto 1fr").style("container-overview-group")
pending = put_column([
put_text(t("Gui.Overview.Pending")).style("group-overview-title"),
put_html('<hr class="hr-group">'),
self.alas_pending,
], size="auto auto 1fr").style("container-overview-group")
waiting = put_column([
put_text(t("Gui.Overview.Waiting")).style("group-overview-title"),
put_html('<hr class="hr-group">'),
self.alas_waiting,
], size="auto auto 1fr").style("container-overview-group")
if self.is_mobile:
self.content.append(
put_column([
scheduler,
running,
pending,
waiting,
], size="auto 7.75rem 13rem 13rem"),
put_column([
log,
self.alas_logs.output
], size="auto 1fr").style("height: 100%; overflow-y: auto")
)
else:
self.content.append(
put_row([
put_column([
scheduler,
running,
pending,
waiting,
], size="auto 7.75rem minmax(7.75rem, 13rem) minmax(7.75rem, 1fr)"
).style("height: 100%; overflow-y: auto"),
put_column([
log,
self.alas_logs.output
], size="auto 1fr").style("height: 100%; overflow-y: auto"),
], size="minmax(16rem, 20rem) minmax(24rem, 1fr)"
).style("height: 100%; overflow-y: auto"),
)
def refresh_overview_tasks():
while True:
yield self.alas_update_overiew_tasks()
self.task_handler.add(refresh_overview_tasks(), 10, True)
self.task_handler.add(self.alas_put_log(), 0.2, True)
def alas_put_log(self) -> Generator[None, None, None]:
last_idx = len(self.alas.log)
self.alas_logs.append(''.join(self.alas.log))
lines = 0
while True:
yield
idx = len(self.alas.log)
if idx < last_idx:
last_idx -= self.alas.log_reduce_length
if idx != last_idx:
try:
self.alas_logs.append(''.join(self.alas.log[last_idx:idx]))
except SessionNotFoundException:
break
lines += idx - last_idx
last_idx = idx
def _alas_thread_wait_config_change(self) -> None:
paths = []
for path, d in deep_iter(self.ALAS_ARGS, depth=3):
if d['type'] == 'disable':
continue
paths.append(self.path_to_idx['.'.join(path)])
while self.alive:
try:
val = pin_wait_change(*paths)
self.modified_config_queue.put(val)
except SessionClosedException:
break
def _alas_thread_update_config(self) -> None:
modified = {}
valid = []
invalid = []
while self.alive:
try:
d = self.modified_config_queue.get(timeout=10)
config_name = self.alas_name
except queue.Empty:
continue
modified[self.idx_to_path[d['name']]] = parse_pin_value(d['value'])
while True:
try:
d = self.modified_config_queue.get(timeout=1)
modified[self.idx_to_path[d['name']]] = parse_pin_value(d['value'])
except queue.Empty:
config = read_file(filepath_config(config_name))
for k, v in modified.copy().items():
validate = deep_get(self.ALAS_ARGS, k + '.validate')
if not len(str(v)):
default = deep_get(self.ALAS_ARGS, k + '.value')
deep_set(config, k, default)
valid.append(self.path_to_idx[k])
modified[k] = default
elif not validate or re_fullmatch(validate, v):
deep_set(config, k, v)
valid.append(self.path_to_idx[k])
else:
modified.pop(k)
invalid.append(self.path_to_idx[k])
logger.warning(f'Invalid value {v} for key {k}, skip saving.')
# toast(t("Gui.Toast.InvalidConfigValue").format(
# t('.'.join(k.split('.')[1:] + ['name']))),
# duration=0, position='right', color='warn')
self.pin_remove_invalid_mark(valid)
self.pin_set_invalid_mark(invalid)
if modified:
toast(t("Gui.Toast.ConfigSaved"),
duration=1, position='right', color='success')
logger.info(f'Save config {filepath_config(config_name)}, {dict_to_kv(modified)}')
write_file(filepath_config(config_name), config)
modified.clear()
valid.clear()
invalid.clear()
break
def alas_update_status(self) -> None:
if hasattr(self, 'alas'):
if self.alas.alive:
self.set_status(1)
elif len(self.alas.log) == 0 or self.alas.log[-1] == "Scheduler stopped.\n":
self.set_status(2)
else:
self.set_status(-1)
else:
self.set_status(0)
def alas_update_overiew_tasks(self) -> None:
if not self.visible:
return
self.alas_config.load()
self.alas_config.get_next_task()
if len(self.alas_config.pending_task) >= 1:
if self.alas.alive:
running = self.alas_config.pending_task[:1]
pending = self.alas_config.pending_task[1:]
else:
running = []
pending = self.alas_config.pending_task[:]
else:
running = []
pending = []
waiting = self.alas_config.waiting_task
def box(func: Function):
return put_row([
put_column([
put_text(t(f'Task.{func.command}.name')).style("arg-title"),
put_text(str(func.next_run)).style("arg-help"),
], size="auto auto"),
put_button(
label=t("Gui.Button.Setting"),
onclick=lambda: self.alas_set_group(func.command),
color="scheduler-on"
),
], size="1fr auto").style("container-overview-args")
# Another version without setting button, box become clickable
# return put_column([
# put_text(t(f'Task.{func.command}.name')).style("arg-title"),
# put_text(str(func.next_run)).style("arg-help"),
# ], size="auto auto"
# ).style("container-overview-args"
# ).onclick(lambda: self.alas_set_group(func.command))
no_task_style = "text-align:center; font-size: 0.875rem; color: darkgrey;"
if running:
self.alas_running.reset(*[box(task) for task in running])
else:
self.alas_running.reset(
put_text(t("Gui.Overview.NoTask")).style(no_task_style)
)
if pending:
self.alas_pending.reset(*[box(task) for task in pending])
else:
self.alas_pending.reset(
put_text(t("Gui.Overview.NoTask")).style(no_task_style)
)
if waiting:
self.alas_waiting.reset(*[box(task) for task in waiting])
else:
self.alas_waiting.reset(
put_text(t("Gui.Overview.NoTask")).style(no_task_style)
)
def alas_daemon_overview(self, task: str) -> None:
self.init_menu(name=task)
self.title.reset(f"{t(f'Task.{task}.name')}")
scheduler = put_row([
put_text(t("Gui.Overview.Scheduler")).style(
"font-size: 1.25rem; margin: auto .5rem auto;"),
None,
put_buttons(
buttons=[
{"label": t("Gui.Button.Start"),
"value": "Start", "color": "scheduler-on"},
{"label": t("Gui.Button.Stop"),
"value": "Stop", "color": "scheduler-off"},
],
onclick=[
lambda: self.alas.start(task),
self.alas.stop,
]
)
], size="auto 1fr auto").style("container-overview-group")
log = put_row([
put_text(t("Gui.Overview.Log")).style(
"font-size: 1.25rem; margin: auto .5rem auto;"),
None,
put_buttons(
buttons=[
{"label": t("Gui.Button.ClearLog"),
"value": "ClearLog", "color": "scheduler-on"},
{"label": t("Gui.Button.ScrollON"),
"value": "ScrollON", "color": "scheduler-on"},
{"label": t("Gui.Button.ScrollOFF"),
"value": "ScrollOFF", "color": "scheduler-on"},
],
onclick=[
self.alas_logs.reset,
lambda: self.alas_logs.set_scroll(True),
lambda: self.alas_logs.set_scroll(False),
],
)
], size="auto 1fr auto").style("container-overview-group")
setting = output().style("container-overview-group")
if self.is_mobile:
self.content.append(
put_column([
scheduler,
setting,
log,
self.alas_logs.output
], size="auto auto auto 1fr"
).style("height: 100%; overflow-y: auto")
)
else:
self.content.append(
put_row([
None,
put_column([
put_row([
scheduler,
log,
], size="auto auto"),
setting,
self.alas_logs.output
], size="auto minmax(6rem, auto) minmax(15rem, 1fr)"
).style("height: 100%; overflow-y: auto"),
None,
], size="1fr minmax(25rem, 6fr) 1fr"
).style("height: 100%; overflow-y: auto")
)
config = config_updater.update_config(self.alas_name)
for group, arg_dict in deep_iter(self.ALAS_ARGS[task], depth=1):
group = group[0]
setting.append(put_text(t(f"{group}._info.name")).style("group-title"))
group_help = t(f"{group}._info.help")
if group_help:
setting.append(put_text(group_help).style("group-help"))
list_arg = []
for arg, d in deep_iter(arg_dict, depth=1):
arg = arg[0]
arg_type = d['type']
if arg_type == 'disable':
continue
value = deep_get(config, f'{task}.{group}.{arg}', d['value'])
value = str(value) if isinstance(value, datetime) else value
# Option
options = deep_get(d, 'option', None)
if options:
option = []
for opt in options:
o = {"label": t(f"{group}.{arg}.{opt}"), "value": opt}
if value == opt:
o["selected"] = True
option.append(o)
else:
option = None
# Help
arg_help = t(f"{group}.{arg}.help")
if arg_help == "" or not arg_help:
arg_help = None
list_arg.append(get_output(
arg_type=arg_type,
name=self.path_to_idx[f"{task}.{group}.{arg}"],
title=t(f"{group}.{arg}.name"),
arg_help=arg_help,
value=value,
options=option,
))
if list_arg:
setting.append(*list_arg)
self.task_handler.add(self.alas_put_log(), 0.2, True)
# Develop
def dev_set_menu(self) -> None:
self.init_menu(collapse_menu=False, name="Develop")
self.menu.append(
put_buttons([
{"label": t("Gui.MenuDevelop.HomePage"),
"value": "HomePage", "color": "menu"}
], onclick=[self.show]).style(f'--menu-HomePage--'),
put_buttons([
{"label": t("Gui.MenuDevelop.Translate"),
"value": "Translate", "color": "menu"}
], onclick=[self.dev_translate]).style(f'--menu-Translate--'),
# put_buttons([
# {"label": t("Gui.MenuDevelop.Something"),
# "value": "Something", "color": "menu"}
# ], onclick=[self.dev_something]).style(f'--menu-Something--'),
)
def dev_translate(self) -> None:
go_app('translate', new_window=True)
lang.TRANSLATE_MODE = True
self.show()
# Aside UI route
def ui_develop(self) -> None:
self.init_aside(name="Develop")
self.title.reset(f"{t('Gui.Aside.Develop')}")
self.dev_set_menu()
self.alas_name = ''
if hasattr(self, 'alas'):
del self.alas
self.set_status(0)
def ui_alas(self, config_name: str) -> None:
if config_name == self.alas_name:
self.expand_menu()
return
self.init_aside(name=config_name)
self.content.reset()
self.alas_name = config_name
self.alas = AlasManager.get_alas(config_name)
self.alas_config = AzurLaneConfig(config_name, '')
self.alas_update_status()
self.alas_set_menu()
def ui_setting(self) -> None:
toast('Not implemented', position='right', color='error')
return
self.init_aside(name="Setting")
self.alas_name = ''
def ui_add_alas(self) -> None:
with popup(t("Gui.AddAlas.PopupTitle")) as s:
def get_unused_name():
all_name = alas_instance()
for i in range(2, 100):
if f'alas{i}' not in all_name:
return f'alas{i}'
else:
return ''
def add():
name = pin["AddAlas_name"]
origin = pin["AddAlas_copyfrom"]
if name not in alas_instance():
r = read_file(filepath_config(origin))
write_file(filepath_config(name), r)
self.set_aside()
self.active_button("aside", self.alas_name)
close_popup()
else:
clear(s)
put(name, origin)
put_error(t("Gui.AddAlas.FileExist"), scope=s)
def put(name=None, origin=None):
put_input(
name="AddAlas_name",
label=t("Gui.AddAlas.NewName"),
value=name or get_unused_name(),
scope=s
),
put_select(
name="AddAlas_copyfrom",
label=t("Gui.AddAlas.CopyFrom"),
options=['template'] + alas_instance(),
value=origin or 'template',
scope=s
),
put_button(
label=t("Gui.AddAlas.Confirm"),
onclick=add,
scope=s
)
put()
def show(self) -> None:
clear()
self.main_area.show()
self.set_aside()
self.collapse_menu()
self.alas_name = ''
if hasattr(self, 'alas'):
del self.alas
self.set_status(0)
def set_language(l):
lang.set_language(l)
self.show()
def set_theme(t):
"""
Theme support is not available in pywebio 1.4
You need to upgrade to 1.5 dev version manually by run
`pip install -U https://code.aliyun.com/wang0618/pywebio/repository/archive.zip`
"""
from pywebio.__version__ import __version__ as version
if 'dev' not in version and '1.4' in version:
toast("Not supported", position='right', color='error')
return
type(self).theme = t
webconfig(theme=t)
run_js("location.reload()")
# temporary buttons, there is no setting page now :(
self.content.append(
put_text("Select your language / 选择语言").style("text-align: center"),
put_buttons(
[{"label": "简体中文", "value": "zh-CN"},
{"label": "繁體中文", "value": "zh-TW"},
{"label": "English", "value": "en-US"},
{"label": "日本語", "value": "ja-JP"}],
onclick=lambda l: set_language(l),
).style("text-align: center"),
put_text("Change theme / 更改主题").style("text-align: center"),
put_buttons(
[{"label": "Light", "value": "default", "color": "light"},
{"label": "Dark", "value": "dark", "color": "dark"}],
onclick=lambda t: set_theme(t),
).style("text-align: center")
)
# show something
self.content.append(output(output(
put_markdown("""
Alas is a free open source software, if you paid for Alas from any channel, please refund.
Alas 是一款免费开源软件,如果你在任何渠道付费购买了Alas,请退款。
Project repository 项目地址:`https://github.com/LmeSzinc/AzurLaneAutoScript`
""")).style('text-align: center')))
# self.content.append(output(output(
# put_markdown("""
# ## AzurLaneAutoScript
# if you encounter any error or find a bug, [create new issue](https://github.com/LmeSzinc/AzurLaneAutoScript/issues/new/choose) or `@18870#0856` in discord with error logs.
# You may found logs in python console or browser console (`Ctrl`+`Shift`+`I` - `Console`)
# ![](https://i.loli.net/2021/10/03/5pNYoS8EFcvrhIs.png)
# ![](https://i.loli.net/2021/10/03/5xCaATjl6oK7S1f.png)
# ## Join in translation
# Go `Develop` - `Translate`
# """)).style('welcome')))
if lang.TRANSLATE_MODE:
lang.reload()
def _disable():
lang.TRANSLATE_MODE = False
self.show()
toast(_t("Gui.Toast.DisableTranslateMode"), duration=0, position='right', onclick=_disable)
def run(self) -> None:
# setup gui
set_env(title="Alas", output_animation=False)
add_css(filepath_css('alas'))
if self.is_mobile:
add_css(filepath_css('alas-mobile'))
else:
add_css(filepath_css('alas-pc'))
if self.theme == 'dark':
add_css(filepath_css('dark-alas'))
else:
add_css(filepath_css('light-alas'))
self.show()
# detect config change
_thread_wait_config_change = Thread(
target=self._alas_thread_wait_config_change)
register_thread(_thread_wait_config_change)
_thread_wait_config_change.start()
# save config
_thread_save_config = Thread(
target=self._alas_thread_update_config)
register_thread(_thread_save_config)
_thread_save_config.start()
def refresh_status():
while True:
yield self.alas_update_status()
def refresh_visibility_state():
_visible = self.visible
while True:
v = get_window_visibility_state()
if v != _visible:
self.task_handler.remove_running_task()
self.visible = v
if v:
logger.info("Window running in the foreground")
self.task_handler.add_task(
Task(refresh_visibility_state(), 15, time.time() + 15))
else:
logger.info("Window running in the background")
self.task_handler.add_task(
Task(refresh_visibility_state(), 2, time.time() + 2))
yield
self.task_handler.add(refresh_status(), 2)
self.task_handler.add(refresh_visibility_state(), 15)
self.task_handler.start()
def debug():
"""For interactive python.
$ python3
>>> from gui import *
>>> debug()
>>>
"""
AlasManager.sync_manager = Manager()
AlasGUI.shorten_path()
lang.reload()
AlasGUI().run()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Alas web service')
parser.add_argument('-p', '--port', type=int, default=22267,
help='Port to listen. Default to 22267')
parser.add_argument('-b', '--backend', type=str, default='starlette',
help='Backend framework of web server, starlette or tornado. Default to starlette')
parser.add_argument('-k', '--key', type=str, default='',
help='Password of alas. No password by default')
args = parser.parse_args()
AlasManager.sync_manager = Manager()
AlasGUI.shorten_path()
lang.reload()
def index():
if args.key != '' and not login(args.key):
logger.warning(f"{info.user_ip} login failed.")
time.sleep(1.5)
run_js('location.reload();')
return
AlasGUI().run()
if args.backend == 'starlette':
from pywebio.platform.fastapi import start_server
else:
from pywebio.platform.tornado import start_server
try:
start_server([index, translate], port=args.port, debug=True)
finally:
for alas in AlasManager.all_alas.values():
alas.stop()
logger.info("Alas closed.")