Skip to content

Instantly share code, notes, and snippets.

@ftalbrecht
Created August 11, 2020 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftalbrecht/7d447c7aa4a21d1f8e588df7a402a1c3 to your computer and use it in GitHub Desktop.
Save ftalbrecht/7d447c7aa4a21d1f8e588df7a402a1c3 to your computer and use it in GitHub Desktop.
watch_and_notify.py
#!/usr/bin/env python
from subprocess import CalledProcessError, DEVNULL, PIPE, run
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
def notify(msg):
run(['kdeconnect-cli', '-n', 'felix-handy', '--ping-msg', f'"{msg}"'], check=True, stdout=PIPE, stderr=PIPE)
class NotifyEventHandler(FileSystemEventHandler):
# def on_moved(self, event):
# super(NotifyEventHandler, self).on_moved(event)
# what = 'directory' if event.is_directory else 'file'
# logging.info("Moved %s: from %s to %s", what, event.src_path,
# event.dest_path)
def on_created(self, event):
super(NotifyEventHandler, self).on_created(event)
notify(event.src_path)
# what = 'directory' if event.is_directory else 'file'
# logging.info("Created %s: %s", what, event.src_path)
# def on_deleted(self, event):
# super(NotifyEventHandler, self).on_deleted(event)
# what = 'directory' if event.is_directory else 'file'
# logging.info("Deleted %s: %s", what, event.src_path)
def on_modified(self, event):
super(NotifyEventHandler, self).on_modified(event)
notify(event.src_path)
# what = 'directory' if event.is_directory else 'file'
# logging.info("Modified %s: %s", what, event.src_path)
if __name__ == '__main__':
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = NotifyEventHandler()
observer = Observer()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment