From 3af6036d9eed9d770c9d417de528268b2a4ca463 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 13 Aug 2012 17:31:54 -0400 Subject: [PATCH] cc-4105: major code reformatting effort --- .../media-monitor2/media/monitor/airtime.py | 37 +++++++++++-------- .../media-monitor2/media/monitor/events.py | 10 ++--- .../media-monitor2/media/monitor/listeners.py | 30 ++------------- python_apps/media-monitor2/mm2.py | 26 +++++++------ 4 files changed, 44 insertions(+), 59 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/airtime.py b/python_apps/media-monitor2/media/monitor/airtime.py index 9381bfdc9..c38abc049 100644 --- a/python_apps/media-monitor2/media/monitor/airtime.py +++ b/python_apps/media-monitor2/media/monitor/airtime.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from kombu.messaging import Exchange, Queue, Consumer +from kombu.messaging import Exchange, Queue, Consumer from kombu.connection import BrokerConnection import json import os @@ -7,12 +7,12 @@ import copy import traceback from media.monitor.exceptions import BadSongFile -from media.monitor.metadata import Metadata -from media.monitor.log import Loggable -from media.monitor.syncdb import AirtimeDB +from media.monitor.metadata import Metadata +from media.monitor.log import Loggable +from media.monitor.syncdb import AirtimeDB from media.monitor.exceptions import DirectoryIsNotListed -from media.monitor.bootstrap import Bootstrapper -from media.monitor.listeners import FileMediator +from media.monitor.bootstrap import Bootstrapper +from media.monitor.listeners import FileMediator from api_clients import api_client as apc @@ -39,7 +39,7 @@ class AirtimeNotifier(Loggable): self.connection = BrokerConnection(cfg["rabbitmq_host"], cfg["rabbitmq_user"], cfg["rabbitmq_password"], cfg["rabbitmq_vhost"]) - channel = self.connection.channel() + channel = self.connection.channel() consumer = Consumer(channel, schedule_queue) consumer.register_callback(self.handle_message) consumer.consume() @@ -63,12 +63,12 @@ class AirtimeNotifier(Loggable): class AirtimeMessageReceiver(Loggable): def __init__(self, cfg, manager): self.dispatch_table = { - 'md_update' : self.md_update, - 'new_watch' : self.new_watch, + 'md_update' : self.md_update, + 'new_watch' : self.new_watch, 'remove_watch' : self.remove_watch, 'rescan_watch' : self.rescan_watch, - 'change_stor' : self.change_storage, - 'file_delete' : self.file_delete, + 'change_stor' : self.change_storage, + 'file_delete' : self.file_delete, } self.cfg = cfg self.manager = manager @@ -99,9 +99,9 @@ class AirtimeMessageReceiver(Loggable): raise ValueError("You must provide either directory_id or \ directory") sdb = AirtimeDB(apc.AirtimeApiClient.create_right_config()) - if directory: directory = os.path.normpath(directory) - if directory_id == None: directory_id = sdb.to_id(directory) - if directory == None: directory = sdb.to_directory(directory_id) + if directory : directory = os.path.normpath(directory) + if directory_id == None : directory_id = sdb.to_id(directory) + if directory == None : directory = sdb.to_directory(directory_id) try: bs = Bootstrapper( sdb, self.manager.watch_signal() ) bs.flush_watch( directory=directory, last_ran=self.cfg.last_ran() ) @@ -181,6 +181,11 @@ class AirtimeMessageReceiver(Loggable): try: self.logger.info("Attempting to delete '%s'" % msg['filepath']) + # We use FileMediator to ignore any paths with + # msg['filepath'] so that we do not send a duplicate delete + # request that we'd normally get form pyinotify. But right + # now event contractor would take care of this sort of + # thing anyway so this might not be necessary after all FileMediator.ignore(msg['filepath']) os.unlink(msg['filepath']) if not os.path.exists(msg['filepath']): @@ -189,11 +194,11 @@ class AirtimeMessageReceiver(Loggable): except Exception as e: self.logger.info("Failed to delete '%s'" % msg['filepath']) self.logger.info("Error: " % str(e)) - else: + else: # validation for filepath existence failed self.logger.info("Attempting to delete file '%s' that does not \ exist. Full request coming:" % msg['filepath']) self.logger.info(msg) - else: + else: # we did not get the special 'delete' tag. no deleting self.logger.info("No clippy confirmation, ignoring event. \ Out of curiousity we will print some details.") self.logger.info(msg) diff --git a/python_apps/media-monitor2/media/monitor/events.py b/python_apps/media-monitor2/media/monitor/events.py index 84809b19f..214f2230a 100644 --- a/python_apps/media-monitor2/media/monitor/events.py +++ b/python_apps/media-monitor2/media/monitor/events.py @@ -95,11 +95,11 @@ class BaseEvent(Loggable): # nothing to see here, please move along def morph_into(self, evt): self.logger.info("Morphing %s into %s" % ( str(self), str(evt) ) ) - self._raw_event = evt - self.path = evt.path - self.add_safe_pack_hook(evt._pack_hook) - self.__class__ = evt.__class__ + self._raw_event = evt + self.path = evt.path + self.__class__ = evt.__class__ evt._morph_target = True + self.add_safe_pack_hook(evt._pack_hook) return self class FakePyinotify(object): @@ -186,7 +186,7 @@ class DeleteDirWatch(BaseEvent): super(DeleteDirWatch, self).__init__(*args, **kwargs) def pack(self): req_dict = {} - req_dict['mode'] = u'delete_dir' + req_dict['mode'] = u'delete_dir' req_dict['MDATA_KEY_FILEPATH'] = unicode( self.path + "/" ) return [req_dict] diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index 67d06faa6..93c77f394 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -36,13 +36,12 @@ from media.monitor.log import Loggable, get_logger # OrganizeListener('watch_signal') <= wrong # OrganizeListener(signal='watch_signal') <= right -# TODO : remove this FileMediator stuff it's not used anywhere and it's too -# complicated class FileMediator(object): + """ + FileMediator is used an intermediate mechanism that filters out certain + events. + """ 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 @@ -51,31 +50,10 @@ class FileMediator(object): def ignore(path): FileMediator.ignored_set.add(path) @staticmethod def unignore(path): FileMediator.ignored_set.remove(path) - @staticmethod - 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: - # 1. python's scoping rules are sometimes strange. - # 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: getattr(v,kwargs['key']) == 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 - FileMediator.logger.info("Skip checked: '%s'" % str(event)) - return if FileMediator.is_ignored(event.pathname): FileMediator.logger.info("Ignoring: '%s' (once)" % event.pathname) FileMediator.unignore(event.pathname) diff --git a/python_apps/media-monitor2/mm2.py b/python_apps/media-monitor2/mm2.py index f1f794f17..762f7ad67 100644 --- a/python_apps/media-monitor2/mm2.py +++ b/python_apps/media-monitor2/mm2.py @@ -2,20 +2,22 @@ import sys import os -from media.monitor.manager import Manager -from media.monitor.bootstrap import Bootstrapper -from media.monitor.log import get_logger, setup_logging -from media.monitor.config import MMConfig -from media.monitor.toucher import ToucherThread -from media.monitor.syncdb import AirtimeDB -from media.monitor.exceptions import FailedToObtainLocale, FailedToSetLocale, \ - NoConfigFile -from media.monitor.airtime import AirtimeNotifier, AirtimeMessageReceiver -from media.monitor.watchersyncer import WatchSyncer -from media.monitor.eventdrainer import EventDrainer +from media.monitor.manager import Manager +from media.monitor.bootstrap import Bootstrapper +from media.monitor.log import get_logger, setup_logging +from media.monitor.config import MMConfig +from media.monitor.toucher import ToucherThread +from media.monitor.syncdb import AirtimeDB +from media.monitor.exceptions import FailedToObtainLocale, \ + FailedToSetLocale, \ + NoConfigFile +from media.monitor.airtime import AirtimeNotifier, \ + AirtimeMessageReceiver +from media.monitor.watchersyncer import WatchSyncer +from media.monitor.eventdrainer import EventDrainer from media.update.replaygainupdater import ReplayGainUpdater -import media.monitor.pure as mmp +import media.monitor.pure as mmp from api_clients import api_client as apc