home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
- import traceback
- import os
- from calibre import CurrentDir
-
- class ConversionError(Exception):
-
- def __init__(self, msg, only_msg = False):
- Exception.__init__(self, msg)
- self.only_msg = only_msg
-
-
-
- class UnknownFormatError(Exception):
- pass
-
-
- class DRMError(ValueError):
- pass
-
- BOOK_EXTENSIONS = [
- 'lrf',
- 'rar',
- 'zip',
- 'rtf',
- 'lit',
- 'txt',
- 'htm',
- 'xhtm',
- 'html',
- 'xhtml',
- 'pdf',
- 'pdb',
- 'pdr',
- 'prc',
- 'mobi',
- 'azw',
- 'doc',
- 'epub',
- 'fb2',
- 'djvu',
- 'lrx',
- 'cbr',
- 'cbz',
- 'cbc',
- 'oebzip',
- 'rb',
- 'imp',
- 'odt',
- 'chm',
- 'tpz',
- 'azw1',
- 'pml',
- 'mbp',
- 'tan']
-
- class HTMLRenderer(object):
-
- def __init__(self, page, loop):
- self.page = page
- self.loop = loop
- self.data = ''
- self.exception = None
- self.tb = None
-
-
- def __call__(self, ok):
- QImage = QImage
- QPainter = QPainter
- QByteArray = QByteArray
- QBuffer = QBuffer
- import PyQt4.Qt
-
- try:
- if not ok:
- raise RuntimeError('Rendering of HTML failed.')
- ok
- image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
- image.setDotsPerMeterX(96 * (100 / 2.54))
- image.setDotsPerMeterY(96 * (100 / 2.54))
- painter = QPainter(image)
- self.page.mainFrame().render(painter)
- painter.end()
- ba = QByteArray()
- buf = QBuffer(ba)
- buf.open(QBuffer.WriteOnly)
- image.save(buf, 'JPEG')
- self.data = str(ba.data())
- except Exception:
- e = None
- self.exception = e
- self.traceback = traceback.format_exc()
- finally:
- self.loop.exit(0)
-
-
-
-
- def extract_cover_from_embedded_svg(html, base, log):
- etree = etree
- import lxml
- XPath = XPath
- SVG = SVG
- XLINK = XLINK
- import calibre.ebooks.oeb.base
- root = etree.fromstring(html)
- svg = XPath('//svg:svg')(root)
- if len(svg) == 1 and len(svg[0]) == 1 and svg[0][0].tag == SVG('image'):
- image = svg[0][0]
- href = image.get(XLINK('href'), None)
- path = os.path.join(base, *href.split('/'))
- if href and os.access(path, os.R_OK):
- return open(path, 'rb').read()
-
-
-
- def extract_calibre_cover(raw, base, log):
- BeautifulSoup = BeautifulSoup
- import calibre.ebooks.BeautifulSoup
- soup = BeautifulSoup(raw)
- matches = soup.find(name = [
- 'h1',
- 'h2',
- 'h3',
- 'h4',
- 'h5',
- 'h6',
- 'p',
- 'span',
- 'font',
- 'br'])
- images = soup.findAll('img')
- if matches is None and len(images) == 1 and images[0].get('alt', '') == 'cover':
- img = images[0]
- img = os.path.join(base, *img['src'].split('/'))
- if os.path.exists(img):
- return open(img, 'rb').read()
-
-
-
- def render_html_svg_workaround(path_to_html, log, width = 590, height = 750):
- SVG_NS = SVG_NS
- import calibre.ebooks.oeb.base
- raw = open(path_to_html, 'rb').read()
- data = None
- if SVG_NS in raw:
-
- try:
- data = extract_cover_from_embedded_svg(raw, os.path.dirname(path_to_html), log)
-
-
- if data is None:
-
- try:
- data = extract_calibre_cover(raw, os.path.dirname(path_to_html), log)
-
-
- if data is None:
- renderer = render_html(path_to_html, width, height)
- data = getattr(renderer, 'data', None)
-
- return data
-
-
- def render_html(path_to_html, width = 590, height = 750):
- QWebPage = QWebPage
- import PyQt4.QtWebKit
- QEventLoop = QEventLoop
- QPalette = QPalette
- Qt = Qt
- SIGNAL = SIGNAL
- QUrl = QUrl
- QSize = QSize
- import PyQt4.Qt
- is_ok_to_use_qt = is_ok_to_use_qt
- import calibre.gui2
- if not is_ok_to_use_qt():
- return None
- path_to_html = os.path.abspath(path_to_html)
- CurrentDir(os.path.dirname(path_to_html)).__enter__()
-
- try:
- page = QWebPage()
- pal = page.palette()
- pal.setBrush(QPalette.Background, Qt.white)
- page.setPalette(pal)
- page.setViewportSize(QSize(width, height))
- page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
- page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
- loop = QEventLoop()
- renderer = HTMLRenderer(page, loop)
- page.connect(page, SIGNAL('loadFinished(bool)'), renderer, Qt.QueuedConnection)
- page.mainFrame().load(QUrl.fromLocalFile(path_to_html))
- loop.exec_()
- finally:
- pass
-
- renderer.loop = None
- renderer.page = None
- del page
- del loop
- return renderer
-
-
- def check_ebook_format(stream, current_guess):
- ans = current_guess
- if current_guess.lower() in ('prc', 'mobi', 'azw', 'azw1'):
- stream.seek(0)
- if stream.read(3) == 'TPZ':
- ans = 'tpz'
-
- stream.seek(0)
-
- return ans
-
-