Exemplo 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)
Exemplo 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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)