-
Notifications
You must be signed in to change notification settings - Fork 74
/
run_GalTransl.py
140 lines (120 loc) · 4.84 KB
/
run_GalTransl.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
import os
import sys
from command import BulletMenu
from GalTransl import (
AUTHOR,
CONFIG_FILENAME,
CONTRIBUTORS,
GALTRANSL_VERSION,
PROGRAM_SPLASH,
TRANSLATOR_SUPPORTED,
)
from GalTransl.__main__ import worker
INPUT_PROMPT_TMP = "请输入/拖入项目文件夹,或项目文件夹内的yaml配置文件[default]:"
class ProjectManager:
def __init__(self):
self.user_input = ""
self.project_dir = ""
self.config_file_name = CONFIG_FILENAME
self.translator = ""
def validate_project_path(self, user_input):
user_input = os.path.abspath(user_input)
if user_input.endswith(".yaml"):
config_file_name = os.path.basename(user_input)
project_dir = os.path.dirname(user_input)
else:
config_file_name = CONFIG_FILENAME
project_dir = user_input
if not os.path.exists(os.path.join(project_dir, config_file_name)):
print(
f"{project_dir}文件夹内不存在配置文件{config_file_name},请检查后重新输入\n"
)
return None, None, None
return user_input, project_dir, config_file_name
def get_user_input(self):
while True:
input_prompt = INPUT_PROMPT_TMP.replace(
"[default]",
f"(留空继续『{self.project_name()}』项目)" if self.project_dir else "",
)
user_input = input(input_prompt).strip('"') or self.user_input
if not user_input:
continue
user_input = user_input.strip('"').strip("'")
self.user_input, self.project_dir, self.config_file_name = (
self.validate_project_path(user_input)
)
if not self.project_dir:
continue
return
def print_program_info(self):
print(PROGRAM_SPLASH)
print(f"Ver: {GALTRANSL_VERSION}")
print(f"Author: {AUTHOR}")
print(f"Contributors: {CONTRIBUTORS}\n")
def choose_translator(self):
default_choice = (
list(TRANSLATOR_SUPPORTED.keys()).index(self.translator)
if self.translator
else 0
)
os.system("") # 解决cmd的ANSI转义bug
self.translator = BulletMenu(
f"请为『{self.project_name()}』项目选择翻译器:", TRANSLATOR_SUPPORTED
).run(default_choice)
def project_name(self):
return self.project_dir.split(os.sep)[-1] if self.project_dir else ""
def create_shortcut_win(self):
TEMPLATE = '@echo off\nchcp 65001\nset "CURRENT_PATH=%CD%"\ncd /d "{0}"\n{1} "{2}" {3}\npause\ncd /d "%CURRENT_PATH%"'
run_com = "python.exe " + os.path.basename(__file__)
program_dir = os.path.dirname(os.path.abspath(__file__))
shortcut_path = f"{self.project_dir}{os.sep}run_GalTransl_v{GALTRANSL_VERSION}_{self.translator}.bat"
conf_path = "%CURRENT_PATH%\\" + self.config_file_name
if "nt" not in os.name: # not windows
return
if getattr(sys, "frozen", False): # PyInstaller
run_com = os.path.basename(sys.executable)
program_dir = os.path.dirname(sys.executable)
with open(shortcut_path, "w", encoding="utf-8") as f:
text = TEMPLATE.format(program_dir, run_com, conf_path, self.translator)
f.write(text)
def run(self):
# 检查命令行参数
if len(sys.argv) > 1:
self.user_input = sys.argv[1]
self.user_input, self.project_dir, self.config_file_name = (
self.validate_project_path(self.user_input)
)
if len(sys.argv) > 2 and sys.argv[2] in TRANSLATOR_SUPPORTED.keys():
self.translator = sys.argv[2]
while True:
self.print_program_info()
# 如果初始路径无效或未提供,进入交互式输入阶段
if not self.project_dir:
try:
self.get_user_input()
except KeyboardInterrupt:
print("\nGoodbye.")
return
if not self.translator:
try:
self.choose_translator()
except KeyboardInterrupt:
print("\nGoodbye.")
return
if self.translator not in ["show-plugs", "dump-name"]:
self.create_shortcut_win()
worker(
self.project_dir,
self.config_file_name,
self.translator,
show_banner=False,
)
print("翻译任务完成,准备重新开始...")
self.user_input = ""
self.translator = ""
os.system("pause")
os.system("cls")
if __name__ == "__main__":
manager = ProjectManager()
manager.run()