Return a value from watchdog observer-python

198 views
Skip to first unread message

shahi...@gmail.com

unread,
Aug 23, 2018, 11:57:05 AM8/23/18
to watchdog-python
I am using watchdog for watching a directory for any changes for instance when a new image is added to the directory I want to trigger a function on the main class.
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, LoggingEventHandler

# from config import LOCAL_PATH_TO_WATCHER
import os 
LOCAL_PATH_TO_WATCHER = '/path/to/dir/'
class Watcher:
    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Event()
        self.observer.schedule(event_handler, path =LOCAL_PATH_TO_WATCHER, recursive=True)
        self.observer.start()
        try:
            while True:
              time.sleep(1)
        except:
            self.observer.stop()
            print ("Error")

        self.observer.join()

class Event(LoggingEventHandler):

  def dispatch(self, event):
      if event.is_directory:
          return None
      elif event.event_type == 'created':
          return event.src_path

watcher.py

Now i want to initialize a Watcher object and trigger the function in my main file.

from watcher import Watcher

def crop(img):
  # some implementation

if __name__ == '__main__':
    # This will run the pipeline file for the first time.
    w = Watcher()
    w.run()

main.py

I want to access the image from watcher object's dispatch function to run crop function in the main file

Reply all
Reply to author
Forward
0 new messages