diff --git a/src/calibre/ebooks/conversion/plugins/epub_input.py b/src/calibre/ebooks/conversion/plugins/epub_input.py index 8089cc3443..e83d0e902f 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_input.py +++ b/src/calibre/ebooks/conversion/plugins/epub_input.py @@ -284,13 +284,20 @@ class EPUBInput(InputFormatPlugin): if opf is None: raise ValueError(f'{path} is not a valid EPUB file (could not find opf)') - if is_kepub and not self.for_viewer: - log('Removing Kobo markup...') + if is_kepub: from calibre.ebooks.oeb.polish.container import Container - from calibre.ebooks.oeb.polish.kepubify import unkepubify_container + from calibre.ebooks.oeb.polish.errors import drm_message + from calibre.ebooks.oeb.polish.kepubify import check_for_kobo_drm, unkepubify_container container = Container(os.getcwd(), opf, log) - unkepubify_container(container) - container.commit() + if self.for_viewer: + log('Checking for Kobo DRM...') + with drm_message(_('The file {} is locked with DRM. It cannot be viewed').format(path)): + check_for_kobo_drm(container) + else: + log('Removing Kobo markup...') + with drm_message(_('The file {} is locked with DRM. It cannot be converted').format(path)): + unkepubify_container(container) + container.commit() del container opf = os.path.relpath(opf, os.getcwd()) diff --git a/src/calibre/ebooks/oeb/polish/errors.py b/src/calibre/ebooks/oeb/polish/errors.py index 9878d1e1e9..b224d2de3b 100644 --- a/src/calibre/ebooks/oeb/polish/errors.py +++ b/src/calibre/ebooks/oeb/polish/errors.py @@ -5,6 +5,8 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' __docformat__ = 'restructuredtext en' +from contextlib import contextmanager + from calibre.ebooks import DRMError as _DRMError @@ -12,10 +14,23 @@ class InvalidBook(ValueError): pass +_drm_message = '' + + +@contextmanager +def drm_message(msg: str) -> None: + global _drm_message + orig, _drm_message = _drm_message, msg + try: + yield + finally: + _drm_message = orig + + class DRMError(_DRMError): def __init__(self): - super().__init__(_('This file is locked with DRM. It cannot be edited.')) + super().__init__(_drm_message or _('This file is locked with DRM. It cannot be edited.')) class MalformedMarkup(ValueError): diff --git a/src/calibre/ebooks/oeb/polish/kepubify.py b/src/calibre/ebooks/oeb/polish/kepubify.py index 7b0551d81f..b9239739aa 100644 --- a/src/calibre/ebooks/oeb/polish/kepubify.py +++ b/src/calibre/ebooks/oeb/polish/kepubify.py @@ -473,6 +473,7 @@ def remove_kobo_files(container): def unkepubify_container(container: Container, max_workers: int = 0) -> None: + check_for_kobo_drm(container) remove_dummy_cover_image(container) remove_dummy_title_page(container) remove_kobo_files(container) @@ -537,7 +538,6 @@ def check_for_kobo_drm(container: Container) -> None: def unkepubify_path(path, outpath='', max_workers=0, allow_overwrite=False): container = get_container(path, tweak_mode=True, ebook_cls=EpubContainer) - check_for_kobo_drm(container) unkepubify_container(container, max_workers) base, ext = os.path.splitext(path) outpath = outpath or base + '.epub'