home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1039 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.3 KB  |  82 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import struct
  8. import zlib
  9. from calibre.ebooks.pdb.formatwriter import FormatWriter
  10. from calibre.ebooks.pdb.header import PdbHeaderBuilder
  11. from calibre.ebooks.txt.txtml import TXTMLizer
  12. from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
  13. MAX_RECORD_SIZE = 8192
  14.  
  15. class Writer(FormatWriter):
  16.     
  17.     def __init__(self, opts, log):
  18.         self.opts = opts
  19.         self.log = log
  20.  
  21.     
  22.     def write_content(self, oeb_book, out_stream, metadata = None):
  23.         if self.opts.title:
  24.             pass
  25.         elif oeb_book.metadata.title != []:
  26.             pass
  27.         
  28.         title = _('Unknown')
  29.         (txt_records, txt_length) = self._generate_text(oeb_book)
  30.         crc32 = 0
  31.         section_lengths = []
  32.         compressor = zlib.compressobj(9)
  33.         self.log.info('Compressing data...')
  34.         for i in range(0, len(txt_records)):
  35.             self.log.debug('\tCompressing record %i' % i)
  36.             txt_records[i] = compressor.compress(txt_records[i])
  37.             txt_records[i] = txt_records[i] + compressor.flush(zlib.Z_FULL_FLUSH)
  38.             section_lengths.append(len(txt_records[i]))
  39.             crc32 = zlib.crc32(txt_records[i], crc32) & 0xFFFFFFFFL
  40.         
  41.         header_record = self._header_record(txt_length, len(txt_records), crc32)
  42.         section_lengths.insert(0, len(header_record))
  43.         out_stream.seek(0)
  44.         hb = PdbHeaderBuilder('zTXTGPlm', title)
  45.         hb.build_header(section_lengths, out_stream)
  46.         for record in [
  47.             header_record] + txt_records:
  48.             out_stream.write(record)
  49.         
  50.  
  51.     
  52.     def _generate_text(self, oeb_book):
  53.         writer = TXTMLizer(self.log)
  54.         txt = writer.extract_content(oeb_book, self.opts)
  55.         self.log.debug('\tReplacing newlines with selected type...')
  56.         txt = specified_newlines(TxtNewlines('windows').newline, txt).encode(self.opts.output_encoding, 'replace')
  57.         txt_length = len(txt)
  58.         txt_records = []
  59.         for i in range(0, len(txt) / MAX_RECORD_SIZE + 1):
  60.             txt_records.append(txt[i * MAX_RECORD_SIZE:i * MAX_RECORD_SIZE + MAX_RECORD_SIZE])
  61.         
  62.         return (txt_records, txt_length)
  63.  
  64.     
  65.     def _header_record(self, txt_length, record_count, crc32):
  66.         record = ''
  67.         record += struct.pack('>H', 300)
  68.         record += struct.pack('>H', record_count)
  69.         record += struct.pack('>L', txt_length)
  70.         record += struct.pack('>H', MAX_RECORD_SIZE)
  71.         record += struct.pack('>H', 0)
  72.         record += struct.pack('>H', 0)
  73.         record += struct.pack('>H', 0)
  74.         record += struct.pack('>H', 0)
  75.         record += struct.pack('>B', 1)
  76.         record += struct.pack('>B', 0)
  77.         record += struct.pack('>L', crc32)
  78.         record += struct.pack('>LL', 0, 0)
  79.         return record
  80.  
  81.  
  82.