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"""

        if (metadata is None):
            return
        elif (self.get_metadata() is not None):
            #current file has metadata, so replace it with new metadata
            self.update_metadata(ID3v22Comment.converted(metadata))
        else:
            #current file has no metadata, so append new ID3 block

            import tempfile
            from .bitstream import BitstreamRecorder

            def chunk_filter(chunks, id3_chunk_data):
                for chunk in chunks:
                    yield chunk

                yield AIFF_Chunk("ID3 ",
                                 len(id3_chunk_data),
                                 id3_chunk_data)

            #turn our ID3v2.2 tag into a raw binary chunk
            id3_chunk = BitstreamRecorder(0)
            ID3v22Comment.converted(metadata).build(id3_chunk)
            id3_chunk = id3_chunk.data()

            #generate a temporary AIFF file in which our new ID3v2.2 chunk
            #is appended to the file's set of chunks
            new_aiff = tempfile.NamedTemporaryFile(suffix=self.SUFFIX)
            self.__class__.aiff_from_chunks(new_aiff.name,
                                            chunk_filter(self.chunks(),
                                                         id3_chunk))

            #replace the existing file with data from the temporary file
            new_file = open(new_aiff.name, 'rb')
            old_file = open(self.filename, 'wb')
            transfer_data(new_file.read, old_file.write)
            old_file.close()
            new_file.close()
Esempio n. 2
0
 def get_metadata(self):
     for (chunk_id, chunk_length, chunk_offset) in self.chunks():
         if (chunk_id == 'ID3 '):
             f = open(self.filename, 'rb')
             f.seek(chunk_offset, 0)
             id3 = ID3v22Comment.parse(f)
             f.close()
             return id3
     else:
         return None
    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."""

        if (metadata is None):
            return
        else:
            self.update_metadata(ID3v22Comment.converted(metadata))
    def get_metadata(self):
        """returns a MetaData object, or None

        raises IOError if unable to read the file"""

        from .bitstream import BitstreamReader

        for chunk in self.chunks():
            if (chunk.id == 'ID3 '):
                return ID3v22Comment.parse(BitstreamReader(chunk.data(), 0))
        else:
            return None
    def get_metadata(self):
        """Returns a MetaData object, or None.

        Raises IOError if unable to read the file."""

        from .bitstream import BitstreamReader

        for (chunk_id, chunk_size, chunk_data) in self.chunks():
            if (chunk_id == 'ID3 '):
                return ID3v22Comment.parse(
                    BitstreamReader(cStringIO.StringIO(chunk_data), 0))
        else:
            return None
Esempio n. 6
0
    def get_metadata(self):
        """Returns a MetaData object, or None.

        Raises IOError if unable to read the file."""

        for (chunk_id, chunk_length, chunk_offset) in self.chunks():
            if (chunk_id == 'ID3 '):
                f = open(self.filename, 'rb')
                f.seek(chunk_offset, 0)
                id3 = ID3v22Comment.parse(f)
                f.close()
                return id3
        else:
            return None
Esempio n. 7
0
    def get_metadata(self):
        """Returns a MetaData object, or None.

        Raises IOError if unable to read the file."""

        for (chunk_id, chunk_length, chunk_offset) in self.chunks():
            if (chunk_id == 'ID3 '):
                f = open(self.filename, 'rb')
                f.seek(chunk_offset, 0)
                id3 = ID3v22Comment.parse(f)
                f.close()
                return id3
        else:
            return None
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."""

        if (metadata is None):
            return

        import tempfile

        id3_chunk = ID3v22Comment.converted(metadata).build()

        new_aiff = tempfile.TemporaryFile()
        new_aiff.seek(12, 0)

        id3_found = False
        for (chunk_id, chunk_length, chunk_file) in self.chunk_files():
            if (chunk_id != 'ID3 '):
                new_aiff.write(
                    self.CHUNK_HEADER.build(
                        Con.Container(chunk_id=chunk_id,
                                      chunk_length=chunk_length)))
                transfer_data(chunk_file.read, new_aiff.write)
            else:
                new_aiff.write(
                    self.CHUNK_HEADER.build(
                        Con.Container(chunk_id='ID3 ',
                                      chunk_length=len(id3_chunk))))
                new_aiff.write(id3_chunk)
                id3_found = True

        if (not id3_found):
            new_aiff.write(
                self.CHUNK_HEADER.build(
                    Con.Container(chunk_id='ID3 ',
                                  chunk_length=len(id3_chunk))))
            new_aiff.write(id3_chunk)

        header = Con.Container(aiff_id='FORM',
                               aiff_size=new_aiff.tell() - 8,
                               aiff_type='AIFF')
        new_aiff.seek(0, 0)
        new_aiff.write(self.AIFF_HEADER.build(header))
        new_aiff.seek(0, 0)
        f = open(self.filename, 'wb')
        transfer_data(new_aiff.read, f.write)
        new_aiff.close()
        f.close()
Esempio n. 9
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."""

        if (metadata is None):
            return

        import tempfile

        id3_chunk = ID3v22Comment.converted(metadata).build()

        new_aiff = tempfile.TemporaryFile()
        new_aiff.seek(12, 0)

        id3_found = False
        for (chunk_id, chunk_length, chunk_file) in self.chunk_files():
            if (chunk_id != 'ID3 '):
                new_aiff.write(self.CHUNK_HEADER.build(
                        Con.Container(chunk_id=chunk_id,
                                      chunk_length=chunk_length)))
                transfer_data(chunk_file.read, new_aiff.write)
            else:
                new_aiff.write(self.CHUNK_HEADER.build(
                        Con.Container(chunk_id='ID3 ',
                                      chunk_length=len(id3_chunk))))
                new_aiff.write(id3_chunk)
                id3_found = True

        if (not id3_found):
            new_aiff.write(self.CHUNK_HEADER.build(
                    Con.Container(chunk_id='ID3 ',
                                  chunk_length=len(id3_chunk))))
            new_aiff.write(id3_chunk)

        header = Con.Container(
            aiff_id='FORM',
            aiff_size=new_aiff.tell() - 8,
            aiff_type='AIFF')
        new_aiff.seek(0, 0)
        new_aiff.write(self.AIFF_HEADER.build(header))
        new_aiff.seek(0, 0)
        f = open(self.filename, 'wb')
        transfer_data(new_aiff.read, f.write)
        new_aiff.close()
        f.close()
Esempio n. 10
0
    def set_metadata(self, metadata):
        if (metadata is None):
            return

        import tempfile

        id3_chunk = ID3v22Comment.converted(metadata).build()

        new_aiff = tempfile.TemporaryFile()
        new_aiff.seek(12, 0)

        id3_found = False
        for (chunk_id, chunk_length, chunk_file) in self.chunk_files():
            if (chunk_id != 'ID3 '):
                new_aiff.write(
                    self.CHUNK_HEADER.build(
                        construct.Container(chunk_id=chunk_id,
                                            chunk_length=chunk_length)))
                transfer_data(chunk_file.read, new_aiff.write)
            else:
                new_aiff.write(
                    self.CHUNK_HEADER.build(
                        construct.Container(chunk_id='ID3 ',
                                            chunk_length=len(id3_chunk))))
                new_aiff.write(id3_chunk)
                id3_found = True

        if (not id3_found):
            new_aiff.write(
                self.CHUNK_HEADER.build(
                    construct.Container(chunk_id='ID3 ',
                                        chunk_length=len(id3_chunk))))
            new_aiff.write(id3_chunk)

        header = construct.Container(aiff_id='FORM',
                                     aiff_size=new_aiff.tell() - 8,
                                     aiff_type='AIFF')
        new_aiff.seek(0, 0)
        new_aiff.write(self.AIFF_HEADER.build(header))
        new_aiff.seek(0, 0)
        f = open(self.filename, 'wb')
        transfer_data(new_aiff.read, f.write)
        new_aiff.close()
        f.close()