Esempio n. 1
0
    def set_cuesheet(self, cuesheet):
        """imports cuesheet data from a Sheet object

        Raises IOError if an error occurs setting the cuesheet"""

        import os.path
        from io import BytesIO
        from audiotools import (MetaData, Filename, FS_ENCODING)
        from audiotools.ape import ApeTag
        from audiotools.cue import write_cuesheet

        if (cuesheet is None):
            return self.delete_cuesheet()

        metadata = self.get_metadata()
        if (metadata is None):
            metadata = ApeTag([])
        else:
            metadata = ApeTag.converted(metadata)

        cuesheet_data = BytesIO()
        write_cuesheet(cuesheet,
                       u"%s" % (Filename(self.filename).basename(), ),
                       cuesheet_data)

        metadata[b'Cuesheet'] = ApeTag.ITEM.string(
            b'Cuesheet',
            cuesheet_data.getvalue().decode(FS_ENCODING, 'replace'))

        self.update_metadata(metadata)
Esempio n. 2
0
    def set_cuesheet(self, cuesheet):
        """imports cuesheet data from a Sheet object

        Raises IOError if an error occurs setting the cuesheet"""

        import os.path
        from io import BytesIO
        from audiotools import (MetaData, Filename, FS_ENCODING)
        from audiotools.ape import ApeTag
        from audiotools.cue import write_cuesheet

        if (cuesheet is None):
            return self.delete_cuesheet()

        metadata = self.get_metadata()
        if (metadata is None):
            metadata = ApeTag([])
        else:
            metadata = ApeTag.converted(metadata)

        cuesheet_data = BytesIO()
        write_cuesheet(cuesheet,
                       u"%s" % (Filename(self.filename).basename(),),
                       cuesheet_data)

        metadata[b'Cuesheet'] = ApeTag.ITEM.string(
            b'Cuesheet',
            cuesheet_data.getvalue().decode(FS_ENCODING, 'replace'))

        self.update_metadata(metadata)
Esempio n. 3
0
    def get_metadata(self):
        """returns a MetaData object, or None

        raises IOError if unable to read the file"""

        f = open(self.filename, "rb")

        # first, attempt to find APEv2 comment at end of file
        f.seek(-32, 2)
        if (f.read(10) == "APETAGEX\xd0\x07"):
            from audiotools import ApeTag

            return ApeTag.read(f)
        else:
            # then, look for ID3v2 comment at beginning of file
            f.seek(0, 0)
            if (f.read(3) == "ID3"):
                from audiotools.id3 import read_id3v2_comment
                try:
                    id3v2 = read_id3v2_comment(self.filename)
                except ValueError:
                    id3v2 = None
            else:
                id3v2 = None

            # and look for ID3v1 comment at end of file
            try:
                f.seek(-128, 2)
                if (f.read(3) == "TAG"):
                    from audiotools.id3v1 import ID3v1Comment
                    try:
                        id3v1 = ID3v1Comment.parse(f)
                    except ValueError:
                        id3v1 = None
                else:
                    id3v1 = None
            except IOError:
                id3v1 = None

            # if both ID3v2 and ID3v1 are present, return a pair
            if ((id3v2 is not None) and (id3v1 is not None)):
                from audiotools.id3 import ID3CommentPair
                return ID3CommentPair(id3v2, id3v1)
            elif (id3v2 is not None):
                return id3v2
            else:
                return id3v1
Esempio n. 4
0
    def get_metadata(self):
        """returns a MetaData object, or None

        raises IOError if unable to read the file"""

        f = open(self.filename, "rb")

        # first, attempt to find APEv2 comment at end of file
        f.seek(-32, 2)
        if (f.read(10) == "APETAGEX\xd0\x07"):
            from audiotools import ApeTag

            return ApeTag.read(f)
        else:
            # then, look for ID3v2 comment at beginning of file
            f.seek(0, 0)
            if (f.read(3) == "ID3"):
                from audiotools.id3 import read_id3v2_comment
                try:
                    id3v2 = read_id3v2_comment(self.filename)
                except ValueError:
                    id3v2 = None
            else:
                id3v2 = None

            # and look for ID3v1 comment at end of file
            try:
                f.seek(-128, 2)
                if (f.read(3) == "TAG"):
                    from audiotools.id3v1 import ID3v1Comment
                    try:
                        id3v1 = ID3v1Comment.parse(f)
                    except ValueError:
                        id3v1 = None
                else:
                    id3v1 = None
            except IOError:
                id3v1 = None

            # if both ID3v2 and ID3v1 are present, return a pair
            if ((id3v2 is not None) and (id3v1 is not None)):
                from audiotools.id3 import ID3CommentPair
                return ID3CommentPair(id3v2, id3v1)
            elif (id3v2 is not None):
                return id3v2
            else:
                return id3v1
Esempio n. 5
0
    def set_replay_gain(self, replaygain):
        """given a ReplayGain object, sets the track's gain to those values

        may raise IOError if unable to read or write the file"""

        if (replaygain is None):
            return self.delete_replay_gain()

        from audiotools.ape import ApeTag

        metadata = self.get_metadata()
        if ((metadata is None) or (isinstance(metadata, ApeTag))):
            ApeGainedAudio.set_replay_gain(self, replaygain)
        else:
            metadata = ApeTag.converted(metadata)
            self.update_metadata(metadata)
            ApeGainedAudio.set_replay_gain(self, replaygain)
Esempio n. 6
0
    def set_replay_gain(self, replaygain):
        """given a ReplayGain object, sets the track's gain to those values

        may raise IOError if unable to read or write the file"""

        if (replaygain is None):
            return self.delete_replay_gain()

        from audiotools.ape import ApeTag

        metadata = self.get_metadata()
        if ((metadata is None) or (isinstance(metadata, ApeTag))):
            ApeGainedAudio.set_replay_gain(self, replaygain)
        else:
            metadata = ApeTag.converted(metadata)
            self.update_metadata(metadata)
            ApeGainedAudio.set_replay_gain(self, replaygain)
Esempio n. 7
0
    def set_metadata(self, metadata):
        """takes a MetaData object and sets this track's metadata

        this metadata includes track name, album name, and so on
        raises IOError if unable to write the file"""

        import os
        from audiotools.ape import ApeTag
        from audiotools.bitstream import BitstreamWriter

        if (metadata is None):
            return
        else:
            new_metadata = ApeTag.converted(metadata)

        if (not os.access(self.filename, os.W_OK)):
            raise IOError(self.filename)

        # if current metadata is present and in a particular format
        # set_metadata() should continue using that format
        old_metadata = ApeTag.converted(self.get_metadata())
        if (old_metadata is not None):
            # transfer ReplayGain tags from old metadata to new metadata
            for tag in [b"replaygain_track_gain",
                        b"replaygain_track_peak",
                        b"replaygain_album_gain",
                        b"replaygain_album_peak"]:
                try:
                    # if old_metadata has tag, shift it over
                    new_metadata[tag] = old_metadata[tag]
                except KeyError:
                    try:
                        # otherwise, if new_metadata has tag, delete it
                        del(new_metadata[tag])
                    except KeyError:
                        # if neither has tag, ignore it
                        continue

            # transfer Cuesheet from old metadata to new metadata
            if (b"Cuesheet" in old_metadata):
                new_metadata[b"Cuesheet"] = old_metadata[b"Cuesheet"]
            elif (b"Cuesheet" in new_metadata):
                del(new_metadata[b"Cuesheet"])

            self.update_metadata(new_metadata)
        else:
            # delete ReplayGain tags from new metadata
            for tag in [b"replaygain_track_gain",
                        b"replaygain_track_peak",
                        b"replaygain_album_gain",
                        b"replaygain_album_peak"]:
                try:
                    del(new_metadata[tag])
                except KeyError:
                    continue

            # delete Cuesheet from new metadata
            if (b"Cuesheet" in new_metadata):
                del(new_metadata[b"Cuesheet"])

            # no current metadata, so append a fresh APEv2 tag
            with BitstreamWriter(open(self.filename, "ab"), True) as writer:
                new_metadata.build(writer)
Esempio n. 8
0
    def set_metadata(self, metadata):
        """takes a MetaData object and sets this track's metadata

        this metadata includes track name, album name, and so on
        raises IOError if unable to write the file"""

        import os
        from audiotools.ape import ApeTag
        from audiotools.bitstream import BitstreamWriter

        if (metadata is None):
            return
        else:
            new_metadata = ApeTag.converted(metadata)

        if (not os.access(self.filename, os.W_OK)):
            raise IOError(self.filename)

        # if current metadata is present and in a particular format
        # set_metadata() should continue using that format
        old_metadata = ApeTag.converted(self.get_metadata())
        if (old_metadata is not None):
            # transfer ReplayGain tags from old metadata to new metadata
            for tag in [
                    b"replaygain_track_gain", b"replaygain_track_peak",
                    b"replaygain_album_gain", b"replaygain_album_peak"
            ]:
                try:
                    # if old_metadata has tag, shift it over
                    new_metadata[tag] = old_metadata[tag]
                except KeyError:
                    try:
                        # otherwise, if new_metadata has tag, delete it
                        del (new_metadata[tag])
                    except KeyError:
                        # if neither has tag, ignore it
                        continue

            # transfer Cuesheet from old metadata to new metadata
            if (b"Cuesheet" in old_metadata):
                new_metadata[b"Cuesheet"] = old_metadata[b"Cuesheet"]
            elif (b"Cuesheet" in new_metadata):
                del (new_metadata[b"Cuesheet"])

            self.update_metadata(new_metadata)
        else:
            # delete ReplayGain tags from new metadata
            for tag in [
                    b"replaygain_track_gain", b"replaygain_track_peak",
                    b"replaygain_album_gain", b"replaygain_album_peak"
            ]:
                try:
                    del (new_metadata[tag])
                except KeyError:
                    continue

            # delete Cuesheet from new metadata
            if (b"Cuesheet" in new_metadata):
                del (new_metadata[b"Cuesheet"])

            # no current metadata, so append a fresh APEv2 tag
            with BitstreamWriter(open(self.filename, "ab"), True) as writer:
                new_metadata.build(writer)