From 0c9d3c552bcf64df92b7370a0e121470f836b120 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Thu, 6 Dec 2012 16:44:54 +0000 Subject: [PATCH] * morituri/common/program.py: * morituri/rip/cd.py: * morituri/rip/image.py: Add stdout to program. Use it for getting musicbrainz info. --- ChangeLog | 7 +++++ morituri/common/program.py | 63 +++++++++++++++++++++++--------------- morituri/rip/cd.py | 5 +-- morituri/rip/image.py | 2 +- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index ae9eefc..67efb22 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2012-12-06 Thomas Vander Stichele + + * morituri/common/program.py: + * morituri/rip/cd.py: + * morituri/rip/image.py: + Add stdout to program. Use it for getting musicbrainz info. + 2012-12-04 Thomas Vander Stichele * morituri/program/cdparanoia.py: diff --git a/morituri/common/program.py b/morituri/common/program.py index 7652677..ddb6dc5 100644 --- a/morituri/common/program.py +++ b/morituri/common/program.py @@ -25,6 +25,7 @@ Common functionality and class for all programs using morituri. """ import os +import sys import time from morituri.common import common, log, musicbrainzngs, cache @@ -53,12 +54,15 @@ class Program(log.Loggable): outdir = None result = None - def __init__(self, record=False): + _stdout = None + + def __init__(self, record=False, stdout=sys.stdout): """ @param record: whether to record results of API calls for playback. """ self._record = record self._cache = cache.ResultCache() + self._stdout = stdout def _getTableCachePath(self): path = os.path.join(os.path.expanduser('~'), '.morituri', 'cache', @@ -211,8 +215,8 @@ class Program(log.Loggable): def getMusicBrainz(self, ittoc, mbdiscid, release=None): # look up disc on musicbrainz - print 'Disc duration: %s' % common.formatTime( - ittoc.duration() / 1000.0) + self._stdout.write('Disc duration: %s\n' % common.formatTime( + ittoc.duration() / 1000.0)) self.debug('MusicBrainz submit url: %r', ittoc.getMusicBrainzSubmitURL()) ret = None @@ -227,26 +231,27 @@ class Program(log.Loggable): except musicbrainzngs.NotFoundException, e: break except musicbrainzngs.MusicBrainzException, e: - print "Warning:", e + self._stdout.write("Warning: %r\n" % (e, )) time.sleep(5) continue if not metadatas: if e: - print "Error:", e - print 'Continuing without metadata' + self._stdout.write("Error: %r\n" % (e, )) + self._stdout.write('Continuing without metadata\n') if metadatas: - print - print 'Matching releases:' + self._stdout.write('\nMatching releases:\n') deltas = {} for metadata in metadatas: - print 'Artist : %s' % metadata.artist.encode('utf-8') - print 'Title : %s' % metadata.title.encode('utf-8') - print 'Duration: %s' % common.formatTime( - metadata.duration / 1000.0) - print 'URL : %s' % metadata.url + self._stdout.write('Artist : %s\n' % + metadata.artist.encode('utf-8')) + self._stdout.write('Title : %s\n' % + metadata.title.encode('utf-8')) + self._stdout.write('Duration: %s\n' % + common.formatTime(metadata.duration / 1000.0)) + self._stdout.write('URL : %s\n' % metadata.url) delta = abs(metadata.duration - ittoc.duration()) if not delta in deltas: @@ -258,12 +263,16 @@ class Program(log.Loggable): self.debug('Asked for release %r, only kept %r', release, metadatas) if len(metadatas) == 1: - print - print 'Picked requested release id %s' % release - print 'Artist : %s' % metadatas[0].artist.encode('utf-8') - print 'Title : %s' % metadatas[0].title.encode('utf-8') + self._stdout.write('\n') + self._stdout.write('Picked requested release id %s\n' % + release) + self._stdout.write('Artist : %s\n' % + metadatas[0].artist.encode('utf-8')) + self._stdout.write('Title : %s\n' % + metadatas[0].title.encode('utf-8')) elif not metadatas: - print 'Requested release id %s but none match' % release + self._stdout.write( + 'Requested release id %s but none match' % release) return else: # Select the release that most closely matches the duration. @@ -286,19 +295,23 @@ class Program(log.Loggable): releaseTitle, i, metadata.releaseTitle)) if (not release and len(deltas.keys()) > 1): - print - print 'Picked closest match in duration.' - print 'Others may be wrong in musicbrainz, please correct.' - print 'Artist : %s' % artist.encode('utf-8') - print 'Title : %s' % metadatas[0].title.encode('utf-8') + self._stdout.write('\n') + self._stdout.write('Picked closest match in duration.\n') + self._stdout.write('Others may be wrong in musicbrainz, ' + 'please correct.\n') + self._stdout.write('Artist : %s\n' % + artist.encode('utf-8')) + self._stdout.write('Title : %s\n' % + metadatas[0].title.encode('utf-8')) # Select one of the returned releases. We just pick the first one. ret = metadatas[0] else: - print 'Submit this disc to MusicBrainz at the above URL.' + self._stdout.write( + 'Submit this disc to MusicBrainz at the above URL.\n') ret = None - print + self._stdout.write('\n') return ret def getTagList(self, number): diff --git a/morituri/rip/cd.py b/morituri/rip/cd.py index ac52f18..f78fa44 100644 --- a/morituri/rip/cd.py +++ b/morituri/rip/cd.py @@ -136,7 +136,8 @@ Log files will log the path to tracks relative to this directory. options.offset) def do(self, args): - prog = program.Program(record=self.getRootCommand().record) + prog = program.Program(record=self.getRootCommand().record, + stdout=self.stdout) runner = task.SyncRunner() def function(r, t): @@ -178,7 +179,7 @@ Log files will log the path to tracks relative to this directory. ittoc.getMusicBrainzSubmitURL()) prog.metadata = prog.getMusicBrainz(ittoc, mbdiscid, - self.options.release_id) + release=self.options.release_id) if not prog.metadata: # fall back to FreeDB for lookup diff --git a/morituri/rip/image.py b/morituri/rip/image.py index 589fbfc..20d4507 100644 --- a/morituri/rip/image.py +++ b/morituri/rip/image.py @@ -104,7 +104,7 @@ class Retag(logcommand.LogCommand): summary = "retag image files" def do(self, args): - prog = program.Program() + prog = program.Program(stdout=self.stdout) runner = task.SyncRunner() for arg in args: