Beispiel #1
0
 def test_chunk_delete(self):
     del self.iff_1_tmp[u'ID3']
     self.failIf(u'ID3' in self.iff_1_tmp)
     self.failIf(u'ID3' in IFFFile(self.file_1_tmp))
     self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054)
     del self.iff_2_tmp[u'SSND']
     self.failIf(u'SSND' in self.iff_2_tmp)
     self.failIf(u'SSND' in IFFFile(self.file_2_tmp))
     self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38)
Beispiel #2
0
 def _load_op1_data(self, filename):
     f = open(filename, 'r')
     iff = IFFFile(f)
     appl = iff.__getitem__(u'APPL')     # sorry for accessing internal functions, but TE hides the metadata well
     rawdata = appl.read()
     print rawdata
     data = rawdata.split('op-1')[1]
     f.close()
     return json.loads(data)
Beispiel #3
0
    def test_child_chunk_resize(self):
        self.iff_1_tmp[u'ID3'].resize(128)

        id3 = self.iff_1_tmp[u'ID3']
        id3.write(b"\xff" * 128)
        self.assertEqual(id3.read(), b"\xff" * 128)

        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182)
Beispiel #4
0
 def _write_op1_data(self, filename):
     """
     not yet working
     """
     f = open(filename, 'w')
     iff = IFFFile(f)
     appl = iff.__getitem__(u'APPL')     # sorry for accessing internal functions, but TE hides the metadata well
     print self.op1data
     data = "op-1%s" % json.dumps(self.op1data, sort_keys=True)
     print data
     appl.write(data)
     f.close()
Beispiel #5
0
    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        self.tmp_1_name = get_temp_copy(self.has_tags)
        self.file_1_tmp = open(self.tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        self.tmp_2_name = get_temp_copy(self.no_tags)
        self.file_2_tmp = open(self.tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)
Beispiel #6
0
    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        fd_1, tmp_1_name = mkstemp(suffix='.aif')
        shutil.copy(self.has_tags, tmp_1_name)
        self.file_1_tmp = open(tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        fd_2, tmp_2_name = mkstemp(suffix='.aif')
        shutil.copy(self.no_tags, tmp_2_name)
        self.file_2_tmp = open(tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)
Beispiel #7
0
 def test_corrupt_tag(self):
     with open(self.filename_1, "r+b") as h:
         chunk = IFFFile(h)[u'ID3']
         h.seek(chunk.data_offset)
         h.seek(4, 1)
         h.write(b"\xff\xff")
     self.assertRaises(AIFFError, AIFF, self.filename_1)
Beispiel #8
0
 def test_delete_padded_chunks(self):
     iff_file = self.iff_2_tmp
     iff_file.insert_chunk(u'TST')
     # Resize to odd length, should insert 1 padding byte
     iff_file[u'TST'].resize(3)
     # Insert another chunk after the first one
     iff_file.insert_chunk(u'TST2')
     iff_file[u'TST2'].resize(2)
     self.failUnlessEqual(iff_file[u'FORM'].size, 16076)
     self.failUnlessEqual(iff_file[u'FORM'].data_size, 16068)
     self.failUnlessEqual(iff_file[u'TST'].size, 12)
     self.failUnlessEqual(iff_file[u'TST'].data_size, 3)
     self.failUnlessEqual(iff_file[u'TST'].data_offset, 16062)
     self.failUnlessEqual(iff_file[u'TST2'].size, 10)
     self.failUnlessEqual(iff_file[u'TST2'].data_size, 2)
     self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16074)
     # Delete the odd chunk
     iff_file.delete_chunk(u'TST')
     self.failUnlessEqual(iff_file[u'FORM'].size, 16064)
     self.failUnlessEqual(iff_file[u'FORM'].data_size, 16056)
     self.failUnlessEqual(iff_file[u'TST2'].size, 10)
     self.failUnlessEqual(iff_file[u'TST2'].data_size, 2)
     self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16062)
     # Reloading the file should give the same results
     new_iff_file = IFFFile(self.file_2_tmp)
     self.failUnlessEqual(new_iff_file[u'FORM'].size,
                          iff_file[u'FORM'].size)
     self.failUnlessEqual(new_iff_file[u'TST2'].size,
                          iff_file[u'TST2'].size)
     self.failUnlessEqual(new_iff_file[u'TST2'].data_size,
                          iff_file[u'TST2'].data_size)
     self.failUnlessEqual(new_iff_file[u'TST2'].data_offset,
                          iff_file[u'TST2'].data_offset)
Beispiel #9
0
    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk(u'ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless(u'ID3' in new_iff)
        self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk))
        self.failUnlessEqual(new_iff[u'FORM'].size, 16062)
        self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff[u'ID3'].size, 8)
        self.failUnlessEqual(new_iff[u'ID3'].data_size, 0)
Beispiel #10
0
    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        self.tmp_1_name = get_temp_copy(self.has_tags)
        self.file_1_tmp = open(self.tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        self.tmp_2_name = get_temp_copy(self.no_tags)
        self.file_2_tmp = open(self.tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)
Beispiel #11
0
    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        fd_1, tmp_1_name = mkstemp(suffix='.aif')
        shutil.copy(self.has_tags, tmp_1_name)
        self.file_1_tmp = open(tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        fd_2, tmp_2_name = mkstemp(suffix='.aif')
        shutil.copy(self.no_tags, tmp_2_name)
        self.file_2_tmp = open(tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)
Beispiel #12
0
class TIFFFile(TestCase):
    has_tags = os.path.join(DATA_DIR, 'with-id3.aif')
    no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        self.tmp_1_name = get_temp_copy(self.has_tags)
        self.file_1_tmp = open(self.tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        self.tmp_2_name = get_temp_copy(self.no_tags)
        self.file_2_tmp = open(self.tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)

    def tearDown(self):
        self.file_1.close()
        self.file_2.close()
        self.file_1_tmp.close()
        self.file_2_tmp.close()
        os.unlink(self.tmp_1_name)
        os.unlink(self.tmp_2_name)

    def test_has_chunks(self):
        self.failUnless(u'FORM' in self.iff_1)
        self.failUnless(u'COMM' in self.iff_1)
        self.failUnless(u'SSND' in self.iff_1)
        self.failUnless(u'ID3' in self.iff_1)

        self.failUnless(u'FORM' in self.iff_2)
        self.failUnless(u'COMM' in self.iff_2)
        self.failUnless(u'SSND' in self.iff_2)

    def test_is_chunks(self):
        self.failUnless(isinstance(self.iff_1[u'FORM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'COMM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'SSND'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'ID3'], IFFChunk))

    def test_chunk_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].size, 17096)
        self.failUnlessEqual(self.iff_2[u'FORM'].size, 16054)

    def test_chunk_data_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].data_size, 17088)
        self.failUnlessEqual(self.iff_2[u'FORM'].data_size, 16046)

    def test_FORM_chunk_resize(self):
        self.iff_1_tmp[u'FORM'].resize(17000)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000)
        self.iff_2_tmp[u'FORM'].resize(0)
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 0)

    def test_child_chunk_resize(self):
        self.iff_1_tmp[u'ID3'].resize(128)

        id3 = self.iff_1_tmp[u'ID3']
        id3.write(b"\xff" * 128)
        self.assertEqual(id3.read(), b"\xff" * 128)

        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182)

    def test_chunk_delete(self):
        del self.iff_1_tmp[u'ID3']
        self.failIf(u'ID3' in self.iff_1_tmp)
        self.failIf(u'ID3' in IFFFile(self.file_1_tmp))
        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054)
        del self.iff_2_tmp[u'SSND']
        self.failIf(u'SSND' in self.iff_2_tmp)
        self.failIf(u'SSND' in IFFFile(self.file_2_tmp))
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38)

    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk(u'ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless(u'ID3' in new_iff)
        self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk))
        self.failUnlessEqual(new_iff[u'FORM'].size, 16062)
        self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff[u'ID3'].size, 8)
        self.failUnlessEqual(new_iff[u'ID3'].data_size, 0)
Beispiel #13
0
class TIFFFile(TestCase):
    has_tags = os.path.join('tests', 'data', 'with-id3.aif')
    no_tags = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        fd_1, tmp_1_name = mkstemp(suffix='.aif')
        shutil.copy(self.has_tags, tmp_1_name)
        self.file_1_tmp = open(tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        fd_2, tmp_2_name = mkstemp(suffix='.aif')
        shutil.copy(self.no_tags, tmp_2_name)
        self.file_2_tmp = open(tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)

    def test_has_chunks(self):
        self.failUnless('FORM' in self.iff_1)
        self.failUnless('COMM' in self.iff_1)
        self.failUnless('SSND' in self.iff_1)
        self.failUnless('ID3' in self.iff_1)

        self.failUnless('FORM' in self.iff_2)
        self.failUnless('COMM' in self.iff_2)
        self.failUnless('SSND' in self.iff_2)

    def test_is_chunks(self):
        self.failUnless(isinstance(self.iff_1['FORM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['COMM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['SSND'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['ID3'], IFFChunk))

    def test_chunk_size(self):
        self.failUnlessEqual(self.iff_1['FORM'].size, 17096)
        self.failUnlessEqual(self.iff_2['FORM'].size, 16054)

    def test_chunk_data_size(self):
        self.failUnlessEqual(self.iff_1['FORM'].data_size, 17088)
        self.failUnlessEqual(self.iff_2['FORM'].data_size, 16046)

    def test_FORM_chunk_resize(self):
        self.iff_1_tmp['FORM'].resize(17000)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 17000)
        self.iff_2_tmp['FORM'].resize(0)
        self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].data_size, 0)

    def test_child_chunk_resize(self):
        self.iff_1_tmp['ID3'].resize(128)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['ID3'].data_size, 128)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 16182)

    def test_chunk_delete(self):
        del self.iff_1_tmp['ID3']
        self.failIf('ID3' in self.iff_1_tmp)
        self.failIf('ID3' in IFFFile(self.file_1_tmp))
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].size, 16054)
        del self.iff_2_tmp['SSND']
        self.failIf('SSND' in self.iff_2_tmp)
        self.failIf('SSND' in IFFFile(self.file_2_tmp))
        self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].size, 38)

    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk('ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless('ID3' in new_iff)
        self.failUnless(isinstance(new_iff['ID3'], IFFChunk))
        self.failUnlessEqual(new_iff['FORM'].size, 16062)
        self.failUnlessEqual(new_iff['FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff['ID3'].size, 8)
        self.failUnlessEqual(new_iff['ID3'].data_size, 0)

    def tearDown(self):
        self.file_1.close()
        self.file_2.close()
        self.file_1_tmp.close()
        self.file_2_tmp.close()
Beispiel #14
0
 def test_child_chunk_resize(self):
     self.iff_1_tmp['ID3'].resize(128)
     self.failUnlessEqual(IFFFile(self.file_1_tmp)['ID3'].data_size, 128)
     self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 16182)
Beispiel #15
0
 def test_FORM_chunk_resize(self):
     self.iff_1_tmp[u'FORM'].resize(17000)
     self.failUnlessEqual(
         IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000)
     self.iff_2_tmp[u'FORM'].resize(0)
     self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 0)
Beispiel #16
0
class TIFFFile(TestCase):
    has_tags = os.path.join('tests', 'data', 'with-id3.aif')
    no_tags = os.path.join('tests', 'data', '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        fd_1, tmp_1_name = mkstemp(suffix='.aif')
        shutil.copy(self.has_tags, tmp_1_name)
        self.file_1_tmp = open(tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        fd_2, tmp_2_name = mkstemp(suffix='.aif')
        shutil.copy(self.no_tags, tmp_2_name)
        self.file_2_tmp = open(tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)

    def test_has_chunks(self):
        self.failUnless('FORM' in self.iff_1)
        self.failUnless('COMM' in self.iff_1)
        self.failUnless('SSND' in self.iff_1)
        self.failUnless('ID3' in self.iff_1)

        self.failUnless('FORM' in self.iff_2)
        self.failUnless('COMM' in self.iff_2)
        self.failUnless('SSND' in self.iff_2)

    def test_is_chunks(self):
        self.failUnless(isinstance(self.iff_1['FORM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['COMM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['SSND'], IFFChunk))
        self.failUnless(isinstance(self.iff_1['ID3'], IFFChunk))

    def test_chunk_size(self):
        self.failUnlessEqual(self.iff_1['FORM'].size, 17096)
        self.failUnlessEqual(self.iff_2['FORM'].size, 16054)

    def test_chunk_data_size(self):
        self.failUnlessEqual(self.iff_1['FORM'].data_size, 17088)
        self.failUnlessEqual(self.iff_2['FORM'].data_size, 16046)

    def test_FORM_chunk_resize(self):
        self.iff_1_tmp['FORM'].resize(17000)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 17000)
        self.iff_2_tmp['FORM'].resize(0)
        self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].data_size, 0)

    def test_child_chunk_resize(self):
        self.iff_1_tmp['ID3'].resize(128)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['ID3'].data_size, 128)
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].data_size, 16182)

    def test_chunk_delete(self):
        del self.iff_1_tmp['ID3']
        self.failIf('ID3' in self.iff_1_tmp)
        self.failIf('ID3' in IFFFile(self.file_1_tmp))
        self.failUnlessEqual(IFFFile(self.file_1_tmp)['FORM'].size, 16054)
        del self.iff_2_tmp['SSND']
        self.failIf('SSND' in self.iff_2_tmp)
        self.failIf('SSND' in IFFFile(self.file_2_tmp))
        self.failUnlessEqual(IFFFile(self.file_2_tmp)['FORM'].size, 38)

    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk('ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless('ID3' in new_iff)
        self.failUnless(isinstance(new_iff['ID3'], IFFChunk))
        self.failUnlessEqual(new_iff['FORM'].size, 16062)
        self.failUnlessEqual(new_iff['FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff['ID3'].size, 8)
        self.failUnlessEqual(new_iff['ID3'].data_size, 0)

    def tearDown(self):
        self.file_1.close()
        self.file_2.close()
        self.file_1_tmp.close()
        self.file_2_tmp.close()
Beispiel #17
0
class TIFFFile(TestCase):
    has_tags = os.path.join(DATA_DIR, 'with-id3.aif')
    no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        self.tmp_1_name = get_temp_copy(self.has_tags)
        self.file_1_tmp = open(self.tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        self.tmp_2_name = get_temp_copy(self.no_tags)
        self.file_2_tmp = open(self.tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)

    def tearDown(self):
        self.file_1.close()
        self.file_2.close()
        self.file_1_tmp.close()
        self.file_2_tmp.close()
        os.unlink(self.tmp_1_name)
        os.unlink(self.tmp_2_name)

    def test_has_chunks(self):
        self.failUnless(u'FORM' in self.iff_1)
        self.failUnless(u'COMM' in self.iff_1)
        self.failUnless(u'SSND' in self.iff_1)
        self.failUnless(u'ID3' in self.iff_1)

        self.failUnless(u'FORM' in self.iff_2)
        self.failUnless(u'COMM' in self.iff_2)
        self.failUnless(u'SSND' in self.iff_2)

    def test_is_chunks(self):
        self.failUnless(isinstance(self.iff_1[u'FORM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'COMM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'SSND'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'ID3'], IFFChunk))

    def test_chunk_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].size, 17096)
        self.failUnlessEqual(self.iff_2[u'FORM'].size, 16054)

    def test_chunk_data_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].data_size, 17088)
        self.failUnlessEqual(self.iff_2[u'FORM'].data_size, 16046)

    def test_FORM_chunk_resize(self):
        self.iff_1_tmp[u'FORM'].resize(17000)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000)
        self.iff_2_tmp[u'FORM'].resize(0)
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 0)

    def test_child_chunk_resize(self):
        self.iff_1_tmp[u'ID3'].resize(128)

        id3 = self.iff_1_tmp[u'ID3']
        id3.write(b"\xff" * 128)
        self.assertEqual(id3.read(), b"\xff" * 128)

        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182)

    def test_chunk_delete(self):
        del self.iff_1_tmp[u'ID3']
        self.failIf(u'ID3' in self.iff_1_tmp)
        self.failIf(u'ID3' in IFFFile(self.file_1_tmp))
        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054)
        del self.iff_2_tmp[u'SSND']
        self.failIf(u'SSND' in self.iff_2_tmp)
        self.failIf(u'SSND' in IFFFile(self.file_2_tmp))
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38)

    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk(u'ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless(u'ID3' in new_iff)
        self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk))
        self.failUnlessEqual(new_iff[u'FORM'].size, 16062)
        self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff[u'ID3'].size, 8)
        self.failUnlessEqual(new_iff[u'ID3'].data_size, 0)
Beispiel #18
0
class TIFFFile(TestCase):
    has_tags = os.path.join(DATA_DIR, 'with-id3.aif')
    no_tags = os.path.join(DATA_DIR, '8k-1ch-1s-silence.aif')

    def setUp(self):
        self.file_1 = open(self.has_tags, 'rb')
        self.iff_1 = IFFFile(self.file_1)
        self.file_2 = open(self.no_tags, 'rb')
        self.iff_2 = IFFFile(self.file_2)

        self.tmp_1_name = get_temp_copy(self.has_tags)
        self.file_1_tmp = open(self.tmp_1_name, 'rb+')
        self.iff_1_tmp = IFFFile(self.file_1_tmp)

        self.tmp_2_name = get_temp_copy(self.no_tags)
        self.file_2_tmp = open(self.tmp_2_name, 'rb+')
        self.iff_2_tmp = IFFFile(self.file_2_tmp)

    def tearDown(self):
        self.file_1.close()
        self.file_2.close()
        self.file_1_tmp.close()
        self.file_2_tmp.close()
        os.unlink(self.tmp_1_name)
        os.unlink(self.tmp_2_name)

    def test_has_chunks(self):
        self.failUnless(u'FORM' in self.iff_1)
        self.failUnless(u'COMM' in self.iff_1)
        self.failUnless(u'SSND' in self.iff_1)
        self.failUnless(u'ID3' in self.iff_1)

        self.failUnless(u'FORM' in self.iff_2)
        self.failUnless(u'COMM' in self.iff_2)
        self.failUnless(u'SSND' in self.iff_2)

    def test_is_chunks(self):
        self.failUnless(isinstance(self.iff_1[u'FORM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'COMM'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'SSND'], IFFChunk))
        self.failUnless(isinstance(self.iff_1[u'ID3'], IFFChunk))

    def test_chunk_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].size, 17096)
        self.failUnlessEqual(self.iff_2[u'FORM'].size, 16054)

    def test_chunk_data_size(self):
        self.failUnlessEqual(self.iff_1[u'FORM'].data_size, 17088)
        self.failUnlessEqual(self.iff_2[u'FORM'].data_size, 16046)

    def test_FORM_chunk_resize(self):
        self.iff_1_tmp[u'FORM'].resize(17000)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 17000)
        self.iff_2_tmp[u'FORM'].resize(4)
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].data_size, 4)

    def test_child_chunk_resize(self):
        self.iff_1_tmp[u'ID3'].resize(128)

        id3 = self.iff_1_tmp[u'ID3']
        id3.write(b"\xff" * 128)
        self.assertEqual(id3.read(), b"\xff" * 128)

        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'ID3'].data_size, 128)
        self.failUnlessEqual(
            IFFFile(self.file_1_tmp)[u'FORM'].data_size, 16182)

    def test_chunk_delete(self):
        del self.iff_1_tmp[u'ID3']
        self.failIf(u'ID3' in self.iff_1_tmp)
        self.failIf(u'ID3' in IFFFile(self.file_1_tmp))
        self.failUnlessEqual(IFFFile(self.file_1_tmp)[u'FORM'].size, 16054)
        del self.iff_2_tmp[u'SSND']
        self.failIf(u'SSND' in self.iff_2_tmp)
        self.failIf(u'SSND' in IFFFile(self.file_2_tmp))
        self.failUnlessEqual(IFFFile(self.file_2_tmp)[u'FORM'].size, 38)

    def test_insert_chunk(self):
        self.iff_2_tmp.insert_chunk(u'ID3')

        new_iff = IFFFile(self.file_2_tmp)
        self.failUnless(u'ID3' in new_iff)
        self.failUnless(isinstance(new_iff[u'ID3'], IFFChunk))
        self.failUnlessEqual(new_iff[u'FORM'].size, 16062)
        self.failUnlessEqual(new_iff[u'FORM'].data_size, 16054)
        self.failUnlessEqual(new_iff[u'ID3'].size, 8)
        self.failUnlessEqual(new_iff[u'ID3'].data_size, 0)

    def test_insert_padded_chunks(self):
        padded = self.iff_2_tmp.insert_chunk(u'TST1')
        unpadded = self.iff_2_tmp.insert_chunk(u'TST2')
        # The second chunk needs no padding
        unpadded.resize(4)
        self.failUnlessEqual(4, unpadded.data_size)
        self.failUnlessEqual(0, unpadded.padding())
        self.failUnlessEqual(12, unpadded.size)
        # Resize the first chunk so it needs padding
        padded.resize(3)
        self.failUnlessEqual(3, padded.data_size)
        self.failUnlessEqual(1, padded.padding())
        self.failUnlessEqual(12, padded.size)
        self.failUnlessEqual(padded.offset + padded.size, unpadded.offset)
        # Verify the padding byte gets written correctly
        self.file_2_tmp.seek(padded.data_offset)
        self.file_2_tmp.write(b'ABCD')
        padded.write(b'ABC')
        self.file_2_tmp.seek(padded.data_offset)
        self.failUnlessEqual(b'ABC\x00', self.file_2_tmp.read(4))
        # Verify the second chunk got not overwritten
        self.file_2_tmp.seek(unpadded.offset)
        self.failUnlessEqual(b'TST2', self.file_2_tmp.read(4))

    def test_delete_padded_chunks(self):
        iff_file = self.iff_2_tmp
        iff_file.insert_chunk(u'TST')
        # Resize to odd length, should insert 1 padding byte
        iff_file[u'TST'].resize(3)
        # Insert another chunk after the first one
        iff_file.insert_chunk(u'TST2')
        iff_file[u'TST2'].resize(2)
        self.failUnlessEqual(iff_file[u'FORM'].size, 16076)
        self.failUnlessEqual(iff_file[u'FORM'].data_size, 16068)
        self.failUnlessEqual(iff_file[u'TST'].size, 12)
        self.failUnlessEqual(iff_file[u'TST'].data_size, 3)
        self.failUnlessEqual(iff_file[u'TST'].data_offset, 16062)
        self.failUnlessEqual(iff_file[u'TST2'].size, 10)
        self.failUnlessEqual(iff_file[u'TST2'].data_size, 2)
        self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16074)
        # Delete the odd chunk
        iff_file.delete_chunk(u'TST')
        self.failUnlessEqual(iff_file[u'FORM'].size, 16064)
        self.failUnlessEqual(iff_file[u'FORM'].data_size, 16056)
        self.failUnlessEqual(iff_file[u'TST2'].size, 10)
        self.failUnlessEqual(iff_file[u'TST2'].data_size, 2)
        self.failUnlessEqual(iff_file[u'TST2'].data_offset, 16062)
        # Reloading the file should give the same results
        new_iff_file = IFFFile(self.file_2_tmp)
        self.failUnlessEqual(new_iff_file[u'FORM'].size,
                             iff_file[u'FORM'].size)
        self.failUnlessEqual(new_iff_file[u'TST2'].size,
                             iff_file[u'TST2'].size)
        self.failUnlessEqual(new_iff_file[u'TST2'].data_size,
                             iff_file[u'TST2'].data_size)
        self.failUnlessEqual(new_iff_file[u'TST2'].data_offset,
                             iff_file[u'TST2'].data_offset)