- threading/queueing implemented
This commit is contained in:
parent
9aeafdfc9e
commit
ab24d03d3f
@ -3,7 +3,7 @@ import tkinter as tk
|
|||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
from tkinter import filedialog, messagebox
|
from tkinter import filedialog, messagebox
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEvent, FileSystemEventHandler
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
@ -21,13 +21,18 @@ class SyncClient:
|
|||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
_sself = self
|
_sself = self
|
||||||
|
|
||||||
self.__event_queue = queue.Queue()
|
self._event_queue = queue.Queue()
|
||||||
|
|
||||||
def comm_thread_routine():
|
def comm_thread_routine(queue: queue.Queue):
|
||||||
while True:
|
run = True
|
||||||
event = self.__event_queue.get()
|
while run:
|
||||||
|
event: FileSystemEvent = queue.get()
|
||||||
|
|
||||||
rel_path = str(event.src_path).removeprefix(_sself.dir())
|
if event.src_path == "**CLOSE**" and event.is_synthetic == True:
|
||||||
|
run = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
rel_path = str(event.src_path).removeprefix(_sself.dir()).removeprefix(os.sep)
|
||||||
if event.event_type == "created" or event.event_type == "modified":
|
if event.event_type == "created" or event.event_type == "modified":
|
||||||
_sself.upload(rel_path)
|
_sself.upload(rel_path)
|
||||||
elif event.event_type == "moved":
|
elif event.event_type == "moved":
|
||||||
@ -46,9 +51,10 @@ class SyncClient:
|
|||||||
def on_any_event(self, event):
|
def on_any_event(self, event):
|
||||||
if event.event_type in FileChangeHandler.EVENTS_WATCHED:
|
if event.event_type in FileChangeHandler.EVENTS_WATCHED:
|
||||||
if event.src_path != _sself.dir():
|
if event.src_path != _sself.dir():
|
||||||
_sself.__event_queue.put(event)
|
_sself._event_queue.put(event)
|
||||||
|
|
||||||
self.__comm_thread = threading.Thread()
|
self.__comm_thread = threading.Thread(target=comm_thread_routine, args=(self._event_queue,))
|
||||||
|
self.__comm_thread.start()
|
||||||
|
|
||||||
observer = Observer()
|
observer = Observer()
|
||||||
event_handler = FileChangeHandler()
|
event_handler = FileChangeHandler()
|
||||||
@ -65,9 +71,7 @@ class SyncClient:
|
|||||||
self.__key = key
|
self.__key = key
|
||||||
|
|
||||||
self.__comm_thread: threading.Thread
|
self.__comm_thread: threading.Thread
|
||||||
self.__event_queue: queue.Queue
|
self._event_queue: queue.Queue
|
||||||
|
|
||||||
self.start()
|
|
||||||
|
|
||||||
|
|
||||||
def dir(self) -> str:
|
def dir(self) -> str:
|
||||||
@ -159,6 +163,9 @@ class SyncClient:
|
|||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
self.__observer.stop()
|
self.__observer.stop()
|
||||||
|
self._event_queue.put(FileSystemEvent("**CLOSE**", is_synthetic=True))
|
||||||
|
self.__comm_thread.join()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def upload_all(self) -> None:
|
def upload_all(self) -> None:
|
||||||
@ -327,8 +334,9 @@ class SyncClientGui:
|
|||||||
|
|
||||||
def on_close():
|
def on_close():
|
||||||
self.__save_settings()
|
self.__save_settings()
|
||||||
self.__win.destroy()
|
|
||||||
self.stop_sync()
|
self.stop_sync()
|
||||||
|
self.__win.destroy()
|
||||||
|
|
||||||
|
|
||||||
self.__win.protocol("WM_DELETE_WINDOW", on_close)
|
self.__win.protocol("WM_DELETE_WINDOW", on_close)
|
||||||
|
|
||||||
@ -352,6 +360,7 @@ class SyncClientGui:
|
|||||||
|
|
||||||
def mainloop(self) -> None:
|
def mainloop(self) -> None:
|
||||||
self.__win.mainloop()
|
self.__win.mainloop()
|
||||||
|
return
|
||||||
|
|
||||||
# ------------------
|
# ------------------
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user