-
Notifications
You must be signed in to change notification settings - Fork 151
/
main.py
93 lines (72 loc) · 2.08 KB
/
main.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
import argparse
import logging
import gevent.monkey
gevent.monkey.patch_ssl()
gevent.monkey.patch_socket()
import sys
from config import config, dump_config
from utils.webview2 import can_use_webview
logger = logging.getLogger(__name__)
def start_ui(mode=None):
import ui
ui.main(mode=mode)
return 0
def start_webview_ui():
import ui_webview
ui_webview.main()
return 0
def try_start_webview():
try:
return start_webview_ui()
except Exception as e:
logger.error('Error occur in start_webview_ui', e)
return fallback_to_browser()
def fallback_to_browser():
config.setting.ui.mode = 'browser'
dump_config()
return start_ui(None)
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--mode",
choices=['webview', 'browser', 'chrome', 'edge', 'user default'],
help="指定 ui 启动方式",
)
parser.add_argument(
"--switch-mode",
choices=['auto', 'webview', 'browser', 'chrome', 'edge', 'user default'],
help="切换 ui 启动方式",
)
parser.add_argument(
"--no-sentry",
action='store_true',
help="禁用 sentry",
)
return parser
def main():
parser = create_parser()
args = parser.parse_args()
logger.info(f'args: {args}')
if args.switch_mode is not None:
logger.info(f'switch mode: {args.switch_mode}')
config.setting.ui.mode = args.switch_mode
dump_config()
return 0
from module.external.bat_scripts import create_scripts
create_scripts()
if not args.no_sentry:
from module.sentry import init_sentry
init_sentry()
ui_mode = args.mode or config.setting.ui.mode
logger.info(f'ui mode: {ui_mode}')
if ui_mode is None or ui_mode == 'auto':
ui_mode = 'webview' if can_use_webview() else 'browser'
if ui_mode == 'browser':
return start_ui(None)
elif ui_mode == 'webview':
return try_start_webview()
else:
return start_ui(ui_mode)
if __name__ == '__main__':
sys.exit(main())