From cd630834a985be4b39a673d022e180de3ff20517 Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Sat, 13 Dec 2014 16:41:35 +0000 Subject: Initial commit --- watcher.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 watcher.py (limited to 'watcher.py') diff --git a/watcher.py b/watcher.py new file mode 100644 index 0000000..7dbd434 --- /dev/null +++ b/watcher.py @@ -0,0 +1,66 @@ +import atexit +import configparser + +import pyinotify + +import library + + +class EventHandler(pyinotify.ProcessEvent): + def process_IN_CREATE(self, event): + print("Creating:", event.pathname) + library.run(event.pathname) + + def process_IN_DELETE(self, event): + print("Removing:", event.pathname) + library.delete_file(event.pathname) + + def process_IN_MOVED_TO(self, event): + print("Moved to:", event.pathname) + library.update_track_filename(event.src_pathname, event.pathname) + + def process_IN_MODIFY(self, event): + print("Modified:", event.pathname) + library.update_file(event.pathname) + + +class LibraryWatcher: + + def __init__(self, path): + print("Setting up library watcher") + + if not hasattr(self, "path"): + setattr(self, "path", path) + + if not hasattr(self, "wm"): + setattr(self, "wm", pyinotify.WatchManager()) + + mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | \ + pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM | \ + pyinotify.IN_MODIFY + + if not hasattr(self, "notifier"): + setattr(self, + "notifier", + pyinotify.ThreadedNotifier(self.wm, EventHandler())) + + self.notifier.coalesce_events() + self.notifier.start() + + if not hasattr(self, "wdd"): + setattr(self, "wdd", self.wm.add_watch(path, mask, rec=True, + auto_add=True)) + + print("Set up watch on ", path) + + atexit.register(self.stop) + + def stop(self): + if self.wdd[self.path] > 0: + self.wm.rm_watch(self.wdd[self.path], rec=True) + +if __name__ == "__main__": + config = configparser.ConfigParser() + config.read("mach2.ini") + + watch = LibraryWatcher(config["DEFAULT"]["media_dir"]) -- cgit v1.2.3