Beispiel #1
0
 def copyTags(self, original, target):
     #print ("Moving tags from "+original+" to "+target)
     oldSong = taglib.File(original)
     #print (oldSong.tags)
     newSong = taglib.File(target)
     newSong.tags = oldSong.tags
     newSong.save()
 def test_unicode_tags(self):
     """Ensure unicode keys and values are accepted."""
     with copyTestFile('rare_frames.mp3') as f:
         tf = taglib.File(f)
         tf.tags[u'UNICODE'] = [u'ONE', u'TWO']
         tf.save()
         del tf
         tf = taglib.File(f)
         self.assertEqual(tf.tags['UNICODE'], ['ONE', 'TWO'])
 def test_bytes_tags(self):
     """Ensure bytes keys and values are accepted."""
     with copyTestFile('rare_frames.mp3') as f:
         tf = taglib.File(f)
         tf.tags[b'BYTES'] = [b'ONE', b'TWO']
         tf.save()
         del tf
         tf = taglib.File(f)
         self.assertEqual(tf.tags['BYTES'], ['ONE', 'TWO'])
def test_accepts_unicode_keys_and_tags(tmpdir):
    f = copy_test_file('rare_frames.mp3', tmpdir)
    tf = taglib.File(f)
    tf.tags[u'UNICODE'] = [u'OnE', u'twO']
    tf.save()
    tf.close()

    tf = taglib.File(f)
    assert tf.tags['UNICODE'] == ['OnE', 'twO']
    tf.close()
def test_accepts_bytes_keys_and_values(tmpdir):
    f = copy_test_file('rare_frames.mp3', tmpdir)
    tf = taglib.File(f)
    tf.tags[b'BYTES'] = [b'OnE', b'twO']
    tf.save()
    tf.close()

    tf = taglib.File(f)
    assert tf.tags['BYTES'] == ['OnE', 'twO']
    tf.close()
Beispiel #6
0
    def test_unicode_value(self):
        with copyTestFile('testöü.flac') as copy_file:
            tfile = taglib.File(copy_file)
            tfile.tags["ARTIST"] = ["artøst 1", "artöst 2"]
            tfile.save()

            tfile = taglib.File(copy_file)
            self.assertEqual(len(tfile.tags["ARTIST"]), 2)
            self.assertEqual(tfile.tags["ARTIST"][0], "artøst 1")
            self.assertEqual(tfile.tags["ARTIST"][1], "artöst 2")
Beispiel #7
0
def test_not_existing_file_raises():
    """Ensure OSError is raised if a file does not exist, or is a directory."""
    with pytest.raises(OSError):
        taglib.File('/this/file/almost/certainly/does/not/exist.flac')
    with pytest.raises(OSError):
        taglib.File('/spæciäl/chàracterß.mp3')
    with pytest.raises(OSError):
        taglib.File('/usr')  # directory
    with pytest.raises(OSError):
        taglib.File("/nonexistent.ogg")
Beispiel #8
0
    def test_delete_key(self):
        with copyTestFile('issue19.flac') as copy_file:
            tfile = taglib.File(copy_file)
            del tfile.tags['COMMENT']
            tfile.save()
            tfile.close()

            tfile = taglib.File(copy_file)
            self.assertNotIn('COMMENT', tfile.tags)
            tfile.close()
def test_set_value_to_empty_list_removes_tag(tmpdir):
    copy_file = copy_test_file('issue19.flac', tmpdir)
    tfile = taglib.File(copy_file)
    tfile.tags['COMMENT'] = []
    tfile.save()
    tfile.close()

    tfile = taglib.File(copy_file)
    assert 'COMMENT' not in tfile.tags
    tfile.close()
Beispiel #10
0
    def test_unicode_value(self):
        with copyTestFile('testöü.flac') as copy_file:
            tfile = taglib.File(copy_file)
            tfile.tags['ARTIST'] = ['artøst 1', 'artöst 2']
            tfile.save()

            tfile = taglib.File(copy_file)
            self.assertEqual(len(tfile.tags['ARTIST']), 2)
            self.assertEqual(tfile.tags['ARTIST'][0], 'artøst 1')
            self.assertEqual(tfile.tags['ARTIST'][1], 'artöst 2')
Beispiel #11
0
def getAllTags(fileList):
    allKeys = {key for file in fileList for key in taglib.File(file).tags}

    allTags = defaultdict(list)
    for file in fileList:
        allTags['FILE'].append(file)
        for key in allKeys:
            allTags[key].append(', '.join(taglib.File(file).tags.get(key, '')))

    return allTags
Beispiel #12
0
def test_flac_supports_unicode_value(tmpdir):
    copy_file = copy_test_file('testöü.flac', tmpdir)
    tfile = taglib.File(copy_file)
    tfile.tags['ARTIST'] = ['artøst 1', 'artöst 2']
    tfile.save()
    tfile.close()

    tfile = taglib.File(copy_file)
    assert tfile.tags['ARTIST'] == ['artøst 1', 'artöst 2']
    tfile.close()
def test_delete_key_removes_tag(tmpdir):
    copy_file = copy_test_file('issue19.flac', tmpdir)
    tfile = taglib.File(copy_file)
    del tfile.tags['COMMENT']
    tfile.save()
    tfile.close()

    tfile = taglib.File(copy_file)
    assert 'COMMENT' not in tfile.tags
    tfile.close()
Beispiel #14
0
    def test_set_to_empty_list(self):
        with copyTestFile('issue19.flac') as copy_file:
            tfile = taglib.File(copy_file)
            tfile.tags['COMMENT'] = []
            tfile.save()
            tfile.close()

            tfile = taglib.File(copy_file)
            self.assertNotIn('COMMENT', tfile.tags)
            tfile.close()
def test_set_value_to_space_does_not_remove_tag(tmpdir):
    copy_file = copy_test_file('issue19.flac', tmpdir)
    tfile = taglib.File(copy_file)
    tfile.tags['COMMENT'] = [' ']
    tfile.save()
    tfile.close()

    tfile = taglib.File(copy_file)
    assert 'COMMENT' in tfile.tags
    assert tfile.tags['COMMENT'][0] == ' '
    tfile.close()
Beispiel #16
0
def test_string_value_is_converted_to_list(tmpdir):
    f = copy_test_file('testöü.flac', tmpdir)
    tf = taglib.File(f)
    tf.tags['AAA'] = u'A TAG'
    tf.tags['BBB'] = b'ANOTHER TAG'
    tf.save()
    del tf
    tf = taglib.File(f)
    assert tf.tags['AAA'] == ['A TAG']
    assert tf.tags['BBB'] == ['ANOTHER TAG']
    tf.close()
Beispiel #17
0
    def test_id3v1Tov2(self):
        with copyTestFile('onlyv1.mp3') as f:
            tfile = taglib.File(f, True)
            self.assert_('ARTIST' in tfile.tags)
            self.assertEqual(tfile.tags['ARTIST'][0], 'Bla')
            tfile.tags["NONID3V1"] = ["omg", "wtf"]
            ret = tfile.save()
            self.assertEqual(len(ret), 0)

            tfile = taglib.File(f)
            self.assert_('NONID3V1' in tfile.tags)
Beispiel #18
0
 def test_removeFrame2(self):
     """See https://bugs.kde.org/show_bug.cgi?id=298183."""
     with copyTestFile('r2.mp3') as f:
         tfile = taglib.File(f, True)
         self.assert_('TITLE' in tfile.tags)
         self.assertEqual(len(tfile.tags['TITLE']), 1)
         del tfile.tags['TITLE']
         tfile.save()
         del tfile
         tfile = taglib.File(f)
         self.assert_('TITLE' not in tfile.tags)
Beispiel #19
0
    def test_set_to_space(self):
        with copyTestFile('issue19.flac') as copy_file:
            tfile = taglib.File(copy_file)
            tfile.tags['COMMENT'] = [' ']
            tfile.save()
            tfile.close()

            tfile = taglib.File(copy_file)
            self.assertIn('COMMENT', tfile.tags)
            self.assertEqual(tfile.tags['COMMENT'][0], ' ')
            tfile.close()
Beispiel #20
0
 def test_strval(self):
     """Ensure writing single tag values instead of lists is supported (using both bytes and
     unicode)."""
     with copyTestFile('testöü.flac') as f:
         tf = taglib.File(f)
         tf.tags['AAA'] = u'A TAG'
         tf.tags['BBB'] = b'ANOTHER TAG'
         tf.save()
         del tf
         tf = taglib.File(f)
         self.assertEqual(tf.tags['AAA'], ['A TAG'])
         self.assertEqual(tf.tags['BBB'], ['ANOTHER TAG'])
Beispiel #21
0
    def test_id3v1Tov2(self):
        with copyTestFile('onlyv1.mp3') as f:
            tfile = taglib.File(f)
            self.assertTrue('ARTIST' in tfile.tags)
            self.assertEqual(tfile.tags['ARTIST'][0], 'Bla')
            tfile.tags['NONID3V1'] = ['omg', 'wtf']
            ret = tfile.save()
            self.assertEqual(len(ret), 0)
            tfile.close()

            tfile = taglib.File(f)
            self.assertTrue('NONID3V1' in tfile.tags)
            tfile.close()
Beispiel #22
0
def test_id3v1_is_converted_to_v2_on_save(tmpdir):
    f = copy_test_file('onlyv1.mp3', tmpdir)
    tfile = taglib.File(f)
    assert 'ARTIST' in tfile.tags
    assert tfile.tags['ARTIST'][0] == 'Bla'
    tfile.tags['NONID3V1'] = ['omg', 'wtf']
    ret = tfile.save()
    assert len(ret) == 0
    tfile.close()

    tfile = taglib.File(f)
    assert 'NONID3V1' in tfile.tags
    tfile.close()
Beispiel #23
0
 def test_removeFrame1(self):
     """See https://bugs.kde.org/show_bug.cgi?id=298183
     
     Should be fixed in recent taglib versions.
     """
     with copyTestFile('rare_frames.mp3') as f:
         tfile = taglib.File(f, True)
         self.assert_('GENRE' in tfile.tags)
         self.assertEqual(len(tfile.tags['GENRE']), 1)
         del tfile.tags['GENRE']
         tfile.save()
         del tfile
         tfile = taglib.File(f)
         self.assert_('GENRE' not in tfile.tags)
Beispiel #24
0
def test_remove_title_frame_from_mp3(tmpdir):
    """See https://bugs.kde.org/show_bug.cgi?id=298183."""
    f = copy_test_file('r2.mp3', tmpdir)
    tfile = taglib.File(f)
    assert 'TITLE' in tfile.tags
    assert len(tfile.tags['TITLE']) == 1

    del tfile.tags['TITLE']
    tfile.save()
    tfile.close()

    tfile = taglib.File(f)
    assert 'TITLE' not in tfile.tags
    tfile.close()
Beispiel #25
0
 def loadFile(self, _filePath):
     self.tags = None
     self.tagFile = None
     self.filePath = _filePath
     self.isCorrect = False
     self.isSave = False
     self.isNeedUpdate = False
     try:
         self.tagFile = taglib.File(
             uni.trEncode(self.filePath, fu.fileSystemEncoding))
         self.tags = self.tagFile.tags
     except:
         self.tagFile = taglib.File(self.filePath)
         self.tags = self.tagFile.tags
Beispiel #26
0
    def test_bytes_tags(self):
        """Ensure bytes keys and values are accepted.
        
        Update 2017-05-12: Use mixed-case tag values as a regression test for issue #33.
        """
        with copyTestFile('rare_frames.mp3') as f:
            tf = taglib.File(f)
            tf.tags[b'BYTES'] = [b'OnE', b'twO']
            tf.save()
            tf.close()

            tf = taglib.File(f)
            self.assertEqual(tf.tags['BYTES'], ['OnE', 'twO'])
            tf.close()
Beispiel #27
0
    def test_removeFrame1(self):
        """See https://bugs.kde.org/show_bug.cgi?id=298183
        """
        with copyTestFile('rare_frames.mp3') as f:
            tfile = taglib.File(f)
            self.assertTrue('GENRE' in tfile.tags)
            self.assertEqual(len(tfile.tags['GENRE']), 1)
            del tfile.tags['GENRE']
            tfile.save()
            tfile.close()

            tfile = taglib.File(f)
            self.assertTrue('GENRE' not in tfile.tags)
            tfile.close()
Beispiel #28
0
 def test_removeFrame1(self):
     """See https://bugs.kde.org/show_bug.cgi?id=298183
     
     Uses the applyID3v2Hack to work on older taglib releases. With taglib>=1.9, should
     also work without the hack.
     """
     with copyTestFile('rare_frames.mp3') as f:
         tfile = taglib.File(f, True)
         self.assert_('GENRE' in tfile.tags)
         self.assertEqual(len(tfile.tags['GENRE']), 1)
         del tfile.tags['GENRE']
         tfile.save()
         del tfile
         tfile = taglib.File(f)
         self.assert_('GENRE' not in tfile.tags)
Beispiel #29
0
    def getFileInfo(self, name, pathname):

        try:
            audio = None
            size = os.stat(pathname).st_size

            if size > 0:
                try:
                    audio = taglib.File(pathname)
                except IOError:
                    pass

            if audio is not None:
                bitrateinfo = (
                    int(audio.bitrate), int(False)
                )  # Second argument used to be VBR (variable bitrate)
                fileinfo = (name, size, bitrateinfo, int(audio.length))
            else:
                fileinfo = (name, size, None, None)

            return fileinfo

        except Exception as errtuple:
            message = _("Error while scanning file %(path)s: %(error)s") % {
                'path': pathname,
                'error': errtuple
            }
            self.logMessage(message)
            displayTraceback(sys.exc_info()[2])
Beispiel #30
0
    def pick_music_info(self, files):
        infos = []

        for file in files:
            file_type = mimetypes.guess_type(file)[0]
            if file_type and file_type.startswith("audio/"):
                tags = taglib.File(file).tags

                info = {
                    "name":
                    tags["TITLE"][0].strip() if "TITLE" in tags
                    and len(tags["TITLE"]) > 0 else os.path.splitext(
                        os.path.basename(file))[0],
                    "path":
                    file,
                    "artist":
                    tags["ARTIST"][0].strip()
                    if "ARTIST" in tags and len(tags["ARTIST"]) > 0 else "",
                    "album":
                    tags["ALBUM"][0].strip()
                    if "ALBUM" in tags and len(tags["ALBUM"]) > 0 else ""
                }
                infos.append(info)

        infos.sort(key=cmp_to_key(self.music_compare))

        return infos