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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  7. import traceback
  8. import os
  9. from calibre import CurrentDir
  10.  
  11. class ConversionError(Exception):
  12.     
  13.     def __init__(self, msg, only_msg = False):
  14.         Exception.__init__(self, msg)
  15.         self.only_msg = only_msg
  16.  
  17.  
  18.  
  19. class UnknownFormatError(Exception):
  20.     pass
  21.  
  22.  
  23. class DRMError(ValueError):
  24.     pass
  25.  
  26. BOOK_EXTENSIONS = [
  27.     'lrf',
  28.     'rar',
  29.     'zip',
  30.     'rtf',
  31.     'lit',
  32.     'txt',
  33.     'htm',
  34.     'xhtm',
  35.     'html',
  36.     'xhtml',
  37.     'pdf',
  38.     'pdb',
  39.     'pdr',
  40.     'prc',
  41.     'mobi',
  42.     'azw',
  43.     'doc',
  44.     'epub',
  45.     'fb2',
  46.     'djvu',
  47.     'lrx',
  48.     'cbr',
  49.     'cbz',
  50.     'cbc',
  51.     'oebzip',
  52.     'rb',
  53.     'imp',
  54.     'odt',
  55.     'chm',
  56.     'tpz',
  57.     'azw1',
  58.     'pml',
  59.     'mbp',
  60.     'tan']
  61.  
  62. class HTMLRenderer(object):
  63.     
  64.     def __init__(self, page, loop):
  65.         self.page = page
  66.         self.loop = loop
  67.         self.data = ''
  68.         self.exception = None
  69.         self.tb = None
  70.  
  71.     
  72.     def __call__(self, ok):
  73.         QImage = QImage
  74.         QPainter = QPainter
  75.         QByteArray = QByteArray
  76.         QBuffer = QBuffer
  77.         import PyQt4.Qt
  78.         
  79.         try:
  80.             if not ok:
  81.                 raise RuntimeError('Rendering of HTML failed.')
  82.             ok
  83.             image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
  84.             image.setDotsPerMeterX(96 * (100 / 2.54))
  85.             image.setDotsPerMeterY(96 * (100 / 2.54))
  86.             painter = QPainter(image)
  87.             self.page.mainFrame().render(painter)
  88.             painter.end()
  89.             ba = QByteArray()
  90.             buf = QBuffer(ba)
  91.             buf.open(QBuffer.WriteOnly)
  92.             image.save(buf, 'JPEG')
  93.             self.data = str(ba.data())
  94.         except Exception:
  95.             e = None
  96.             self.exception = e
  97.             self.traceback = traceback.format_exc()
  98.         finally:
  99.             self.loop.exit(0)
  100.  
  101.  
  102.  
  103.  
  104. def extract_cover_from_embedded_svg(html, base, log):
  105.     etree = etree
  106.     import lxml
  107.     XPath = XPath
  108.     SVG = SVG
  109.     XLINK = XLINK
  110.     import calibre.ebooks.oeb.base
  111.     root = etree.fromstring(html)
  112.     svg = XPath('//svg:svg')(root)
  113.     if len(svg) == 1 and len(svg[0]) == 1 and svg[0][0].tag == SVG('image'):
  114.         image = svg[0][0]
  115.         href = image.get(XLINK('href'), None)
  116.         path = os.path.join(base, *href.split('/'))
  117.         if href and os.access(path, os.R_OK):
  118.             return open(path, 'rb').read()
  119.     
  120.  
  121.  
  122. def extract_calibre_cover(raw, base, log):
  123.     BeautifulSoup = BeautifulSoup
  124.     import calibre.ebooks.BeautifulSoup
  125.     soup = BeautifulSoup(raw)
  126.     matches = soup.find(name = [
  127.         'h1',
  128.         'h2',
  129.         'h3',
  130.         'h4',
  131.         'h5',
  132.         'h6',
  133.         'p',
  134.         'span',
  135.         'font',
  136.         'br'])
  137.     images = soup.findAll('img')
  138.     if matches is None and len(images) == 1 and images[0].get('alt', '') == 'cover':
  139.         img = images[0]
  140.         img = os.path.join(base, *img['src'].split('/'))
  141.         if os.path.exists(img):
  142.             return open(img, 'rb').read()
  143.     
  144.  
  145.  
  146. def render_html_svg_workaround(path_to_html, log, width = 590, height = 750):
  147.     SVG_NS = SVG_NS
  148.     import calibre.ebooks.oeb.base
  149.     raw = open(path_to_html, 'rb').read()
  150.     data = None
  151.     if SVG_NS in raw:
  152.         
  153.         try:
  154.             data = extract_cover_from_embedded_svg(raw, os.path.dirname(path_to_html), log)
  155.  
  156.     
  157.     if data is None:
  158.         
  159.         try:
  160.             data = extract_calibre_cover(raw, os.path.dirname(path_to_html), log)
  161.  
  162.     
  163.     if data is None:
  164.         renderer = render_html(path_to_html, width, height)
  165.         data = getattr(renderer, 'data', None)
  166.     
  167.     return data
  168.  
  169.  
  170. def render_html(path_to_html, width = 590, height = 750):
  171.     QWebPage = QWebPage
  172.     import PyQt4.QtWebKit
  173.     QEventLoop = QEventLoop
  174.     QPalette = QPalette
  175.     Qt = Qt
  176.     SIGNAL = SIGNAL
  177.     QUrl = QUrl
  178.     QSize = QSize
  179.     import PyQt4.Qt
  180.     is_ok_to_use_qt = is_ok_to_use_qt
  181.     import calibre.gui2
  182.     if not is_ok_to_use_qt():
  183.         return None
  184.     path_to_html = os.path.abspath(path_to_html)
  185.     CurrentDir(os.path.dirname(path_to_html)).__enter__()
  186.     
  187.     try:
  188.         page = QWebPage()
  189.         pal = page.palette()
  190.         pal.setBrush(QPalette.Background, Qt.white)
  191.         page.setPalette(pal)
  192.         page.setViewportSize(QSize(width, height))
  193.         page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
  194.         page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
  195.         loop = QEventLoop()
  196.         renderer = HTMLRenderer(page, loop)
  197.         page.connect(page, SIGNAL('loadFinished(bool)'), renderer, Qt.QueuedConnection)
  198.         page.mainFrame().load(QUrl.fromLocalFile(path_to_html))
  199.         loop.exec_()
  200.     finally:
  201.         pass
  202.  
  203.     renderer.loop = None
  204.     renderer.page = None
  205.     del page
  206.     del loop
  207.     return renderer
  208.  
  209.  
  210. def check_ebook_format(stream, current_guess):
  211.     ans = current_guess
  212.     if current_guess.lower() in ('prc', 'mobi', 'azw', 'azw1'):
  213.         stream.seek(0)
  214.         if stream.read(3) == 'TPZ':
  215.             ans = 'tpz'
  216.         
  217.         stream.seek(0)
  218.     
  219.     return ans
  220.  
  221.