Nested watchers was not a good idea
This commit is contained in:
parent
bd262e8f42
commit
0098e172cc
2 changed files with 17 additions and 40 deletions
32
src/main.pyw
32
src/main.pyw
|
|
@ -65,36 +65,24 @@ def main(
|
|||
logger.info(f"Watching directory {source}")
|
||||
logger.info(f"Backing up to {destination}")
|
||||
|
||||
# Spin up a different thread for each subdirectory to keep up with performance
|
||||
subdirs = sorted(
|
||||
list(
|
||||
filter(
|
||||
lambda x: source.joinpath(x).resolve().is_dir(),
|
||||
os.listdir(source),
|
||||
)
|
||||
)
|
||||
watcher = Watcher(
|
||||
logger,
|
||||
source.resolve().absolute(),
|
||||
destination.resolve().absolute(),
|
||||
ignore_pattern,
|
||||
)
|
||||
|
||||
watchers = [
|
||||
Watcher(
|
||||
logger,
|
||||
source.joinpath(subdir).resolve().absolute(),
|
||||
destination.joinpath(subdir).resolve().absolute(),
|
||||
ignore_pattern,
|
||||
)
|
||||
for subdir in subdirs
|
||||
]
|
||||
watcher.start()
|
||||
|
||||
[x.start() for x in watchers]
|
||||
|
||||
while True:
|
||||
try:
|
||||
time.sleep(0.01)
|
||||
time.sleep(0.001)
|
||||
except KeyboardInterrupt:
|
||||
print("Quitting...")
|
||||
[x.stop() for x in watchers]
|
||||
watcher.stop()
|
||||
break
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ from watchdog.events import (
|
|||
FileSystemMovedEvent,
|
||||
)
|
||||
from watchdog.observers import Observer
|
||||
from threading import current_thread
|
||||
import re
|
||||
|
||||
|
||||
|
|
@ -24,6 +23,7 @@ class Watcher:
|
|||
self.source = source.absolute()
|
||||
self.dest = destination.absolute()
|
||||
self.observer = Observer()
|
||||
self.observer.daemon = True
|
||||
self.handler = Handler(self.logger, self.source, self.dest, ignore_pattern)
|
||||
|
||||
def start(self):
|
||||
|
|
@ -38,9 +38,8 @@ class Watcher:
|
|||
FileSystemMovedEvent,
|
||||
],
|
||||
)
|
||||
self.observer.setDaemon(True)
|
||||
|
||||
self.observer.start()
|
||||
|
||||
self.logger.info(f"The Observer for {self.source} has been started!")
|
||||
|
||||
def stop(self):
|
||||
|
|
@ -65,9 +64,7 @@ class Handler(FileSystemEventHandler):
|
|||
event (watchdog.events.FileSystemEvent): the event fired
|
||||
"""
|
||||
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
||||
self.logger.debug(
|
||||
f"type: {event.event_type}, src: {event.src_path}{dest_str}"
|
||||
)
|
||||
self.logger.debug(f"type: {event.event_type}, src: {event.src_path}{dest_str}")
|
||||
|
||||
def __in_destination(self, path: Path) -> Path:
|
||||
"""
|
||||
|
|
@ -124,9 +121,7 @@ class Handler(FileSystemEventHandler):
|
|||
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
||||
shutil.copy2(src, self.__in_destination(src))
|
||||
except Exception as exc:
|
||||
self.logger.warn(
|
||||
f"Exception thrown in Handler#on_created:\n\t{exc}"
|
||||
)
|
||||
self.logger.warn(f"Exception thrown in Handler#on_created:\n\t{exc}")
|
||||
|
||||
def on_modified(self, event: FileModifiedEvent) -> None:
|
||||
"""
|
||||
|
|
@ -154,9 +149,7 @@ class Handler(FileSystemEventHandler):
|
|||
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
||||
shutil.copy2(src, in_dest)
|
||||
except Exception as exc:
|
||||
self.logger.warn(
|
||||
f"Exception thrown in Handler#on_modified:\n\t{exc}"
|
||||
)
|
||||
self.logger.warn(f"Exception thrown in Handler#on_modified:\n\t{exc}")
|
||||
|
||||
def on_moved(self, event: FileSystemMovedEvent):
|
||||
"""
|
||||
|
|
@ -198,9 +191,7 @@ class Handler(FileSystemEventHandler):
|
|||
|
||||
self.__recursively_clean_dirs_upwards(in_dest_before.parent.absolute())
|
||||
except Exception as exc:
|
||||
self.logger.warn(
|
||||
f"Exception thrown in Handler#on_moved:\n\t{exc}"
|
||||
)
|
||||
self.logger.warn(f"Exception thrown in Handler#on_moved:\n\t{exc}")
|
||||
|
||||
def on_deleted(self, event: FileDeletedEvent) -> None:
|
||||
"""
|
||||
|
|
@ -224,6 +215,4 @@ class Handler(FileSystemEventHandler):
|
|||
|
||||
self.__recursively_clean_dirs_upwards(in_dest.parent.absolute())
|
||||
except Exception as exc:
|
||||
self.logger.warn(
|
||||
f"Exception thrown in Handler#on_deleted:\n\t{exc}"
|
||||
)
|
||||
self.logger.warn(f"Exception thrown in Handler#on_deleted:\n\t{exc}")
|
||||
|
|
|
|||
Loading…
Reference in a new issue