From f06e974777778f205da79ae9783441a4a5e795e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Wiesner?= Date: Wed, 25 Feb 2026 09:57:53 +0100 Subject: [PATCH] - Windows compatibility - state retention added --- .vscode/launch.json | 16 +++++++++ sync_client.py | 85 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e4d3244 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug SyncClient", + "type": "debugpy", + "request": "launch", + "program": "${workspaceFolder}/sync_client.py", + "console": "integratedTerminal", + "args": [] + } + ] +} \ No newline at end of file diff --git a/sync_client.py b/sync_client.py index 516bd4c..d960467 100644 --- a/sync_client.py +++ b/sync_client.py @@ -1,13 +1,21 @@ +import platform import tkinter as tk from tkinter import ttk -from tkinter import filedialog +from tkinter import filedialog, messagebox from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import os import requests +import json class SyncClient: - + @classmethod + def __convert_to_unix_path(cls, path: str) -> str: + if platform.system() == "Windows": + return path.replace("\\", "/").removeprefix("/") + else: + return path.removeprefix("/") + def start(self) -> None: _sself = self @@ -60,14 +68,14 @@ class SyncClient: return "directory" else: return "file" - + def upload(self, filename: str) -> bool: print("[UPLOAD]", filename) params = { "action": "upload", - "filename": filename, + "filename": SyncClient.__convert_to_unix_path(filename), "type": self._get_file_type(filename), "session_id": self.__id, "session_key": self.__key @@ -90,7 +98,7 @@ class SyncClient: params = { "action": "delete", - "filename": filename, + "filename": SyncClient.__convert_to_unix_path(filename), "type": self._get_file_type(filename), "session_id": self.__id, "session_key": self.__key @@ -122,8 +130,8 @@ class SyncClient: params = { "action": "move", - "filename": src, - "filename2": dst, + "filename": SyncClient.__convert_to_unix_path(src), + "filename2": SyncClient.__convert_to_unix_path(dst), "type": self._get_file_type(dst), "session_id": self.__id, "session_key": self.__key @@ -142,8 +150,8 @@ class SyncClient: def diriter(path, rel_path = "", types = [ "directory", "file" ]): listing = [] for entry in os.scandir(path): - path = (rel_path + os.sep + entry.name).removeprefix("/") - name = entry.name.removeprefix("/") + path = (rel_path + os.sep + entry.name).removeprefix(os.sep) + name = entry.name.removeprefix(os.sep) if entry.is_dir(): if "directory" in types: listing.append({"type": "directory", "path": path, "name": name}) @@ -172,8 +180,9 @@ class SyncClient: # ------------------ class SyncClientGui: + SETTINGS_FILE = "settings.json" - def init_gui(self) -> None: + def __init_gui(self) -> None: win = tk.Tk() win.title("CodeCast Kliens") win.geometry("400x120") @@ -234,7 +243,7 @@ class SyncClientGui: def toggle_sync(): if not self.__sync_enabled: - self.__url = url_tf.get().strip() + self.__url = url_tf.get().strip().removesuffix("/") + "/sync" self.__id = id_tf.get().strip() self.__key = key_tf.get().strip() @@ -242,16 +251,53 @@ class SyncClientGui: self.start_sync() else: self.stop_sync() - + + if self.__sync_enabled: on_off_btn.config(text="Szinkronizálás KI") + state = tk.DISABLED else: on_off_btn.config(text="Szinkronizálás BE") + state = tk.NORMAL + + dirsel_btn.configure(state=state) + url_tf.configure(state=state) + id_tf.configure(state=state) + key_tf.configure(state=state) on_off_btn = ttk.Button(action_frame, text="Szinkronizálás BE", command=toggle_sync) on_off_btn.pack(fill="x") + self.__url_tf = url_tf + self.__id_tf = id_tf + self.__key_tf = key_tf + self.__dirsel_btn = dirsel_btn + + def __load_last_settings(self) -> None: + try: + with open(SyncClientGui.SETTINGS_FILE, "r") as sf: + settings = json.loads(sf.read()) + + self.__dirsel_btn.configure(text=settings.get("dir", "(nincs kiválasztva)")) + self.__dir = settings.get("dir", "") + self.__url_tf.insert(0, settings.get("url", "")) + self.__id_tf.insert(0, settings.get("id", "")) + self.__key_tf.insert(0, settings.get("key", "")) + except: + pass + + def __save_settings(self) -> None: + with open(SyncClientGui.SETTINGS_FILE, "w") as sf: + settings = { + "dir": self.__dir, + "url": self.__url_tf.get(), + "id": self.__id_tf.get(), + "key": self.__key_tf.get(), + } + sf.write(json.dumps(settings)) + + def __init__(self) -> None: self.__dir = "" self.__url = "" @@ -259,7 +305,16 @@ class SyncClientGui: self.__key = "" self.__sc: SyncClient self.__sync_enabled = False - self.init_gui() + + self.__init_gui() + self.__load_last_settings() + + def on_close(): + self.__save_settings() + self.__win.destroy() + self.stop_sync() + + self.__win.protocol("WM_DELETE_WINDOW", on_close) def start_sync(self) -> None: @@ -282,11 +337,9 @@ class SyncClientGui: def mainloop(self) -> None: self.__win.mainloop() - self.stop_sync() - # ------------------ if __name__ == "__main__": gui = SyncClientGui() - gui.mainloop() \ No newline at end of file + gui.mainloop()