From b012ae991cb1b2ea673a4bf2e8656c634d88ad6f Mon Sep 17 00:00:00 2001 From: Zhao Zuohong <1040110848@qq.com> Date: Tue, 2 Nov 2021 23:15:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E4=B8=8E=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C=E5=B9=B6=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manage.py | 167 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 112 insertions(+), 55 deletions(-) diff --git a/manage.py b/manage.py index 00d7231..961fdd6 100644 --- a/manage.py +++ b/manage.py @@ -1,8 +1,9 @@ from configparser import ConfigParser from pathlib import Path -from json import dump +from json import dump, load import subprocess from tkinter import * +from tkinter import filedialog from tkinter.ttk import * config = ConfigParser() @@ -29,11 +30,6 @@ def set_default_config(config: ConfigParser, options: dict[str, list[str]]) -> N config.write(f) -config.read("config.ini") -if not check_config_valid(config, options): - set_default_config(config, options) - - def generate_db(config: ConfigParser) -> None: local_dir = Path(config["file"]["local"]) categories = Path(local_dir, "categories") @@ -51,79 +47,140 @@ def generate_db(config: ConfigParser) -> None: # generate_db(config) -sync_all_command = [ - config["ftp"]["lftp"], - f'-e mirror -R -e --parallel=10 {config["file"]["local"]} {config["file"]["remote"]}; exit', - "-u", - f'{config["ftp"]["user"]},{config["ftp"]["pass"]}', - config["ftp"]["host"], -] -sync_files_command = [ - config["ftp"]["lftp"], - f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit', - "-u", - f'{config["ftp"]["user"]},{config["ftp"]["pass"]}', - config["ftp"]["host"], -] +def sync_all_command() -> list[str]: + return [ + config["ftp"]["lftp"], + f'-e mirror -R -e --parallel=10 {config["file"]["local"]} {config["file"]["remote"]}; exit', + "-u", + f'{config["ftp"]["user"]},{config["ftp"]["pass"]}', + config["ftp"]["host"], + ] -def tfunc(txt: str) -> None: - print(txt) +def sync_files_command() -> list[str]: + return [ + config["ftp"]["lftp"], + f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit', + "-u", + f'{config["ftp"]["user"]},{config["ftp"]["pass"]}', + config["ftp"]["host"], + ] + + +def select_lftp(_: Event) -> None: + file_path = filedialog.askopenfilename() + config["ftp"]["lftp"] = file_path + lftp_entry.delete(0, END) + lftp_entry.insert(0, file_path) + + +def select_local(_: Event) -> None: + file_path = filedialog.askdirectory() + config["file"]["local"] = file_path + local_entry.delete(0, END) + local_entry.insert(0, file_path) + + +def load_config(_: Event) -> None: + config.read("config.ini") + if not check_config_valid(config, options): + set_default_config(config, options) + host_entry.delete(0, END) + host_entry.insert(0, config["ftp"]["host"]) + user_entry.delete(0, END) + user_entry.insert(0, config["ftp"]["user"]) + pass_entry.delete(0, END) + pass_entry.insert(0, config["ftp"]["pass"]) + lftp_entry.delete(0, END) + lftp_entry.insert(0, config["ftp"]["lftp"]) + local_entry.delete(0, END) + local_entry.insert(0, config["file"]["local"]) + remote_entry.delete(0, END) + remote_entry.insert(0, config["file"]["remote"]) + + +def get_config() -> None: + config["ftp"]["host"] = host_entry.get() + config["ftp"]["user"] = user_entry.get() + config["ftp"]["pass"] = pass_entry.get() + config["ftp"]["lftp"] = lftp_entry.get() + config["file"]["local"] = local_entry.get() + config["file"]["remote"] = remote_entry.get() + + +def save_config(_: Event) -> None: + get_config() + with open("config.ini", "w") as f: + config.write(f) # print(subprocess.run(sync_files_command)) +config.read("config.ini") +if not check_config_valid(config, options): + set_default_config(config, options) + root = Tk() -ftp_header_frm = Frame(root) -ftp_header_frm.grid(row=0, column=0) +ftp_frm = Frame(root, border=1) +ftp_frm.grid(row=0, column=0, pady=10, padx=10) -Label(ftp_header_frm, text="FTP Settings").grid(row=0, column=0, padx=5, pady=5) +Label(ftp_frm, text="FTP Settings").grid(row=0, column=0, columnspan=3) -ftp_settings_frm = Frame(root) -ftp_settings_frm.grid(row=1, column=0) - -Label(ftp_settings_frm, text="Host").grid(row=0, column=0, padx=5, pady=5) -host_entry = Entry(ftp_settings_frm) +Label(ftp_frm, text="Host", width=10).grid(row=1, column=0) +host_entry = Entry(ftp_frm, width=40) host_entry.insert(0, config["ftp"]["host"]) -host_entry.grid(row=0, column=1, padx=5, pady=5) +host_entry.grid(row=1, column=1, columnspan=2) -Label(ftp_settings_frm, text="Username").grid(row=1, column=0, padx=5, pady=5) -user_entry = Entry(ftp_settings_frm) +Label(ftp_frm, text="Username", width=10).grid(row=2, column=0) +user_entry = Entry(ftp_frm, width=40) user_entry.insert(0, config["ftp"]["user"]) -user_entry.grid(row=1, column=1, padx=5, pady=5) +user_entry.grid(row=2, column=1, columnspan=2) -Label(ftp_settings_frm, text="Password").grid(row=2, column=0, padx=5, pady=5) -pass_entry = Entry(ftp_settings_frm) +Label(ftp_frm, text="Password", width=10).grid(row=3, column=0) +pass_entry = Entry(ftp_frm, width=40, show="*") pass_entry.insert(0, config["ftp"]["pass"]) -pass_entry.grid(row=2, column=1, padx=5, pady=5) +pass_entry.grid(row=3, column=1, columnspan=2) -Label(ftp_settings_frm, text="lftp Path").grid(row=3, column=0, padx=5, pady=5) -lftp_entry = Entry(ftp_settings_frm) +Label(ftp_frm, text="lftp Path", width=10).grid(row=4, column=0) +lftp_entry = Entry(ftp_frm, width=29) lftp_entry.insert(0, config["ftp"]["lftp"]) -lftp_entry.grid(row=3, column=1, padx=5, pady=5) +lftp_entry.grid(row=4, column=1) +lftp_select_btn = Button(ftp_frm, text="Select...", width=10) +lftp_select_btn.grid(row=4, column=2) +lftp_select_btn.bind("", select_lftp) -file_header_frm = Frame(root) -file_header_frm.grid(row=2, column=0) +file_frm = Frame(root) +file_frm.grid(row=1, column=0, pady=10, padx=10) -Label(file_header_frm, text="File Path Settings").grid( - row=0, column=0, columnspan=2, padx=5, pady=5 -) +Label(file_frm, text="File Path Settings").grid(row=0, column=0, columnspan=3) -file_settings_frm = Frame(root) -file_settings_frm.grid(row=3, column=0) - -Label(file_settings_frm, text="Local Path").grid(row=0, column=0, padx=5, pady=5) -local_entry = Entry(file_settings_frm) +Label(file_frm, text="Local Path", width=12).grid(row=1, column=0) +local_entry = Entry(file_frm, width=27) local_entry.insert(0, config["file"]["local"]) -local_entry.grid(row=0, column=1, padx=5, pady=5) +local_entry.grid(row=1, column=1) +local_select_btn = Button(file_frm, text="Select...", width=10) +local_select_btn.grid(row=1, column=2) +local_select_btn.bind("", select_local) -Label(file_settings_frm, text="Remote Path").grid(row=1, column=0, padx=5, pady=5) -remote_entry = Entry(file_settings_frm) +Label(file_frm, text="Remote Path", width=12).grid(row=2, column=0) +remote_entry = Entry(file_frm, width=38) remote_entry.insert(0, config["file"]["remote"]) -remote_entry.grid(row=1, column=1, padx=5, pady=5) +remote_entry.grid(row=2, column=1, columnspan=2) +actions_frm = Frame(root) +actions_frm.grid(row=2, column=0, pady=10, padx=10) + +load_btn = Button(actions_frm, text="Load Config", width=12) +load_btn.grid(row=0, column=0) +load_btn.bind("", load_config) + +save_btn = Button(actions_frm, text="Save Config", width=12) +save_btn.grid(row=0, column=1) +save_btn.bind("", save_config) + +Button(actions_frm, text="Sync all Files", width=12).grid(row=0, column=2) +Button(actions_frm, text="Sync PDF Files", width=12).grid(row=0, column=3) -# Button(ftp_settings_frm, text="Save Config").grid(row=5, column=0, columnspan=2, padx=5, pady=5) root.mainloop()