home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1036 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.1 KB  |  96 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 os
  8. import struct
  9. import zlib
  10. from calibre.ebooks.pdb.formatreader import FormatReader
  11. from calibre.ebooks.pdb.ztxt import zTXTError
  12. from calibre.ebooks.txt.processor import convert_basic, opf_writer, separate_paragraphs_single_line, separate_paragraphs_print_formatted
  13. SUPPORTED_VERSION = (1, 40)
  14.  
  15. class HeaderRecord(object):
  16.     
  17.     def __init__(self, raw):
  18.         (self.version,) = struct.unpack('>H', raw[0:2])
  19.         (self.num_records,) = struct.unpack('>H', raw[2:4])
  20.         (self.size,) = struct.unpack('>L', raw[4:8])
  21.         (self.record_size,) = struct.unpack('>H', raw[8:10])
  22.         (self.flags,) = struct.unpack('>B', raw[18:19])
  23.  
  24.  
  25.  
  26. class Reader(FormatReader):
  27.     
  28.     def __init__(self, header, stream, log, options):
  29.         self.stream = stream
  30.         self.log = log
  31.         self.encoding = options.input_encoding
  32.         self.single_line_paras = options.single_line_paras
  33.         self.print_formatted_paras = options.print_formatted_paras
  34.         self.sections = []
  35.         for i in range(header.num_sections):
  36.             self.sections.append(header.section_data(i))
  37.         
  38.         self.header_record = HeaderRecord(self.section_data(0))
  39.         vmajor = (self.header_record.version & 65280) >> 8
  40.         vminor = self.header_record.version & 255
  41.         if (vmajor < 1 or vmajor == 1) and vminor < 40:
  42.             raise zTXTError('Unsupported ztxt version (%i.%i). Only versions newer than %i.%i are supported.' % (vmajor, vminor, SUPPORTED_VERSION[0], SUPPORTED_VERSION[1]))
  43.         vminor < 40
  44.         if self.header_record.flags & 1 == 0:
  45.             raise zTXTError('Only compression method 1 (random access) is supported')
  46.         self.header_record.flags & 1 == 0
  47.         self.log.debug('Foud ztxt version: %i.%i' % (vmajor, vminor))
  48.         self.uncompressor = zlib.decompressobj()
  49.         self.uncompressor.decompress(self.section_data(1))
  50.  
  51.     
  52.     def section_data(self, number):
  53.         return self.sections[number]
  54.  
  55.     
  56.     def decompress_text(self, number):
  57.         if number == 1:
  58.             self.uncompressor = zlib.decompressobj()
  59.         
  60.         return None(self.uncompressor.decompress(self.section_data(number)).decode if self.encoding is None else self.encoding, 'replace')
  61.  
  62.     
  63.     def extract_content(self, output_dir):
  64.         txt = ''
  65.         self.log.info('Decompressing text...')
  66.         for i in range(1, self.header_record.num_records + 1):
  67.             self.log.debug('\tDecompressing text section %i' % i)
  68.             txt += self.decompress_text(i)
  69.         
  70.         self.log.info('Converting text to OEB...')
  71.         if self.single_line_paras:
  72.             txt = separate_paragraphs_single_line(txt)
  73.         
  74.         if self.print_formatted_paras:
  75.             txt = separate_paragraphs_print_formatted(txt)
  76.         
  77.         html = convert_basic(txt)
  78.         
  79.         try:
  80.             index = _[1]
  81.             index.write(html.encode('utf-8'))
  82.         finally:
  83.             pass
  84.  
  85.         get_metadata = get_metadata
  86.         import calibre.ebooks.metadata.meta
  87.         mi = get_metadata(self.stream, 'pdb')
  88.         manifest = [
  89.             ('index.html', None)]
  90.         spine = [
  91.             'index.html']
  92.         opf_writer(output_dir, 'metadata.opf', manifest, spine, mi)
  93.         return os.path.join(output_dir, 'metadata.opf')
  94.  
  95.  
  96.