Make recursive watching optional and rename reset()

This commit is contained in:
R. Richard 2019-06-11 11:21:46 +02:00
parent ca6797b354
commit d2131682a8
2 changed files with 10 additions and 10 deletions

View File

@ -64,21 +64,21 @@ class InotifyDirDeletedEvent(InotifyEvent):
class InotifyObserver: class InotifyObserver:
def __init__(self, path, patterns=None, exclude=None): def __init__(self, path, patterns=None, exclude=None, recursive=False):
self._path = path self._path = path
self._patterns = patterns self._patterns = patterns
self._exclude = exclude self._exclude = exclude
self.observer = Observer() 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 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()
self.observer.schedule(self.handler, path, recursive=True) self.observer.schedule(self.handler, path, recursive)
@property @property
def path(self): def path(self):
@ -87,7 +87,7 @@ class InotifyObserver:
@path.setter @path.setter
def path(self, path): def path(self, path):
self._path = path self._path = path
self.reset(path, self._patterns, self._exclude) self._reset(path, self._patterns, self._exclude)
@property @property
def patterns(self): def patterns(self):
@ -96,7 +96,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._path, patterns, self._exclude)
@property @property
def exclude(self): def exclude(self):
@ -105,7 +105,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._path, self._patterns, exclude)
def start(self): def start(self):
self.observer.start() self.observer.start()

View File

@ -66,10 +66,10 @@ class InotifyContext:
class InotifyTestObserver(InotifyObserver): 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.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) super().__init__(path, 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)
@ -99,7 +99,7 @@ def context():
mkdir(newdir) mkdir(newdir)
Path(join(newdir, generate_name()+'.txt')).touch() Path(join(newdir, generate_name()+'.txt')).touch()
monitor = InotifyTestObserver(workdir) monitor = InotifyTestObserver(workdir, recursive=True)
monitor.start() monitor.start()
yield InotifyContext(workdir, monitor) yield InotifyContext(workdir, monitor)