From 5081bb93f136fbe919085e0a3d26ae8505d13ea3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 27 Feb 2014 18:53:51 +0530 Subject: [PATCH] Allow passing importable file formats as arguments to ebook-edit --- src/calibre/ebooks/oeb/polish/import_book.py | 2 ++ src/calibre/gui2/tweak_book/boss.py | 7 ++++++- src/calibre/gui2/tweak_book/widgets.py | 10 +++++++--- src/calibre/linux.py | 3 ++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/import_book.py b/src/calibre/ebooks/oeb/polish/import_book.py index 7fa4fbf80c..5c014b89ad 100644 --- a/src/calibre/ebooks/oeb/polish/import_book.py +++ b/src/calibre/ebooks/oeb/polish/import_book.py @@ -15,6 +15,8 @@ from calibre.ebooks.epub import initialize_container from calibre.utils.logging import default_log +IMPORTABLE = {'htm', 'xhtml', 'html', 'xhtm', 'docx'} + def auto_fill_manifest(container): manifest_id_map = container.manifest_id_map manifest_name_map = {v:k for k, v in manifest_id_map.iteritems()} diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 6405d6bf90..685db9bd67 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -167,10 +167,12 @@ class Boss(QObject): create_book(d.mi, path, fmt=fmt) self.open_book(path=path) - def import_book(self): + def import_book(self, path=None): if not self._check_before_open(): return d = ImportForeign(self.gui) + if hasattr(path, 'rstrip'): + d.set_src(os.path.abspath(path)) if d.exec_() == d.Accepted: from calibre.ebooks.oeb.polish.import_book import import_book_as_epub src, dest = d.data @@ -192,6 +194,9 @@ class Boss(QObject): ext = path.rpartition('.')[-1].upper() if ext not in SUPPORTED: + from calibre.ebooks.oeb.polish.import_book import IMPORTABLE + if ext.lower() in IMPORTABLE: + return self.import_book(path) return error_dialog(self.gui, _('Unsupported format'), _('Tweaking is only supported for books in the %s formats.' ' Convert your book to one of these formats first.') % _(' and ').join(sorted(SUPPORTED)), diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index 084e7dac02..c0344b178f 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -189,11 +189,15 @@ class ImportForeign(Dialog): # {{{ l.addRow(self.bb) def choose_source(self): + from calibre.ebooks.oeb.polish.import_book import IMPORTABLE path = choose_files(self, 'edit-book-choose-file-to-import', _('Choose file'), filters=[ - (_('HTML or DOCX files'), ['htm', 'html', 'xhtml', 'xhtm', 'docx'])], select_only_single_file=True) + (_('Importable files'), list(IMPORTABLE))], select_only_single_file=True) if path: - self.src.setText(path[0]) - self.dest.setText(self.data[1]) + self.set_src(path[0]) + + def set_src(self, path): + self.src.setText(path) + self.dest.setText(self.data[1]) def choose_destination(self): path = choose_save_file(self, 'edit-book-destination-for-generated-epub', _('Choose destination'), filters=[ diff --git a/src/calibre/linux.py b/src/calibre/linux.py index 58e614e477..c746606bee 100644 --- a/src/calibre/linux.py +++ b/src/calibre/linux.py @@ -549,11 +549,12 @@ class PostInstall: from calibre.utils.smtp import option_parser as smtp_op from calibre.library.server.main import option_parser as serv_op from calibre.ebooks.oeb.polish.main import option_parser as polish_op, SUPPORTED + from calibre.ebooks.oeb.polish.import_book import IMPORTABLE from calibre.debug import option_parser as debug_op from calibre.ebooks import BOOK_EXTENSIONS from calibre.customize.ui import available_input_formats input_formats = sorted(all_input_formats()) - tweak_formats = sorted(x.lower() for x in SUPPORTED) + tweak_formats = sorted(x.lower() for x in SUPPORTED|IMPORTABLE) zsh = ZshCompleter(self.opts) bc = os.path.join(os.path.dirname(self.opts.staging_sharedir), 'bash-completion')