Last active
April 30, 2022 22:32
-
-
Save mplewis/9af84fd24c21a8c14ae3 to your computer and use it in GitHub Desktop.
Watch a single file and output a diff when it changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from diff_match_patch import diff_match_patch | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
import sys | |
import time | |
from os.path import abspath, dirname | |
class DiffHandler(FileSystemEventHandler): | |
def __init__(self, to_watch): | |
self.differ = diff_match_patch() | |
self.to_watch = to_watch | |
with open(self.to_watch) as f: | |
self.last_contents = f.read() | |
def on_modified(self, event): | |
if event.src_path == self.to_watch: | |
with open(self.to_watch) as f: | |
new_contents = f.read() | |
patch = self.differ.patch_make(self.last_contents, new_contents) | |
patch_text = self.differ.patch_toText(patch) | |
print(patch_text) | |
self.last_contents = new_contents | |
if len(sys.argv) > 1: | |
to_watch = abspath(sys.argv[1]) | |
else: | |
print('Error: Select a file to watch.') | |
sys.exit(1) | |
observer = Observer() | |
observer.schedule(DiffHandler(to_watch), dirname(to_watch)) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff-match-patch==20121119 | |
watchdog==0.8.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment