Update metadata in the file from calibre db when opening for Tweaking

This commit is contained in:
Kovid Goyal 2013-12-04 15:21:45 +05:30
parent 756be9b409
commit d8dc718abf
4 changed files with 37 additions and 1 deletions

View File

@ -24,6 +24,23 @@ from calibre import prints, guess_type
from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars
from calibre.utils.config import tweaks
pretty_print_opf = False
class PrettyPrint(object):
def __enter__(self):
global pretty_print_opf
pretty_print_opf = True
def __exit__(self, *args):
global pretty_print_opf
pretty_print_opf = False
pretty_print = PrettyPrint()
def _pretty_print(root):
from calibre.ebooks.oeb.polish.pretty import pretty_opf, pretty_xml_tree
pretty_opf(root)
pretty_xml_tree(root)
class Resource(object): # {{{
'''
Represents a resource (usually a file on the filesystem or a URL pointing
@ -1210,6 +1227,8 @@ class OPF(object): # {{{
a['content'] = c
self.write_user_metadata()
if pretty_print_opf:
_pretty_print(self.root)
raw = etree.tostring(self.root, encoding=encoding, pretty_print=True)
if not raw.lstrip().startswith('<?xml '):
raw = '<?xml version="1.0" encoding="%s"?>\n'%encoding.upper()+raw
@ -1562,6 +1581,9 @@ def metadata_to_opf(mi, as_string=True, default_lang=None):
attrib={'type':'cover', 'title':_('Cover'), 'href':mi.cover})
r.tail = '\n' +(' '*4)
guide.append(r)
if pretty_print_opf:
_pretty_print(root)
return etree.tostring(root, pretty_print=True, encoding='utf-8',
xml_declaration=True) if as_string else root

View File

@ -110,8 +110,8 @@ class TweakEpubAction(InterfaceAction):
_('The book must be in the %s formats to tweak.'
'\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))),
show=True)
if len(tweakable_fmts) > 1:
from calibre.gui2.tweak_book import tprefs
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:
@ -131,6 +131,12 @@ class TweakEpubAction(InterfaceAction):
' library maintenance.') % fmt, show=True)
tweak = 'ebook-tweak'
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
mi = db.new_api.get_metadata(book_id)
with pretty_print, open(path, 'r+b') as f:
set_metadata(f, mi, stream_type=fmt.lower())
try:
self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(args=[tweak, path]))
time.sleep(2)

View File

@ -17,6 +17,7 @@ tprefs.defaults['editor_tab_stop_width'] = 2
tprefs.defaults['preview_refresh_time'] = 2
tprefs.defaults['choose_tweak_fmt'] = True
tprefs.defaults['tweak_fmt_order'] = ['EPUB', 'AZW3']
tprefs.defaults['update_metadata_from_calibre'] = True
_current_container = None

View File

@ -178,6 +178,13 @@ class IntegrationSettings(BasicSettings):
self.l = l = QFormLayout(self)
self.setLayout(l)
um = self('update_metadata_from_calibre')
um.setText(_('Update metadata embedded in the book when opening'))
um.setToolTip('<p>' + _(
'When the file is opened, update the metadata embedded in the book file to the current metadata'
' in the calibre library.'))
l.addRow(um)
ask = self('choose_tweak_fmt')
ask.setText(_('Ask which format to tweak if more than one format is available for the book'))
l.addRow(ask)