mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-12 20:07:16 +00:00
Make observing multiple directories possible
This commit is contained in:
parent
65c8bcee2b
commit
597523b3b9
@ -1,6 +1,7 @@
|
|||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemMovedEvent, PatternMatchingEventHandler
|
from watchdog.events import FileSystemMovedEvent, PatternMatchingEventHandler
|
||||||
@ -64,31 +65,37 @@ class InotifyDirDeletedEvent(InotifyEvent):
|
|||||||
|
|
||||||
|
|
||||||
class InotifyObserver:
|
class InotifyObserver:
|
||||||
def __init__(self, path, patterns=None, exclude=None, recursive=False):
|
def __init__(self, paths, patterns=None, exclude=None, recursive=False):
|
||||||
self._path = path
|
self._paths = paths
|
||||||
self._patterns = patterns
|
self._patterns = patterns
|
||||||
self._exclude = exclude
|
self._exclude = exclude
|
||||||
self._recursive = recursive
|
self._recursive = recursive
|
||||||
self._observer = Observer()
|
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
|
dispatch_event = self.dispatch_event
|
||||||
class TransformerEventHandler(PatternMatchingEventHandler):
|
class TransformerEventHandler(PatternMatchingEventHandler):
|
||||||
def on_any_event(self, event):
|
def on_any_event(self, event):
|
||||||
dispatch_event(event)
|
dispatch_event(event)
|
||||||
self.handler = TransformerEventHandler(patterns, exclude)
|
self.handler = TransformerEventHandler(patterns, exclude)
|
||||||
self._observer.unschedule_all()
|
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)
|
self._observer.schedule(self.handler, path, self._recursive)
|
||||||
|
else:
|
||||||
|
raise ValueError('Expected one or more strings representing paths.')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path(self):
|
def paths(self):
|
||||||
return self._path
|
return self._paths
|
||||||
|
|
||||||
@path.setter
|
@paths.setter
|
||||||
def path(self, path):
|
def paths(self, paths):
|
||||||
self._path = path
|
self._paths = paths
|
||||||
self._reset(path, self._patterns, self._exclude)
|
self._reset(paths, self._patterns, self._exclude)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def patterns(self):
|
def patterns(self):
|
||||||
@ -97,7 +104,7 @@ class InotifyObserver:
|
|||||||
@patterns.setter
|
@patterns.setter
|
||||||
def patterns(self, patterns):
|
def patterns(self, patterns):
|
||||||
self._patterns = patterns
|
self._patterns = patterns
|
||||||
self._reset(self._path, patterns, self._exclude)
|
self._reset(self._paths, patterns, self._exclude)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def exclude(self):
|
def exclude(self):
|
||||||
@ -106,7 +113,7 @@ class InotifyObserver:
|
|||||||
@exclude.setter
|
@exclude.setter
|
||||||
def exclude(self, exclude):
|
def exclude(self, exclude):
|
||||||
self._exclude = exclude
|
self._exclude = exclude
|
||||||
self._reset(self._path, self._patterns, exclude)
|
self._reset(self._paths, self._patterns, exclude)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._observer.start()
|
self._observer.start()
|
||||||
|
@ -71,10 +71,10 @@ class InotifyContext:
|
|||||||
|
|
||||||
|
|
||||||
class InotifyTestObserver(InotifyObserver):
|
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.any_list = []
|
||||||
self.create_queue, self.modify_queue, self.move_queue, self.delete_queue = [Queue() for _ in range(4)]
|
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):
|
def on_any_event(self, event):
|
||||||
self.any_list.append(event)
|
self.any_list.append(event)
|
||||||
@ -138,12 +138,12 @@ def test_delete(context):
|
|||||||
context.check_event(InotifyDirDeletedEvent, context.subdir)
|
context.check_event(InotifyDirDeletedEvent, context.subdir)
|
||||||
assert context.check_any()
|
assert context.check_any()
|
||||||
|
|
||||||
def test_path(context):
|
def test_paths(context):
|
||||||
context.observer.path = context.subdir
|
context.observer.paths = context.subdir
|
||||||
context.create_random_folder(context.workdir)
|
context.create_random_folder(context.workdir)
|
||||||
newfile = context.create_random_file(context.subdir, '.txt')
|
newfile = context.create_random_file(context.subdir, '.txt')
|
||||||
context.check_event(InotifyFileCreatedEvent, newfile)
|
context.check_event(InotifyFileCreatedEvent, newfile)
|
||||||
context.observer.path = context.subdir
|
context.observer.paths = context.subdir
|
||||||
assert context.check_any()
|
assert context.check_any()
|
||||||
|
|
||||||
def test_patterns(context):
|
def test_patterns(context):
|
||||||
|
Loading…
Reference in New Issue
Block a user