cc-4105: minor code formatting. refactored replaygain a little bit

This commit is contained in:
Rudi Grinberg 2012-08-08 17:56:46 -04:00
parent e98eda144a
commit ce89d53ea0
4 changed files with 34 additions and 35 deletions

View File

@ -637,8 +637,9 @@ class AirtimeApiClient():
def get_files_without_replay_gain_value(self, dir_id): 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 Download a list of files that need to have their ReplayGain value
of files is downloaded into a file and the path to this file is the return 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 #http://localhost/api/get-files-without-replay-gain/dir_id/1

View File

@ -101,7 +101,9 @@ class WatchSyncer(ReportHandler,Loggable):
def target_path(self): return self.path def target_path(self): return self.path
def handle(self, sender, event): 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 # TODO : more types of events need to be handled here
if hasattr(event, 'pack'): if hasattr(event, 'pack'):
# We push this event into queue # We push this event into queue

View File

@ -2,14 +2,13 @@ from threading import Thread
import traceback import traceback
import os import os
import logging
import json
from api_clients import api_client from api_clients import api_client
from media.update import replaygain 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 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 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. automatically have its ReplayGain value calculated.
""" """
def __init__(self, logger): def __init__(self):
Thread.__init__(self) Thread.__init__(self)
self.logger = logger
self.api_client = api_client.AirtimeApiClient() self.api_client = api_client.AirtimeApiClient()
def main(self): def main(self):
@ -34,24 +32,20 @@ class ReplayGainUpdater(Thread):
for dir_id, dir_path in directories.iteritems(): for dir_id, dir_path in directories.iteritems():
try: try:
processed_data = [] # keep getting few rows at a time for current music_dir (stor
# or watched folder).
#keep getting few rows at a time for current music_dir (stor or while True:
#watched folder). #When we get a response with 0 rows, then we # return a list of pairs where the first value is the
#will set 'finished' to True. # file's database row id and the second value is the
finished = False # filepath
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
files = self.api_client.get_files_without_replay_gain_value(dir_id) files = self.api_client.get_files_without_replay_gain_value(dir_id)
processed_data = []
for f in files: for f in files:
full_path = os.path.join(dir_path, f['fp']) full_path = os.path.join(dir_path, f['fp'])
processed_data.append((f['id'], replaygain.calculate_replay_gain(full_path))) processed_data.append((f['id'], replaygain.calculate_replay_gain(full_path)))
self.api_client.update_replay_gain_values(processed_data) self.api_client.update_replay_gain_values(processed_data)
finished = (len(files) == 0) if len(files) == 0: break
except Exception, e: except Exception, e:
self.logger.error(e) self.logger.error(e)
@ -63,9 +57,5 @@ class ReplayGainUpdater(Thread):
self.logger.error(e) self.logger.error(e)
if __name__ == "__main__": if __name__ == "__main__":
try: rgu = ReplayGainUpdater()
rgu = ReplayGainUpdater(logging) rgu.main()
rgu.main()
except Exception, e:
print e
print traceback.format_exc()

View File

@ -8,7 +8,8 @@ from media.monitor.log import get_logger, setup_logging
from media.monitor.config import MMConfig from media.monitor.config import MMConfig
from media.monitor.toucher import ToucherThread from media.monitor.toucher import ToucherThread
from media.monitor.syncdb import AirtimeDB 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.airtime import AirtimeNotifier, AirtimeMessageReceiver
from media.monitor.watchersyncer import WatchSyncer from media.monitor.watchersyncer import WatchSyncer
from media.monitor.eventdrainer import EventDrainer from media.monitor.eventdrainer import EventDrainer
@ -16,8 +17,9 @@ import media.monitor.pure as mmp
from api_clients import api_client as apc from api_clients import api_client as apc
global_config = u'/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg' base_path = u'/home/rudi/Airtime/python_apps/media-monitor2/tests'
api_client_config = u'/home/rudi/Airtime/python_apps/media-monitor2/tests/live_client.cfg' 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 # MMConfig is a proxy around ConfigObj instances. it does not allow itself
# users of MMConfig instances to modify any config options directly through the # 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...") log.info("Failed to set the locale...")
sys.exit(1) sys.exit(1)
except FailedToObtainLocale as e: 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) sys.exit(1)
except Exception as e: except Exception as e:
log.info("Failed to set the locale for unknown reason. Logging exception.") 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 # Create the watch_directory here
try: os.makedirs(watch_dir) try: os.makedirs(watch_dir)
except Exception as e: 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): if os.path.exists(watch_dir):
airtime_receiver.new_watch({ 'directory':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() ) #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 # Launch the toucher that updates the last time when the script was ran every n
# n seconds. # seconds.
tt = ToucherThread(path=config['index_path'], interval=int(config['touch_interval'])) tt = ToucherThread(path=config['index_path'],
interval=int(config['touch_interval']))
pyi = manager.pyinotify() pyi = manager.pyinotify()
pyi.loop() pyi.loop()