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
This commit is contained in:
Kovid Goyal 2018-09-11 12:16:09 +05:30
parent f91b9c8e51
commit 57f3db82e0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 31 additions and 11 deletions

View File

@ -69,7 +69,23 @@ class RecipeInput(InputFormatPlugin):
recipe.needs_subscription = False recipe.needs_subscription = False
self.recipe_object = recipe(opts, log, self.report_progress) self.recipe_object = recipe(opts, log, self.report_progress)
else: 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() self.recipe_source = open(recipe_or_file, 'rb').read()
recipe = compile_recipe(self.recipe_source) recipe = compile_recipe(self.recipe_source)
log('Using custom recipe') log('Using custom recipe')

View File

@ -4,6 +4,7 @@ __license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>' __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os
from optparse import OptionParser from optparse import OptionParser
from calibre.customize.conversion import OptionRecommendation, DummyReporter from calibre.customize.conversion import OptionRecommendation, DummyReporter
@ -26,6 +27,14 @@ def gui_convert(input, output, recommendations, notification=DummyReporter(),
plumber.run() 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(), def gui_convert_override(input, output, recommendations, notification=DummyReporter(),
abort_after_input_dump=False, log=None): abort_after_input_dump=False, log=None):
gui_convert(input, output, recommendations, notification=notification, 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 # Returns 0 if successful, 1 if no catalog built
plugin = plugin_for_catalog_format(fmt) plugin = plugin_for_catalog_format(fmt)
return plugin.run(out_file_name, opts, db, notification=notification) return plugin.run(out_file_name, opts, db, notification=notification)

View File

@ -22,7 +22,6 @@ from PyQt5.Qt import (
from calibre.gui2 import config as gconf, error_dialog, gprefs from calibre.gui2 import config as gconf, error_dialog, gprefs
from calibre.gui2.search_box import SearchBox2 from calibre.gui2.search_box import SearchBox2
from calibre.web.feeds.recipes.model import RecipeModel from calibre.web.feeds.recipes.model import RecipeModel
from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.date import utcnow from calibre.utils.date import utcnow
from calibre.utils.network import internet_connected from calibre.utils.network import internet_connected
from calibre import force_unicode from calibre import force_unicode
@ -654,16 +653,11 @@ class Scheduler(QObject):
if account_info is not None: if account_info is not None:
un, pw = account_info un, pw = account_info
add_title_tag, custom_tags, keep_issues = customize_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 = { arg = {
'username': un, 'username': un,
'password': pw, 'password': pw,
'add_title_tag':add_title_tag, 'add_title_tag':add_title_tag,
'custom_tags':custom_tags, 'custom_tags':custom_tags,
'recipe':pt.name,
'title':recipe.get('title',''), 'title':recipe.get('title',''),
'urn':urn, 'urn':urn,
'keep_issues':keep_issues 'keep_issues':keep_issues

View File

@ -306,13 +306,13 @@ def fetch_scheduled_recipe(arg): # {{{
for opt in p.options: for opt in p.options:
recs.append((opt.option.name, pdf.get(opt.option.name, opt.recommended_value), OptionRecommendation.HIGH)) 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: if arg['username'] is not None:
recs.append(('username', arg['username'], OptionRecommendation.HIGH)) recs.append(('username', arg['username'], OptionRecommendation.HIGH))
if arg['password'] is not None: if arg['password'] is not None:
recs.append(('password', arg['password'], OptionRecommendation.HIGH)) 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]
# }}} # }}}

View File

@ -35,6 +35,9 @@ PARALLEL_FUNCS = {
'gui_convert' : 'gui_convert' :
('calibre.gui2.convert.gui_conversion', 'gui_convert', 'notification'), ('calibre.gui2.convert.gui_conversion', 'gui_convert', 'notification'),
'gui_convert_recipe' :
('calibre.gui2.convert.gui_conversion', 'gui_convert_recipe', 'notification'),
'gui_polish' : 'gui_polish' :
('calibre.ebooks.oeb.polish.main', 'gui_polish', None), ('calibre.ebooks.oeb.polish.main', 'gui_polish', None),