From a0e0ae456abcba76fc6117e383647e4ff328d806 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Apr 2017 13:53:30 +0530 Subject: [PATCH] Port calibredb show_metadata --- src/calibre/db/cli/cmd_show_metadata.py | 47 +++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/cli/cmd_show_metadata.py b/src/calibre/db/cli/cmd_show_metadata.py index 81b38b65b3..cffbb5eb58 100644 --- a/src/calibre/db/cli/cmd_show_metadata.py +++ b/src/calibre/db/cli/cmd_show_metadata.py @@ -4,19 +4,54 @@ from __future__ import absolute_import, division, print_function, unicode_literals -readonly = False +import os +import sys + +from calibre import prints +from calibre.ebooks.metadata.opf2 import OPFCreator + +readonly = True 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): + with db.safe_read_lock: + if not db.has_id(book_id): + return + return db.get_metadata(book_id) def option_parser(get_parser): - pass + parser = get_parser( + _( + ''' +%prog show_metadata [options] id + +Show the metadata stored in the calibre database for the book identified by id. +id is an id number from the search command. +''' + ) + ) + parser.add_option( + '--as-opf', + default=False, + action='store_true', + help=_('Print metadata in OPF form (XML)') + ) + return parser def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + if len(args) < 1: + raise SystemExit(_('You must specify an id')) + book_id = int(args[0]) + mi = dbctx.run('show_metadata', book_id) + if mi is None: + raise SystemExit('Id #%d is not present in database.' % id) + if opts.as_opf: + mi = OPFCreator(os.getcwdu(), mi) + mi.render(sys.stdout) + else: + prints(unicode(mi)) + return 0