From 57f3db82e0d2a96f6a4d195c2de08bc033b0d784 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 11 Sep 2018 12:16:09 +0530 Subject: [PATCH] When downloading recipes from the GUI download the actual recipe in the worker process rather than doing it in the GUI Makes for better logging in case of errors --- .../ebooks/conversion/plugins/recipe_input.py | 18 +++++++++++++++++- src/calibre/gui2/convert/gui_conversion.py | 11 +++++++++-- src/calibre/gui2/dialogs/scheduler.py | 6 ------ src/calibre/gui2/tools.py | 4 ++-- src/calibre/utils/ipc/worker.py | 3 +++ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/recipe_input.py b/src/calibre/ebooks/conversion/plugins/recipe_input.py index 9202ab6432..0b69b66a69 100644 --- a/src/calibre/ebooks/conversion/plugins/recipe_input.py +++ b/src/calibre/ebooks/conversion/plugins/recipe_input.py @@ -69,7 +69,23 @@ class RecipeInput(InputFormatPlugin): recipe.needs_subscription = False self.recipe_object = recipe(opts, log, self.report_progress) else: - if os.access(recipe_or_file, os.R_OK): + if os.environ.get('CALIBRE_RECIPE_URN'): + from calibre.web.feeds.recipes.collection import get_custom_recipe, get_builtin_recipe_by_id + urn = os.environ['CALIBRE_RECIPE_URN'] + log('Downloading recipe urn: ' + urn) + rtype, recipe_id = urn.partition(':')[::2] + if not recipe_id: + raise ValueError('Invalid recipe urn: ' + urn) + if rtype == 'custom': + self.recipe_source = get_custom_recipe(recipe_id) + else: + self.recipe_source = get_builtin_recipe_by_id(urn) + if not self.recipe_source: + raise ValueError('Could not find recipe with urn: ' + urn) + if not isinstance(self.recipe_source, bytes): + self.recipe_source = self.recipe_source.encode('utf-8') + recipe = compile_recipe(self.recipe_source) + elif os.access(recipe_or_file, os.R_OK): self.recipe_source = open(recipe_or_file, 'rb').read() recipe = compile_recipe(self.recipe_source) log('Using custom recipe') diff --git a/src/calibre/gui2/convert/gui_conversion.py b/src/calibre/gui2/convert/gui_conversion.py index ff47c41c07..d2b4877598 100644 --- a/src/calibre/gui2/convert/gui_conversion.py +++ b/src/calibre/gui2/convert/gui_conversion.py @@ -4,6 +4,7 @@ __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' +import os from optparse import OptionParser from calibre.customize.conversion import OptionRecommendation, DummyReporter @@ -26,6 +27,14 @@ def gui_convert(input, output, recommendations, notification=DummyReporter(), plumber.run() +def gui_convert_recipe(input, output, recommendations, notification=DummyReporter(), + abort_after_input_dump=False, log=None, override_input_metadata=False): + os.environ['CALIBRE_RECIPE_URN'] = input + gui_convert('from-gui.recipe', output, recommendations, notification=notification, + abort_after_input_dump=abort_after_input_dump, log=log, + override_input_metadata=override_input_metadata) + + def gui_convert_override(input, output, recommendations, notification=DummyReporter(), abort_after_input_dump=False, log=None): gui_convert(input, output, recommendations, notification=notification, @@ -69,5 +78,3 @@ def gui_catalog(fmt, title, dbspec, ids, out_file_name, sync, fmt_options, conne # Returns 0 if successful, 1 if no catalog built plugin = plugin_for_catalog_format(fmt) return plugin.run(out_file_name, opts, db, notification=notification) - - diff --git a/src/calibre/gui2/dialogs/scheduler.py b/src/calibre/gui2/dialogs/scheduler.py index 5d5b8c44ee..e3f3eee073 100644 --- a/src/calibre/gui2/dialogs/scheduler.py +++ b/src/calibre/gui2/dialogs/scheduler.py @@ -22,7 +22,6 @@ from PyQt5.Qt import ( from calibre.gui2 import config as gconf, error_dialog, gprefs from calibre.gui2.search_box import SearchBox2 from calibre.web.feeds.recipes.model import RecipeModel -from calibre.ptempfile import PersistentTemporaryFile from calibre.utils.date import utcnow from calibre.utils.network import internet_connected from calibre import force_unicode @@ -654,16 +653,11 @@ class Scheduler(QObject): if account_info is not None: un, pw = account_info add_title_tag, custom_tags, keep_issues = customize_info - script = self.recipe_model.get_recipe(urn) - pt = PersistentTemporaryFile('_builtin.recipe') - pt.write(script) - pt.close() arg = { 'username': un, 'password': pw, 'add_title_tag':add_title_tag, 'custom_tags':custom_tags, - 'recipe':pt.name, 'title':recipe.get('title',''), 'urn':urn, 'keep_issues':keep_issues diff --git a/src/calibre/gui2/tools.py b/src/calibre/gui2/tools.py index 45b9152bb8..a2844eb162 100644 --- a/src/calibre/gui2/tools.py +++ b/src/calibre/gui2/tools.py @@ -306,13 +306,13 @@ def fetch_scheduled_recipe(arg): # {{{ for opt in p.options: recs.append((opt.option.name, pdf.get(opt.option.name, opt.recommended_value), OptionRecommendation.HIGH)) - args = [arg['recipe'], pt.name, recs] + args = [arg['urn'], pt.name, recs] if arg['username'] is not None: recs.append(('username', arg['username'], OptionRecommendation.HIGH)) if arg['password'] is not None: recs.append(('password', arg['password'], OptionRecommendation.HIGH)) - return 'gui_convert', args, _('Fetch news from %s')%arg['title'], fmt.upper(), [pt] + return 'gui_convert_recipe', args, _('Fetch news from %s')%arg['title'], fmt.upper(), [pt] # }}} diff --git a/src/calibre/utils/ipc/worker.py b/src/calibre/utils/ipc/worker.py index 762e41321d..e48d94e860 100644 --- a/src/calibre/utils/ipc/worker.py +++ b/src/calibre/utils/ipc/worker.py @@ -35,6 +35,9 @@ PARALLEL_FUNCS = { 'gui_convert' : ('calibre.gui2.convert.gui_conversion', 'gui_convert', 'notification'), + 'gui_convert_recipe' : + ('calibre.gui2.convert.gui_conversion', 'gui_convert_recipe', 'notification'), + 'gui_polish' : ('calibre.ebooks.oeb.polish.main', 'gui_polish', None),