- Windows compatibility
- state retention added
This commit is contained in:
parent
b45fcb1c33
commit
f06e974777
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -1,13 +1,21 @@
|
|||||||
|
import platform
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
from tkinter import filedialog
|
from tkinter import filedialog, messagebox
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
class SyncClient:
|
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:
|
def start(self) -> None:
|
||||||
_sself = self
|
_sself = self
|
||||||
|
|
||||||
@ -60,14 +68,14 @@ class SyncClient:
|
|||||||
return "directory"
|
return "directory"
|
||||||
else:
|
else:
|
||||||
return "file"
|
return "file"
|
||||||
|
|
||||||
|
|
||||||
def upload(self, filename: str) -> bool:
|
def upload(self, filename: str) -> bool:
|
||||||
print("[UPLOAD]", filename)
|
print("[UPLOAD]", filename)
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"action": "upload",
|
"action": "upload",
|
||||||
"filename": filename,
|
"filename": SyncClient.__convert_to_unix_path(filename),
|
||||||
"type": self._get_file_type(filename),
|
"type": self._get_file_type(filename),
|
||||||
"session_id": self.__id,
|
"session_id": self.__id,
|
||||||
"session_key": self.__key
|
"session_key": self.__key
|
||||||
@ -90,7 +98,7 @@ class SyncClient:
|
|||||||
|
|
||||||
params = {
|
params = {
|
||||||
"action": "delete",
|
"action": "delete",
|
||||||
"filename": filename,
|
"filename": SyncClient.__convert_to_unix_path(filename),
|
||||||
"type": self._get_file_type(filename),
|
"type": self._get_file_type(filename),
|
||||||
"session_id": self.__id,
|
"session_id": self.__id,
|
||||||
"session_key": self.__key
|
"session_key": self.__key
|
||||||
@ -122,8 +130,8 @@ class SyncClient:
|
|||||||
|
|
||||||
params = {
|
params = {
|
||||||
"action": "move",
|
"action": "move",
|
||||||
"filename": src,
|
"filename": SyncClient.__convert_to_unix_path(src),
|
||||||
"filename2": dst,
|
"filename2": SyncClient.__convert_to_unix_path(dst),
|
||||||
"type": self._get_file_type(dst),
|
"type": self._get_file_type(dst),
|
||||||
"session_id": self.__id,
|
"session_id": self.__id,
|
||||||
"session_key": self.__key
|
"session_key": self.__key
|
||||||
@ -142,8 +150,8 @@ class SyncClient:
|
|||||||
def diriter(path, rel_path = "", types = [ "directory", "file" ]):
|
def diriter(path, rel_path = "", types = [ "directory", "file" ]):
|
||||||
listing = []
|
listing = []
|
||||||
for entry in os.scandir(path):
|
for entry in os.scandir(path):
|
||||||
path = (rel_path + os.sep + entry.name).removeprefix("/")
|
path = (rel_path + os.sep + entry.name).removeprefix(os.sep)
|
||||||
name = entry.name.removeprefix("/")
|
name = entry.name.removeprefix(os.sep)
|
||||||
if entry.is_dir():
|
if entry.is_dir():
|
||||||
if "directory" in types:
|
if "directory" in types:
|
||||||
listing.append({"type": "directory", "path": path, "name": name})
|
listing.append({"type": "directory", "path": path, "name": name})
|
||||||
@ -172,8 +180,9 @@ class SyncClient:
|
|||||||
# ------------------
|
# ------------------
|
||||||
|
|
||||||
class SyncClientGui:
|
class SyncClientGui:
|
||||||
|
SETTINGS_FILE = "settings.json"
|
||||||
|
|
||||||
def init_gui(self) -> None:
|
def __init_gui(self) -> None:
|
||||||
win = tk.Tk()
|
win = tk.Tk()
|
||||||
win.title("CodeCast Kliens")
|
win.title("CodeCast Kliens")
|
||||||
win.geometry("400x120")
|
win.geometry("400x120")
|
||||||
@ -234,7 +243,7 @@ class SyncClientGui:
|
|||||||
|
|
||||||
def toggle_sync():
|
def toggle_sync():
|
||||||
if not self.__sync_enabled:
|
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.__id = id_tf.get().strip()
|
||||||
self.__key = key_tf.get().strip()
|
self.__key = key_tf.get().strip()
|
||||||
|
|
||||||
@ -242,16 +251,53 @@ class SyncClientGui:
|
|||||||
self.start_sync()
|
self.start_sync()
|
||||||
else:
|
else:
|
||||||
self.stop_sync()
|
self.stop_sync()
|
||||||
|
|
||||||
|
|
||||||
if self.__sync_enabled:
|
if self.__sync_enabled:
|
||||||
on_off_btn.config(text="Szinkronizálás KI")
|
on_off_btn.config(text="Szinkronizálás KI")
|
||||||
|
state = tk.DISABLED
|
||||||
else:
|
else:
|
||||||
on_off_btn.config(text="Szinkronizálás BE")
|
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 = ttk.Button(action_frame, text="Szinkronizálás BE", command=toggle_sync)
|
||||||
on_off_btn.pack(fill="x")
|
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:
|
def __init__(self) -> None:
|
||||||
self.__dir = ""
|
self.__dir = ""
|
||||||
self.__url = ""
|
self.__url = ""
|
||||||
@ -259,7 +305,16 @@ class SyncClientGui:
|
|||||||
self.__key = ""
|
self.__key = ""
|
||||||
self.__sc: SyncClient
|
self.__sc: SyncClient
|
||||||
self.__sync_enabled = False
|
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:
|
def start_sync(self) -> None:
|
||||||
@ -282,11 +337,9 @@ class SyncClientGui:
|
|||||||
def mainloop(self) -> None:
|
def mainloop(self) -> None:
|
||||||
self.__win.mainloop()
|
self.__win.mainloop()
|
||||||
|
|
||||||
self.stop_sync()
|
|
||||||
|
|
||||||
# ------------------
|
# ------------------
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
gui = SyncClientGui()
|
gui = SyncClientGui()
|
||||||
|
|
||||||
gui.mainloop()
|
gui.mainloop()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user