From f99ae299f535d56bcca9ee671a69659772221574 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 May 2017 13:59:32 +0530 Subject: [PATCH] Port calibredb embed_metadata --- src/calibre/db/cli/cmd_embed_metadata.py | 49 +++++++++++++++++++++--- src/calibre/db/cli/main.py | 4 +- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/calibre/db/cli/cmd_embed_metadata.py b/src/calibre/db/cli/cmd_embed_metadata.py index 2f6f22ae56..37b20c97e0 100644 --- a/src/calibre/db/cli/cmd_embed_metadata.py +++ b/src/calibre/db/cli/cmd_embed_metadata.py @@ -4,19 +4,58 @@ from __future__ import absolute_import, division, print_function, unicode_literals +from calibre import prints +from calibre.db.cli import integers_from_string +from calibre.srv.changes import formats_added + readonly = False version = 0 # change this if you change signature of implementation() -def implementation(db, notify_changes, *args): - is_remote = notify_changes is not None - is_remote +def implementation(db, notify_changes, book_id, only_fmts): + if book_id is None: + return db.all_book_ids() + with db.write_lock: + if db.has_id(book_id): + db.embed_metadata((book_id,), only_fmts=only_fmts) + formats_added({book_id: db.formats(book_id)}) + return db.field_for('title', book_id) def option_parser(get_parser, args): - pass + parser = get_parser(_( +''' +%prog embed_metadata [options] book_id + +Update the metadata in the actual book files stored in the calibre library from +the metadata in the calibre database. Normally, metadata is updated only when +exporting files from calibre, this command is useful if you want the files to +be updated in place. Note that different file formats support different amounts +of metadata. You can use the special value 'all' for book_id to update metadata +in all books. You can also specify many book ids separated by spaces and id ranges +separated by hyphens. For example: %prog embed_metadata 1 2 10-15 23''')) + parser.add_option('-f', '--only-formats', action='append', default=[], help=_( + 'Only update metadata in files of the specified format. Specify it multiple' + ' times for multiple formats. By default, all formats are updated.')) + return parser def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + ids = set() + for arg in args: + if arg == 'all': + ids = None + break + ids |= set(integers_from_string(arg)) + only_fmts = opts.only_formats or None + if ids is None: + ids = dbctx.run('embed_metadata', None, None) + + def progress(i, title): + prints(_('Processed {0} ({1} of {2})').format(title, i, len(ids))) + + for i, book_id in enumerate(ids): + title = dbctx.run('embed_metadata', book_id, only_fmts) + progress(i+1, title or _('No book with id: {}').format(book_id)) + return 0 diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index 75bc727f59..40b368cdc4 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -22,8 +22,8 @@ COMMANDS = ( 'list', 'add', 'remove', 'add_format', 'remove_format', 'show_metadata', 'set_metadata', 'export', 'catalog', 'saved_searches', 'add_custom_column', 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database', - 'check_library', 'list_categories', 'backup_metadata', 'clone', - # 'embed_metadata', 'search' + 'check_library', 'list_categories', 'backup_metadata', 'clone', 'embed_metadata', + # 'search' )