From 11d745b3e0d9f94650c1fad591dd064c1ba5450e Mon Sep 17 00:00:00 2001 From: Zhao Zuohong <1040110848@qq.com> Date: Wed, 3 Nov 2021 20:28:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=B9=B6=E8=AF=BB=E5=8F=96lf?= =?UTF-8?q?tp=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manage.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/manage.py b/manage.py index 961fdd6..0798079 100644 --- a/manage.py +++ b/manage.py @@ -1,13 +1,17 @@ from configparser import ConfigParser from pathlib import Path -from json import dump, load -import subprocess +from json import dump, dumps +from subprocess import STDOUT, Popen, PIPE +from threading import Thread +from queue import Empty, SimpleQueue from tkinter import * from tkinter import filedialog from tkinter.ttk import * config = ConfigParser() options = {"ftp": ["host", "user", "pass", "lftp"], "file": ["local", "remote"]} +output_queue = SimpleQueue() +finished = False def check_config_valid(config: ConfigParser, options: dict[str, list[str]]) -> bool: @@ -30,7 +34,7 @@ def set_default_config(config: ConfigParser, options: dict[str, list[str]]) -> N config.write(f) -def generate_db(config: ConfigParser) -> None: +def generate_db(config: ConfigParser) -> str: local_dir = Path(config["file"]["local"]) categories = Path(local_dir, "categories") output = [] @@ -43,9 +47,7 @@ def generate_db(config: ConfigParser) -> None: output.append(output_category) with open(Path(local_dir, "db.json"), "w") as f: dump(output, f) - - -# generate_db(config) + return dumps(output) def sync_all_command() -> list[str]: @@ -58,7 +60,7 @@ def sync_all_command() -> list[str]: ] -def sync_files_command() -> list[str]: +def sync_pdf_command() -> list[str]: return [ config["ftp"]["lftp"], f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit', @@ -68,6 +70,47 @@ def sync_files_command() -> list[str]: ] +def sync_func(command: list[str]) -> None: + global finished + finished = False + db_log = generate_db(config) + output_queue.put_nowait(f"db.json: {db_log}\n") + output_queue.put_nowait(f"command: {command}\n") + output_queue.put_nowait("Started lftp...\n") + proc = Popen(command, stdout=PIPE, stderr=STDOUT) + ret_val = None + while ret_val is None: + ret_val = proc.poll() + line = proc.stdout.readline() + if line: + output_queue.put_nowait(line) + output_queue.put_nowait(f"lftp exited with code {ret_val}.\n") + finished = True + + +def display_output(): + try: + while True: + line = output_queue.get_nowait() + lftp_output.insert(END, line) + except Empty: + pass + if not finished: + root.after(200, display_output) + + +def sync_all_files(_: Event) -> None: + lftp_output.delete("1.0", END) + Thread(target=sync_func, args=(sync_all_command(),), daemon=True).start() + root.after(500, display_output) + + +def sync_pdf_files(_: Event) -> None: + lftp_output.delete("1.0", END) + Thread(target=sync_func, args=(sync_pdf_command(),), daemon=True).start() + root.after(500, display_output) + + def select_lftp(_: Event) -> None: file_path = filedialog.askopenfilename() config["ftp"]["lftp"] = file_path @@ -115,8 +158,6 @@ def save_config(_: Event) -> None: 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) @@ -128,7 +169,7 @@ ftp_frm.grid(row=0, column=0, pady=10, padx=10) Label(ftp_frm, text="FTP Settings").grid(row=0, column=0, columnspan=3) -Label(ftp_frm, text="Host", width=10).grid(row=1, column=0) +Label(ftp_frm, text="Hostname", 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=1, column=1, columnspan=2) @@ -180,7 +221,15 @@ 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) +sync_all_btn = Button(actions_frm, text="Sync all Files", width=12) +sync_all_btn.grid(row=0, column=2) +sync_all_btn.bind("", sync_all_files) + +sync_pdf_btn = Button(actions_frm, text="Sync PDF Files", width=12) +sync_pdf_btn.grid(row=0, column=3) +sync_pdf_btn.bind("", sync_pdf_files) + +lftp_output = Text(actions_frm, width=51, height=12) +lftp_output.grid(row=1, column=0, columnspan=4) root.mainloop()