From 597523b3b9ae8728594c23ea2f6f977767a9c1df Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Mon, 24 Jun 2019 14:27:15 +0200 Subject: [PATCH] Make observing multiple directories possible --- lib/tfw/components/inotify/inotify.py | 33 +++++++++++++--------- lib/tfw/components/inotify/test_inotify.py | 10 +++---- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/tfw/components/inotify/inotify.py b/lib/tfw/components/inotify/inotify.py index 032fd67..24abb8c 100644 --- a/lib/tfw/components/inotify/inotify.py +++ b/lib/tfw/components/inotify/inotify.py @@ -1,6 +1,7 @@ # pylint: disable=too-few-public-methods from time import time +from typing import Iterable from watchdog.observers import Observer from watchdog.events import FileSystemMovedEvent, PatternMatchingEventHandler @@ -64,31 +65,37 @@ class InotifyDirDeletedEvent(InotifyEvent): class InotifyObserver: - def __init__(self, path, patterns=None, exclude=None, recursive=False): - self._path = path + def __init__(self, paths, patterns=None, exclude=None, recursive=False): + self._paths = paths self._patterns = patterns self._exclude = exclude self._recursive = recursive self._observer = Observer() - self._reset(path, patterns, exclude) + self._reset(paths, patterns, exclude) - def _reset(self, path, patterns, exclude): + def _reset(self, paths, 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, self._recursive) + if isinstance(paths, str): + self._observer.schedule(self.handler, paths, self._recursive) + elif isinstance(paths, Iterable): + for path in paths: + self._observer.schedule(self.handler, path, self._recursive) + else: + raise ValueError('Expected one or more strings representing paths.') @property - def path(self): - return self._path + def paths(self): + return self._paths - @path.setter - def path(self, path): - self._path = path - self._reset(path, self._patterns, self._exclude) + @paths.setter + def paths(self, paths): + self._paths = paths + self._reset(paths, self._patterns, self._exclude) @property def patterns(self): @@ -97,7 +104,7 @@ class InotifyObserver: @patterns.setter def patterns(self, patterns): self._patterns = patterns - self._reset(self._path, patterns, self._exclude) + self._reset(self._paths, patterns, self._exclude) @property def exclude(self): @@ -106,7 +113,7 @@ class InotifyObserver: @exclude.setter def exclude(self, exclude): self._exclude = exclude - self._reset(self._path, self._patterns, exclude) + self._reset(self._paths, 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 5eccd1c..d0ac637 100644 --- a/lib/tfw/components/inotify/test_inotify.py +++ b/lib/tfw/components/inotify/test_inotify.py @@ -71,10 +71,10 @@ class InotifyContext: class InotifyTestObserver(InotifyObserver): - def __init__(self, path, patterns=None, exclude=None, recursive=False): + def __init__(self, paths, 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, recursive) + super().__init__(paths, patterns, exclude, recursive) def on_any_event(self, event): self.any_list.append(event) @@ -138,12 +138,12 @@ def test_delete(context): context.check_event(InotifyDirDeletedEvent, context.subdir) assert context.check_any() -def test_path(context): - context.observer.path = context.subdir +def test_paths(context): + context.observer.paths = context.subdir context.create_random_folder(context.workdir) newfile = context.create_random_file(context.subdir, '.txt') context.check_event(InotifyFileCreatedEvent, newfile) - context.observer.path = context.subdir + context.observer.paths = context.subdir assert context.check_any() def test_patterns(context):