tmp
This commit is contained in:
parent
a3cf1dcb9a
commit
8ce1cc7885
1 changed files with 31 additions and 4 deletions
|
|
@ -8,6 +8,7 @@ from watchdog.events import (
|
|||
FileCreatedEvent,
|
||||
FileDeletedEvent,
|
||||
FileModifiedEvent,
|
||||
FileSystemEvent,
|
||||
FileSystemEventHandler,
|
||||
FileSystemMovedEvent,
|
||||
)
|
||||
|
|
@ -24,17 +25,26 @@ class Watcher:
|
|||
self.running = False
|
||||
|
||||
def start(self):
|
||||
|
||||
self.observer.schedule(self.handler, self.source, recursive=True)
|
||||
self.observer.schedule(
|
||||
self.handler,
|
||||
self.source,
|
||||
recursive=True,
|
||||
event_filter=[
|
||||
FileCreatedEvent,
|
||||
FileDeletedEvent,
|
||||
FileModifiedEvent,
|
||||
FileSystemMovedEvent,
|
||||
],
|
||||
)
|
||||
self.logger.info("Custom handler has been scheduled with the Observer")
|
||||
|
||||
# self.observer.setDaemon(True)
|
||||
self.observer.start()
|
||||
self.logger.info("The Observer has been started!")
|
||||
|
||||
self.logger.info("Starting main loop @ 1s polling rate")
|
||||
self.running = True
|
||||
while self.running:
|
||||
sleep(1)
|
||||
sleep(0.01)
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
|
@ -48,6 +58,15 @@ class Handler(FileSystemEventHandler):
|
|||
self.source = source
|
||||
self.dest = destination
|
||||
|
||||
def __log_event(self, event: FileSystemEvent) -> None:
|
||||
"""
|
||||
Logs all events for the sake of it
|
||||
Arguments:
|
||||
event (watchdog.events.FileSystemEvent): the event fired
|
||||
"""
|
||||
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
||||
self.logger.info(f"type: {event.event_type}, src: {event.src_path}{dest_str}")
|
||||
|
||||
def __in_destination(self, path: Path) -> Path:
|
||||
"""
|
||||
Given a path in the watch directory, converts it to the destination's path instead
|
||||
|
|
@ -89,6 +108,8 @@ class Handler(FileSystemEventHandler):
|
|||
"""
|
||||
if event.is_directory:
|
||||
return
|
||||
|
||||
self.__log_event(event)
|
||||
src = Path(event.src_path).absolute()
|
||||
in_dest = self.__in_destination(src)
|
||||
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
||||
|
|
@ -104,6 +125,8 @@ class Handler(FileSystemEventHandler):
|
|||
if event.is_directory:
|
||||
return
|
||||
|
||||
self.__log_event(event)
|
||||
|
||||
src = Path(event.src_path).absolute()
|
||||
in_dest = self.__in_destination(src)
|
||||
|
||||
|
|
@ -126,6 +149,8 @@ class Handler(FileSystemEventHandler):
|
|||
if event.is_directory:
|
||||
return
|
||||
|
||||
self.__log_event(event)
|
||||
|
||||
src = Path(event.src_path).absolute()
|
||||
dest = Path(event.dest_path).absolute()
|
||||
|
||||
|
|
@ -151,6 +176,8 @@ class Handler(FileSystemEventHandler):
|
|||
if event.is_directory:
|
||||
return
|
||||
|
||||
self.__log_event(event)
|
||||
|
||||
in_dest = self.__in_destination(event.src_path)
|
||||
|
||||
if in_dest.exists():
|
||||
|
|
|
|||
Loading…
Reference in a new issue