def ebook_edit_format(self, book_id, fmt): ''' Also called from edit_metadata formats list. In that context, SUPPORTED check was already done. ''' db = self.gui.library_view.model().db from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process path = db.new_api.format_abspath(book_id, fmt) if path is None: return error_dialog( self.gui, _('File missing'), _('The %s format is missing from the calibre library. You should run' ' library maintenance.') % fmt, show=True) tweak = 'ebook-edit' try: self.gui.setCursor(Qt.BusyCursor) if tprefs['update_metadata_from_calibre']: db.new_api.embed_metadata((book_id, ), only_fmts={fmt}) notify = '%d:%s:%s:%s' % (book_id, fmt, db.library_id, db.library_path) self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(path=path, notify=notify)) time.sleep(2) finally: self.gui.unsetCursor()
def do_tweak(self, book_id): if self.gui.current_view() is not self.gui.library_view: return error_dialog(self.gui, _('Cannot edit book'), _( 'Editing of books on the device is not supported'), show=True) from calibre.ebooks.oeb.polish.main import SUPPORTED db = self.gui.library_view.model().db fmts = db.formats(book_id, index_is_id=True) or '' fmts = [x.upper().strip() for x in fmts.split(',')] tweakable_fmts = set(fmts).intersection(SUPPORTED) if not tweakable_fmts: return error_dialog(self.gui, _('Cannot edit book'), _('The book must be in the %s formats to edit.' '\n\nFirst convert the book to one of these formats.') % (_(' or ').join(SUPPORTED)), show=True) from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process if len(tweakable_fmts) > 1: if tprefs['choose_tweak_fmt']: d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) if d.exec_() != d.Accepted: return tweakable_fmts = {d.fmt} else: fmts = [f for f in tprefs['tweak_fmt_order'] if f in tweakable_fmts] if not fmts: fmts = [f for f in tprefs.defaults['tweak_fmt_order'] if f in tweakable_fmts] tweakable_fmts = {fmts[0]} fmt = tuple(tweakable_fmts)[0] self.ebook_edit_format(book_id, fmt)
def do_tweak(self, book_id): if self.gui.current_view() is not self.gui.library_view: return error_dialog( self.gui, _('Cannot edit book'), _('Editing of books on the device is not supported'), show=True) from calibre.ebooks.oeb.polish.main import SUPPORTED db = self.gui.library_view.model().db fmts = db.formats(book_id, index_is_id=True) or '' fmts = [x.upper().strip() for x in fmts.split(',') if x] tweakable_fmts = set(fmts).intersection(SUPPORTED) if not tweakable_fmts: if not fmts: if not question_dialog( self.gui, _('No editable formats'), _('Do you want to create an empty EPUB file to edit?') ): return tweakable_fmts = {'EPUB'} self.gui.iactions['Add Books'].add_empty_format_to_book( book_id, 'EPUB') current_idx = self.gui.library_view.currentIndex() if current_idx.isValid(): self.gui.library_view.model().current_changed( current_idx, current_idx) else: return error_dialog( self.gui, _('Cannot edit book'), _('The book must be in the %s formats to edit.' '\n\nFirst convert the book to one of these formats.') % (_(' or ').join(SUPPORTED)), show=True) from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process if len(tweakable_fmts) > 1: if tprefs['choose_tweak_fmt']: d = Choose( sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) if d.exec_() != d.Accepted: return tweakable_fmts = {d.fmt} else: fmts = [ f for f in tprefs['tweak_fmt_order'] if f in tweakable_fmts ] if not fmts: fmts = [ f for f in tprefs.defaults['tweak_fmt_order'] if f in tweakable_fmts ] tweakable_fmts = {fmts[0]} fmt = tuple(tweakable_fmts)[0] self.ebook_edit_format(book_id, fmt)
def do_tweak(self, book_id): if self.gui.current_view() is not self.gui.library_view: return error_dialog(self.gui, _('Cannot Edit Book'), _( 'Editing of books on the device is not supported'), show=True) from calibre.ebooks.oeb.polish.main import SUPPORTED db = self.gui.library_view.model().db fmts = db.formats(book_id, index_is_id=True) or '' fmts = [x.upper().strip() for x in fmts.split(',')] tweakable_fmts = set(fmts).intersection(SUPPORTED) if not tweakable_fmts: return error_dialog(self.gui, _('Cannot Edit Book'), _('The book must be in the %s formats to edit.' '\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))), show=True) from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process if len(tweakable_fmts) > 1: if tprefs['choose_tweak_fmt']: d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) if d.exec_() != d.Accepted: return tweakable_fmts = {d.fmt} else: fmts = [f for f in tprefs['tweak_fmt_order'] if f in tweakable_fmts] if not fmts: fmts = [f for f in tprefs.defaults['tweak_fmt_order'] if f in tweakable_fmts] tweakable_fmts = {fmts[0]} fmt = tuple(tweakable_fmts)[0] path = db.new_api.format_abspath(book_id, fmt) if path is None: return error_dialog(self.gui, _('File missing'), _( 'The %s format is missing from the calibre library. You should run' ' library maintenance.') % fmt, show=True) tweak = 'ebook-edit' self.gui.setCursor(Qt.BusyCursor) if tprefs['update_metadata_from_calibre']: from calibre.ebooks.metadata.opf2 import pretty_print from calibre.ebooks.metadata.meta import set_metadata from calibre.customize.ui import apply_null_metadata mi = db.new_api.get_metadata(book_id, get_cover=True) with pretty_print, apply_null_metadata, open(path, 'r+b') as f: set_metadata(f, mi, stream_type=fmt.lower()) notify = '%d:%s:%s:%s' % (book_id, fmt, db.library_id, db.library_path) try: self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(path=path, notify=notify)) time.sleep(2) finally: self.gui.unsetCursor()
def do_tweak(self, book_id): if self.gui.current_view() is not self.gui.library_view: return error_dialog(self.gui, _('Cannot Edit Book'), _( 'Editing of books on the device is not supported'), show=True) from calibre.ebooks.oeb.polish.main import SUPPORTED db = self.gui.library_view.model().db fmts = db.formats(book_id, index_is_id=True) or '' fmts = [x.upper().strip() for x in fmts.split(',')] tweakable_fmts = set(fmts).intersection(SUPPORTED) if not tweakable_fmts: return error_dialog(self.gui, _('Cannot Edit Book'), _('The book must be in the %s formats to edit.' '\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))), show=True) from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process if len(tweakable_fmts) > 1: if tprefs['choose_tweak_fmt']: d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) if d.exec_() != d.Accepted: return tweakable_fmts = {d.fmt} else: fmts = [f for f in tprefs['tweak_fmt_order'] if f in tweakable_fmts] if not fmts: fmts = [f for f in tprefs.defaults['tweak_fmt_order'] if f in tweakable_fmts] tweakable_fmts = {fmts[0]} fmt = tuple(tweakable_fmts)[0] path = db.new_api.format_abspath(book_id, fmt) if path is None: return error_dialog(self.gui, _('File missing'), _( 'The %s format is missing from the calibre library. You should run' ' library maintenance.') % fmt, show=True) tweak = 'ebook-edit' try: self.gui.setCursor(Qt.BusyCursor) if tprefs['update_metadata_from_calibre']: db.new_api.embed_metadata((book_id,), only_fmts={fmt}) notify = '%d:%s:%s:%s' % (book_id, fmt, db.library_id, db.library_path) self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(path=path, notify=notify)) time.sleep(2) finally: self.gui.unsetCursor()
def ebook_edit_format(self, book_id, fmt): ''' Also called from edit_metadata formats list. In that context, SUPPORTED check was already done. ''' db = self.gui.library_view.model().db from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process path = db.new_api.format_abspath(book_id, fmt) if path is None: return error_dialog(self.gui, _('File missing'), _( 'The %s format is missing from the calibre library. You should run' ' library maintenance.') % fmt, show=True) tweak = 'ebook-edit' try: self.gui.setCursor(Qt.BusyCursor) if tprefs['update_metadata_from_calibre']: db.new_api.embed_metadata((book_id,), only_fmts={fmt}) notify = '%d:%s:%s:%s' % (book_id, fmt, db.library_id, db.library_path) self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(path=path, notify=notify)) time.sleep(2) finally: self.gui.unsetCursor()