From 95d33b9248670f5c9a117645d7115be7c301a297 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 14 Aug 2012 15:21:46 -0400 Subject: [PATCH] cc-4105: removed code duplication --- .../media-monitor2/media/monitor/airtime.py | 24 +++++++------------ .../media-monitor2/media/monitor/bootstrap.py | 4 ++-- .../media/monitor/exceptions.py | 3 ++- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/airtime.py b/python_apps/media-monitor2/media/monitor/airtime.py index 540388793..768a8384e 100644 --- a/python_apps/media-monitor2/media/monitor/airtime.py +++ b/python_apps/media-monitor2/media/monitor/airtime.py @@ -102,25 +102,21 @@ class AirtimeMessageReceiver(Loggable): bs = Bootstrapper( sdb, self.manager.watch_signal() ) bs.flush_watch( directory=directory, last_ran=self.cfg.last_ran() ) except Exception as e: - self.logger.info( "Exception bootstrapping: (dir,id)=(%s,%s)" % - (directory, directory_id) ) - self.logger.info( str(e) ) - self.logger.error( traceback.format_exc() ) - raise DirectoryIsNotListed(directory) + self.fatal_exception("Exception bootstrapping: (dir,id)=(%s,%s)" % + (directory, directory_id), e) + raise DirectoryIsNotListed(directory, cause=e) def md_update(self, msg): self.logger.info("Updating metadata for: '%s'" % msg['MDATA_KEY_FILEPATH']) md_path = msg['MDATA_KEY_FILEPATH'] - try: - Metadata.write_unsafe(path=md_path, md=msg) + try: Metadata.write_unsafe(path=md_path, md=msg) except BadSongFile as e: self.logger.info("Cannot find metadata file: '%s'" % e.path) except Exception as e: # TODO : add md_path to problem path or something? - self.logger.info("Unknown error when writing metadata to: '%s'" % - md_path) - self.logger.info( traceback.format_exc() ) + self.fatal_exception("Unknown error when writing metadata to: '%s'" + % md_path, e) def new_watch(self, msg): self.logger.info("Creating watch for directory: '%s'" % @@ -128,14 +124,10 @@ class AirtimeMessageReceiver(Loggable): if not os.path.exists(msg['directory']): try: os.makedirs(msg['directory']) except Exception as e: - self.logger.info("Failed to create watched dir '%s'" % - msg['directory']) - self.logger.info(str(e)) + self.fatal_exception("Failed to create watched dir '%s'" % + msg['directory'],e) else: self.new_watch(msg) else: - # TODO : Refactor this; breaks encapsulation. - # TODO : Should not flush events, should be smarted and bootstrap - #self.manager.watch_listener.flush_events(msg['directory']) self.__request_now_bootstrap( directory=msg['directory'] ) self.manager.add_watch_directory(msg['directory']) diff --git a/python_apps/media-monitor2/media/monitor/bootstrap.py b/python_apps/media-monitor2/media/monitor/bootstrap.py index fd368020d..1e5bd1a22 100644 --- a/python_apps/media-monitor2/media/monitor/bootstrap.py +++ b/python_apps/media-monitor2/media/monitor/bootstrap.py @@ -50,11 +50,11 @@ class Bootstrapper(Loggable): # system. These are the files marked for deletions for to_delete in db_songs.difference(songs): dispatcher.send(signal=self.watch_signal, sender=self, - event=DeleteFile(to_delete)) + event=DeleteFile(to_delete)) deleted += 1 for to_add in songs.difference(db_songs): dispatcher.send(signal=self.watch_signal, sender=self, - event=NewFile(to_add)) + event=NewFile(to_add)) added += 1 self.logger.info( "Flushed watch directory (%s). \ (added, modified, deleted) = (%d, %d, %d)" diff --git a/python_apps/media-monitor2/media/monitor/exceptions.py b/python_apps/media-monitor2/media/monitor/exceptions.py index bcfcea6a7..c6b3ec445 100644 --- a/python_apps/media-monitor2/media/monitor/exceptions.py +++ b/python_apps/media-monitor2/media/monitor/exceptions.py @@ -29,8 +29,9 @@ class CouldNotCreateIndexFile(Exception): def __str__(self): return "Failed to create touch file '%s'" % self.path class DirectoryIsNotListed(Exception): - def __init__(self,dir_id): + def __init__(self,dir_id,cause=None): self.dir_id = dir_id + self.cause = cause def __str__(self): return "%d was not listed as a directory in the database" % self.dir_id