From 2e4100e8dde1e65189fd72f34253ea87c39734ed Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 May 2017 23:38:52 +0530 Subject: [PATCH] Port calibredb catalog --- src/calibre/db/cli/cmd_catalog.py | 122 ++++++++++++++++++++++++++++-- src/calibre/db/cli/main.py | 2 +- src/calibre/srv/cdb.py | 2 + 3 files changed, 118 insertions(+), 8 deletions(-) diff --git a/src/calibre/db/cli/cmd_catalog.py b/src/calibre/db/cli/cmd_catalog.py index 2f6f22ae56..62d2b8606c 100644 --- a/src/calibre/db/cli/cmd_catalog.py +++ b/src/calibre/db/cli/cmd_catalog.py @@ -4,19 +4,127 @@ from __future__ import absolute_import, division, print_function, unicode_literals -readonly = False +import os + +from calibre.customize.ui import available_catalog_formats, plugin_for_catalog_format +from calibre.db.cli import integers_from_string + +readonly = True version = 0 # change this if you change signature of implementation() +needs_srv_ctx = True -def implementation(db, notify_changes, *args): - is_remote = notify_changes is not None - is_remote +def implementation(db, notify_changes, ctx): + raise NotImplementedError() -def option_parser(get_parser, args): - pass +def option_parser(get_parser, args): # {{{ + + def add_plugin_parser_options(fmt, parser): + # Fetch the extension-specific CLI options from the plugin + # library.catalogs..py + plugin = plugin_for_catalog_format(fmt) + for option in plugin.cli_options: + if option.action: + parser.add_option( + option.option, + default=option.default, + dest=option.dest, + action=option.action, + help=option.help + ) + else: + parser.add_option( + option.option, + default=option.default, + dest=option.dest, + help=option.help + ) + + # Entry point + parser = get_parser( + _( + '''\ +%prog catalog /path/to/destination.(csv|epub|mobi|xml...) [options] + +Export a catalog in format specified by path/to/destination extension. +Options control how entries are displayed in the generated catalog output. +''' + ) + ) + + # Add options common to all catalog plugins + parser.add_option( + '-i', + '--ids', + default=None, + dest='ids', + help=_( + "Comma-separated list of database IDs to catalog.\n" + "If declared, --search is ignored.\n" + "Default: all" + ) + ) + parser.add_option( + '-s', + '--search', + default=None, + dest='search_text', + help=_( + "Filter the results by the search query. " + "For the format of the search query, please see " + "the search-related documentation in the User Manual.\n" + "Default: no filtering" + ) + ) + parser.add_option( + '-v', + '--verbose', + default=False, + action='store_true', + dest='verbose', + help=_('Show detailed output information. Useful for debugging') + ) + fmt = 'epub' + if args and '.' in args[0]: + fmt = args[0].rpartition('.')[-1].lower() + if fmt not in available_catalog_formats(): + fmt = 'epub' + + # Add options specific to fmt plugin + add_plugin_parser_options(fmt, parser) + + return parser + + +# }}} def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + if len(args) < 1: + raise SystemExit(_('You must specify a catalog output file')) + if dbctx.is_remote: + raise SystemExit(_('Generating catalogs from server libraries is not supported')) + if opts.ids: + opts.ids = list(integers_from_string(opts.ids)) + fmt = args[0].rpartition('.')[-1] + if fmt not in available_catalog_formats(): + raise SystemExit( + _('Cannot generate a catalog in the {} format').format(fmt.upper()) + ) + + # No support for connected device in CLI environment + # Parallel initialization in calibre.gui2.tools:generate_catalog() + opts.connected_device = { + 'is_device_connected': False, + 'kind': None, + 'name': None, + 'save_template': None, + 'serial': None, + 'storage': None, + } + dest = os.path.abspath(os.path.expanduser(args[0])) + plugin = plugin_for_catalog_format(fmt) + with plugin: + plugin.run(dest, opts, dbctx.db) return 0 diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index ce5244e316..2efa0fbccb 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -239,7 +239,7 @@ def main(args=sys.argv): parser.print_help() return 1 del args[i] - parser = option_parser_for(cmd, args)() + parser = option_parser_for(cmd, args[1:])() opts, args = parser.parse_args(args) return run_cmd(cmd, opts, args[1:], DBCtx(opts)) diff --git a/src/calibre/srv/cdb.py b/src/calibre/srv/cdb.py index b847d56970..2a97f200b5 100644 --- a/src/calibre/srv/cdb.py +++ b/src/calibre/srv/cdb.py @@ -36,6 +36,8 @@ def cdb_run(ctx, rd, which, version): except Exception: raise HTTPBadRequest('args are not valid encoded data') db = get_library_data(ctx, rd, strict_library_id=True)[0] + if getattr(m, 'needs_srv_ctx', False): + args = [ctx] + list(args) try: result = m.implementation(db, ctx.notify_changes, *args) except Exception as err: