From ce89d53ea053ee14987862c0d994b31f2e2d5f69 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 8 Aug 2012 17:56:46 -0400 Subject: [PATCH] cc-4105: minor code formatting. refactored replaygain a little bit --- python_apps/api_clients/api_client.py | 5 +-- .../media/monitor/watchersyncer.py | 4 ++- .../media/update/replaygainupdater.py | 36 +++++++------------ python_apps/media-monitor2/mm2.py | 24 ++++++++----- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index e6d516060..5b8b93809 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -637,8 +637,9 @@ class AirtimeApiClient(): def get_files_without_replay_gain_value(self, dir_id): """ - Download a list of files that need to have their ReplayGain value calculated. This list - of files is downloaded into a file and the path to this file is the return value. + Download a list of files that need to have their ReplayGain value + calculated. This list of files is downloaded into a file and the path + to this file is the return value. """ #http://localhost/api/get-files-without-replay-gain/dir_id/1 diff --git a/python_apps/media-monitor2/media/monitor/watchersyncer.py b/python_apps/media-monitor2/media/monitor/watchersyncer.py index 5178a3ca2..ee8223c17 100644 --- a/python_apps/media-monitor2/media/monitor/watchersyncer.py +++ b/python_apps/media-monitor2/media/monitor/watchersyncer.py @@ -101,7 +101,9 @@ class WatchSyncer(ReportHandler,Loggable): def target_path(self): return self.path def handle(self, sender, event): - """We implement this abstract method from ReportHandler""" + """ + We implement this abstract method from ReportHandler + """ # TODO : more types of events need to be handled here if hasattr(event, 'pack'): # We push this event into queue diff --git a/python_apps/media-monitor2/media/update/replaygainupdater.py b/python_apps/media-monitor2/media/update/replaygainupdater.py index 05ff3d7bd..e89883e68 100644 --- a/python_apps/media-monitor2/media/update/replaygainupdater.py +++ b/python_apps/media-monitor2/media/update/replaygainupdater.py @@ -2,14 +2,13 @@ from threading import Thread import traceback import os -import logging -import json from api_clients import api_client from media.update import replaygain +from media.monitor.log import Loggable -class ReplayGainUpdater(Thread): +class ReplayGainUpdater(Thread, Loggable): """ The purpose of the class is to query the server for a list of files which do not have a ReplayGain value calculated. This class will iterate over the @@ -22,9 +21,8 @@ class ReplayGainUpdater(Thread): automatically have its ReplayGain value calculated. """ - def __init__(self, logger): + def __init__(self): Thread.__init__(self) - self.logger = logger self.api_client = api_client.AirtimeApiClient() def main(self): @@ -34,24 +32,20 @@ class ReplayGainUpdater(Thread): for dir_id, dir_path in directories.iteritems(): try: - processed_data = [] - - #keep getting few rows at a time for current music_dir (stor or - #watched folder). #When we get a response with 0 rows, then we - #will set 'finished' to True. - finished = False - - while not finished: - # return a list of pairs where the first value is the file's database row id - # and the second value is the filepath + # keep getting few rows at a time for current music_dir (stor + # or watched folder). + while True: + # return a list of pairs where the first value is the + # file's database row id and the second value is the + # filepath files = self.api_client.get_files_without_replay_gain_value(dir_id) - + processed_data = [] for f in files: full_path = os.path.join(dir_path, f['fp']) processed_data.append((f['id'], replaygain.calculate_replay_gain(full_path))) self.api_client.update_replay_gain_values(processed_data) - finished = (len(files) == 0) + if len(files) == 0: break except Exception, e: self.logger.error(e) @@ -63,9 +57,5 @@ class ReplayGainUpdater(Thread): self.logger.error(e) if __name__ == "__main__": - try: - rgu = ReplayGainUpdater(logging) - rgu.main() - except Exception, e: - print e - print traceback.format_exc() + rgu = ReplayGainUpdater() + rgu.main() diff --git a/python_apps/media-monitor2/mm2.py b/python_apps/media-monitor2/mm2.py index 094ac78a1..905c4eab3 100644 --- a/python_apps/media-monitor2/mm2.py +++ b/python_apps/media-monitor2/mm2.py @@ -8,7 +8,8 @@ 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.exceptions import FailedToObtainLocale, FailedToSetLocale, \ + NoConfigFile from media.monitor.airtime import AirtimeNotifier, AirtimeMessageReceiver from media.monitor.watchersyncer import WatchSyncer from media.monitor.eventdrainer import EventDrainer @@ -16,8 +17,9 @@ import media.monitor.pure as mmp from api_clients import api_client as apc -global_config = u'/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg' -api_client_config = u'/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg' +base_path = u'/home/rudi/Airtime/python_apps/media-monitor2/tests' +global_config = os.path.join(base_path,u'live_client.cfg') +api_client_config = global_config # MMConfig is a proxy around ConfigObj instances. it does not allow itself # users of MMConfig instances to modify any config options directly through the @@ -43,7 +45,8 @@ except FailedToSetLocale as e: log.info("Failed to set the locale...") sys.exit(1) except FailedToObtainLocale as e: - log.info("Failed to obtain the locale form the default path: '/etc/default/locale'") + log.info("Failed to obtain the locale form the default path: \ + '/etc/default/locale'") sys.exit(1) except Exception as e: log.info("Failed to set the locale for unknown reason. Logging exception.") @@ -73,7 +76,8 @@ for watch_dir in store[u'watched_dirs']: # Create the watch_directory here try: os.makedirs(watch_dir) except Exception as e: - log.error("Could not create watch directory: '%s' (given from the database)." % watch_dir) + log.error("Could not create watch directory: '%s' \ + (given from the database)." % watch_dir) if os.path.exists(watch_dir): airtime_receiver.new_watch({ 'directory':watch_dir }) @@ -82,11 +86,13 @@ bs = Bootstrapper( db=sdb, watch_signal='watch' ) #bs.flush_all( config.last_ran() ) -ed = EventDrainer(airtime_notifier.connection,interval=float(config['rmq_event_wait'])) +ed = EventDrainer(airtime_notifier.connection, + interval=float(config['rmq_event_wait'])) -# Launch the toucher that updates the last time when the script was ran every -# n seconds. -tt = ToucherThread(path=config['index_path'], interval=int(config['touch_interval'])) +# Launch the toucher that updates the last time when the script was ran every n +# seconds. +tt = ToucherThread(path=config['index_path'], + interval=int(config['touch_interval'])) pyi = manager.pyinotify() pyi.loop()