- # FIXME: query_info contains code that we could use instead of TRY
- # EXCEPT
- #See CDDB documentation for more information.
+ #See CDDB documentation for more information
#http://cddb-py.sourceforge.net/CDDB/README
-
- if query_info is not None:
- # query_info variable's type depends on how many matches we get.
- # If we get just one match then it's a map and if we get more
- # than one it's a list.
- try:
- title = query_info['title']
- self.artist = title[:title.index(' / ')]
- self.album = title[title.index(' / ') + 3:]
-
- # Get track titles
- (read_status, read_info) = CDDB.read(query_info['category'],
- query_info['disc_id'])
- cumulative_length = 0
- for i in range(disc_id[1]):
- if i + 4 == len(disc_id):
- # We must calculate last track length different way
- length = disc_id[len(disc_id) - 1] - cumulative_length
- else:
- # Calculate track length in seconds
- length = (disc_id[i+3] - disc_id[i+2]) / 75
- cumulative_length = cumulative_length + length
-
- track_title = read_info['TTITLE' + str(i)]
- self.tracks.append(CompactDiscTrack(i + 1, track_title,
- length))
- except:
- query = query_info[0]
- self.artist = query['title'][:query['title'].index(' / ')]
- self.album = query['title'][query['title'].index(' / ') + 3:]
-
- # Get track titles
- (read_status, read_info) = CDDB.read(query_info[0]['category'],
- query_info[0]['disc_id'])
- cumulative_length = 0
- for i in range(disc_id[1]):
- if i + 4 == len(disc_id):
- # We must calculate last track length different way
- length = disc_id[len(disc_id) - 1] - cumulative_length
- else:
- # Calculate track length in seconds
- length = (disc_id[i+3] - disc_id[i+2]) / 75
- cumulative_length = cumulative_length + length
-
- track_title = read_info['TTITLE' + str(i)]
- self.tracks.append(CompactDiscTrack(i + 1, track_title,
- length))
+ # query_info variable's type depends on query_status
+
+ if query_status == 200:
+ # On 200, a dictionary containing three items is returned
+ self._get_informations_from_result_element(query_info, disc_id)
+ elif query_status == 210 or query_status == 211:
+ # On 211 or 210, a list will be returned as the second item.
+ # Each element of the list will be a dictionary containing
+ # three items, exactly the same as a single 200 success return.
+ self._get_informations_from_result_element(query_info[0], disc_id)
else:
# No metadata found for this disc self.album = _("Unknown title") self.artist = _("Unknown artist")
for i in range(disc_id[1]): self.tracks.append(CompactDiscTrack(i + 1,
- _("Unknown track %(num)s.") % \
- {'num': str(i+1)}))
+ _("Unknown track %(num)s.") % {'num': str(i+1)}))
+
+ def _get_informations_from_result_element(self, result, disc_id):
+ """Catch any information from a CDDB result dictionnary"""
+ title = unicode(result['title'], "iso-8859-1")
+ category = unicode(result['category'], "iso-8859-1")
+ disc = unicode(result['disc_id'], "iso-8859-1")
+
+ self.artist = title[:title.index(' / ')]
+ self.album = title[title.index(' / ') + 3:]
+
+ # Get track titles
+ (status, info) = CDDB.read(category, disc)
+ cumulative_length = 0
+ for i in range(disc_id[1]):
+ if i + 4 == len(disc_id):
+ # We must calculate last track length different way
+ length = disc_id[len(disc_id) - 1] - cumulative_length
+ else:
+ # Calculate track length in seconds
+ length = (disc_id[i + 3] - disc_id[ i + 2]) / 75
+ cumulative_length = cumulative_length + length
+
+ track_title = unicode(info['TTITLE' + str(i)], "iso-8859-1")
+ self.tracks.append(CompactDiscTrack(i + 1, track_title, length))
+ def testAlbumOfCompactDisc(self):
+ """CompactDisc - Ensures we catch cd's Album name from CDDB"""
+ self.assertEqual(self.cd.album, 'The Joshua Tree')
+
+ def testArtistOfCompactDisc(self):
+ """CompactDisc - Ensures we catch cd's Artist name from CDDB"""
+ self.assertEqual(self.cd.artist, 'U2')
+
+ def testTracksOfCompactDisc(self):
+ """CompactDisc - Ensures we catch cd's Track names from CDDB"""
+ tracks = [track.get_title() for track in self.cd.tracks]
+
+ self.assertEqual(tracks, ['Where the Streets Have No Name',
+ "I Still haven't Found What I'm Looking For", 'With or Without You',
+ 'Bullet the Blue Sky', 'Running To Stand Still',
+ 'Red Hill Mining Town', "In God's Country",
+ 'Trip Through Your Wires', 'One Tree Hill', 'Exit',
+ 'Mothers of the Disappeared'])
+
Devs,
So I've tested that part of code without internet connection and there's no freeze.
It seems to have a timeout in CDDB queries which create an exception so the result is we have after no more than 1 second a "no cd" message.
I've added 3 tests to check that CDDB queries return the results we expect.
Album name, Artist name and Tracks names are checked for the best Album ever "The Joshua Tree" ;)
New diff bellow :
Samuel.
=== modified file 'entertainerlib /frontend/ medialibrary/ music.py' frontend/ medialibrary/ music.py 2009-01-11 02:51:18 +0000 frontend/ medialibrary/ music.py 2009-01-31 20:31:33 +0000
--- entertainerlib/
+++ entertainerlib/
@@ -601,65 +601,49 @@
- # FIXME: query_info contains code that we could use instead of TRY cddb-py. sourceforge. net/CDDB/ README title.index( ' / ')] query_info[ 'category' ], 'disc_id' ]) len(disc_ id) - 1] - cumulative_length append( CompactDiscTrac k(i + 1, track_title, title'] [:query[ 'title' ].index( ' / ')] title'] [query[ 'title' ].index( ' / ') + 3:] query_info[ 0]['category' ], 0]['disc_ id']) len(disc_ id) - 1] - cumulative_length append( CompactDiscTrac k(i + 1, track_title, informations_ from_result_ element( query_info, disc_id) informations_ from_result_ element( query_info[ 0], disc_id)
self. album = _("Unknown title")
self. artist = _("Unknown artist")
self. tracks. append( CompactDiscTrac k(i + 1, ns_from_ result_ element( self, result, disc_id): result[ 'title' ], "iso-8859-1") result[ 'category' ], "iso-8859-1") result[ 'disc_id' ], "iso-8859-1") title.index( ' / ')] len(disc_ id) - 1] - cumulative_length info['TTITLE' + str(i)], "iso-8859-1") append( CompactDiscTrac k(i + 1, track_title, length))
- # EXCEPT
- #See CDDB documentation for more information.
+ #See CDDB documentation for more information
#http://
-
- if query_info is not None:
- # query_info variable's type depends on how many matches we get.
- # If we get just one match then it's a map and if we get more
- # than one it's a list.
- try:
- title = query_info['title']
- self.artist = title[:
- self.album = title[title.index(' / ') + 3:]
-
- # Get track titles
- (read_status, read_info) = CDDB.read(
- query_info[
- cumulative_length = 0
- for i in range(disc_id[1]):
- if i + 4 == len(disc_id):
- # We must calculate last track length different way
- length = disc_id[
- else:
- # Calculate track length in seconds
- length = (disc_id[i+3] - disc_id[i+2]) / 75
- cumulative_length = cumulative_length + length
-
- track_title = read_info['TTITLE' + str(i)]
- self.tracks.
- length))
- except:
- query = query_info[0]
- self.artist = query['
- self.album = query['
-
- # Get track titles
- (read_status, read_info) = CDDB.read(
- query_info[
- cumulative_length = 0
- for i in range(disc_id[1]):
- if i + 4 == len(disc_id):
- # We must calculate last track length different way
- length = disc_id[
- else:
- # Calculate track length in seconds
- length = (disc_id[i+3] - disc_id[i+2]) / 75
- cumulative_length = cumulative_length + length
-
- track_title = read_info['TTITLE' + str(i)]
- self.tracks.
- length))
+ # query_info variable's type depends on query_status
+
+ if query_status == 200:
+ # On 200, a dictionary containing three items is returned
+ self._get_
+ elif query_status == 210 or query_status == 211:
+ # On 211 or 210, a list will be returned as the second item.
+ # Each element of the list will be a dictionary containing
+ # three items, exactly the same as a single 200 success return.
+ self._get_
else:
# No metadata found for this disc
for i in range(disc_id[1]):
- _("Unknown track %(num)s.") % \
- {'num': str(i+1)}))
+ _("Unknown track %(num)s.") % {'num': str(i+1)}))
+
+ def _get_informatio
+ """Catch any information from a CDDB result dictionnary"""
+ title = unicode(
+ category = unicode(
+ disc = unicode(
+
+ self.artist = title[:
+ self.album = title[title.index(' / ') + 3:]
+
+ # Get track titles
+ (status, info) = CDDB.read(category, disc)
+ cumulative_length = 0
+ for i in range(disc_id[1]):
+ if i + 4 == len(disc_id):
+ # We must calculate last track length different way
+ length = disc_id[
+ else:
+ # Calculate track length in seconds
+ length = (disc_id[i + 3] - disc_id[ i + 2]) / 75
+ cumulative_length = cumulative_length + length
+
+ track_title = unicode(
+ self.tracks.
def get_artist(self):
"""
=== modified file 'entertainerlib /tests/ MusicLibrary_ test.py' tests/MusicLibr ary_test. py 2009-01-11 02:51:18 +0000 tests/MusicLibr ary_test. py 2009-01-31 20:36:56 +0000 connection failed"
self. musiclibrary = MusicLibrary( self.backend_ connection) [2299253003L, 11, 150, 25433, 46436, 68737, 89292,
--- entertainerlib/
+++ entertainerlib/
@@ -19,6 +19,8 @@
except:
print "\nbackend_
+ self.cd = CompactDisc(
+ 108536, 130667, 144099, 160174, 181568, 203695, 3027])
def tearDown(self):
self. backend_ connection. close_connectio n()
self. musiclibrary. save_lyrics( self.track, 'some lyrics here')
self. assertEqual( self.track. get_lyrics( ), 'some lyrics here')
@@ -130,3 +132,22 @@
+ def testAlbumOfComp actDisc( self): l(self. cd.album, 'The Joshua Tree') pactDisc( self): l(self. cd.artist, 'U2') pactDisc( self): l(tracks, ['Where the Streets Have No Name',
+ """CompactDisc - Ensures we catch cd's Album name from CDDB"""
+ self.assertEqua
+
+ def testArtistOfCom
+ """CompactDisc - Ensures we catch cd's Artist name from CDDB"""
+ self.assertEqua
+
+ def testTracksOfCom
+ """CompactDisc - Ensures we catch cd's Track names from CDDB"""
+ tracks = [track.get_title() for track in self.cd.tracks]
+
+ self.assertEqua
+ "I Still haven't Found What I'm Looking For", 'With or Without You',
+ 'Bullet the Blue Sky', 'Running To Stand Still',
+ 'Red Hill Mining Town', "In God's Country",
+ 'Trip Through Your Wires', 'One Tree Hill', 'Exit',
+ 'Mothers of the Disappeared'])
+