Port calibredb saved_searches

This commit is contained in:
Kovid Goyal 2017-05-02 08:41:18 +05:30
parent 02aa7f1be0
commit f0155f4b2a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 61 additions and 6 deletions

View File

@ -7,16 +7,70 @@ from __future__ import absolute_import, division, print_function, unicode_litera
readonly = False readonly = False
version = 0 # change this if you change signature of implementation() 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 def implementation(db, notify_changes, action, *args):
is_remote 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): 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): 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 return 0

View File

@ -21,7 +21,8 @@ from calibre.utils.serialize import MSGPACK_MIME
COMMANDS = ( COMMANDS = (
'list', 'add', 'remove', 'add_format', 'remove_format', 'show_metadata', 'list', 'add', 'remove', 'add_format', 'remove_format', 'show_metadata',
'set_metadata', 'export', 'catalog', 'set_metadata', 'export', 'catalog',
# 'saved_searches', 'add_custom_column', 'saved_searches',
# 'add_custom_column',
# 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database', # 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database',
# 'check_library', 'list_categories', 'backup_metadata', 'clone', 'embed_metadata', # 'check_library', 'list_categories', 'backup_metadata', 'clone', 'embed_metadata',
# 'search' # 'search'