This page explains the contents and usage of the FolderWatcher module used for watching a folder and supplying a given function with paths that should be handled. This module acts as a buffer before documents enter a part of the pipeline.
The module contains two classes that together contain all the functionality required to watch a folder and apply a function to the found files.
Using builtin funtionality from watchdog we created this "on_created" event which is called everytime a new file is created or moved into the folder being watched.
Because of the way watchdog works it will react on files before they are fully transfered to the folder, so for this reason we have a loop inside that checks the size of the file and keeps checking until the file does not change in size, this way we know that the file was fully transfered.
def on_created(self, event):
if not event.is_directory:
size_before = -1
while os.path.getsize(event.src_path) != size_before:
size_before = os.path.getsize(event.src_path)
sleep(0.1)
full_path = os.path.join(os.getcwd(), event.src_path)
full_path = event.src_path
self.function_to_run(full_path)
This is just a wrapper function that is used to easily start the folder watcher.
def watch(self):
watcher = _Watcher(self.function_to_run)
observer = Observer()
observer.schedule(watcher, path=self.path_to_watch, recursive=True)
observer.start()
try:
while True:
sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
An example on how the folder watcher can be used to watch a folder and call a function is seen below:
from folder_watcher.folder_watcher import FolderWatcher
fw = FolderWatcher(‘path/to/folder’, someFunction)
fw.watch()