From f0155f4b2a8fbdc4ea082ba60e231acfc22a1d08 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 May 2017 08:41:18 +0530 Subject: [PATCH] Port calibredb saved_searches --- src/calibre/db/cli/cmd_saved_searches.py | 64 ++++++++++++++++++++++-- src/calibre/db/cli/main.py | 3 +- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/cli/cmd_saved_searches.py b/src/calibre/db/cli/cmd_saved_searches.py index 2f6f22ae56..686ea16246 100644 --- a/src/calibre/db/cli/cmd_saved_searches.py +++ b/src/calibre/db/cli/cmd_saved_searches.py @@ -7,16 +7,70 @@ from __future__ import absolute_import, division, print_function, unicode_litera readonly = False version = 0 # change this if you change signature of implementation() +from calibre import prints -def implementation(db, notify_changes, *args): - is_remote = notify_changes is not None - is_remote + +def implementation(db, notify_changes, action, *args): + if action == 'list': + with db.safe_read_lock: + names = db.saved_search_names() + return {n: db.saved_search_lookup(n) for n in names} + if action == 'add': + name, val = args + db.saved_search_add(name, val) + return + if action == 'remove': + name = args[0] + db.saved_search_delete(name) + return def option_parser(get_parser, args): - pass + parser = get_parser( + _( + '''\ +%prog saved_searches [options] (list|add|remove) + +Manage the saved searches stored in this database. +If you try to add a query with a name that already exists, it will be +replaced. + +Syntax for adding: + +%prog saved_searches add search_name search_expression + +Syntax for removing: + +%prog saved_searches remove search_name + ''' + ) + ) + return parser def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + args = args or ['list'] + if args[0] == 'list': + for name, value in dbctx.run('saved_searches', 'list').iteritems(): + prints(_('Name:'), name) + prints(_('Search string:'), value) + print() + elif args[0] == 'add': + if len(args) < 3: + raise SystemExit(_('Error: You must specify a name and a search string')) + dbctx.run('saved_searches', 'add', args[1], args[2]) + prints(args[1], _('added')) + elif args[0] == 'remove': + if len(args) < 2: + raise SystemExit(_('Error: You must specify a name')) + dbctx.run('saved_searches', 'remove', args[1]) + prints(args[1], _('removed')) + else: + raise SystemExit( + _( + 'Error: Action %s not recognized, must be one ' + 'of: (add|remove|list)' + ) % args[0] + ) + return 0 diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index 3c2cc14270..035eac26d8 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -21,7 +21,8 @@ from calibre.utils.serialize import MSGPACK_MIME COMMANDS = ( 'list', 'add', 'remove', 'add_format', 'remove_format', 'show_metadata', 'set_metadata', 'export', 'catalog', - # 'saved_searches', 'add_custom_column', + 'saved_searches', + # 'add_custom_column', # 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database', # 'check_library', 'list_categories', 'backup_metadata', 'clone', 'embed_metadata', # 'search'