def test_segments(self): f = self.sample_daf() d = DAF(f) summaries = list(d.summaries()) eq = self.assertEqual eq(len(summaries), 2) eq(summaries[0], (b'Summary Name 1', (101.0, 202.0, 303, 513, 640))) eq(summaries[1], (b'Summary Name 2', (111.0, 222.0, 333, 641, 768))) eq = self.assertSequenceEqual eq(list(d.map(summaries[0][1])), [1001.0] * 128) eq(list(d.map(summaries[1][1])), [2002.0] * 128)
def test_add_segment_when_summary_block_is_full(self): f = self.sample_daf() d = DAF(f) # Update n_summaries of final summary block to full. d.file.seek(6 * 1024 + 16) d.file.write(Struct('d').pack(d.summaries_per_record)) d.add_array(b'Summary Name 3', (121.0, 232.0, 343), [3003.0] * 200) # Reset n_summaries of that block back to its real value. d.file.seek(6 * 1024 + 16) d.file.write(Struct('d').pack(1)) summaries = list(d.summaries()) eq = self.assertEqual eq(len(summaries), 3) eq(summaries[0], (b'Summary Name 1', (101.0, 202.0, 303, 513, 640))) eq(summaries[1], (b'Summary Name 2', (111.0, 222.0, 333, 641, 768))) eq(summaries[2], (b'Summary Name 3', (121.0, 232.0, 343, 1281, 1480))) eq = self.assertSequenceEqual eq(list(d.map(summaries[0][1])), [1001.0] * 128) eq(list(d.map(summaries[1][1])), [2002.0] * 128) eq(list(d.map(summaries[2][1])), [3003.0] * 200)
def test_header(self): f = self.sample_daf() d = DAF(f) eq = self.assertEqual eq(d.locidw, b'DAF/SPK') eq(d.nd, 2) eq(d.ni, 3) eq(d.locifn_text, b'Internal Name') eq(d.fward, 3) eq(d.bward, 7) eq(d.free, 0x401) eq(d.locfmt, b'LTL-IEEE')
def test_add_segment(self): f = self.sample_daf() d = DAF(f) d.add_array(b'Summary Name 3', (121.0, 232.0, 343), [3003.0] * 128) summaries = list(d.summaries()) eq = self.assertEqual eq(len(summaries), 3) eq(summaries[0], (b'Summary Name 1', (101.0, 202.0, 303, 513, 640))) eq(summaries[1], (b'Summary Name 2', (111.0, 222.0, 333, 641, 768))) eq(summaries[2], (b'Summary Name 3', (121.0, 232.0, 343, 1025, 1152))) eq = self.assertSequenceEqual eq(list(d.map(summaries[0][1])), [1001.0] * 128) eq(list(d.map(summaries[1][1])), [2002.0] * 128) eq(list(d.map(summaries[2][1])), [3003.0] * 128)
def get_summary(url, spk=True): ''' simple function to retrieve the header of a BSP file and return SPK object''' # connect to file at URL bspurl = urllib2.urlopen(url) # retrieve the "tip" of a file at URL bsptip = bspurl.read(10**5) # first 100kB # save data in fake file object (in-memory) bspstr = StringIO(bsptip) # load into DAF object daf = DAF(bspstr) # return either SPK or DAF object if spk: # make a SPK object spk = SPK(daf) # return representation return spk else: # return representation return daf
def open(cls, path): """Open the file at `path` and return an SPK instance. """ return cls(DAF(open(path, 'rb')))