home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2009, John Schember <john@nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- import struct
- import zlib
- from calibre.ebooks.pdb.formatwriter import FormatWriter
- from calibre.ebooks.pdb.header import PdbHeaderBuilder
- from calibre.ebooks.txt.txtml import TXTMLizer
- from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
- MAX_RECORD_SIZE = 8192
-
- class Writer(FormatWriter):
-
- def __init__(self, opts, log):
- self.opts = opts
- self.log = log
-
-
- def write_content(self, oeb_book, out_stream, metadata = None):
- if self.opts.title:
- pass
- elif oeb_book.metadata.title != []:
- pass
-
- title = _('Unknown')
- (txt_records, txt_length) = self._generate_text(oeb_book)
- crc32 = 0
- section_lengths = []
- compressor = zlib.compressobj(9)
- self.log.info('Compressing data...')
- for i in range(0, len(txt_records)):
- self.log.debug('\tCompressing record %i' % i)
- txt_records[i] = compressor.compress(txt_records[i])
- txt_records[i] = txt_records[i] + compressor.flush(zlib.Z_FULL_FLUSH)
- section_lengths.append(len(txt_records[i]))
- crc32 = zlib.crc32(txt_records[i], crc32) & 0xFFFFFFFFL
-
- header_record = self._header_record(txt_length, len(txt_records), crc32)
- section_lengths.insert(0, len(header_record))
- out_stream.seek(0)
- hb = PdbHeaderBuilder('zTXTGPlm', title)
- hb.build_header(section_lengths, out_stream)
- for record in [
- header_record] + txt_records:
- out_stream.write(record)
-
-
-
- def _generate_text(self, oeb_book):
- writer = TXTMLizer(self.log)
- txt = writer.extract_content(oeb_book, self.opts)
- self.log.debug('\tReplacing newlines with selected type...')
- txt = specified_newlines(TxtNewlines('windows').newline, txt).encode(self.opts.output_encoding, 'replace')
- txt_length = len(txt)
- txt_records = []
- for i in range(0, len(txt) / MAX_RECORD_SIZE + 1):
- txt_records.append(txt[i * MAX_RECORD_SIZE:i * MAX_RECORD_SIZE + MAX_RECORD_SIZE])
-
- return (txt_records, txt_length)
-
-
- def _header_record(self, txt_length, record_count, crc32):
- record = ''
- record += struct.pack('>H', 300)
- record += struct.pack('>H', record_count)
- record += struct.pack('>L', txt_length)
- record += struct.pack('>H', MAX_RECORD_SIZE)
- record += struct.pack('>H', 0)
- record += struct.pack('>H', 0)
- record += struct.pack('>H', 0)
- record += struct.pack('>H', 0)
- record += struct.pack('>B', 1)
- record += struct.pack('>B', 0)
- record += struct.pack('>L', crc32)
- record += struct.pack('>LL', 0, 0)
- return record
-
-
-