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):
"""
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

View File

@ -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

View File

@ -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()

View File

@ -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()