Make observing multiple directories possible

This commit is contained in:
R. Richard 2019-06-24 14:27:15 +02:00 committed by Kristóf Tóth
parent 65c8bcee2b
commit 597523b3b9
2 changed files with 25 additions and 18 deletions

View File

@ -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()
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()

View File

@ -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):