From 5fa6def11153bd329723d0a6646e9635b426d57a Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 3 Aug 2012 11:06:08 -0400 Subject: [PATCH] cc-4105: added handling for delete dir event --- .../media-monitor2/media/monitor/listeners.py | 19 ++++++++++++++----- python_apps/media-monitor2/pyitest.py | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index f129b7193..2b2c58199 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -4,7 +4,7 @@ from pydispatch import dispatcher import media.monitor.pure as mmp from media.monitor.pure import IncludeOnly -from media.monitor.events import OrganizeFile, NewFile, DeleteFile, ModifyFile +from media.monitor.events import OrganizeFile, NewFile, DeleteFile, ModifyFile, DeleteDir from media.monitor.log import Loggable, get_logger # We attempt to document a list of all special cases and hacks that the @@ -48,7 +48,9 @@ class FileMediator(object): @staticmethod def unignore(path): FileMediator.ignored_set.remove(path) @staticmethod - def skip_next(*what_to_skip): + def skip_next(*what_to_skip,**kwargs): + # Poor man's default arguments + if 'key' not in kwargs: kwargs['key'] = 'maskname' for skip in what_to_skip: # standard nasty hack, too long to explain completely in comments but # the gist of it is: @@ -56,7 +58,7 @@ class FileMediator(object): # 2. workaround is very similar to what you do in javascript when # you write stuff like (function (x,y) { console.log(x+y); })(2,4) # to be avoid clobbering peoples' namespace. - skip_check = (lambda skip: lambda v: v.maskname == skip)(skip) + skip_check = (lambda skip: lambda v: getattr(v,kwargs['key']) == skip)(skip) FileMediator.skip_checks.add( skip_check ) def mediate_ignored(fn): @@ -103,14 +105,17 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): def process_IN_CLOSE_WRITE(self, event): self.process_create(event) def process_IN_MOVED_TO(self, event): self.process_create(event) - def process_IN_MOVED_FROM(self, event): self.process_delete(event) + def process_IN_MOVED_FROM(self, event): + # Is either delete dir or delete file + if event.is_dir: self.process_delete_dir(event) + else: self.process_delete(event) def process_IN_DELETE(self,event): self.process_delete(event) def process_IN_MODIFY(self,event): self.process_modify(event) @mediate_ignored @IncludeOnly(mmp.supported_extensions) def process_modify(self, event): - FileMediator.skip_next('IN_MODIFY','IN_CLOSE_WRITE') + FileMediator.skip_next('IN_MODIFY','IN_CLOSE_WRITE',key='maskname') dispatcher.send(signal=self.signal, sender=self, event=ModifyFile(event)) @mediate_ignored @@ -123,6 +128,10 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): def process_delete(self, event): dispatcher.send(signal=self.signal, sender=self, event=DeleteFile(event)) + @mediate_ignored + def process_delete_dir(self, event): + dispatcher.send(signal=self.signal, sender=self, event=DeleteDir(event)) + def flush_events(self, path): """ walk over path and send a NewFile event for every file in this directory. diff --git a/python_apps/media-monitor2/pyitest.py b/python_apps/media-monitor2/pyitest.py index 9d3870b5b..471ba727e 100755 --- a/python_apps/media-monitor2/pyitest.py +++ b/python_apps/media-monitor2/pyitest.py @@ -23,6 +23,7 @@ def main(): sys.exit(0) wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm) + print("Watching: '%s'" % ldir) wm.add_watch(ldir, pyinotify.ALL_EVENTS, auto_add=True, rec=True, proc_fun=AT()) notifier.loop()