Fix #2597 (ValueError: chr() arg not in range(256))

This commit is contained in:
Kovid Goyal 2009-06-17 06:25:13 -07:00
parent bd59e2453c
commit 5b1aacfdca

View File

@ -327,6 +327,8 @@ class MobiWriter(object):
self._primary_index_record = None self._primary_index_record = None
self._HTMLRecords = [] self._HTMLRecords = []
self._tbSequence = "" self._tbSequence = ""
self._initialIndexRecordFound = False
@classmethod @classmethod
def generate(cls, opts): def generate(cls, opts):
@ -470,36 +472,40 @@ class MobiWriter(object):
ctoc_offset = self._ctoc_map[child] ctoc_offset = self._ctoc_map[child]
last_name = "%04d" % myIndex last_name = "%04d" % myIndex
myIndex += 1 myIndex += 1
'''
# Dump the accumulated HTML Record Data
x = 0
while x < len(self._HTMLRecords):
self._HTMLRecords[x].dumpData(x, self._oeb)
x += 1
'''
def _build_TBS_Book(self, nrecords, lastrecord): def _build_TBS_Book(self, nrecords, lastrecord):
# Variables for trailing byte sequence # Variables for trailing byte sequence
tbsType = 0x00 tbsType = 0x00
tbSequence = "" tbSequence = ""
#print "_build_TBS_Book: nrecords = %d, lastrecord = %d" % (nrecords, lastrecord)
# Generate TBS for type 0x002 - mobi_book # Generate TBS for type 0x002 - mobi_book
if nrecords == 0 : if self._initialIndexRecordFound == False :
# First HTML record is a special case
if self._HTMLRecords[nrecords].currentSectionNodeCount == 1 : # Is there any indexed content yet?
tbsType = 2 if self._HTMLRecords[nrecords].currentSectionNodeCount == -1 :
# No indexing data - write vwi length of 1 only
tbSequence = decint(len(tbSequence) + 1, DECINT_FORWARD)
else : else :
tbsType = 6 # First indexed HTML record is a special case
# One or more nodes
tbSequence = decint(tbsType, DECINT_FORWARD) self._initialIndexRecordFound = True
tbSequence += decint(0x00, DECINT_FORWARD) if self._HTMLRecords[nrecords].currentSectionNodeCount == 1 :
# Don't write a nodecount for opening type 2 record tbsType = 2
if tbsType != 2 : else :
tbSequence += chr(self._HTMLRecords[nrecords].currentSectionNodeCount) tbsType = 6
tbSequence += decint(len(tbSequence) + 1, DECINT_FORWARD)
tbSequence = decint(tbsType, DECINT_FORWARD)
tbSequence += decint(0x00, DECINT_FORWARD)
# Don't write a nodecount for opening type 2 record
if tbsType != 2 :
# Check that <> -1
tbSequence += chr(self._HTMLRecords[nrecords].currentSectionNodeCount)
tbSequence += decint(len(tbSequence) + 1, DECINT_FORWARD)
else : else :
# Determine tbsType for HTMLRecords > 0 # Determine tbsType for indexed HTMLRecords
if nrecords == lastrecord and self._HTMLRecords[nrecords].currentSectionNodeCount == 1 : if nrecords == lastrecord and self._HTMLRecords[nrecords].currentSectionNodeCount == 1 :
# Ending record with singleton node # Ending record with singleton node
tbsType = 2 tbsType = 2
@ -726,7 +732,7 @@ class MobiWriter(object):
indx1.write(indices) indx1.write(indices)
indx1 = indx1.getvalue() indx1 = indx1.getvalue()
idxt0 = chr(len(last_name)) + last_name + pack('>H', indxt_count) idxt0 = chr(len(last_name)) + last_name + pack('>H', indxt_count + 1)
idxt0 = align_block(idxt0) idxt0 = align_block(idxt0)
indx0 = StringIO() indx0 = StringIO()
@ -764,7 +770,7 @@ class MobiWriter(object):
header.write(iana2mobi(str(self._oeb.metadata.language[0]))) header.write(iana2mobi(str(self._oeb.metadata.language[0])))
# 0x24 - 0x27 : Number of TOC entries in INDX1 # 0x24 - 0x27 : Number of TOC entries in INDX1
header.write(pack('>I', indxt_count)) header.write(pack('>I', indxt_count + 1))
# 0x28 - 0x2b : ORDT Offset # 0x28 - 0x2b : ORDT Offset
header.write('\0'*4) header.write('\0'*4)