Example #1
2
    def test_tag(self):
        for frame_id, data, value, intval, info in self.DATA:
            kind = self._get_frame(frame_id)
            tag = kind._fromData(_23, 0, data)
            self.failUnless(tag.HashKey)
            self.failUnless(tag.pprint())
            self.assertEquals(value, tag)
            if 'encoding' not in info:
                self.assertRaises(AttributeError, getattr, tag, 'encoding')
            for attr, value in iteritems(info):
                t = tag
                if not isinstance(value, list):
                    value = [value]
                    t = [t]
                for value, t in izip(value, iter(t)):
                    if isinstance(value, float):
                        self.failUnlessAlmostEqual(value, getattr(t, attr), 5)
                    else:
                        self.assertEquals(value, getattr(t, attr))

                    if isinstance(intval, integer_types):
                        self.assertEquals(intval, operator.pos(t))
                    else:
                        self.assertRaises(TypeError, operator.pos, t)
Example #2
0
 def test_update_offsets(self):
     aa = self.__read_offsets(self.original)
     self.audio["\xa9nam"] = "wheeeeeeee"
     self.audio.save()
     bb = self.__read_offsets(self.filename)
     for a, b in izip(aa, bb):
         self.failUnlessEqual(a, b)
Example #3
0
    def test_keys(self):
        self.failUnless("Track" in self.audio.keys())
        self.failUnless("AnArtist" in self.audio.values())

        self.failUnlessEqual(
            self.audio.items(),
            list(izip(self.audio.keys(), self.audio.values())))
Example #4
0
 def test_update_offsets(self):
     aa = self.__read_offsets(self.original)
     self.audio["\xa9nam"] = "wheeeeeeee"
     self.audio.save()
     bb = self.__read_offsets(self.filename)
     for a, b in izip(aa, bb):
         self.failUnlessEqual(a, b)
Example #5
0
    def test_keys(self):
        self.failUnless("Track" in self.audio.keys())
        self.failUnless("AnArtist" in self.audio.values())

        self.failUnlessEqual(
            self.audio.items(),
            list(izip(self.audio.keys(), self.audio.values())))
Example #6
0
def _item_sort_key(key, value):
    # iTunes always writes the tags in order of "relevance", try
    # to copy it as closely as possible.
    order = ["\xa9nam", "\xa9ART", "\xa9wrt", "\xa9alb",
             "\xa9gen", "gnre", "trkn", "disk",
             "\xa9day", "cpil", "pgap", "pcst", "tmpo",
             "\xa9too", "----", "covr", "\xa9lyr"]
    order = dict(izip(order, xrange(len(order))))
    last = len(order)
    # If there's no key-based way to distinguish, order by length.
    # If there's still no way, go by string comparison on the
    # values, so we at least have something determinstic.
    return (order.get(key[:4], last), len(repr(value)), repr(value))
Example #7
0
def _item_sort_key(key, value):
    # iTunes always writes the tags in order of "relevance", try
    # to copy it as closely as possible.
    order = ["\xa9nam", "\xa9ART", "\xa9wrt", "\xa9alb",
             "\xa9gen", "gnre", "trkn", "disk",
             "\xa9day", "cpil", "pgap", "pcst", "tmpo",
             "\xa9too", "----", "covr", "\xa9lyr"]
    order = dict(izip(order, xrange(len(order))))
    last = len(order)
    # If there's no key-based way to distinguish, order by length.
    # If there's still no way, go by string comparison on the
    # values, so we at least have something determinstic.
    return (order.get(key[:4], last), len(repr(value)), repr(value))
Example #8
0
 def set_key(self, key, value, result=None, expected=True):
     self.audio[key] = value
     self.audio.save()
     self.audio = ASF(self.audio.filename)
     self.failUnless(key in self.audio)
     self.failUnless(key in self.audio.tags)
     self.failUnless(key in self.audio.tags.keys())
     self.failUnless(key in self.audio.tags.as_dict().keys())
     newvalue = self.audio[key]
     if isinstance(newvalue, list):
         for a, b in izip(sorted(newvalue), sorted(result or value)):
             self.failUnlessEqual(a, b)
     else:
         self.failUnlessEqual(self.audio[key], result or value)
Example #9
0
 def set_key(self, key, value, result=None, expected=True):
     self.audio[key] = value
     self.audio.save()
     self.audio = ASF(self.audio.filename)
     self.failUnless(key in self.audio)
     self.failUnless(key in self.audio.tags)
     self.failUnless(key in self.audio.tags.keys())
     self.failUnless(key in self.audio.tags.as_dict().keys())
     newvalue = self.audio[key]
     if isinstance(newvalue, list):
         for a, b in izip(sorted(newvalue), sorted(result or value)):
             self.failUnlessEqual(a, b)
     else:
         self.failUnlessEqual(self.audio[key], result or value)
Example #10
0
    def parse(self, asf, data):
        super(ContentDescriptionObject, self).parse(asf, data)
        lengths = struct.unpack("<HHHHH", data[:10])
        texts = []
        pos = 10
        for length in lengths:
            end = pos + length
            if length > 0:
                texts.append(data[pos:end].decode("utf-16-le").strip(u"\x00"))
            else:
                texts.append(None)
            pos = end

        for key, value in izip(self.NAMES, texts):
            if value is not None:
                value = ASFUnicodeAttribute(value=value)
                asf._tags.setdefault(self.GUID, []).append((key, value))
Example #11
0
    def parse(self, asf, data):
        super(ContentDescriptionObject, self).parse(asf, data)
        lengths = struct.unpack("<HHHHH", data[:10])
        texts = []
        pos = 10
        for length in lengths:
            end = pos + length
            if length > 0:
                texts.append(data[pos:end].decode("utf-16-le").strip(u"\x00"))
            else:
                texts.append(None)
            pos = end

        for key, value in izip(self.NAMES, texts):
            if value is not None:
                value = ASFUnicodeAttribute(value=value)
                asf._tags.setdefault(self.GUID, []).append((key, value))
Example #12
0
    def keys(self):
        """Return a sequence of all keys in the comment."""

        return self and set(next(izip(*self)))
Example #13
0
def File(filething, options=None, easy=False):
    """File(filething, options=None, easy=False)

    Guess the type of the file and try to open it.

    The file type is decided by several things, such as the first 128
    bytes (which usually contains a file type identifier), the
    filename extension, and the presence of existing tags.

    If no appropriate type could be found, None is returned.

    Args:
        filething (filething)
        options: Sequence of :class:`FileType` implementations,
            defaults to all included ones.
        easy (bool):  If the easy wrappers should be returnd if available.
            For example :class:`EasyMP3 <mp3.EasyMP3>` instead of
            :class:`MP3 <mp3.MP3>`.

    Returns:
        FileType: A FileType instance for the detected type or `None` in case
            the type couln't be determined.

    Raises:
        MutagenError: in case the detected type fails to load the file.
    """

    if options is None:
        from mutagen.asf import ASF
        from mutagen.apev2 import APEv2File
        from mutagen.flac import FLAC
        if easy:
            from mutagen.easyid3 import EasyID3FileType as ID3FileType
        else:
            from mutagen.id3 import ID3FileType
        if easy:
            from mutagen.mp3 import EasyMP3 as MP3
        else:
            from mutagen.mp3 import MP3
        from mutagen.oggflac import OggFLAC
        from mutagen.oggspeex import OggSpeex
        from mutagen.oggtheora import OggTheora
        from mutagen.oggvorbis import OggVorbis
        from mutagen.oggopus import OggOpus
        if easy:
            from mutagen.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen.trueaudio import TrueAudio
        from mutagen.wavpack import WavPack
        if easy:
            from mutagen.easymp4 import EasyMP4 as MP4
        else:
            from mutagen.mp4 import MP4
        from mutagen.musepack import Musepack
        from mutagen.monkeysaudio import MonkeysAudio
        from mutagen.optimfrog import OptimFROG
        from mutagen.aiff import AIFF
        from mutagen.aac import AAC
        from mutagen.smf import SMF
        from mutagen.dsf import DSF
        options = [MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC,
                   FLAC, AIFF, APEv2File, MP4, ID3FileType, WavPack,
                   Musepack, MonkeysAudio, OptimFROG, ASF, OggOpus, AAC,
                   SMF, DSF]

    if not options:
        return None

    fileobj = filething.fileobj

    try:
        header = fileobj.read(128)
    except IOError:
        header = b""

    # Sort by name after score. Otherwise import order affects
    # Kind sort order, which affects treatment of things with
    # equals scores.
    results = [(Kind.score(filething.name, fileobj, header), Kind.__name__)
               for Kind in options]

    results = list(izip(results, options))
    results.sort()
    (score, name), Kind = results[-1]
    if score > 0:
        try:
            fileobj.seek(0, 0)
        except IOError:
            pass
        return Kind(fileobj, filename=filething.filename)
    else:
        return None
Example #14
0
    def keys(self):
        """Return a sequence of all keys in the comment."""

        return self and set(next(izip(*self)))
Example #15
0
def File(filething, options=None, easy=False):
    """File(filething, options=None, easy=False)

    Guess the type of the file and try to open it.

    The file type is decided by several things, such as the first 128
    bytes (which usually contains a file type identifier), the
    filename extension, and the presence of existing tags.

    If no appropriate type could be found, None is returned.

    Args:
        filething (filething)
        options: Sequence of :class:`FileType` implementations,
            defaults to all included ones.
        easy (bool):  If the easy wrappers should be returnd if available.
            For example :class:`EasyMP3 <mp3.EasyMP3>` instead of
            :class:`MP3 <mp3.MP3>`.

    Returns:
        FileType: A FileType instance for the detected type or `None` in case
            the type couln't be determined.

    Raises:
        MutagenError: in case the detected type fails to load the file.
    """

    if options is None:
        from mutagen.asf import ASF
        from mutagen.apev2 import APEv2File
        from mutagen.flac import FLAC
        if easy:
            from mutagen.easyid3 import EasyID3FileType as ID3FileType
        else:
            from mutagen.id3 import ID3FileType
        if easy:
            from mutagen.mp3 import EasyMP3 as MP3
        else:
            from mutagen.mp3 import MP3
        from mutagen.oggflac import OggFLAC
        from mutagen.oggspeex import OggSpeex
        from mutagen.oggtheora import OggTheora
        from mutagen.oggvorbis import OggVorbis
        from mutagen.oggopus import OggOpus
        if easy:
            from mutagen.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen.trueaudio import TrueAudio
        from mutagen.wavpack import WavPack
        if easy:
            from mutagen.easymp4 import EasyMP4 as MP4
        else:
            from mutagen.mp4 import MP4
        from mutagen.musepack import Musepack
        from mutagen.monkeysaudio import MonkeysAudio
        from mutagen.optimfrog import OptimFROG
        from mutagen.aiff import AIFF
        from mutagen.aac import AAC
        from mutagen.ac3 import AC3
        from mutagen.smf import SMF
        from mutagen.tak import TAK
        from mutagen.dsf import DSF
        options = [
            MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC, FLAC,
            AIFF, APEv2File, MP4, ID3FileType, WavPack, Musepack, MonkeysAudio,
            OptimFROG, ASF, OggOpus, AAC, AC3, SMF, TAK, DSF
        ]

    if not options:
        return None

    fileobj = filething.fileobj

    try:
        header = fileobj.read(128)
    except IOError:
        header = b""

    # Sort by name after score. Otherwise import order affects
    # Kind sort order, which affects treatment of things with
    # equals scores.
    results = [(Kind.score(filething.name, fileobj, header), Kind.__name__)
               for Kind in options]

    results = list(izip(results, options))
    results.sort()
    (score, name), Kind = results[-1]
    if score > 0:
        try:
            fileobj.seek(0, 0)
        except IOError:
            pass
        return Kind(fileobj, filename=filething.filename)
    else:
        return None
Example #16
0
def File(filename, options=None, easy=False):
    """Guess the type of the file and try to open it.

    The file type is decided by several things, such as the first 128
    bytes (which usually contains a file type identifier), the
    filename extension, and the presence of existing tags.

    If no appropriate type could be found, None is returned.

    :param options: Sequence of :class:`FileType` implementations, defaults to
                    all included ones.

    :param easy: If the easy wrappers should be returnd if available.
                 For example :class:`EasyMP3 <mp3.EasyMP3>` instead
                 of :class:`MP3 <mp3.MP3>`.
    """

    if options is None:
        from mutagen.asf import ASF
        from mutagen.apev2 import APEv2File
        from mutagen.flac import FLAC
        if easy:
            from mutagen.easyid3 import EasyID3FileType as ID3FileType
        else:
            from mutagen.id3 import ID3FileType
        if easy:
            from mutagen.mp3 import EasyMP3 as MP3
        else:
            from mutagen.mp3 import MP3
        from mutagen.oggflac import OggFLAC
        from mutagen.oggspeex import OggSpeex
        from mutagen.oggtheora import OggTheora
        from mutagen.oggvorbis import OggVorbis
        from mutagen.oggopus import OggOpus
        if easy:
            from mutagen.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen.trueaudio import TrueAudio
        from mutagen.wavpack import WavPack
        if easy:
            from mutagen.easymp4 import EasyMP4 as MP4
        else:
            from mutagen.mp4 import MP4
        from mutagen.musepack import Musepack
        from mutagen.monkeysaudio import MonkeysAudio
        from mutagen.optimfrog import OptimFROG
        from mutagen.aiff import AIFF
        from mutagen.aac import AAC
        options = [
            MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC, FLAC,
            AIFF, APEv2File, MP4, ID3FileType, WavPack, Musepack, MonkeysAudio,
            OptimFROG, ASF, OggOpus, AAC
        ]

    if not options:
        return None

    with open(filename, "rb") as fileobj:
        header = fileobj.read(128)
        # Sort by name after score. Otherwise import order affects
        # Kind sort order, which affects treatment of things with
        # equals scores.
        results = [(Kind.score(filename, fileobj, header), Kind.__name__)
                   for Kind in options]

    results = list(izip(results, options))
    results.sort()
    (score, name), Kind = results[-1]
    if score > 0:
        return Kind(filename)
    else:
        return None