From 4a94c55e3cedde22c96798c0bc91e1afb98be974 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Thu, 2 Aug 2012 16:41:50 -0400 Subject: [PATCH] cc-4105: removed extraneous requests being made to airtime when metadata is modified --- .../media-monitor2/media/monitor/listeners.py | 24 ++++++++++++++++++- .../media-monitor2/media/monitor/pure.py | 16 +++++++++++++ .../media/monitor/watchersyncer.py | 4 +++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index 61e89eac2..bc24b897a 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -35,18 +35,39 @@ from media.monitor.log import Loggable, get_logger # This could easily be a module class FileMediator(object): - ignored_set = set([]) + ignored_set = set([]) # for paths only + # TODO : unify ignored and skipped. + # for "special" conditions. could be generalized but too lazy now. + skip_checks = set([]) logger = get_logger() + @staticmethod def is_ignored(path): return path in FileMediator.ignored_set @staticmethod def ignore(path): FileMediator.ignored_set.add(path) @staticmethod def unignore(path): FileMediator.ignored_set.remove(path) + @staticmethod + def skip_next(*what_to_skip): + for skip in what_to_skip: + # standard nasty hack, too long to explain completely in comments but + # the gist of it is: + # 1. python's scoping rules are shitty and unintuitive (no "true" + # lexical scoping) + # 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) + FileMediator.skip_checks.add( skip_check ) def mediate_ignored(fn): def wrapped(self, event, *args,**kwargs): event.pathname = unicode(event.pathname, "utf-8") + skip_events = [s_check for s_check in FileMediator.skip_checks if s_check(event)] + for s_check in skip_events: + FileMediator.skip_checks.remove( s_check ) + # Only process skip_checks one at a time + return if FileMediator.is_ignored(event.pathname): FileMediator.logger.info("Ignoring: '%s' (once)" % event.pathname) FileMediator.unignore(event.pathname) @@ -90,6 +111,7 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): @mediate_ignored @IncludeOnly(mmp.supported_extensions) def process_modify(self, event): + FileMediator.skip_next('IN_MODIFY','IN_CLOSE_WRITE') dispatcher.send(signal=self.signal, sender=self, event=ModifyFile(event)) @mediate_ignored diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index cf4546782..b5bd89fbd 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -337,6 +337,22 @@ def sub_path(directory,f): common = os.path.commonprefix([ directory, normpath(f) ]) return common == normalized +def auto_enum(*sequential, **named): + enums = dict(zip(sequential, range(len(sequential))), **named) + return type('Enum', (), enums) + +def enum(**enums): + """ + >>> MyEnum = enum(ONE=1, TWO=2, THREE='three') + >>> MyEnum.ONE + 1 + >>> MyEnum.TWO + 2 + >>> MyEnum.THREE + 'three' + """ + return type('Enum', (), enums) + if __name__ == '__main__': import doctest doctest.testmod() diff --git a/python_apps/media-monitor2/media/monitor/watchersyncer.py b/python_apps/media-monitor2/media/monitor/watchersyncer.py index 8c393718b..0baab9654 100644 --- a/python_apps/media-monitor2/media/monitor/watchersyncer.py +++ b/python_apps/media-monitor2/media/monitor/watchersyncer.py @@ -111,7 +111,9 @@ class WatchSyncer(ReportHandler,Loggable): # TODO : more types of events need to be handled here if hasattr(event, 'pack'): # We push this event into queue - self.logger.info("Received event '%s'. Path: '%s'" % ( "", getattr(event,'path','No path exists') )) + self.logger.info("Received event '%s'. Path: '%s'" % \ + ( event.__class__.__name__, + getattr(event,'path','No path exists') )) try: self.push_queue( event ) except BadSongFile as e: self.fatal_exception("Received bas song file '%s'" % e.path, e)