From 67e6a01fb2bdf71f291c61e3bc9facd6f9263f0d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 31 Oct 2013 09:40:12 +0530 Subject: [PATCH] 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). --- src/calibre/ebooks/conversion/cli.py | 37 ++++++++++++++++++- .../ebooks/conversion/plugins/recipe_input.py | 9 +++-- src/calibre/web/feeds/jsnews.py | 2 +- src/calibre/web/feeds/news.py | 10 +++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index 3d079b9181..e19df1cc91 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -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] diff --git a/src/calibre/ebooks/conversion/plugins/recipe_input.py b/src/calibre/ebooks/conversion/plugins/recipe_input.py index 2630ff0c85..1f7e35f10b 100644 --- a/src/calibre/ebooks/conversion/plugins/recipe_input.py +++ b/src/calibre/ebooks/conversion/plugins/recipe_input.py @@ -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: diff --git a/src/calibre/web/feeds/jsnews.py b/src/calibre/web/feeds/jsnews.py index df16e84ace..bb8d891d06 100644 --- a/src/calibre/web/feeds/jsnews.py +++ b/src/calibre/web/feeds/jsnews.py @@ -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') diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index fcf346f6ed..5d09071423 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -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')