def read_indices(self): self.flow_table = () if self.header.fdstidx != NULL_INDEX: header = self.kf8_sections[self.header.fdstidx][0] if header[:4] != b'FDST': raise ValueError('KF8 does not have a valid FDST record') sec_start, num_sections = struct.unpack_from(b'>LL', header, 4) secs = struct.unpack_from(b'>%dL' % (num_sections * 2), header, sec_start) self.flow_table = tuple(zip(secs[::2], secs[1::2])) self.files = [] if self.header.skelidx != NULL_INDEX: table = read_index(self.kf8_sections, self.header.skelidx, self.header.codec)[0] File = namedtuple( 'File', 'file_number name divtbl_count start_position length') for i, text in enumerate(table): tag_map = table[text] self.files.append( File(i, text, tag_map[1][0], tag_map[6][0], tag_map[6][1])) self.elems = [] if self.header.dividx != NULL_INDEX: table, cncx = read_index(self.kf8_sections, self.header.dividx, self.header.codec) for i, text in enumerate(table): tag_map = table[text] toc_text = cncx[tag_map[2][0]] self.elems.append( Elem(int(text), toc_text, tag_map[3][0], tag_map[4][0], tag_map[6][0], tag_map[6][1])) self.guide = [] if self.header.othidx != NULL_INDEX: table, cncx = read_index(self.kf8_sections, self.header.othidx, self.header.codec) Item = namedtuple('Item', 'type title pos_fid') for i, ref_type in enumerate(table): tag_map = table[ref_type] # ref_type, ref_title, div/frag number title = cncx[tag_map[1][0]] fileno = None if 3 in list(tag_map.keys()): fileno = tag_map[3][0] if 6 in list(tag_map.keys()): fileno = tag_map[6] if isinstance(ref_type, bytes): ref_type = ref_type.decode(self.header.codec) self.guide.append(Item(ref_type, title, fileno))
def read_indices(self): self.flow_table = () if self.header.fdstidx != NULL_INDEX: header = self.kf8_sections[self.header.fdstidx][0] if header[:4] != b'FDST': raise ValueError('KF8 does not have a valid FDST record') sec_start, num_sections = struct.unpack_from(b'>LL', header, 4) secs = struct.unpack_from(b'>%dL' % (num_sections*2), header, sec_start) self.flow_table = tuple(izip(secs[::2], secs[1::2])) self.files = [] if self.header.skelidx != NULL_INDEX: table = read_index(self.kf8_sections, self.header.skelidx, self.header.codec)[0] File = namedtuple('File', 'file_number name divtbl_count start_position length') for i, text in enumerate(table.iterkeys()): tag_map = table[text] self.files.append(File(i, text, tag_map[1][0], tag_map[6][0], tag_map[6][1])) self.elems = [] if self.header.dividx != NULL_INDEX: table, cncx = read_index(self.kf8_sections, self.header.dividx, self.header.codec) for i, text in enumerate(table.iterkeys()): tag_map = table[text] toc_text = cncx[tag_map[2][0]] self.elems.append(Elem(int(text), toc_text, tag_map[3][0], tag_map[4][0], tag_map[6][0], tag_map[6][1])) self.guide = [] if self.header.othidx != NULL_INDEX: table, cncx = read_index(self.kf8_sections, self.header.othidx, self.header.codec) Item = namedtuple('Item', 'type title pos_fid') for i, ref_type in enumerate(table.iterkeys()): tag_map = table[ref_type] # ref_type, ref_title, div/frag number title = cncx[tag_map[1][0]] fileno = None if 3 in tag_map.keys(): fileno = tag_map[3][0] if 6 in tag_map.keys(): fileno = tag_map[6] self.guide.append(Item(ref_type.decode(self.header.codec), title, fileno))
def read_ncx(sections, index, codec): index_entries = [] if index != NULL_INDEX: table, cncx = read_index(sections, index, codec) for num, x in enumerate(table.iteritems()): text, tag_map = x entry = default_entry.copy() entry['name'] = text entry['num'] = num for tag in tag_fieldname_map.iterkeys(): fieldname, i = tag_fieldname_map[tag] if tag in tag_map: fieldvalue = tag_map[tag][i] if tag == 6: # Appears to be an idx into the KF8 elems table with an # offset fieldvalue = tuple(tag_map[tag]) entry[fieldname] = fieldvalue for which, name in {3:'text', 5:'kind', 70:'description', 71:'author', 72:'image_caption', 73:'image_attribution'}.iteritems(): if tag == which: entry[name] = cncx.get(fieldvalue, default_entry[name]) index_entries.append(entry) return index_entries
def read_ncx(sections, index, codec): index_entries = [] if index != NULL_INDEX: table, cncx = read_index(sections, index, codec) for num, x in enumerate(table.items()): text, tag_map = x entry = default_entry.copy() entry['name'] = text entry['num'] = num for tag in tag_fieldname_map.keys(): fieldname, i = tag_fieldname_map[tag] if tag in tag_map: fieldvalue = tag_map[tag][i] if tag == 6: # Appears to be an idx into the KF8 elems table with an # offset fieldvalue = tuple(tag_map[tag]) entry[fieldname] = fieldvalue for which, name in { 3: 'text', 5: 'kind', 70: 'description', 71: 'author', 72: 'image_caption', 73: 'image_attribution' }.items(): if tag == which: entry[name] = cncx.get(fieldvalue, default_entry[name]) index_entries.append(entry) return index_entries