2021-10-05 01:02:43 +08:00
import argparse
2021-10-03 19:18:47 +08:00
import logging
2021-10-03 21:40:05 +08:00
import queue
import time
from multiprocessing import Manager , Process
2021-10-03 19:18:47 +08:00
from pywebio . exceptions import *
2021-10-14 23:10:03 +08:00
from pywebio . session import defer_call , go_app , info , register_thread , set_env
2021-10-05 01:02:43 +08:00
2021-10-03 19:18:47 +08:00
import module . webui . lang as lang
2021-10-05 01:02:43 +08:00
from module . config . config_updater import ConfigUpdater
2021-10-06 16:49:47 +08:00
from module . config . utils import *
from module . logger import logger # Change folder
2021-10-03 21:40:05 +08:00
from module . webui . lang import _t , t
2021-10-03 19:18:47 +08:00
from module . webui . translate import translate
2021-10-09 01:12:04 +08:00
from module . webui . utils import Icon , QueueHandler
2021-10-03 21:40:05 +08:00
from module . webui . utils import ThreadWithException as Thread
2021-10-14 23:10:03 +08:00
from module . webui . utils import ( active_button , add_css , filepath_css ,
get_output , login , parse_pin_value )
2021-10-03 21:40:05 +08:00
from module . webui . widgets import *
2021-10-03 19:18:47 +08:00
all_alas = { }
2021-10-05 01:02:43 +08:00
config_updater = ConfigUpdater ( )
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def get_alas ( config_name ) :
"""
Create a new alas if not exists .
"""
if config_name not in all_alas :
all_alas [ config_name ] = Alas ( config_name )
return all_alas [ config_name ]
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def run_alas ( config_name , q ) :
# Setup logger
qh = QueueHandler ( q )
2021-10-03 21:40:05 +08:00
formatter = logging . Formatter (
fmt = ' %(asctime)s . %(msecs)03d | %(levelname)s | %(message)s ' ,
datefmt = ' % Y- % m- %d % H: % M: % S ' )
2021-10-03 19:18:47 +08:00
webconsole = logging . StreamHandler ( stream = qh )
webconsole . setFormatter ( formatter )
logging . getLogger ( ' alas ' ) . addHandler ( webconsole )
# Run alas
from alas import AzurLaneAutoScript
AzurLaneAutoScript ( config_name = config_name ) . loop ( )
2021-10-03 21:40:05 +08:00
class Alas :
2021-10-03 19:18:47 +08:00
def __init__ ( self , config_name = ' alas ' ) :
self . config_name = config_name
self . manager = Manager ( )
self . log_queue = self . manager . Queue ( )
self . log = [ ]
self . log_max_length = 500
self . log_reduce_length = 100
self . process = Process ( )
2021-10-03 21:40:05 +08:00
self . thd_log_queue_handler = Thread ( )
2021-10-03 19:18:47 +08:00
def start ( self ) :
if not self . process . is_alive ( ) :
2021-10-03 21:40:05 +08:00
self . process = Process ( target = run_alas , args = (
self . config_name , self . log_queue , ) )
2021-10-03 19:18:47 +08:00
self . process . start ( )
2021-10-03 21:40:05 +08:00
self . thd_log_queue_handler = Thread (
target = self . _thread_log_queue_handler )
2021-10-03 19:18:47 +08:00
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 ) :
2021-10-10 01:26:58 +08:00
if self . process . is_alive ( ) :
self . process . terminate ( )
2021-10-14 23:10:03 +08:00
self . log . append ( " Scheduler stopped. \n " )
2021-10-10 01:26:58 +08:00
if self . thd_log_queue_handler . is_alive ( ) :
self . thd_log_queue_handler . stop ( )
2021-10-03 19:18:47 +08:00
def _thread_log_queue_handler ( self ) :
while self . process . is_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 : ]
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
ALAS_MENU = read_file ( filepath_args ( ' menu ' ) )
ALAS_ARGS = read_file ( filepath_args ( ' args ' ) )
# Reduce pin_wait_change() command content-length
2021-10-03 21:40:05 +08:00
# Using full path name will transfer ~16KB per command,
2021-10-03 19:18:47 +08:00
# may lag when remote control or in bad internet condition.
# Use ~4KB after doing this.
path_to_idx = { }
idx_to_path = { }
2021-10-03 21:40:05 +08:00
def shorten_path ( ) :
i = 0
2021-10-06 23:22:56 +08:00
for list_path , _ in deep_iter ( ALAS_ARGS , depth = 3 ) :
path_to_idx [ ' . ' . join ( list_path ) ] = f ' a { i } '
idx_to_path [ f ' a { i } ' ] = ' . ' . join ( list_path )
2021-10-03 21:40:05 +08:00
i + = 1
shorten_path ( )
2021-10-03 19:18:47 +08:00
class AlasGUI :
alas : Alas
def __init__ ( self ) :
self . modified_config_queue = queue . Queue ( )
self . _thread_kill_after_leave = [ ]
2021-10-03 21:40:05 +08:00
self . alas_name = ' alas '
self . alive = True
self . aside = output ( ) . style ( " container-aside " )
self . menu = output ( ) . style ( " container-menu " )
2021-10-12 13:38:21 +08:00
self . content = output ( ) . style ( " container-content " )
2021-10-03 21:40:05 +08:00
self . title = output ( ) . style ( " title-text-title " )
2021-10-14 13:31:54 +08:00
self . status = output ( ) . style ( " title-status " )
self . _status = 0
2021-10-12 13:38:21 +08:00
self . header = put_row ( [
put_html ( Icon . ALAS ) . style ( " title-icon-alas " ) ,
put_text ( " Alas " ) . style ( " title-text-alas " ) ,
2021-10-14 13:31:54 +08:00
self . status ,
2021-10-12 13:38:21 +08:00
self . title ,
2021-10-14 13:31:54 +08:00
] , size = " 5.6rem 3.75rem 8rem minmax(8rem, 65rem) " ) . style ( " container-title " )
2021-10-12 13:38:21 +08:00
self . asides = put_column ( [
self . aside ,
None ,
put_icon_buttons (
Icon . SETTING ,
buttons = [
{ " label " : t ( " Gui.Aside.Setting " ) ,
" value " : " setting " , " color " : " aside " } ] ,
onclick = [ self . ui_setting ] ,
) . style ( " aside-icon-setting " ) ,
] , size = " auto 1fr auto " ) . style ( " container-aside " )
self . contents = put_row ( [
self . asides ,
self . menu ,
self . content ,
] , size = " auto 12rem 1fr " ) . style ( " container-main " )
self . main_area = output (
put_column ( [
self . header ,
self . contents ,
] , size = " auto 1fr " ) . style ( " container-all " )
) . style ( " container-gui " )
2021-10-16 16:52:35 +08:00
2021-10-03 21:40:05 +08:00
self . logs = ScrollableCode ( )
2021-10-03 19:18:47 +08:00
def set_aside ( self ) :
self . aside . reset ( )
# 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.
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
self . aside . append (
2021-10-03 21:40:05 +08:00
# put_icon_buttons(Icon.INSTALL, buttons=[{"label": t(
# "Gui.Aside.Install"), "value": "install", "color": "aside"}], onclick=[self.ui_install]),
put_icon_buttons ( Icon . DEVELOP , buttons = [ { " label " : t (
" Gui.Aside.Develop " ) , " value " : " develop " , " color " : " aside " } ] , onclick = [ self . ui_develop ] ) ,
# put_icon_buttons(Icon.PERFORMANCE, buttons=[{"label": t(
# "Gui.Aside.Performance"), "value": "performance", "color": "aside"}], onclick=[self.ui_performance]),
2021-10-03 19:18:47 +08:00
)
self . aside . append (
2021-10-16 16:52:35 +08:00
* [ put_icon_buttons (
Icon . RUN , buttons = [ { " label " : name , " value " : name , " color " : " aside " } ] ,
onclick = self . ui_alas )
for name in alas_instance ( ) ]
2021-10-03 19:18:47 +08:00
)
def kill_thread ( self ) :
thd : Thread
for thd in self . _thread_kill_after_leave :
thd . stop ( )
2021-10-14 13:31:54 +08:00
def set_status ( self , status : int ) :
"""
Args :
status ( int ) :
1 ( running ) ,
2 ( not running ) ,
- 1 ( warning , stop unexpectedly ) ,
0 ( hide )
"""
if self . _status == status :
return
2021-10-14 23:10:03 +08:00
self . _status = status
2021-10-16 16:52:35 +08:00
2021-10-14 13:31:54 +08:00
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 = ' '
2021-10-16 16:52:35 +08:00
2021-10-14 13:31:54 +08:00
self . status . reset ( s )
2021-10-03 19:18:47 +08:00
# Alas
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def alas_set_menu ( self ) :
"""
Set menu for alas
"""
self . menu . reset ( )
2021-10-12 13:38:21 +08:00
self . content . reset ( )
2021-10-03 19:18:47 +08:00
self . kill_thread ( )
self . menu . append (
put_buttons ( [
2021-10-03 21:40:05 +08:00
{ " label " : t ( " Gui.MenuAlas.Overview " ) ,
" value " : " Overview " , " color " : " menu " }
2021-10-14 23:10:03 +08:00
] , onclick = [ self . alas_overview ] ) . style ( f ' --menu-Overview-- ' ) ,
2021-10-03 19:18:47 +08:00
# put_buttons([
# {"label": t("Gui.MenuAlas.Log"), "value": "Log", "color": "menu"}
2021-10-14 23:10:03 +08:00
# ], onclick=[self.alas_log]).style(f'--menu-Log--'),
2021-10-03 19:18:47 +08:00
)
2021-10-12 13:38:21 +08:00
for key , tasks in deep_iter ( ALAS_MENU , depth = 2 ) :
2021-10-03 19:18:47 +08:00
# path = '.'.join(key)
menu = key [ 1 ]
self . menu . append (
2021-10-03 21:40:05 +08:00
put_collapse ( t ( f " Menu. { menu } .name " ) ,
[ put_buttons ( [
{ " label " : t ( f ' Task. { task } .name ' ) ,
" value " : task , " color " : " menu " }
2021-10-14 23:10:03 +08:00
] , onclick = self . alas_set_group ) . style ( f ' --menu- { task } -- ' ) for task in tasks ]
2021-10-03 21:40:05 +08:00
)
2021-10-03 19:18:47 +08:00
)
2021-10-16 16:52:35 +08:00
2021-10-14 23:10:03 +08:00
self . alas_overview ( )
2021-10-03 19:18:47 +08:00
2021-10-03 21:40:05 +08:00
def alas_set_group ( self , task ) :
2021-10-03 19:18:47 +08:00
"""
Set arg groups from dict
"""
self . title . reset ( f " { self . alas_name } - { t ( f ' Task. { task } .name ' ) } " )
2021-10-12 13:38:21 +08:00
self . content . reset ( )
2021-10-03 19:18:47 +08:00
self . kill_thread ( )
2021-10-14 23:10:03 +08:00
active_button ( ' menu ' , task )
2021-10-03 19:18:47 +08:00
group_area = output ( )
navigator = output ( )
2021-10-06 23:22:56 +08:00
content_alas = put_row ( [
2021-10-03 19:18:47 +08:00
None ,
group_area ,
navigator ,
] , size = " .5fr minmax(25rem, 5fr) 2fr " )
2021-10-12 13:38:21 +08:00
self . content . append ( content_alas )
2021-10-03 19:18:47 +08:00
2021-10-05 01:02:43 +08:00
config = config_updater . update_config ( self . alas_name )
2021-10-03 19:18:47 +08:00
for group , arg_dict in deep_iter ( ALAS_ARGS [ task ] , depth = 1 ) :
group = group [ 0 ]
2021-10-06 23:22:56 +08:00
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 )
2021-10-12 13:55:24 +08:00
group_area . append ( arg_group )
2021-10-03 19:18:47 +08:00
for arg , d in deep_iter ( arg_dict , depth = 1 ) :
arg = arg [ 0 ]
2021-10-03 21:40:05 +08:00
arg_type = d [ ' type ' ]
2021-10-03 19:18:47 +08:00
value = deep_get ( config , f ' { task } . { group } . { arg } ' , d [ ' value ' ] )
2021-10-05 01:02:43 +08:00
value = str ( value ) if isinstance ( value , datetime ) else value
2021-10-03 19:18:47 +08:00
# Option
options = deep_get ( d , ' option ' , None )
if options :
option = [ ]
2021-10-06 23:22:56 +08:00
for opt in options :
o = { " label " : t ( f " { group } . { arg } . { opt } " ) , " value " : opt }
if value == opt :
o [ " selected " ] = True
option . append ( o )
2021-10-03 19:18:47 +08:00
else :
option = None
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
# Help
arg_help = t ( f " { group } . { arg } .help " )
if arg_help == " " or not arg_help :
arg_help = None
2021-10-12 13:55:24 +08:00
arg_group . append ( get_output (
2021-10-03 21:40:05 +08:00
arg_type = arg_type ,
2021-10-03 19:18:47 +08:00
name = path_to_idx [ f " { task } . { group } . { arg } " ] ,
title = t ( f " { group } . { arg } .name " ) ,
2021-10-03 21:40:05 +08:00
arg_help = arg_help ,
2021-10-03 19:18:47 +08:00
value = value ,
options = option ,
2021-10-12 13:55:24 +08:00
) )
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def alas_overview ( self ) :
self . title . reset ( f " { self . alas_name } - { t ( f ' Gui.MenuAlas.Overview ' ) } " )
2021-10-12 13:38:21 +08:00
self . content . reset ( )
2021-10-03 19:18:47 +08:00
self . kill_thread ( )
2021-10-14 23:10:03 +08:00
active_button ( ' menu ' , ' Overview ' )
2021-10-03 21:40:05 +08:00
2021-10-12 13:38:21 +08:00
self . content . append (
2021-10-03 19:18:47 +08:00
put_column ( [
2021-10-09 17:20:59 +08:00
self . logs . output ,
2021-10-03 21:40:05 +08:00
put_buttons (
buttons = [ ' Start ' , ' Stop ' , ' Scroll ON ' , ' Scroll OFF ' ] ,
onclick = [
self . alas . start ,
self . alas . stop ,
lambda : self . logs . set_scroll ( True ) ,
lambda : self . logs . set_scroll ( False )
]
) ,
2021-10-03 19:18:47 +08:00
] , size = " auto 3rem " ) . style ( " height: 100 % " )
)
thd = Thread ( target = self . _alas_thread_put_log )
register_thread ( thd )
thd . start ( )
self . _thread_kill_after_leave . append ( thd )
def alas_log ( self ) :
toast ( ' Not implemented ' , position = ' right ' , color = ' error ' )
2021-10-14 23:10:03 +08:00
return
active_button ( ' menu ' , ' Log ' )
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def _alas_thread_wait_config_change ( self ) :
paths = [ ]
2021-10-06 23:22:56 +08:00
for path , d in deep_iter ( ALAS_ARGS , depth = 3 ) :
2021-10-03 19:18:47 +08:00
if d [ ' type ' ] == ' disable ' :
continue
2021-10-06 23:22:56 +08:00
paths . append ( path_to_idx [ ' . ' . join ( path ) ] )
2021-10-09 01:12:04 +08:00
while self . alive :
2021-10-03 19:18:47 +08:00
try :
2021-10-14 13:31:54 +08:00
val = pin_wait_change ( paths )
2021-10-09 01:12:04 +08:00
self . modified_config_queue . put ( val )
2021-10-03 19:18:47 +08:00
except SessionClosedException :
break
def _alas_thread_update_config ( self ) :
modified = { }
while self . alive :
try :
d = self . modified_config_queue . get ( timeout = 10 )
2021-10-14 23:10:03 +08:00
config_name = self . alas_name
2021-10-03 21:40:05 +08:00
except queue . Empty :
2021-10-03 19:18:47 +08:00
continue
modified [ idx_to_path [ d [ ' name ' ] ] ] = parse_pin_value ( d [ ' value ' ] )
while True :
try :
d = self . modified_config_queue . get ( timeout = 1 )
2021-10-06 16:49:47 +08:00
modified [ idx_to_path [ d [ ' name ' ] ] ] = parse_pin_value ( d [ ' value ' ] )
2021-10-03 19:18:47 +08:00
except queue . Empty :
2021-10-15 00:20:32 +08:00
config = read_file ( filepath_config ( config_name ) )
2021-10-03 19:18:47 +08:00
for k in modified . keys ( ) :
deep_set ( config , k , modified [ k ] )
2021-10-14 23:10:03 +08:00
logger . info ( f ' Save config { filepath_config ( config_name ) } , { dict_to_kv ( modified ) } ' )
write_file ( filepath_config ( config_name ) , config )
2021-10-03 21:40:05 +08:00
toast ( t ( " Gui.Toast.ConfigSaved " ) ,
2021-10-09 01:12:04 +08:00
duration = 1 , position = ' right ' , color = ' success ' )
2021-10-13 19:15:23 +08:00
modified = { }
2021-10-03 19:18:47 +08:00
break
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def _alas_thread_put_log ( self ) :
last_idx = len ( self . alas . log )
self . logs . append ( ' ' . join ( self . alas . log ) )
self . lines = 0
time . sleep ( 1 )
while True :
time . sleep ( 0.2 )
idx = len ( self . alas . log )
if idx < last_idx :
last_idx - = self . alas . log_reduce_length
if idx != last_idx :
try :
self . logs . append ( ' ' . join ( self . alas . log [ last_idx : idx ] ) )
except SessionNotFoundException :
break
self . lines + = idx - last_idx
last_idx = idx
2021-10-14 13:31:54 +08:00
def alas_update_status ( self ) :
if hasattr ( self , ' alas ' ) :
if self . alas . process . is_alive ( ) :
self . set_status ( 1 )
2021-10-14 23:10:03 +08:00
elif len ( self . alas . log ) == 0 or self . alas . log [ - 1 ] == " Scheduler stopped. \n " :
2021-10-14 13:31:54 +08:00
self . set_status ( 2 )
else :
self . set_status ( - 1 )
else :
self . set_status ( 0 )
def _alas_thread_refresh_status ( self ) :
while self . alive :
self . alas_update_status ( )
time . sleep ( 5 )
2021-10-03 19:18:47 +08:00
# Develop
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def dev_set_menu ( self ) :
self . menu . reset ( )
2021-10-12 13:38:21 +08:00
self . content . reset ( )
2021-10-09 01:12:04 +08:00
self . title . reset ( f " { t ( ' Gui.Aside.Develop ' ) } " )
2021-10-03 19:18:47 +08:00
self . kill_thread ( )
self . menu . append (
put_buttons ( [
2021-10-03 21:40:05 +08:00
{ " label " : t ( " Gui.MenuDevelop.Translate " ) ,
" value " : " Translate " , " color " : " menu " }
2021-10-14 23:10:03 +08:00
] , onclick = [ self . dev_translate ] ) . style ( f ' --menu-Translate-- ' ) ,
2021-10-03 19:18:47 +08:00
# put_buttons([
2021-10-09 01:12:04 +08:00
# {"label": t("Gui.MenuDevelop.Something"),
# "value": "Something", "color": "menu"}
2021-10-14 23:10:03 +08:00
# ], onclick=[self.dev_something]).style(f'--menu-Something--'),
2021-10-03 19:18:47 +08:00
)
def dev_translate ( self ) :
go_app ( ' translate ' , new_window = True )
lang . TRANSLATE_MODE = True
run_js ( " location.reload(); " )
# Aside UI route
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
def ui_develop ( self ) :
self . dev_set_menu ( )
2021-10-14 23:10:03 +08:00
active_button ( ' aside ' , ' develop ' )
2021-10-03 19:18:47 +08:00
def ui_performance ( self ) :
toast ( ' Not implemented ' , position = ' right ' , color = ' error ' )
def ui_alas ( self , config_name ) :
self . alas_name = config_name
self . alas = get_alas ( config_name )
self . title . reset ( f " { self . alas_name } " )
2021-10-14 13:31:54 +08:00
self . alas_update_status ( )
2021-10-14 23:10:03 +08:00
active_button ( ' aside ' , config_name )
2021-10-03 19:18:47 +08:00
self . alas_set_menu ( )
def ui_setting ( self ) :
toast ( ' Not implemented ' , position = ' right ' , color = ' error ' )
2021-10-14 23:10:03 +08:00
return
active_button ( ' aside ' , ' setting ' )
2021-10-03 19:18:47 +08:00
2021-10-03 19:52:03 +08:00
def stop ( self ) :
self . alive = False
2021-10-03 19:18:47 +08:00
def run ( self ) :
# setup gui
set_env ( title = " Alas " , output_animation = False )
add_css ( filepath_css ( ' alas ' ) )
2021-10-03 19:52:03 +08:00
defer_call ( self . stop )
2021-10-12 13:38:21 +08:00
self . main_area . show ( )
2021-10-03 19:18:47 +08:00
self . set_aside ( )
if lang . TRANSLATE_MODE :
2021-10-14 23:10:03 +08:00
lang . reload ( )
2021-10-03 19:18:47 +08:00
def _disable ( ) :
lang . TRANSLATE_MODE = False
run_js ( " location.reload(); " )
2021-10-06 16:49:47 +08:00
2021-10-03 19:18:47 +08:00
toast ( _t ( " Gui.Toast.DisableTranslateMode " ) , duration = 0 , position = ' right ' , onclick = _disable )
# show something
2021-10-12 13:38:21 +08:00
self . content . append ( output ( output (
2021-10-03 19:18:47 +08:00
put_markdown ( """
## AzurLaneAutoScript
This new UI is still under development .
2021-10-09 01:12:04 +08:00
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 ` )
2021-10-03 19:18:47 +08:00
! [ ] ( https : / / i . loli . net / 2021 / 10 / 03 / 5 pNYoS8EFcvrhIs . png )
! [ ] ( https : / / i . loli . net / 2021 / 10 / 03 / 5 xCaATjl6oK7S1f . png )
## Join in translation
2021-10-09 01:12:04 +08:00
Go ` Develop ` - ` Translate `
2021-10-03 19:18:47 +08:00
""" , strip_indent=12)).style( ' welcome ' )))
2021-10-03 21:40:05 +08:00
2021-10-03 19:18:47 +08:00
# temporary buttons, there is no setting page now :(
2021-10-12 13:38:21 +08:00
self . content . append (
2021-10-06 23:22:56 +08:00
put_text ( " Select your language " ) . style ( " text-align: center " ) ,
2021-10-03 21:40:05 +08:00
put_buttons (
[ " zh-CN " , " zh-TW " , " en-US " , " ja-JP " ] ,
onclick = lambda s : lang . set_language ( s , True )
) . style ( " text-align: center " )
)
2021-10-03 19:18:47 +08:00
# detect config change
2021-10-03 21:40:05 +08:00
_thread_wait_config_change = Thread (
target = self . _alas_thread_wait_config_change )
2021-10-03 19:18:47 +08:00
register_thread ( _thread_wait_config_change )
_thread_wait_config_change . start ( )
# save config
2021-10-14 13:31:54 +08:00
_thread_save_config = Thread (
target = self . _alas_thread_update_config )
2021-10-03 19:18:47 +08:00
register_thread ( _thread_save_config )
_thread_save_config . start ( )
2021-10-14 13:31:54 +08:00
# refresh status
_thread_refresh_status = Thread (
target = self . _alas_thread_refresh_status )
register_thread ( _thread_refresh_status )
_thread_refresh_status . start ( )
2021-10-03 19:18:47 +08:00
if __name__ == " __main__ " :
2021-10-05 01:02:43 +08:00
parser = argparse . ArgumentParser ( description = ' Alas web service ' )
parser . add_argument ( " -d " , " --debug " , action = " store_true " ,
help = " show log " )
2021-10-09 01:12:04 +08:00
parser . add_argument ( ' -p ' , ' --port ' , type = int , default = 22267 ,
help = ' Port to listen. Default to 22267 ' )
2021-10-05 01:02:43 +08:00
parser . add_argument ( ' -b ' , ' --backend ' , type = str , default = ' starlette ' ,
help = ' Backend framework of web server, starlette or tornado. Default to starlette ' )
2021-10-09 17:20:59 +08:00
parser . add_argument ( ' -k ' , ' --key ' , type = str , default = ' ' ,
help = ' Password of alas. No password by default ' )
2021-10-05 01:02:43 +08:00
args = parser . parse_args ( )
2021-10-03 19:18:47 +08:00
def index ( ) :
2021-10-09 17:20:59 +08:00
if args . key != ' ' and not login ( args . key ) :
logger . warning ( f " { info . user_ip } login failed. " )
time . sleep ( 2 )
run_js ( ' location.reload(); ' )
return
2021-10-03 19:18:47 +08:00
AlasGUI ( ) . run ( )
2021-10-03 21:40:05 +08:00
2021-10-05 01:02:43 +08:00
if args . backend == ' starlette ' :
2021-10-03 19:18:47 +08:00
from pywebio . platform . fastapi import start_server
else :
from pywebio . platform . tornado import start_server
2021-10-03 21:40:05 +08:00
2021-10-06 16:49:47 +08:00
try :
start_server ( [ index , translate ] , port = args . port , debug = args . debug )
finally :
for alas in all_alas . values ( ) :
alas . stop ( )
logger . info ( " Alas closed. " )