From 6782fde633e60c32faab79529b71035925e6b697 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Apr 2017 10:16:57 +0530 Subject: [PATCH] Port calibredb add_format --- src/calibre/db/cli/cmd_add_format.py | 57 ++++++++++++++++++++++++++++ src/calibre/srv/changes.py | 4 ++ 2 files changed, 61 insertions(+) create mode 100644 src/calibre/db/cli/cmd_add_format.py diff --git a/src/calibre/db/cli/cmd_add_format.py b/src/calibre/db/cli/cmd_add_format.py new file mode 100644 index 0000000000..a1deaba117 --- /dev/null +++ b/src/calibre/db/cli/cmd_add_format.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2017, Kovid Goyal + +from __future__ import absolute_import, division, print_function, unicode_literals + +import os +from io import BytesIO + +from calibre.srv.changes import formats_removed + +readonly = False + + +def implementation(db, notify_changes, book_id, data, fmt, replace): + is_remote = notify_changes is not None + if is_remote: + data = BytesIO(data[1]) + added = db.add_format(book_id, fmt, data, replace=replace) + if is_remote and added: + notify_changes(formats_removed({book_id: fmt})) + return added + + +def option_parser(get_parser): + parser = get_parser( + _( + '''\ +%prog add_format [options] id ebook_file + +Add the e-book in ebook_file to the available formats for the logical book identified \ +by id. You can get id by using the search command. If the format already exists, \ +it is replaced, unless the do not replace option is specified.\ +''' + ) + ) + parser.add_option( + '--dont-replace', + dest='replace', + default=True, + action='store_false', + help=_('Do not replace the format if it already exists') + ) + return parser + + +def main(opts, args, dbctx): + if len(args) < 2: + raise SystemExit(_('You must specify an id and an e-book file')) + + id, path, fmt = int(args[0]), args[1], os.path.splitext(args[1])[-1] + if not fmt: + raise SystemExit(_('e-book file must have an extension')) + fmt = fmt[1:].upper() + if not dbctx.run('add_format', id, dbctx.path(path), fmt, opts.replace): + raise SystemExit(_('A %(fmt)s file already exists for book: %(id)d, not replacing')%dict(fmt=fmt, id=id)) + return 0 diff --git a/src/calibre/srv/changes.py b/src/calibre/srv/changes.py index f06e9db3e7..03c9b33746 100644 --- a/src/calibre/srv/changes.py +++ b/src/calibre/srv/changes.py @@ -13,5 +13,9 @@ def formats_added(formats_map): pass +def formats_removed(formats_map): + pass + + def books_deleted(book_ids): pass