Exemple #1
0
 def _key_sort(item):
     (key, v) = item
     # 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(v)), repr(v))
 def _key_sort(item):
     (key, v) = item
     # 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(v)), repr(v))
Exemple #3
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))
Exemple #4
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))
    def keys(self):
        """Return a sequence of all keys in the comment."""

        return self and set(next(izip(*self)))
Exemple #6
0
    def keys(self):
        """Return a sequence of all keys in the comment."""

        return self and set(next(izip(*self)))
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_culrc.asf import ASF
        from mutagen_culrc.apev2 import APEv2File
        from mutagen_culrc.flac import FLAC
        if easy:
            from mutagen_culrc.easyid3 import EasyID3FileType as ID3FileType
        else:
            from mutagen_culrc.id3 import ID3FileType
        if easy:
            from mutagen_culrc.mp3 import EasyMP3 as MP3
        else:
            from mutagen_culrc.mp3 import MP3
        from mutagen_culrc.oggflac import OggFLAC
        from mutagen_culrc.oggspeex import OggSpeex
        from mutagen_culrc.oggtheora import OggTheora
        from mutagen_culrc.oggvorbis import OggVorbis
        from mutagen_culrc.oggopus import OggOpus
        if easy:
            from mutagen_culrc.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen_culrc.trueaudio import TrueAudio
        from mutagen_culrc.wavpack import WavPack
        if easy:
            from mutagen_culrc.easymp4 import EasyMP4 as MP4
        else:
            from mutagen_culrc.mp4 import MP4
        from mutagen_culrc.musepack import Musepack
        from mutagen_culrc.monkeysaudio import MonkeysAudio
        from mutagen_culrc.optimfrog import OptimFROG
        from mutagen_culrc.aiff import AIFF
        from mutagen_culrc.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
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_culrc.asf import ASF
        from mutagen_culrc.apev2 import APEv2File
        from mutagen_culrc.flac import FLAC

        if easy:
            from mutagen_culrc.easyid3 import EasyID3FileType as ID3FileType
        else:
            from mutagen_culrc.id3 import ID3FileType
        if easy:
            from mutagen_culrc.mp3 import EasyMP3 as MP3
        else:
            from mutagen_culrc.mp3 import MP3
        from mutagen_culrc.oggflac import OggFLAC
        from mutagen_culrc.oggspeex import OggSpeex
        from mutagen_culrc.oggtheora import OggTheora
        from mutagen_culrc.oggvorbis import OggVorbis
        from mutagen_culrc.oggopus import OggOpus

        if easy:
            from mutagen_culrc.trueaudio import EasyTrueAudio as TrueAudio
        else:
            from mutagen_culrc.trueaudio import TrueAudio
        from mutagen_culrc.wavpack import WavPack

        if easy:
            from mutagen_culrc.easymp4 import EasyMP4 as MP4
        else:
            from mutagen_culrc.mp4 import MP4
        from mutagen_culrc.musepack import Musepack
        from mutagen_culrc.monkeysaudio import MonkeysAudio
        from mutagen_culrc.optimfrog import OptimFROG
        from mutagen_culrc.aiff import AIFF
        from mutagen_culrc.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