Skip to content

Commit

Permalink
Fix many tool-level bugs related to new MetaData interface.
Browse files Browse the repository at this point in the history
Update lots of unit tests for new interface also.
  • Loading branch information
tuffy committed Jul 10, 2012
1 parent 36a8bd4 commit db441b1
Show file tree
Hide file tree
Showing 8 changed files with 610 additions and 129 deletions.
35 changes: 24 additions & 11 deletions audiotools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3469,18 +3469,18 @@ def track_number(self):
"""returns this track's number as an integer
this first checks MetaData and then makes a guess from the filename
if neither yields a good result, returns 0"""
if neither yields a good result, returns None"""

metadata = self.get_metadata()
if ((metadata is not None) and (metadata.track_number > 0)):
if (metadata is not None):
return metadata.track_number
else:
try:
return int(re.findall(
r'\d{2,3}',
os.path.basename(self.filename))[0]) % 100
except IndexError:
return 0
return None

def album_number(self):
"""returns this track's album number as an integer
Expand All @@ -3498,7 +3498,7 @@ def album_number(self):
os.path.basename(self.filename))[0])
return long_track_number / 100
except IndexError:
return 0
return None

@classmethod
def track_name(cls, file_path, track_metadata=None, format=None,
Expand All @@ -3521,10 +3521,18 @@ def track_name(cls, file_path, track_metadata=None, format=None,
try:
#prefer track_number and album_number from MetaData, if available
if (track_metadata is not None):
track_number = track_metadata.track_number
album_number = track_metadata.album_number
track_total = track_metadata.track_total
album_total = track_metadata.album_total
track_number = (track_metadata.track_number
if track_metadata.track_number is not None
else 0)
album_number = (track_metadata.album_number
if track_metadata.album_number is not None
else 0)
track_total = (track_metadata.track_total
if track_metadata.track_total is not None
else 0)
album_total = (track_metadata.album_total
if track_metadata.album_total is not None
else 0)
else:
try:
track_number = int(re.findall(
Expand Down Expand Up @@ -3563,9 +3571,14 @@ def track_name(cls, file_path, track_metadata=None, format=None,
for field in track_metadata.FIELDS:
if ((field != "suffix") and
(field not in MetaData.INTEGER_FIELDS)):
format_dict[field.decode('ascii')] = getattr(
track_metadata,
field).replace(u'/', u'-').replace(unichr(0), u' ')
if (getattr(track_metadata, field) is not None):
format_dict[field.decode('ascii')] = getattr(
track_metadata,
field).replace(u'/',
u'-').replace(unichr(0),
u' ')
else:
format_dict[field.decode('ascii')] = u""
else:
for field in MetaData.FIELDS:
if (field not in MetaData.INTEGER_FIELDS):
Expand Down
6 changes: 4 additions & 2 deletions audiotools/ape.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ def __delattr__(self, attr):
else:
del(self['Track'])
elif (attr == 'track_total'):
if (self.track_number is not None):
if ((self.track_number is not None) and
(self.track_number != 0)):
self['Track'] = self.ITEM.string(
'Track', __number_pair__(self.track_number, None))
else:
Expand All @@ -417,7 +418,8 @@ def __delattr__(self, attr):
else:
del(self['Media'])
elif (attr == 'album_total'):
if (self.album_number is not None):
if ((self.album_number is not None) and
(self.album_number != 0)):
self['Media'] = self.ITEM.string(
'Media', __number_pair__(self.album_number, None))
else:
Expand Down
25 changes: 12 additions & 13 deletions audiotools/id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,23 @@ def __attrib_equals__(attributes, o1, o2):
def __number_pair__(current, total):
from . import config

def empty(i):
return i is None

if (config.getboolean_default("ID3", "pad", False)):
unslashed_format = u"%2.2d"
slashed_format = u"%2.2d/%2.2d"
else:
unslashed_format = u"%d"
slashed_format = u"%d/%d"

if (empty(current) and empty(total)):
return unslashed_format % (0,)
elif ((not empty(current)) and empty(total)):
return unslashed_format % (current,)
elif (empty(current) and (not empty(total))):
return slashed_format % (0, total)
else:
#neither current or total are empty
return slashed_format % (current, total)
if (current is None):
if (total is None):
return unslashed_format % (0,)
else:
return slashed_format % (0, total)
else: #current is not None
if (total is None):
return unslashed_format % (current,)
else:
return slashed_format % (current, total)


def read_id3v2_comment(filename):
Expand Down Expand Up @@ -1228,7 +1226,8 @@ def __delattr__(self, attr):
continue
elif ((attr == 'track_total') or (attr == 'album_total')):
#handle the *_total numerical fields
if (frame.number() is not None):
if ((frame.number() is not None) and
(frame.number() != 0)):
#if *_total is deleted, but *_number is present
#build a new frame with only the *_number field
updated_frames.append(
Expand Down
99 changes: 65 additions & 34 deletions test/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def test_metadata(self):
nonblank_metadata = audiotools.MetaData(
track_name=u"Track Name",
track_number=1,
track_total=2,
album_name=u"Album Name")
track.set_metadata(nonblank_metadata)
self.assertEqual(track.get_metadata(), nonblank_metadata)
Expand All @@ -248,9 +249,7 @@ def test_metadata(self):
if (metadata is not None):
self.assertEqual(
metadata,
audiotools.MetaData(track_name=u"",
track_number=0,
album_name=u""))
audiotools.MetaData())

track.set_metadata(nonblank_metadata)
self.assertEqual(track.get_metadata(), nonblank_metadata)
Expand Down Expand Up @@ -312,7 +311,14 @@ def test_convert_progress(self):
try:
track = self.audio_class.from_pcm(temp.name,
BLANK_PCM_Reader(10))
if (track.lossless()):
self.assert_(audiotools.pcm_frame_cmp(
track.to_pcm(),
BLANK_PCM_Reader(10)) is None)
for audio_class in audiotools.AVAILABLE_TYPES:
print "converting %s to %s" % (self.audio_class.NAME,
audio_class.NAME)

outfile = tempfile.NamedTemporaryFile(
suffix="." + audio_class.SUFFIX)
log = Log()
Expand All @@ -330,7 +336,10 @@ def test_convert_progress(self):

if (track.lossless() and track2.lossless()):
self.assert_(audiotools.pcm_frame_cmp(
track.to_pcm(), track2.to_pcm()) is None)
track.to_pcm(), track2.to_pcm()) is None,
"PCM mismatch converting %s to %s" % (
self.audio_class.NAME,
audio_class.NAME))
finally:
outfile.close()
finally:
Expand All @@ -346,22 +355,33 @@ def test_track_number(self):
track = self.audio_class.from_pcm(
os.path.join(temp_dir, "abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 0)
if (track.get_metadata() is None):
self.assertEqual(track.track_number(), None)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 1)
track = self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 1)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 2)
track = self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 2)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 3)
track = self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.track_number(), 3)
else:
self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))

track.set_metadata(audiotools.MetaData(track_number=2))
metadata = track.get_metadata()
Expand Down Expand Up @@ -397,24 +417,35 @@ def test_album_number(self):
track = self.audio_class.from_pcm(
os.path.join(temp_dir, "abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.album_number(), 0)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.album_number(), 0)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
if (track.get_metadata() is None):
self.assertEqual(track.album_number(), 2)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
if (track.get_metadata() is None):
self.assertEqual(track.album_number(), 3)
self.assertEqual(track.album_number(), None)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.assertEqual(track.album_number(), None)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
if (track.get_metadata() is None):
self.assertEqual(track.album_number(), 2)

track = self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
if (track.get_metadata() is None):
self.assertEqual(track.album_number(), 3)
else:
self.audio_class.from_pcm(
os.path.join(temp_dir, "01 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.audio_class.from_pcm(
os.path.join(temp_dir, "202 - abcde" + self.suffix),
BLANK_PCM_Reader(1))
self.audio_class.from_pcm(
os.path.join(temp_dir, "303 45 - abcde" + self.suffix),
BLANK_PCM_Reader(1))

track.set_metadata(audiotools.MetaData(album_number=2))
metadata = track.get_metadata()
Expand Down
4 changes: 2 additions & 2 deletions test/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ def test_field_mapping(self):
#>>> id3.track_number = 2
#>>> id3['TRK'][0] == u"2"
id3.track_number = 3
id3.track_total = 0
id3.track_total = None
self.assertEqual(
unicode(id3[id3_class.TEXT_FRAME.NUMERICAL_IDS[0]][0]),
u"3")
Expand All @@ -1230,7 +1230,7 @@ def test_field_mapping(self):
u"3/8")

id3.album_number = 2
id3.album_total = 0
id3.album_total = None
self.assertEqual(
unicode(id3[id3_class.TEXT_FRAME.NUMERICAL_IDS[1]][0]),
u"2")
Expand Down
Loading

0 comments on commit db441b1

Please sign in to comment.