int casting is now more tolerant (allowing "0 BPM" in "It's Blitz")

--HG--
extra : convert_revision : svn%3A41726ec3-264d-0410-9c23-a9f1637257cc/trunk%40225
This commit is contained in:
adrian.sampson 2009-05-12 19:56:49 +00:00
parent 1c41f8446b
commit 94b071493b
3 changed files with 18 additions and 3 deletions

View file

@ -284,11 +284,19 @@ class MediaField(object):
if self.out_type == int: if self.out_type == int:
if out is None: if out is None:
return 0 return 0
elif isinstance(out, int) or isinstance(out, float):
# Just a number.
return int(out)
else: else:
try: # Process any other type as a string.
if not isinstance(out, basestring):
out = unicode(out)
# Get a number from the front of the string.
out = re.match('[0-9]*', out.strip()).group(0)
if not out:
return 0
else:
return int(out) return int(out)
except: # in case out is not convertible directly to an int
return int(unicode(out))
elif self.out_type == bool: elif self.out_type == bool:
if out is None: if out is None:
return False return False

BIN
test/rsrc/bpm.mp3 Executable file

Binary file not shown.

View file

@ -250,6 +250,12 @@ class EdgeTest(unittest.TestCase):
self.assertEqual(t_time.month, 3) self.assertEqual(t_time.month, 3)
self.assertEqual(t_time.day, 31) self.assertEqual(t_time.day, 31)
def test_tempo_with_bpm(self):
# Some files have a string like "128 BPM" in the tempo field
# rather than just a number.
f = beets.mediafile.MediaFile(os.path.join('rsrc', 'bpm.mp3'))
self.assertEqual(f.bpm, 128)
def suite(): def suite():
s = unittest.TestSuite() s = unittest.TestSuite()
@ -280,6 +286,7 @@ def suite():
s.addTest(EdgeTest('test_emptylist')) s.addTest(EdgeTest('test_emptylist'))
s.addTest(EdgeTest('test_release_time_with_t')) s.addTest(EdgeTest('test_release_time_with_t'))
s.addTest(EdgeTest('test_release_time_with_space')) s.addTest(EdgeTest('test_release_time_with_space'))
s.addTest(EdgeTest('test_tempo_with_bpm'))
return s return s