home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1152 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  7.1 KB  |  235 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.     'snb']
  62.  
  63. class HTMLRenderer(object):
  64.     
  65.     def __init__(self, page, loop):
  66.         self.page = page
  67.         self.loop = loop
  68.         self.data = ''
  69.         self.exception = None
  70.         self.tb = None
  71.  
  72.     
  73.     def __call__(self, ok):
  74.         QImage = QImage
  75.         QPainter = QPainter
  76.         QByteArray = QByteArray
  77.         QBuffer = QBuffer
  78.         import PyQt4.Qt
  79.         
  80.         try:
  81.             if not ok:
  82.                 raise RuntimeError('Rendering of HTML failed.')
  83.             ok
  84.             image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
  85.             image.setDotsPerMeterX(96 * (100 / 2.54))
  86.             image.setDotsPerMeterY(96 * (100 / 2.54))
  87.             painter = QPainter(image)
  88.             self.page.mainFrame().render(painter)
  89.             painter.end()
  90.             ba = QByteArray()
  91.             buf = QBuffer(ba)
  92.             buf.open(QBuffer.WriteOnly)
  93.             image.save(buf, 'JPEG')
  94.             self.data = str(ba.data())
  95.         except Exception:
  96.             e = None
  97.             self.exception = e
  98.             self.traceback = traceback.format_exc()
  99.         finally:
  100.             self.loop.exit(0)
  101.  
  102.  
  103.  
  104.  
  105. def extract_cover_from_embedded_svg(html, base, log):
  106.     etree = etree
  107.     import lxml
  108.     XPath = XPath
  109.     SVG = SVG
  110.     XLINK = XLINK
  111.     import calibre.ebooks.oeb.base
  112.     root = etree.fromstring(html)
  113.     svg = XPath('//svg:svg')(root)
  114.     if len(svg) == 1 and len(svg[0]) == 1 and svg[0][0].tag == SVG('image'):
  115.         image = svg[0][0]
  116.         href = image.get(XLINK('href'), None)
  117.         path = os.path.join(base, *href.split('/'))
  118.         if href and os.access(path, os.R_OK):
  119.             return open(path, 'rb').read()
  120.     
  121.  
  122.  
  123. def extract_calibre_cover(raw, base, log):
  124.     BeautifulSoup = BeautifulSoup
  125.     import calibre.ebooks.BeautifulSoup
  126.     soup = BeautifulSoup(raw)
  127.     matches = soup.find(name = [
  128.         'h1',
  129.         'h2',
  130.         'h3',
  131.         'h4',
  132.         'h5',
  133.         'h6',
  134.         'p',
  135.         'span',
  136.         'font',
  137.         'br'])
  138.     images = soup.findAll('img')
  139.     if matches is None and len(images) == 1 and images[0].get('alt', '') == 'cover':
  140.         img = images[0]
  141.         img = os.path.join(base, *img['src'].split('/'))
  142.         if os.path.exists(img):
  143.             return open(img, 'rb').read()
  144.     
  145.  
  146.  
  147. def render_html_svg_workaround(path_to_html, log, width = 590, height = 750):
  148.     SVG_NS = SVG_NS
  149.     import calibre.ebooks.oeb.base
  150.     raw = open(path_to_html, 'rb').read()
  151.     data = None
  152.     if SVG_NS in raw:
  153.         
  154.         try:
  155.             data = extract_cover_from_embedded_svg(raw, os.path.dirname(path_to_html), log)
  156.  
  157.     
  158.     if data is None:
  159.         
  160.         try:
  161.             data = extract_calibre_cover(raw, os.path.dirname(path_to_html), log)
  162.  
  163.     
  164.     if data is None:
  165.         renderer = render_html(path_to_html, width, height)
  166.         data = getattr(renderer, 'data', None)
  167.     
  168.     return data
  169.  
  170.  
  171. def render_html(path_to_html, width = 590, height = 750):
  172.     QWebPage = QWebPage
  173.     import PyQt4.QtWebKit
  174.     QEventLoop = QEventLoop
  175.     QPalette = QPalette
  176.     Qt = Qt
  177.     SIGNAL = SIGNAL
  178.     QUrl = QUrl
  179.     QSize = QSize
  180.     import PyQt4.Qt
  181.     is_ok_to_use_qt = is_ok_to_use_qt
  182.     import calibre.gui2
  183.     if not is_ok_to_use_qt():
  184.         return None
  185.     path_to_html = os.path.abspath(path_to_html)
  186.     CurrentDir(os.path.dirname(path_to_html)).__enter__()
  187.     
  188.     try:
  189.         page = QWebPage()
  190.         pal = page.palette()
  191.         pal.setBrush(QPalette.Background, Qt.white)
  192.         page.setPalette(pal)
  193.         page.setViewportSize(QSize(width, height))
  194.         page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
  195.         page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
  196.         loop = QEventLoop()
  197.         renderer = HTMLRenderer(page, loop)
  198.         page.connect(page, SIGNAL('loadFinished(bool)'), renderer, Qt.QueuedConnection)
  199.         page.mainFrame().load(QUrl.fromLocalFile(path_to_html))
  200.         loop.exec_()
  201.     finally:
  202.         pass
  203.  
  204.     renderer.loop = None
  205.     renderer.page = None
  206.     del page
  207.     del loop
  208.     return renderer
  209.  
  210.  
  211. def check_ebook_format(stream, current_guess):
  212.     ans = current_guess
  213.     if current_guess.lower() in ('prc', 'mobi', 'azw', 'azw1'):
  214.         stream.seek(0)
  215.         if stream.read(3) == 'TPZ':
  216.             ans = 'tpz'
  217.         
  218.         stream.seek(0)
  219.     
  220.     return ans
  221.  
  222.  
  223. def calibre_cover(title, author_string, series_string = None, output_format = 'jpg', title_size = 46, author_size = 36):
  224.     create_cover_page = create_cover_page
  225.     TextLine = TextLine
  226.     import calibre.utils.magick.draw
  227.     lines = [
  228.         TextLine(title, title_size),
  229.         TextLine(author_string, author_size)]
  230.     if series_string:
  231.         lines.append(TextLine(series_string, author_size))
  232.     
  233.     return create_cover_page(lines, I('library.png'), output_format = 'jpg')
  234.  
  235.