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:
Kovid Goyal 2013-10-31 09:40:12 +05:30
parent f959ec195e
commit 67e6a01fb2
4 changed files with 48 additions and 10 deletions

View File

@ -104,13 +104,46 @@ def option_recommendation_to_cli_option(add_option, rec):
def group_titles():
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):
input_options, output_options = \
plumber.input_options, plumber.output_options
def add_options(group, 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:
title = group_titles()[0]

View File

@ -34,8 +34,11 @@ class RecipeInput(InputFormatPlugin):
options = set([
OptionRecommendation(name='test', recommended_value=False,
help=_('Useful for recipe development. Forces '
'max_articles_per_feed to 2 and downloads at most 2 feeds.')),
help=_(
'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,
help=_('Username for sites that require a login to access '
'content.')),
@ -85,7 +88,7 @@ class RecipeInput(InputFormatPlugin):
self.recipe_source = raw
if recipe.requires_version > numeric_version:
log.warn(
'Downloaded recipe needs calibre version at least: %s' % \
'Downloaded recipe needs calibre version at least: %s' %
('.'.join(recipe.requires_version)))
builtin = True
except:

View File

@ -321,7 +321,7 @@ class JavascriptRecipe(BasicNewsRecipe):
if self.ignore_duplicate_articles is not None:
feeds = self.remove_duplicate_articles(feeds)
if self.test:
feeds = feeds[:2]
feeds = feeds[:self.test[0]]
self.has_single_feed = len(feeds) == 1
index = os.path.join(self.output_dir, 'index.html')

View File

@ -425,7 +425,7 @@ class BasicNewsRecipe(Recipe):
if not self.feeds:
raise NotImplementedError
if self.test:
return self.feeds[:2]
return self.feeds[:self.test[0]]
return self.feeds
@classmethod
@ -839,6 +839,8 @@ class BasicNewsRecipe(Recipe):
self.output_dir = os.path.abspath(os.getcwdu())
self.verbose = options.verbose
self.test = options.test
if self.test and not isinstance(self.test, tuple):
self.test = (2, 2)
self.username = options.username
self.password = options.password
self.lrf = options.lrf
@ -847,8 +849,8 @@ class BasicNewsRecipe(Recipe):
if self.touchscreen:
self.template_css += self.output_profile.touchscreen_news_css
if options.test:
self.max_articles_per_feed = 2
if self.test:
self.max_articles_per_feed = self.test[1]
self.simultaneous_downloads = min(4, self.simultaneous_downloads)
if self.debug:
@ -1163,7 +1165,7 @@ class BasicNewsRecipe(Recipe):
self.resolve_masthead()
if self.test:
feeds = feeds[:2]
feeds = feeds[:self.test[0]]
self.has_single_feed = len(feeds) == 1
index = os.path.join(self.output_dir, 'index.html')