-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.py
357 lines (308 loc) · 13 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
# encoding: utf-8
import ctypes
import json
import webbrowser
import sys
from tkinter import *
from tkinter import scrolledtext, filedialog, messagebox
from tkinter.ttk import *
import os
import subprocess
import processer
WHERE_SCRIPT = os.path.split(os.path.realpath(__file__))[0]
class myStdout():
'''
重定向类
'''
def __init__(self):
# 将其备份
self.stdoutbak = sys.stdout
self.stderrbak = sys.stderr
# 重定向
sys.stdout = self
sys.stderr = self
def write(self, info):
# info信息即标准输出sys.stdout和sys.stderr接收到的输出信息
debug_scrolledtext.insert('end', info) # 在多行文本控件最后一行插入print信息
debug_scrolledtext.update() # 更新显示的文本,不加这句插入的信息无法显示
# 始终显示最后一行,不加这句,当文本溢出控件最后一行时,不会自动显示最后一行
debug_scrolledtext.see(END)
def restoreStd(self):
# 恢复标准输出
sys.stdout = self.stdoutbak
sys.stderr = self.stderrbak
# 输出重定向
mystd = myStdout()
# 创建窗口,main_window可替换成自己定义的窗口
main_window = Tk()
# 调用api设置成由应用程序缩放
ctypes.windll.shcore.SetProcessDpiAwareness(1)
# 调用api获得当前的缩放因子
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
# 设置缩放因子
main_window.tk.call('tk', 'scaling', ScaleFactor/72)
main_window.resizable(0, 0)
BASE_DIR = StringVar()
MARKDOWN_FILE = StringVar()
METADATA_FILE = StringVar()
REF_FILE = StringVar()
OUTPUT_FILE = StringVar()
NO_BLANK_BACK_COVER = IntVar()
def select_file(*, defaultextension=None, filetypes=[]):
'''
选择文件
'''
file_path = filedialog.askopenfilename(
defaultextension=defaultextension, filetypes=filetypes)
return file_path
def select_dir():
'''
选择目录
'''
dir_path = filedialog.askdirectory()
return dir_path
def save_as():
'''
另存为
'''
dir_path = filedialog.asksaveasfilename(initialfile="result",
defaultextension=".docx",
filetypes=[
("Word 文档 (*.docx)", ".docx")])
return dir_path
def set_base_dir(*, overwrite=None):
'''
设置基础目录
'''
if overwrite is None:
BASE_DIR.set(select_dir())
for file in os.listdir(BASE_DIR.get()):
if os.path.splitext(file)[-1][1:] == "md":
set_markdown_file(overwrite=os.path.join(BASE_DIR.get(), file))
elif os.path.splitext(file)[-1][1:] == "yaml":
set_metadata_file(overwrite=os.path.join(BASE_DIR.get(), file))
elif os.path.splitext(file)[-1][1:] == "bib":
set_ref_file(overwrite=os.path.join(BASE_DIR.get(), file))
else:
BASE_DIR.set(overwrite)
entry_base_dir.after_idle(entry_base_dir.xview_moveto, 1)
def set_markdown_file(*, overwrite=None):
'''
设置 markdown 文件
'''
MARKDOWN_FILE.set(select_file(defaultextension=".md", filetypes=[
("Markdown (*.md)", ".md"), ("Markdown (*.markdown)", ".markdown"), ("全部 (*.*)", ".*")]) if overwrite is None else overwrite)
entry_markdown_file.after_idle(entry_markdown_file.xview_moveto, 1)
def set_metadata_file(*, overwrite=None):
'''
设置元数据文件
'''
METADATA_FILE.set(select_file(defaultextension=".yaml", filetypes=[
("YAML (*.yaml)", ".yaml"), ("全部 (*.*)", ".*")]) if overwrite is None else overwrite)
entry_metadata_file.after_idle(entry_metadata_file.xview_moveto, 1)
def set_ref_file(*, overwrite=None):
'''
设置 BibTeX 参考文献引文文件
'''
REF_FILE.set(select_file(defaultextension=".yaml", filetypes=[
("BibTeX Ref (*.bib)", ".bib"), ("全部 (*.*)", ".*")]) if overwrite is None else overwrite)
entry_ref_file.after_idle(entry_ref_file.xview_moveto, 1)
def set_output_file(*, overwrite=None):
'''
设置输出文件
'''
OUTPUT_FILE.set(save_as() if overwrite is None else overwrite)
entry_output_file.after_idle(entry_output_file.xview_moveto, 1)
def detect_venv():
'''
监测 venv
:return: 如果是 venv 环境,True,否则 False
'''
if os.path.exists(os.path.join(WHERE_SCRIPT, "venv")):
return True
else:
return False
def decide_python():
'''
决定使用哪个 python
:return: 如果是 venv 环境,返回 venv 下的 python,否则返回系统 python
'''
if detect_venv():
if os.path.exists(os.path.join(WHERE_SCRIPT, "venv/bin")):
return os.path.join(WHERE_SCRIPT, "venv/bin/python")
elif os.path.exists(os.path.join(WHERE_SCRIPT, "venv/Scripts")):
return os.path.join(WHERE_SCRIPT, "venv/Scripts/python")
else:
return "python"
def generate():
'''
调用 processer.py 进行处理
'''
print("\n*** 开始生成... ***\n")
if (BASE_DIR.get() == ""
or MARKDOWN_FILE.get() == ""
or METADATA_FILE.get() == ""
or OUTPUT_FILE.get() == ""):
messagebox.showerror('错误', '存在尚未填充的字段。请填充所有带星号(*)的字段。')
return
gui_config = {
"base_dir": BASE_DIR.get(),
"markdown_file": MARKDOWN_FILE.get(),
"metadata_file": METADATA_FILE.get(),
"ref_file": REF_FILE.get(),
"output_file": OUTPUT_FILE.get(),
"no_blank_back_cover": NO_BLANK_BACK_COVER.get()
}
with open(os.path.join(WHERE_SCRIPT, ".gui_config.json"), 'w', encoding='utf-8') as gui_config_json:
json.dump(gui_config, gui_config_json, indent=4, ensure_ascii=False)
command = ('"%s" "%s" ' % (decide_python(), os.path.join(WHERE_SCRIPT, 'processer.py'))
+ '-F "%s" ' % MARKDOWN_FILE.get() # 输入 markdown 文件
+ ('-B "%s" ' %
REF_FILE.get() if REF_FILE.get() != "" else '') # 引文文件
+ '-M "%s" ' % METADATA_FILE.get() # 元数据文件
+ '-O "%s" ' % OUTPUT_FILE.get() # 输出文件
+ ('--no-blank-back-cover ' if REF_FILE.get()
== 1 else '') # 是否生成空白封底
)
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
while subprocess.Popen.poll(p) is None:
if(p.stdout is not None):
print(p.stdout.read())
# elif(p.stderr is not None):
# print(p.stderr.read())
print("****************************\n\n")
if(subprocess.Popen.poll(p) == 0):
messagebox.showinfo('成功', 'docx 生成成功,位置:%s' % OUTPUT_FILE.get())
else:
messagebox.showerror('错误', '生成失败。')
def open_project_site():
'''
打开项目主页
'''
webbrowser.open(
"https://github.com/foldblade/XUJC-thesis-markdown", new=0, autoraise=True)
def open_document_site():
'''
打开文档站
'''
webbrowser.open(
"https://foldblade.github.io/XUJC-thesis-markdown", new=0, autoraise=True)
def generate_scaffold():
'''
生成脚手架
'''
dest = filedialog.askdirectory(title="选择目标文件夹")
if (dest == ''):
return
if(len(os.listdir(dest)) != 0):
messagebox.showerror('错误', '目标目录不为空。')
return
print("*** 开始生成脚手架... ***\n")
os.removedirs(dest)
command = '%s "%s" --new "%s"' % (decide_python(),
os.path.join(WHERE_SCRIPT, 'processer.py'), dest)
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
while subprocess.Popen.poll(p) is None:
if(p.stdout is not None):
print(p.stdout.read())
# elif(p.stderr is not None):
# print(p.stderr.read())
print("****************************\n\n")
if(subprocess.Popen.poll(p) == 0):
messagebox.showinfo('成功', "脚手架生成成功,位置:%s" % dest)
else:
messagebox.showerror('错误', '生成失败。')
def check_update():
'''
检查更新
'''
maybe_update = processer.check_update()
if(maybe_update) is not None:
print("最新版本是: v%s" % maybe_update)
print(
"前往下载页:https://github.com/Foldblade/XUJC-thesis-markdown/releases/latest")
response = messagebox.askquestion(
'更新可用', "您当前使用的版本是:v%s,发现新版本:v%s,是否前往下载页?" % (processer.VERSION, maybe_update))
if(response == 'yes'):
webbrowser.open(
"https://github.com/Foldblade/XUJC-thesis-markdown/releases/latest", new=0, autoraise=True)
main_window.destroy()
main_menu = Menu(main_window)
# 新增命令菜单项,使用 add_command() 实现
main_menu.add_command(label="项目主页", command=open_project_site)
main_menu.add_command(label="查看文档", command=open_document_site)
main_menu.add_command(label="生成脚手架", command=generate_scaffold)
# 显示菜单
main_window.config(menu=main_menu)
main_window.after_idle(check_update)
# 组件
main_window.title('XUJC-thesis-markdown')
frame_main_window = Frame(main_window)
frame_main_window.pack(side='top', anchor='center', expand='yes')
label_base_dir = Label(frame_main_window, text='选择基础目录 *')
label_markdown_file = Label(frame_main_window, text='Markdown 文件 *')
label_metadata_file = Label(frame_main_window, text='元数据文件 *')
label_ref_file = Label(frame_main_window, text='BibTeX 格式引文文件 ')
label_output_file = Label(frame_main_window, text='输出 docx 文件另存为 *')
entry_base_dir = Entry(frame_main_window, width=50, textvariable=BASE_DIR)
entry_markdown_file = Entry(
frame_main_window, width=50, textvariable=MARKDOWN_FILE)
entry_metadata_file = Entry(
frame_main_window, width=50, textvariable=METADATA_FILE)
entry_ref_file = Entry(frame_main_window, width=50, textvariable=REF_FILE)
entry_output_file = Entry(frame_main_window, width=50,
textvariable=OUTPUT_FILE)
button_base_dir = Button(frame_main_window, text='...',
width=3, command=set_base_dir)
button_markdown_file = Button(
frame_main_window, text='...', width=3, command=set_markdown_file)
button_metadata_file = Button(
frame_main_window, text='...', width=3, command=set_metadata_file)
button_ref_file = Button(frame_main_window, text='...',
width=3, command=set_ref_file)
button_output_file = Button(frame_main_window, text='...',
width=3, command=set_output_file)
button_generate = Button(frame_main_window, text='生成', command=generate)
check_button_no_blank_back_cover = Checkbutton(
frame_main_window, text="不要在封底添加空白页", variable=NO_BLANK_BACK_COVER)
debug_scrolledtext = scrolledtext.ScrolledText(
frame_main_window, width=20, height=10)
debug_scrolledtext.insert(INSERT, "就绪。\n")
# 布局
label_base_dir.grid(row=0, column=0, pady=3, padx=3, sticky="e")
label_markdown_file.grid(row=1, column=0, pady=3, padx=3, sticky="e")
label_metadata_file.grid(row=2, column=0, pady=3, padx=3, sticky="e")
label_ref_file.grid(row=3, column=0, pady=3, padx=3, sticky="e")
label_output_file.grid(row=4, column=0, pady=3, padx=3, sticky="e")
entry_base_dir.grid(row=0, column=1, pady=3, padx=3)
entry_markdown_file.grid(row=1, column=1, pady=3, padx=3)
entry_metadata_file.grid(row=2, column=1, pady=3, padx=3)
entry_ref_file.grid(row=3, column=1, pady=3, padx=3)
entry_output_file.grid(row=4, column=1, pady=3, padx=3)
button_base_dir.grid(row=0, column=2, pady=3, padx=3)
button_markdown_file.grid(row=1, column=2, pady=3, padx=3)
button_metadata_file.grid(row=2, column=2, pady=3, padx=3)
button_ref_file.grid(row=3, column=2, pady=3, padx=3)
button_output_file.grid(row=4, column=2, pady=3, padx=3)
check_button_no_blank_back_cover.grid(row=5, column=1, columnspan=2, pady=3,
padx=3, sticky="we")
button_generate.grid(row=6, column=0, columnspan=3, pady=3,
padx=3, sticky="nswe")
debug_scrolledtext.grid(row=7, column=0, columnspan=3, pady=3,
padx=3, sticky="nswe")
if os.path.exists(os.path.join(WHERE_SCRIPT, ".gui_config.json")):
with open(os.path.join(WHERE_SCRIPT, ".gui_config.json"), "r", encoding="utf-8") as f:
gui_config_json = json.load(f)
MARKDOWN_FILE.set(gui_config_json["markdown_file"])
main_window.update()
set_base_dir(overwrite=gui_config_json["base_dir"])
set_markdown_file(overwrite=gui_config_json["markdown_file"])
set_metadata_file(overwrite=gui_config_json["metadata_file"])
set_ref_file(overwrite=gui_config_json["ref_file"])
set_output_file(overwrite=gui_config_json["output_file"])
NO_BLANK_BACK_COVER.set(gui_config_json["no_blank_back_cover"])
main_window.mainloop()