+class FileWatcher:
+ def __init__(self, filename, cb):
+ """Initializes this class.
+
+ @type filename: string
+ @param filename: File to watch
+ @type cb: callable
+ @param cb: Function called on file change
+
+ """
+ self._filename = filename
+ self._cb = cb
+
+ wm = pyinotify.WatchManager()
+ self._handler = asyncnotifier.SingleFileEventHandler(wm, self._OnInotify,
+ filename)
+ asyncnotifier.AsyncNotifier(wm, default_proc_fun=self._handler)
+ self._handler.enable()
+
+ def _OnInotify(self, notifier_enabled):
+ """Called upon update of the RAPI users file by pyinotify.
+
+ @type notifier_enabled: boolean
+ @param notifier_enabled: whether the notifier is still enabled
+
+ """
+ logging.info("Reloading modified %s", self._filename)
+
+ self._cb()
+
+ # Renable the watch again if we'd an atomic update of the file (e.g. mv)
+ if not notifier_enabled:
+ self._handler.enable()
+
+