Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created December 17, 2020 11:09
Show Gist options
  • Save velotiotech/e7b750e4b9fe9c9b39c2a436e6797e91 to your computer and use it in GitHub Desktop.
Save velotiotech/e7b750e4b9fe9c9b39c2a436e6797e91 to your computer and use it in GitHub Desktop.
watchdog
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
CODE_PATH = "<codebase path>"
class WatchMyCodebase:
# Set the directory on watch
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = EventHandler()
# recursive flag decides if watcher should collect changes in CODE_PATH directory tree.
self.observer.schedule(event_handler, CODE_PATH, recursive=True)
self.observer.start()
self.observer.join()
class EventHandler(FileSystemEventHandler):
"""Handle events generated by Watchdog Observer"""
@classmethod
def on_any_event(cls, event):
if event.is_directory:
"""Ignore directory level events, like creating new empty directory etc.."""
return None
elif event.event_type == 'modified':
print("file under codebase directory is modified...")
if __name__ == '__main__':
watch = WatchMyCodebase()
watch.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment