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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>, 2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import errno
  8. import os
  9. import sys
  10. import subprocess
  11. from functools import partial
  12. from calibre.ebooks import ConversionError, DRMError
  13. from calibre.ptempfile import PersistentTemporaryFile
  14. from calibre import isosx, iswindows, islinux, isfreebsd
  15. from calibre import CurrentDir
  16. PDFTOHTML = 'pdftohtml'
  17. popen = subprocess.Popen
  18. if isosx and hasattr(sys, 'frameworks_dir'):
  19.     PDFTOHTML = os.path.join(getattr(sys, 'frameworks_dir'), PDFTOHTML)
  20.  
  21. if iswindows and hasattr(sys, 'frozen'):
  22.     PDFTOHTML = os.path.join(os.path.dirname(sys.executable), 'pdftohtml.exe')
  23.     popen = partial(subprocess.Popen, creationflags = 8)
  24.  
  25. if (islinux or isfreebsd) and getattr(sys, 'frozen_path', False):
  26.     PDFTOHTML = os.path.join(getattr(sys, 'frozen_path'), 'pdftohtml')
  27.  
  28.  
  29. def pdftohtml(output_dir, pdf_path, no_images):
  30.     if isinstance(pdf_path, unicode):
  31.         pdf_path = pdf_path.encode(sys.getfilesystemencoding())
  32.     
  33.     if not os.access(pdf_path, os.R_OK):
  34.         raise ConversionError('Cannot read from ' + pdf_path)
  35.     os.access(pdf_path, os.R_OK)
  36.     CurrentDir(output_dir).__enter__()
  37.     
  38.     try:
  39.         index = os.path.join(os.getcwd(), 'index.html')
  40.         pdf_path = os.path.abspath(pdf_path)
  41.         cmd = [
  42.             PDFTOHTML,
  43.             '-enc',
  44.             'UTF-8',
  45.             '-noframes',
  46.             '-p',
  47.             '-nomerge',
  48.             '-nodrm',
  49.             '-q',
  50.             pdf_path,
  51.             os.path.basename(index)]
  52.         logf = PersistentTemporaryFile('pdftohtml_log')
  53.         
  54.         try:
  55.             p = popen(cmd, stderr = logf._fd, stdout = logf._fd, stdin = subprocess.PIPE)
  56.         except OSError:
  57.             None if no_images else CurrentDir(output_dir)
  58.             err = None if no_images else CurrentDir(output_dir)
  59.             if err.errno == 2:
  60.                 raise ConversionError(_('Could not find pdftohtml, check it is in your PATH'))
  61.             err.errno == 2
  62.             raise 
  63.         except:
  64.             None if no_images else CurrentDir(output_dir)
  65.  
  66.         while True:
  67.             
  68.             try:
  69.                 ret = p.wait()
  70.             continue
  71.             except OSError:
  72.                 None if no_images else CurrentDir(output_dir)
  73.                 e = None if no_images else CurrentDir(output_dir)
  74.                 if e.errno == errno.EINTR:
  75.                     continue
  76.                 else:
  77.                     raise 
  78.                 e.errno == errno.EINTR
  79.             
  80.  
  81.             None if no_images else CurrentDir(output_dir)<EXCEPTION MATCH>OSError
  82.         logf.flush()
  83.         logf.close()
  84.         out = open(logf.name, 'rb').read()
  85.         if ret != 0:
  86.             raise ConversionError(out)
  87.         ret != 0
  88.         print 'pdftohtml log:'
  89.         print out
  90.         if not os.path.exists(index) or os.stat(index).st_size < 100:
  91.             raise DRMError()
  92.         os.stat(index).st_size < 100
  93.         
  94.         try:
  95.             i = _[1]
  96.             raw = i.read()
  97.             raw = "<!-- created by calibre's pdftohtml -->\n" + raw
  98.             i.seek(0)
  99.             i.truncate()
  100.             i.write(raw)
  101.         finally:
  102.             pass
  103.  
  104.     finally:
  105.         pass
  106.  
  107.  
  108.