From 4e67c9bf59b6f44e257ccbc0ade6f63fd4f9a2a0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 8 Mar 2013 15:19:07 +0530 Subject: [PATCH] Allow running plugins from the cli with calibre-debug easily --- src/calibre/customize/__init__.py | 8 ++++++++ src/calibre/debug.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/calibre/customize/__init__.py b/src/calibre/customize/__init__.py index 98fa6fdda4..dfead858cd 100644 --- a/src/calibre/customize/__init__.py +++ b/src/calibre/customize/__init__.py @@ -293,6 +293,14 @@ class Plugin(object): # {{{ if hasattr(it, '__exit__'): it.__exit__(*args) + def cli_main(self, args): + ''' + This method is the main entry point for your plugins command line + interface. It is called when the user does: calibre-debug + ''' + raise NotImplementedError('The %s plugin has no command line interface' + %self.name) + # }}} class FileTypePlugin(Plugin): # {{{ diff --git a/src/calibre/debug.py b/src/calibre/debug.py index 741b0adf1a..6ccaf750e2 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -75,9 +75,12 @@ Everything after the -- is passed to the script. help=_('Cause a running calibre instance, if any, to be' ' shutdown. Note that if there are running jobs, they ' 'will be silently aborted, so use with care.')) - parser.add_option('--test-build', help='Test binary modules in build', action='store_true', default=False) + parser.add_option('-r', '--run-plugin', help=_( + 'Run a plugin that provides a command line interface. For example:\n' + 'calibre-debug -r "Add Books" -- file1 --option1\n' + 'Everything after the -- will be passed to the plugin as arguments.')) return parser @@ -262,6 +265,13 @@ def main(args=sys.argv): main(['subset-font']+[opts.subset_font]+args[1:]) elif opts.exec_file: run_script(opts.exec_file, args[1:]) + elif opts.run_plugin: + from calibre.customize.ui import find_plugin + plugin = find_plugin(opts.run_plugin) + if plugin is None: + prints(_('No plugin named %s found')%opts.run_plugin) + raise SystemExit(1) + plugin.cli_main([plugin.name] + args[1:]) elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}: run_script(args[1], args[2:]) else: