From d2131682a8cafd185391aa40d7b8eb49dba86570 Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Tue, 11 Jun 2019 11:21:46 +0200 Subject: [PATCH] Make recursive watching optional and rename reset() --- lib/tfw/components/inotify/inotify.py | 14 +++++++------- lib/tfw/components/inotify/test_inotify.py | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/tfw/components/inotify/inotify.py b/lib/tfw/components/inotify/inotify.py index f26ce6b..2cf9523 100644 --- a/lib/tfw/components/inotify/inotify.py +++ b/lib/tfw/components/inotify/inotify.py @@ -64,21 +64,21 @@ class InotifyDirDeletedEvent(InotifyEvent): class InotifyObserver: - def __init__(self, path, patterns=None, exclude=None): + def __init__(self, path, patterns=None, exclude=None, recursive=False): self._path = path self._patterns = patterns self._exclude = exclude self.observer = Observer() - self.reset(path, patterns, exclude) + self._reset(path, patterns, exclude) - def reset(self, path, patterns, exclude): + def _reset(self, path, patterns, exclude): dispatch_event = self.dispatch_event class TransformerEventHandler(PatternMatchingEventHandler): def on_any_event(self, event): dispatch_event(event) self.handler = TransformerEventHandler(patterns, exclude) self.observer.unschedule_all() - self.observer.schedule(self.handler, path, recursive=True) + self.observer.schedule(self.handler, path, recursive) @property def path(self): @@ -87,7 +87,7 @@ class InotifyObserver: @path.setter def path(self, path): self._path = path - self.reset(path, self._patterns, self._exclude) + self._reset(path, self._patterns, self._exclude) @property def patterns(self): @@ -96,7 +96,7 @@ class InotifyObserver: @patterns.setter def patterns(self, patterns): self._patterns = patterns - self.reset(self._path, patterns, self._exclude) + self._reset(self._path, patterns, self._exclude) @property def exclude(self): @@ -105,7 +105,7 @@ class InotifyObserver: @exclude.setter def exclude(self, exclude): self._exclude = exclude - self.reset(self._path, self._patterns, exclude) + self._reset(self._path, self._patterns, exclude) def start(self): self.observer.start() diff --git a/lib/tfw/components/inotify/test_inotify.py b/lib/tfw/components/inotify/test_inotify.py index 702e2c7..6b4bdb3 100644 --- a/lib/tfw/components/inotify/test_inotify.py +++ b/lib/tfw/components/inotify/test_inotify.py @@ -66,10 +66,10 @@ class InotifyContext: class InotifyTestObserver(InotifyObserver): - def __init__(self, path, patterns=None, exclude=None): + def __init__(self, path, patterns=None, exclude=None, recursive=False): self.any_list = [] self.create_queue, self.modify_queue, self.move_queue, self.delete_queue = [Queue() for _ in range(4)] - super().__init__(path, patterns, exclude) + super().__init__(path, patterns, exclude, recursive) def on_any_event(self, event): self.any_list.append(event) @@ -99,7 +99,7 @@ def context(): mkdir(newdir) Path(join(newdir, generate_name()+'.txt')).touch() - monitor = InotifyTestObserver(workdir) + monitor = InotifyTestObserver(workdir, recursive=True) monitor.start() yield InotifyContext(workdir, monitor)