From 4acb7d92dbd79b9ee84d92e201069e50ad361e9f Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 19 Nov 2012 16:07:26 -0500 Subject: [PATCH] removed hardcoded signals in the code. --- .../media-monitor2/media/monitor/bootstrap.py | 3 ++- .../media-monitor2/media/monitor/events.py | 3 ++- .../media-monitor2/media/monitor/handler.py | 9 +++++---- .../media-monitor2/media/monitor/listeners.py | 20 +++++++++---------- .../media-monitor2/media/monitor/manager.py | 17 ++++++++-------- .../media-monitor2/media/monitor/organizer.py | 5 +++-- .../media/monitor/watchersyncer.py | 4 ++-- .../media-monitor2/media/saas/launcher.py | 4 ++-- 8 files changed, 35 insertions(+), 30 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/bootstrap.py b/python_apps/media-monitor2/media/monitor/bootstrap.py index 0a13ea0b4..6e685c964 100644 --- a/python_apps/media-monitor2/media/monitor/bootstrap.py +++ b/python_apps/media-monitor2/media/monitor/bootstrap.py @@ -2,6 +2,7 @@ import os from pydispatch import dispatcher from media.monitor.events import NewFile, DeleteFile, ModifyFile from media.monitor.log import Loggable +from media.saas.thread import getsig import media.monitor.pure as mmp class Bootstrapper(Loggable): @@ -16,7 +17,7 @@ class Bootstrapper(Loggable): watch_signal - the signals should send events for every file on. """ self.db = db - self.watch_signal = watch_signal + self.watch_signal = getsig(watch_signal) def flush_all(self, last_ran): """ diff --git a/python_apps/media-monitor2/media/monitor/events.py b/python_apps/media-monitor2/media/monitor/events.py index 8fb373704..ac16b3746 100644 --- a/python_apps/media-monitor2/media/monitor/events.py +++ b/python_apps/media-monitor2/media/monitor/events.py @@ -8,6 +8,7 @@ from media.monitor.pure import LazyProperty from media.monitor.metadata import Metadata from media.monitor.log import Loggable from media.monitor.exceptions import BadSongFile +from media.saas.thread import getsig class PathChannel(object): """ @@ -15,7 +16,7 @@ class PathChannel(object): used as a named tuple """ def __init__(self, signal, path): - self.signal = signal + self.signal = getsig(signal) self.path = path # TODO : Move this to it's file. Also possible unsingleton and use it as a diff --git a/python_apps/media-monitor2/media/monitor/handler.py b/python_apps/media-monitor2/media/monitor/handler.py index dd1d3843a..c67a437ef 100644 --- a/python_apps/media-monitor2/media/monitor/handler.py +++ b/python_apps/media-monitor2/media/monitor/handler.py @@ -3,6 +3,7 @@ from pydispatch import dispatcher import abc from media.monitor.log import Loggable +from media.saas.thread import getsig import media.monitor.pure as mmp # Defines the handle interface @@ -21,10 +22,10 @@ class ReportHandler(Handles): """ __metaclass__ = abc.ABCMeta def __init__(self, signal, weak=False): - self.signal = signal - self.report_signal = "badfile" + self.signal = getsig(signal) + self.report_signal = getsig("badfile") def dummy(sender, event): self.handle(sender,event) - dispatcher.connect(dummy, signal=signal, sender=dispatcher.Any, + dispatcher.connect(dummy, signal=self.signal, sender=dispatcher.Any, weak=weak) def report_problem_file(self, event, exception=None): @@ -38,7 +39,7 @@ class ProblemFileHandler(Handles, Loggable): """ def __init__(self, channel, **kwargs): self.channel = channel - self.signal = self.channel.signal + self.signal = getsig(self.channel.signal) self.problem_dir = self.channel.path def dummy(sender, event, exception): self.handle(sender, event, exception) diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index b33a5c1a9..081d0c4a4 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -9,7 +9,7 @@ from media.monitor.events import OrganizeFile, NewFile, MoveFile, DeleteFile, \ DeleteDir, EventRegistry, MoveDir,\ DeleteDirWatch from media.monitor.log import Loggable, get_logger - +from media.saas.thread import getsig # Note: Because of the way classes that inherit from pyinotify.ProcessEvent # interact with constructors. you should only instantiate objects from them # using keyword arguments. For example: @@ -45,7 +45,7 @@ class BaseListener(object): def __str__(self): return "Listener(%s), Signal(%s)" % \ (self.__class__.__name__, self. signal) - def my_init(self, signal): self.signal = signal + def my_init(self, signal): self.signal = getsig(signal) class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable): def process_IN_CLOSE_WRITE(self, event): @@ -66,14 +66,14 @@ class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable): self.logger.info("Bootstrapping: File in 'organize' directory: \ '%s'" % f) if not mmp.file_locked(f): - dispatcher.send(signal=self.signal, sender=self, + dispatcher.send(signal=getsig(self.signal), sender=self, event=OrganizeFile(f)) flushed += 1 #self.logger.info("Flushed organized directory with %d files" % flushed) @IncludeOnly(mmp.supported_extensions) def process_to_organize(self, event): - dispatcher.send(signal=self.signal, sender=self, + dispatcher.send(signal=getsig(self.signal), sender=self, event=OrganizeFile(event)) class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): @@ -101,14 +101,14 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): def delete_watch_dir(self, event): e = DeleteDirWatch(event) - dispatcher.send(signal='watch_move', sender=self, event=e) - dispatcher.send(signal=self.signal, sender=self, event=e) + dispatcher.send(signal=getsig('watch_move'), sender=self, event=e) + dispatcher.send(signal=getsig(self.signal), sender=self, event=e) @mediate_ignored @IncludeOnly(mmp.supported_extensions) def process_create(self, event): evt = NewFile(event) - dispatcher.send(signal=self.signal, sender=self, event=evt) + dispatcher.send(signal=getsig(self.signal), sender=self, event=evt) return evt @mediate_ignored @@ -117,13 +117,13 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): evt = None if event.dir : evt = DeleteDir(event) else : evt = DeleteFile(event) - dispatcher.send(signal=self.signal, sender=self, event=evt) + dispatcher.send(signal=getsig(self.signal), sender=self, event=evt) return evt @mediate_ignored def process_delete_dir(self, event): evt = DeleteDir(event) - dispatcher.send(signal=self.signal, sender=self, event=evt) + dispatcher.send(signal=getsig(self.signal), sender=self, event=evt) return evt def flush_events(self, path): @@ -138,6 +138,6 @@ class StoreWatchListener(BaseListener, Loggable, pyinotify.ProcessEvent): added = 0 for f in mmp.walk_supported(path, clean_empties=False): added += 1 - dispatcher.send( signal=self.signal, sender=self, event=NewFile(f) ) + dispatcher.send( signal=getsig(self.signal), sender=self, event=NewFile(f) ) self.logger.info( "Flushed watch directory. added = %d" % added ) diff --git a/python_apps/media-monitor2/media/monitor/manager.py b/python_apps/media-monitor2/media/monitor/manager.py index d2c0a5f64..1fc7b695b 100644 --- a/python_apps/media-monitor2/media/monitor/manager.py +++ b/python_apps/media-monitor2/media/monitor/manager.py @@ -8,7 +8,7 @@ from media.monitor.log import Loggable from media.monitor.listeners import StoreWatchListener, OrganizeListener from media.monitor.handler import ProblemFileHandler from media.monitor.organizer import Organizer -from media.saas.thread import InstanceInheritingThread +from media.saas.thread import InstanceInheritingThread, getsig import media.monitor.pure as mmp @@ -37,8 +37,8 @@ class Manager(Loggable): def __init__(self): self.wm = pyinotify.WatchManager() # These two instance variables are assumed to be constant - self.watch_channel = 'watch' - self.organize_channel = 'organize' + self.watch_channel = getsig('watch') + self.organize_channel = getsig('organize') self.watch_listener = StoreWatchListener(signal = self.watch_channel) self.__timeout_thread = ManagerTimeout(self) self.__timeout_thread.daemon = True @@ -54,11 +54,11 @@ class Manager(Loggable): self.organize_channel), } def dummy(sender, event): self.watch_move( event.path, sender=sender ) - dispatcher.connect(dummy, signal='watch_move', sender=dispatcher.Any, - weak=False) + dispatcher.connect(dummy, signal=getsig('watch_move'), + sender=dispatcher.Any, weak=False) def subwatch_add(sender, directory): self.__add_watch(directory, self.watch_listener) - dispatcher.connect(subwatch_add, signal='add_subwatch', + dispatcher.connect(subwatch_add, signal=getsig('add_subwatch'), sender=dispatcher.Any, weak=False) # A private mapping path => watch_descriptor # we use the same dictionary for organize, watch, store wd events. @@ -81,7 +81,7 @@ class Manager(Loggable): def watch_signal(self): """ Return the signal string our watch_listener is reading events from """ - return self.watch_listener.signal + return getsig(self.watch_listener.signal) def __remove_watch(self,path): """ Remove path from being watched (first will check if 'path' @@ -131,7 +131,8 @@ class Manager(Loggable): """ Set the path where problem files should go """ self.organize['problem_files_path'] = new_path self.organize['problem_handler'] = \ - ProblemFileHandler( PathChannel(signal='badfile',path=new_path) ) + ProblemFileHandler( PathChannel(signal=getsig('badfile'), + path=new_path) ) def get_recorded_path(self): """ returns the path of the recorded directory """ diff --git a/python_apps/media-monitor2/media/monitor/organizer.py b/python_apps/media-monitor2/media/monitor/organizer.py index ce1849b90..6d72bee4b 100644 --- a/python_apps/media-monitor2/media/monitor/organizer.py +++ b/python_apps/media-monitor2/media/monitor/organizer.py @@ -7,6 +7,7 @@ from media.monitor.exceptions import BadSongFile from media.monitor.events import OrganizeFile from pydispatch import dispatcher from os.path import dirname +from media.saas.thread import getsig import os.path class Organizer(ReportHandler,Loggable): @@ -36,7 +37,7 @@ class Organizer(ReportHandler,Loggable): self.channel = channel self.target_path = target_path self.recorded_path = recorded_path - super(Organizer, self).__init__(signal=self.channel, weak=False) + super(Organizer, self).__init__(signal=getsig(self.channel), weak=False) def handle(self, sender, event): """ Intercept events where a new file has been added to the @@ -63,7 +64,7 @@ class Organizer(ReportHandler,Loggable): def new_dir_watch(d): # TODO : rewrite as return lambda : dispatcher.send(... def cb(): - dispatcher.send(signal="add_subwatch", sender=self, + dispatcher.send(signal=getsig("add_subwatch"), sender=self, directory=d) return cb diff --git a/python_apps/media-monitor2/media/monitor/watchersyncer.py b/python_apps/media-monitor2/media/monitor/watchersyncer.py index a913bb506..558759c89 100644 --- a/python_apps/media-monitor2/media/monitor/watchersyncer.py +++ b/python_apps/media-monitor2/media/monitor/watchersyncer.py @@ -8,7 +8,7 @@ from media.monitor.exceptions import BadSongFile from media.monitor.eventcontractor import EventContractor from media.monitor.events import EventProxy from media.monitor.request import ThreadedRequestSync, RequestSync -from media.saas.thread import InstanceInheritingThread +from media.saas.thread import InstanceInheritingThread, getsig class TimeoutWatcher(InstanceInheritingThread,Loggable): """ @@ -52,7 +52,7 @@ class WatchSyncer(ReportHandler,Loggable): tc = TimeoutWatcher(self, self.timeout) tc.daemon = True tc.start() - super(WatchSyncer, self).__init__(signal=signal) + super(WatchSyncer, self).__init__(signal=getsig(signal)) def handle(self, sender, event): """ diff --git a/python_apps/media-monitor2/media/saas/launcher.py b/python_apps/media-monitor2/media/saas/launcher.py index 5092895ae..e80f46034 100644 --- a/python_apps/media-monitor2/media/saas/launcher.py +++ b/python_apps/media-monitor2/media/saas/launcher.py @@ -7,7 +7,7 @@ import media.monitor.pure as mmp from media.monitor.exceptions import FailedToObtainLocale, FailedToSetLocale from media.monitor.log import get_logger, setup_logging from std_err_override import LogWriter -from media.saas.thread import InstanceThread, user, apc +from media.saas.thread import InstanceThread, user, apc, getsig from media.monitor.log import Loggable from media.monitor.exceptions import CouldNotCreateIndexFile from media.monitor.toucher import ToucherThread @@ -44,7 +44,7 @@ class MM2(InstanceThread, Loggable): manager = Manager() apiclient = apc() config = user().mm_config - watch_syncer = WatchSyncer(signal='watch', + watch_syncer = WatchSyncer(signal=getsig('watch'), chunking_number=config['chunking_number'], timeout=config['request_max_wait']) airtime_receiver = AirtimeMessageReceiver(config,manager)