mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
ebook-convert .recipe: specify number of feeds and articles
When developing recipes using the command line, allow specifying the number of feeds and articles when using the --test parameter. Fixes #1246061 [[Enhancement] Make --test option to ebooks-convert take parameters specifying the number of feeds to download and number of items per feed to download](https://bugs.launchpad.net/calibre/+bug/1246061).
This commit is contained in:
parent
f959ec195e
commit
67e6a01fb2
@ -104,13 +104,46 @@ def option_recommendation_to_cli_option(add_option, rec):
|
|||||||
def group_titles():
|
def group_titles():
|
||||||
return _('INPUT OPTIONS'), _('OUTPUT OPTIONS')
|
return _('INPUT OPTIONS'), _('OUTPUT OPTIONS')
|
||||||
|
|
||||||
|
def recipe_test(option, opt_str, value, parser):
|
||||||
|
assert value is None
|
||||||
|
value = []
|
||||||
|
|
||||||
|
def floatable(str):
|
||||||
|
try:
|
||||||
|
float(str)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for arg in parser.rargs:
|
||||||
|
# stop on --foo like options
|
||||||
|
if arg[:2] == "--":
|
||||||
|
break
|
||||||
|
# stop on -a, but not on -3 or -3.0
|
||||||
|
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
value.append(int(arg))
|
||||||
|
except (TypeError, ValueError, AttributeError):
|
||||||
|
break
|
||||||
|
if len(value) == 2:
|
||||||
|
break
|
||||||
|
del parser.rargs[:len(value)]
|
||||||
|
|
||||||
|
while len(value) < 2:
|
||||||
|
value.append(2)
|
||||||
|
|
||||||
|
setattr(parser.values, option.dest, tuple(value))
|
||||||
|
|
||||||
def add_input_output_options(parser, plumber):
|
def add_input_output_options(parser, plumber):
|
||||||
input_options, output_options = \
|
input_options, output_options = \
|
||||||
plumber.input_options, plumber.output_options
|
plumber.input_options, plumber.output_options
|
||||||
|
|
||||||
def add_options(group, options):
|
def add_options(group, options):
|
||||||
for opt in options:
|
for opt in options:
|
||||||
option_recommendation_to_cli_option(group, opt)
|
if plumber.input_fmt == 'recipe' and opt.option.long_switch == 'test':
|
||||||
|
group(Option('--test', dest='test', action='callback', callback=recipe_test))
|
||||||
|
else:
|
||||||
|
option_recommendation_to_cli_option(group, opt)
|
||||||
|
|
||||||
if input_options:
|
if input_options:
|
||||||
title = group_titles()[0]
|
title = group_titles()[0]
|
||||||
|
@ -34,8 +34,11 @@ class RecipeInput(InputFormatPlugin):
|
|||||||
|
|
||||||
options = set([
|
options = set([
|
||||||
OptionRecommendation(name='test', recommended_value=False,
|
OptionRecommendation(name='test', recommended_value=False,
|
||||||
help=_('Useful for recipe development. Forces '
|
help=_(
|
||||||
'max_articles_per_feed to 2 and downloads at most 2 feeds.')),
|
'Useful for recipe development. Forces'
|
||||||
|
' max_articles_per_feed to 2 and downloads at most 2 feeds.'
|
||||||
|
' You can change the number of feeds and articles by supplying optional arguments.'
|
||||||
|
' For example: --test 3 1 will download at most 3 feeds and only 1 article per feed.')),
|
||||||
OptionRecommendation(name='username', recommended_value=None,
|
OptionRecommendation(name='username', recommended_value=None,
|
||||||
help=_('Username for sites that require a login to access '
|
help=_('Username for sites that require a login to access '
|
||||||
'content.')),
|
'content.')),
|
||||||
@ -85,7 +88,7 @@ class RecipeInput(InputFormatPlugin):
|
|||||||
self.recipe_source = raw
|
self.recipe_source = raw
|
||||||
if recipe.requires_version > numeric_version:
|
if recipe.requires_version > numeric_version:
|
||||||
log.warn(
|
log.warn(
|
||||||
'Downloaded recipe needs calibre version at least: %s' % \
|
'Downloaded recipe needs calibre version at least: %s' %
|
||||||
('.'.join(recipe.requires_version)))
|
('.'.join(recipe.requires_version)))
|
||||||
builtin = True
|
builtin = True
|
||||||
except:
|
except:
|
||||||
|
@ -321,7 +321,7 @@ class JavascriptRecipe(BasicNewsRecipe):
|
|||||||
if self.ignore_duplicate_articles is not None:
|
if self.ignore_duplicate_articles is not None:
|
||||||
feeds = self.remove_duplicate_articles(feeds)
|
feeds = self.remove_duplicate_articles(feeds)
|
||||||
if self.test:
|
if self.test:
|
||||||
feeds = feeds[:2]
|
feeds = feeds[:self.test[0]]
|
||||||
self.has_single_feed = len(feeds) == 1
|
self.has_single_feed = len(feeds) == 1
|
||||||
index = os.path.join(self.output_dir, 'index.html')
|
index = os.path.join(self.output_dir, 'index.html')
|
||||||
|
|
||||||
|
@ -425,7 +425,7 @@ class BasicNewsRecipe(Recipe):
|
|||||||
if not self.feeds:
|
if not self.feeds:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
if self.test:
|
if self.test:
|
||||||
return self.feeds[:2]
|
return self.feeds[:self.test[0]]
|
||||||
return self.feeds
|
return self.feeds
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -839,6 +839,8 @@ class BasicNewsRecipe(Recipe):
|
|||||||
self.output_dir = os.path.abspath(os.getcwdu())
|
self.output_dir = os.path.abspath(os.getcwdu())
|
||||||
self.verbose = options.verbose
|
self.verbose = options.verbose
|
||||||
self.test = options.test
|
self.test = options.test
|
||||||
|
if self.test and not isinstance(self.test, tuple):
|
||||||
|
self.test = (2, 2)
|
||||||
self.username = options.username
|
self.username = options.username
|
||||||
self.password = options.password
|
self.password = options.password
|
||||||
self.lrf = options.lrf
|
self.lrf = options.lrf
|
||||||
@ -847,8 +849,8 @@ class BasicNewsRecipe(Recipe):
|
|||||||
if self.touchscreen:
|
if self.touchscreen:
|
||||||
self.template_css += self.output_profile.touchscreen_news_css
|
self.template_css += self.output_profile.touchscreen_news_css
|
||||||
|
|
||||||
if options.test:
|
if self.test:
|
||||||
self.max_articles_per_feed = 2
|
self.max_articles_per_feed = self.test[1]
|
||||||
self.simultaneous_downloads = min(4, self.simultaneous_downloads)
|
self.simultaneous_downloads = min(4, self.simultaneous_downloads)
|
||||||
|
|
||||||
if self.debug:
|
if self.debug:
|
||||||
@ -1163,7 +1165,7 @@ class BasicNewsRecipe(Recipe):
|
|||||||
self.resolve_masthead()
|
self.resolve_masthead()
|
||||||
|
|
||||||
if self.test:
|
if self.test:
|
||||||
feeds = feeds[:2]
|
feeds = feeds[:self.test[0]]
|
||||||
self.has_single_feed = len(feeds) == 1
|
self.has_single_feed = len(feeds) == 1
|
||||||
|
|
||||||
index = os.path.join(self.output_dir, 'index.html')
|
index = os.path.join(self.output_dir, 'index.html')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user