mirror of
git://github.com/kovidgoyal/calibre.git
synced 2025-12-24 09:06:33 +01:00
GwR revise layout analysis
This commit is contained in:
parent
834fd93bc8
commit
f500bca0fe
1 changed files with 14 additions and 10 deletions
|
|
@ -4,9 +4,10 @@
|
|||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
''' Read/write metadata from Amazon's topaz format '''
|
||||
import copy, StringIO
|
||||
import copy, StringIO, sys
|
||||
from struct import pack, unpack
|
||||
|
||||
from calibre import prints
|
||||
from calibre.ebooks.metadata import MetaInformation
|
||||
|
||||
def read_record(raw, name):
|
||||
|
|
@ -253,7 +254,8 @@ def get_topaz_headers(self):
|
|||
offset = 5
|
||||
topaz_headers = {}
|
||||
dkey_offset = 0
|
||||
highest_payload_offset = 0
|
||||
lowest_payload_offset = sys.maxint
|
||||
lowest_offset_err = None
|
||||
for x in range(self.header_records):
|
||||
marker = self.data[offset]
|
||||
offset += 1
|
||||
|
|
@ -268,10 +270,10 @@ def get_topaz_headers(self):
|
|||
hdr_offset, consumed = self.decode_vwi(self.data[offset:offset+4])
|
||||
if tag == 'dkey':
|
||||
dkey_offset = hdr_offset
|
||||
if tag != 'metadata':
|
||||
if hdr_offset > highest_payload_offset:
|
||||
highest_payload_offset = hdr_offset
|
||||
#print "adjusting highest_payload_offset to 0x%x (%s)" % (hdr_offset,tag)
|
||||
if tag not in ['dkey','metadata']:
|
||||
if hdr_offset < lowest_payload_offset:
|
||||
lowest_payload_offset = hdr_offset
|
||||
lowest_offset_err = "lowest_payload_offset: 0x%x (%s)" % (hdr_offset,tag)
|
||||
offset += consumed
|
||||
len_uncomp, consumed = self.decode_vwi(self.data[offset:offset+4])
|
||||
offset += consumed
|
||||
|
|
@ -280,10 +282,13 @@ def get_topaz_headers(self):
|
|||
blocks[val] = dict(hdr_offset=hdr_offset,len_uncomp=len_uncomp,len_comp=len_comp)
|
||||
topaz_headers[x] = dict(tag=tag,blocks=blocks)
|
||||
self.topaz_headers = topaz_headers
|
||||
self.highest_payload_offset = highest_payload_offset
|
||||
self.eod = self.data[offset]
|
||||
offset += 1
|
||||
self.base = offset
|
||||
self.lowest_payload_offset = lowest_payload_offset + self.base
|
||||
if self.lowest_payload_offset < self.md_header_offset:
|
||||
prints("Unexpected TPZ file layout:\n %s\n metadata_offset: 0x%x" % (lowest_offset_err, self.md_header_offset))
|
||||
prints("metadata needs to be before payload")
|
||||
self.base_value = None
|
||||
if dkey_offset:
|
||||
self.base_value = self.data[offset:offset + dkey_offset]
|
||||
|
|
@ -352,8 +357,8 @@ def update_metadata(tag,value):
|
|||
self.metadata[item]['metadata'] = value
|
||||
return
|
||||
|
||||
if self.md_start > self.highest_payload_offset:
|
||||
raise ValueError('Unable to update metadata')
|
||||
if self.md_start > self.lowest_payload_offset:
|
||||
raise ValueError('Unable to update metadata:')
|
||||
|
||||
try:
|
||||
from calibre.ebooks.conversion.config import load_defaults
|
||||
|
|
@ -386,7 +391,6 @@ def update_metadata(tag,value):
|
|||
self.stream.write(updated_metadata)
|
||||
self.stream.write(tail)
|
||||
|
||||
|
||||
def set_metadata(stream, mi):
|
||||
mu = MetadataUpdater(stream)
|
||||
mu.update(mi)
|
||||
|
|
|
|||
Loading…
Reference in a new issue