From b1594e46c93fe257c655ee822b7a36872857b09d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 4 Jan 2009 10:12:00 -0800 Subject: [PATCH] LRF output:Fix rendering of tables as images --- src/calibre/ebooks/lrf/html/convert_from.py | 71 ++++++++----------- src/calibre/ebooks/lrf/html/table_as_image.py | 34 +++------ src/calibre/web/feeds/recipes/recipe_ftd.py | 1 - src/calibre/web/feeds/recipes/recipe_zdnet.py | 1 - 4 files changed, 42 insertions(+), 65 deletions(-) diff --git a/src/calibre/ebooks/lrf/html/convert_from.py b/src/calibre/ebooks/lrf/html/convert_from.py index b3f394aac8..292ae0b50b 100644 --- a/src/calibre/ebooks/lrf/html/convert_from.py +++ b/src/calibre/ebooks/lrf/html/convert_from.py @@ -245,7 +245,6 @@ class HTMLConverter(object, LoggingInterface): self.override_css = {} self.override_pcss = {} - self.table_render_job_server = None if self._override_css is not None: if os.access(self._override_css, os.R_OK): @@ -266,41 +265,37 @@ class HTMLConverter(object, LoggingInterface): paths = [os.path.abspath(path) for path in paths] paths = [path.decode(sys.getfilesystemencoding()) if not isinstance(path, unicode) else path for path in paths] - try: - while len(paths) > 0 and self.link_level <= self.link_levels: - for path in paths: - if path in self.processed_files: - continue - try: - self.add_file(path) - except KeyboardInterrupt: + while len(paths) > 0 and self.link_level <= self.link_levels: + for path in paths: + if path in self.processed_files: + continue + try: + self.add_file(path) + except KeyboardInterrupt: + raise + except: + if self.link_level == 0: # Die on errors in the first level raise - except: - if self.link_level == 0: # Die on errors in the first level - raise - for link in self.links: - if link['path'] == path: - self.links.remove(link) - break - self.log_warn('Could not process '+path) - if self.verbose: - self.log_exception(' ') - self.links = self.process_links() - self.link_level += 1 - paths = [link['path'] for link in self.links] - - if self.current_page is not None and self.current_page.has_text(): - self.book.append(self.current_page) - - for text, tb in self.extra_toc_entries: - self.book.addTocEntry(text, tb) - - if self.base_font_size > 0: - self.log_info('\tRationalizing font sizes...') - self.book.rationalize_font_sizes(self.base_font_size) - finally: - if self.table_render_job_server is not None: - self.table_render_job_server.killall() + for link in self.links: + if link['path'] == path: + self.links.remove(link) + break + self.log_warn('Could not process '+path) + if self.verbose: + self.log_exception(' ') + self.links = self.process_links() + self.link_level += 1 + paths = [link['path'] for link in self.links] + + if self.current_page is not None and self.current_page.has_text(): + self.book.append(self.current_page) + + for text, tb in self.extra_toc_entries: + self.book.addTocEntry(text, tb) + + if self.base_font_size > 0: + self.log_info('\tRationalizing font sizes...') + self.book.rationalize_font_sizes(self.base_font_size) def is_baen(self, soup): return bool(soup.find('meta', attrs={'name':'Publisher', @@ -1732,15 +1727,11 @@ class HTMLConverter(object, LoggingInterface): self.process_children(tag, tag_css, tag_pseudo_css) elif tagname == 'table' and not self.ignore_tables and not self.in_table: if self.render_tables_as_images: - if self.table_render_job_server is None: - from calibre.parallel import Server - self.table_render_job_server = Server(number_of_workers=1) print 'Rendering table...' from calibre.ebooks.lrf.html.table_as_image import render_table pheight = int(self.current_page.pageStyle.attrs['textheight']) pwidth = int(self.current_page.pageStyle.attrs['textwidth']) - images = render_table(self.table_render_job_server, - self.soup, tag, tag_css, + images = render_table(self.soup, tag, tag_css, os.path.dirname(self.target_prefix), pwidth, pheight, self.profile.dpi, self.text_size_multiplier_for_rendered_tables) diff --git a/src/calibre/ebooks/lrf/html/table_as_image.py b/src/calibre/ebooks/lrf/html/table_as_image.py index 7211dc858e..4b40dc5be6 100644 --- a/src/calibre/ebooks/lrf/html/table_as_image.py +++ b/src/calibre/ebooks/lrf/html/table_as_image.py @@ -6,14 +6,11 @@ __docformat__ = 'restructuredtext en' ''' Render HTML tables as images. ''' -import os, tempfile, atexit, shutil, time -from PyQt4.Qt import QUrl, QApplication, QSize, \ +import os, tempfile, atexit, shutil +from PyQt4.Qt import QUrl, QApplication, QSize, QEventLoop, \ SIGNAL, QPainter, QImage, QObject, Qt from PyQt4.QtWebKit import QWebPage -from calibre.parallel import ParallelJob - -__app = None class HTMLTableRenderer(QObject): @@ -27,13 +24,15 @@ class HTMLTableRenderer(QObject): self.app = None self.width, self.height, self.dpi = width, height, dpi self.base_dir = base_dir + self.images = [] + self.tdir = tempfile.mkdtemp(prefix='calibre_render_table') + self.loop = QEventLoop() self.page = QWebPage() self.connect(self.page, SIGNAL('loadFinished(bool)'), self.render_html) self.page.mainFrame().setTextSizeMultiplier(factor) self.page.mainFrame().setHtml(html, QUrl('file:'+os.path.abspath(self.base_dir))) - self.images = [] - self.tdir = tempfile.mkdtemp(prefix='calibre_render_table') + def render_html(self, ok): try: @@ -63,7 +62,7 @@ class HTMLTableRenderer(QObject): finally: QApplication.quit() -def render_table(server, soup, table, css, base_dir, width, height, dpi, factor=1.0): +def render_table(soup, table, css, base_dir, width, height, dpi, factor=1.0): head = '' for e in soup.findAll(['link', 'style']): head += unicode(e)+'\n\n' @@ -83,24 +82,13 @@ def render_table(server, soup, table, css, base_dir, width, height, dpi, factor= '''%(head, width-10, style, unicode(table)) - job = ParallelJob('render_table', lambda j : j, None, - args=[html, base_dir, width, height, dpi, factor]) - server.add_job(job) - while not job.has_run: - time.sleep(2) - - if job.exception is not None: - print 'Failed to render table' - print job.exception - print job.traceback - images, tdir = job.result + images, tdir = do_render(html, base_dir, width, height, dpi, factor) atexit.register(shutil.rmtree, tdir) return images def do_render(html, base_dir, width, height, dpi, factor): - app = QApplication.instance() - if app is None: - app = QApplication([]) + if QApplication.instance() is None: + QApplication([]) tr = HTMLTableRenderer(html, base_dir, width, height, dpi, factor) - app.exec_() + tr.loop.exec_() return tr.images, tr.tdir \ No newline at end of file diff --git a/src/calibre/web/feeds/recipes/recipe_ftd.py b/src/calibre/web/feeds/recipes/recipe_ftd.py index d8d188d65f..b2c0a8ef50 100644 --- a/src/calibre/web/feeds/recipes/recipe_ftd.py +++ b/src/calibre/web/feeds/recipes/recipe_ftd.py @@ -47,4 +47,3 @@ class FTheiseDe(BasicNewsRecipe): feeds = [ ('FTD', 'http://www.ftd.de/static/ticker/ftd-topnews.rdf') ] -!DSPAM:3,496093f075721028352036! diff --git a/src/calibre/web/feeds/recipes/recipe_zdnet.py b/src/calibre/web/feeds/recipes/recipe_zdnet.py index f31a99a6c1..6378f55ad8 100644 --- a/src/calibre/web/feeds/recipes/recipe_zdnet.py +++ b/src/calibre/web/feeds/recipes/recipe_zdnet.py @@ -44,4 +44,3 @@ class cdnet(BasicNewsRecipe): -!DSPAM:3,496093f075721028352036!