def test_read_standardize_write_file(self): path = self.create_test_file(TEST_TAG_LIST) au_file = audio_file.scan(path) self.assertTrue(au_file is not None) au_file.volume = TEST_VOL au_file.import_timestamp = TEST_TS au_file.album_id = 77777 # Inject the necessary UFID tag. au_file.mutagen_id3[constants.UFID_OWNER_IDENTIFIER] = ufid.ufid_tag( TEST_VOL, TEST_TS, au_file.fingerprint) import_file.standardize_file(au_file) # Do some basic checks for tag in au_file.mutagen_id3.values(): self.assertTrue((tag.FrameID in constants.ID3_TAG_WHITELIST or tag.HashKey in constants.ID3_TAG_WHITELIST)) for frame_id in constants.ID3_TAG_REQUIRED: self.assertTrue(frame_id in au_file.mutagen_id3) # Write the standardized file out, then re-read it and make sure # that everything is as we expected. alt_prefix = os.path.join(self.prefix, "alt") new_path = import_file.write_file(au_file, alt_prefix) new_au_file = audio_file.scan(new_path) self.assertEqual(sorted(au_file.mutagen_id3.keys()), sorted(new_au_file.mutagen_id3.keys())) for key in au_file.mutagen_id3.keys(): self.assertEqual(repr(au_file.mutagen_id3[key]), repr(new_au_file.mutagen_id3[key])) self.assertEqual(au_file.fingerprint, new_au_file.fingerprint) self.assertEqual(au_file.payload, new_au_file.payload)
def close(self): if self._fh is None: return self._fh.close() self._fh = None # Now re-open the MP3 file with mutagen and fix up the tags. mp3 = mutagen.mp3.MP3(self.path) # Put in the correct length. mp3.tags.add( mutagen.id3.TLEN(text=u"%d" % self.duration_ms, encoding=constants.DEFAULT_ID3_TEXT_ENCODING)) # Store a version of the title w/ both start and end times. mp3["TIT1"].text = [self._get_title()] # Add the frame count header. frame_count_tag = mutagen.id3.TXXX( desc=constants.TXXX_FRAME_COUNT_DESCRIPTION, text=[unicode(self.frame_count)], encoding=constants.DEFAULT_ID3_TEXT_ENCODING) mp3.tags.add(frame_count_tag) # Add the frame size header. frame_size_tag = mutagen.id3.TXXX( desc=constants.TXXX_FRAME_SIZE_DESCRIPTION, text=[unicode(self.frame_size)], encoding=constants.DEFAULT_ID3_TEXT_ENCODING) mp3.tags.add(frame_size_tag) # Add a UFID tag. We use the start_ms timestamp as an import # time. mp3.tags.add( ufid.ufid_tag(_ARCHIVE_VOLUME, int(self._start_ms / 1000), self._sha1_calc.hexdigest())) mp3.save()
def ufid_tag(self): """Returns this file's mutagen.id3.UFID tag. This call will fail if has_ufid() returns False. """ return ufid.ufid_tag(self.volume, self.import_timestamp, self.fingerprint)
def close(self): if self._fh is None: return self._fh.close() self._fh = None # Now re-open the MP3 file with mutagen and fix up the tags. mp3 = mutagen.mp3.MP3(self.path) # Put in the correct length. mp3.tags.add( mutagen.id3.TLEN(text=u"%d" % self.duration_ms, encoding=constants.DEFAULT_ID3_TEXT_ENCODING)) # Store a version of the title w/ both start and end times. mp3["TIT1"].text = [self._get_title()] # Add the frame count header. frame_count_tag = mutagen.id3.TXXX( desc=constants.TXXX_FRAME_COUNT_DESCRIPTION, text=[unicode(self.frame_count)], encoding=constants.DEFAULT_ID3_TEXT_ENCODING) mp3.tags.add(frame_count_tag) # Add the frame size header. frame_size_tag = mutagen.id3.TXXX( desc=constants.TXXX_FRAME_SIZE_DESCRIPTION, text=[unicode(self.frame_size)], encoding=constants.DEFAULT_ID3_TEXT_ENCODING) mp3.tags.add(frame_size_tag) # Add a UFID tag. We use the start_ms timestamp as an import # time. mp3.tags.add(ufid.ufid_tag(_ARCHIVE_VOLUME, int(self._start_ms/1000), self._sha1_calc.hexdigest())) mp3.save()
def test_basic(self): test_vol = 11 test_ts_human = "20090102-030405" test_ts = timestamp.parse_human_readable(test_ts_human) test_fp = "1234" * 10 # The UFID prefix should contain the volume and timestamp info. self.assertEqual( "vol0b/%s/" % test_ts_human, # 0b = 11 ufid.ufid_prefix(test_vol, test_ts)) # The UFID should equal the UFID prefix + the fingerprint. test_ufid = ufid.ufid(test_vol, test_ts, test_fp) self.assertEqual( ufid.ufid_prefix(test_vol, test_ts) + test_fp, test_ufid) # We should be able to make a tag too. test_tag = ufid.ufid_tag(test_vol, test_ts, test_fp) self.assertEqual("UFID", test_tag.FrameID) self.assertEqual(constants.UFID_OWNER_IDENTIFIER, test_tag.owner) self.assertEqual(test_ufid, test_tag.data) # Make sure we can parse information back out of the test UFID. vol, ts, fp = ufid.parse(test_ufid) self.assertEqual(test_vol, vol) self.assertEqual(test_ts, ts) self.assertEqual(test_fp, fp) # Raise ValueError if we try to parse a bad UFID. self.assertRaises(ValueError, ufid.parse, "bad") self.assertRaises(ValueError, ufid.parse, "vol01/20091399-666666/" + "1" * 40) self.assertRaises(ValueError, ufid.parse, "vol01/20991001-123456" + "1" * 40)
def test_read_standardize_write_file(self): path = self.create_test_file(TEST_TAG_LIST) au_file = audio_file.scan(path) self.assertTrue(au_file is not None) au_file.volume = TEST_VOL au_file.import_timestamp = TEST_TS au_file.album_id = 77777 # Inject the necessary UFID tag. au_file.mutagen_id3[constants.UFID_OWNER_IDENTIFIER] = ufid.ufid_tag( TEST_VOL, TEST_TS, au_file.fingerprint) import_file.standardize_file(au_file) # Do some basic checks for tag in au_file.mutagen_id3.values(): self.assertTrue( (tag.FrameID in constants.ID3_TAG_WHITELIST or tag.HashKey in constants.ID3_TAG_WHITELIST)) for frame_id in constants.ID3_TAG_REQUIRED: self.assertTrue(frame_id in au_file.mutagen_id3) # Write the standardized file out, then re-read it and make sure # that everything is as we expected. alt_prefix = os.path.join(self.prefix, "alt") new_path = import_file.write_file(au_file, alt_prefix) new_au_file = audio_file.scan(new_path) self.assertEqual(sorted(au_file.mutagen_id3.keys()), sorted(new_au_file.mutagen_id3.keys())) for key in au_file.mutagen_id3.keys(): self.assertEqual(repr(au_file.mutagen_id3[key]), repr(new_au_file.mutagen_id3[key])) self.assertEqual(au_file.fingerprint, new_au_file.fingerprint) self.assertEqual(au_file.payload, new_au_file.payload)
def set_volume_and_import_timestamp(self, vol, imp_ts): for au in self.all_au_files: if au.fingerprint is None: raise AlbumError("Can't set vol/timestamp on fp-less file") au.volume = vol au.import_timestamp = imp_ts au.mutagen_id3[constants.UFID_OWNER_IDENTIFIER] = ufid.ufid_tag( vol, imp_ts, au.fingerprint)
def test_scan_fast_tag_handling(self): test_mp3 = mutagen.mp3.MP3() class MockInfo(object): pass test_mp3.info = MockInfo() test_mp3.add_tags() test_mp3.tags.add(ufid.ufid_tag(TEST_VOL, TEST_TS, TEST_FP)) test_mp3.tags.add(mutagen.id3.TLEN(text=u"11111")) test_mp3.tags.add( mutagen.id3.TXXX(desc=constants.TXXX_ALBUM_ID_DESCRIPTION, text=[u"222"])) test_mp3.tags.add( mutagen.id3.TXXX(desc=constants.TXXX_FRAME_COUNT_DESCRIPTION, text=[u"333"])) test_mp3.tags.add( mutagen.id3.TXXX(desc=constants.TXXX_FRAME_SIZE_DESCRIPTION, text=["444"])) for tag in test_mp3.tags.values(): id3_text.standardize(tag) test_mp3.info.sample_rate = 5555 test_mp3.info.bitrate = 6666 test_mp3.info.mode = 2 au_file = audio_file.scan_fast("/test/path", _read_id3_hook=lambda p: test_mp3) self.assertTrue(audio_file is not None) self.assertEqual("/test/path", au_file.path) self.assertEqual(TEST_VOL, au_file.volume) self.assertEqual(TEST_TS, au_file.import_timestamp) self.assertEqual(TEST_FP, au_file.fingerprint) self.assertEqual(11111, au_file.duration_ms) self.assertEqual(222, au_file.album_id) self.assertEqual(333, au_file.frame_count) self.assertEqual(444, au_file.frame_size) self.assertEqual(5555, au_file.mp3_header.sampling_rate_hz) self.assertEqual(6.666, au_file.mp3_header.bit_rate_kbps) self.assertEqual(2, au_file.mp3_header.channels)
def test_basic(self): test_vol = 11 test_ts_human = "20090102-030405" test_ts = timestamp.parse_human_readable(test_ts_human) test_fp = "1234" * 10 # The UFID prefix should contain the volume and timestamp info. self.assertEqual("vol0b/%s/" % test_ts_human, ufid.ufid_prefix(test_vol, test_ts)) # 0b = 11 # The UFID should equal the UFID prefix + the fingerprint. test_ufid = ufid.ufid(test_vol, test_ts, test_fp) self.assertEqual(ufid.ufid_prefix(test_vol, test_ts) + test_fp, test_ufid) # We should be able to make a tag too. test_tag = ufid.ufid_tag(test_vol, test_ts, test_fp) self.assertEqual("UFID", test_tag.FrameID) self.assertEqual(constants.UFID_OWNER_IDENTIFIER, test_tag.owner) self.assertEqual(test_ufid, test_tag.data) # Make sure we can parse information back out of the test UFID. vol, ts, fp = ufid.parse(test_ufid) self.assertEqual(test_vol, vol) self.assertEqual(test_ts, ts) self.assertEqual(test_fp, fp) # Raise ValueError if we try to parse a bad UFID. self.assertRaises(ValueError, ufid.parse, "bad") self.assertRaises(ValueError, ufid.parse, "vol01/20091399-666666/" + "1" * 40) self.assertRaises(ValueError, ufid.parse, "vol01/20991001-123456" + "1" * 40)
def test_scan_fast_tag_handling(self): test_mp3 = mutagen.mp3.MP3() class MockInfo(object): pass test_mp3.info = MockInfo() test_mp3.add_tags() test_mp3.tags.add(ufid.ufid_tag(TEST_VOL, TEST_TS, TEST_FP)) test_mp3.tags.add(mutagen.id3.TLEN(text=u"11111")) test_mp3.tags.add(mutagen.id3.TXXX( desc=constants.TXXX_ALBUM_ID_DESCRIPTION, text=[u"222"])) test_mp3.tags.add(mutagen.id3.TXXX( desc=constants.TXXX_FRAME_COUNT_DESCRIPTION, text=[u"333"])) test_mp3.tags.add(mutagen.id3.TXXX( desc=constants.TXXX_FRAME_SIZE_DESCRIPTION, text=["444"])) for tag in test_mp3.tags.values(): id3_text.standardize(tag) test_mp3.info.sample_rate = 5555 test_mp3.info.bitrate = 6666 test_mp3.info.mode = 2 au_file = audio_file.scan_fast("/test/path", _read_id3_hook=lambda p: test_mp3) self.assertTrue(audio_file is not None) self.assertEqual("/test/path", au_file.path) self.assertEqual(TEST_VOL, au_file.volume) self.assertEqual(TEST_TS, au_file.import_timestamp) self.assertEqual(TEST_FP, au_file.fingerprint) self.assertEqual(11111, au_file.duration_ms) self.assertEqual(222, au_file.album_id) self.assertEqual(333, au_file.frame_count) self.assertEqual(444, au_file.frame_size) self.assertEqual(5555, au_file.mp3_header.sampling_rate_hz) self.assertEqual(6.666, au_file.mp3_header.bit_rate_kbps) self.assertEqual(2, au_file.mp3_header.channels)