fork download
  1. import os, pyinotify
  2. import pdb
  3.  
  4. class FileWatcher:
  5. notifer = None
  6.  
  7. def start_watch(self, dir, callback):
  8. wm = pyinotify.WatchManager()
  9. self.notifier = pyinotify.Notifier(wm, EventProcessor(callback))
  10.  
  11. mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
  12. wdd = wm.add_watch(dir, mask, rec=True)
  13. while True:
  14. self.notifier.process_events()
  15. if self.notifier.check_events():
  16. self.notifier.read_events()
  17.  
  18. def stop_watch(self):
  19. self.notifier.stop()
  20. print ('\nWatcher stopped')
  21.  
  22. class EventProcessor(pyinotify.ProcessEvent):
  23. def __init__(self, callback):
  24. self.event_callback = callback
  25.  
  26. def process_IN_CREATE(self, event):
  27. if self.event_callback is not None:
  28. self.event_callback.on_file_created(os.path.join(event.path, event.name))
  29.  
  30. def process_IN_MODIFY(self, event):
  31. if self.event_callback is not None:
  32. self.event_callback.on_file_modifed(os.path.join(event.path, event.name))
  33.  
  34. def process_IN_DELETE(self, event):
  35. print('in delete')
  36.  
  37. def process_IN_DELETE_SELF(self, event):
  38. print('in delete self')
  39.  
  40. def process_IN_MOVED_FROM(self, event):
  41. print('in moved_from')
  42.  
  43. def process_IN_MOVED_TO(self, event):
  44. print('in moved to')
Runtime error #stdin #stdout 0.01s 7724KB
stdin
Standard input is empty
stdout
Standard output is empty