mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Update metadata in the file from calibre db when opening for Tweaking
This commit is contained in:
parent
756be9b409
commit
d8dc718abf
@ -24,6 +24,23 @@ from calibre import prints, guess_type
|
|||||||
from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars
|
from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars
|
||||||
from calibre.utils.config import tweaks
|
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): # {{{
|
class Resource(object): # {{{
|
||||||
'''
|
'''
|
||||||
Represents a resource (usually a file on the filesystem or a URL pointing
|
Represents a resource (usually a file on the filesystem or a URL pointing
|
||||||
@ -1210,6 +1227,8 @@ class OPF(object): # {{{
|
|||||||
a['content'] = c
|
a['content'] = c
|
||||||
|
|
||||||
self.write_user_metadata()
|
self.write_user_metadata()
|
||||||
|
if pretty_print_opf:
|
||||||
|
_pretty_print(self.root)
|
||||||
raw = etree.tostring(self.root, encoding=encoding, pretty_print=True)
|
raw = etree.tostring(self.root, encoding=encoding, pretty_print=True)
|
||||||
if not raw.lstrip().startswith('<?xml '):
|
if not raw.lstrip().startswith('<?xml '):
|
||||||
raw = '<?xml version="1.0" encoding="%s"?>\n'%encoding.upper()+raw
|
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})
|
attrib={'type':'cover', 'title':_('Cover'), 'href':mi.cover})
|
||||||
r.tail = '\n' +(' '*4)
|
r.tail = '\n' +(' '*4)
|
||||||
guide.append(r)
|
guide.append(r)
|
||||||
|
if pretty_print_opf:
|
||||||
|
_pretty_print(root)
|
||||||
|
|
||||||
return etree.tostring(root, pretty_print=True, encoding='utf-8',
|
return etree.tostring(root, pretty_print=True, encoding='utf-8',
|
||||||
xml_declaration=True) if as_string else root
|
xml_declaration=True) if as_string else root
|
||||||
|
|
||||||
|
@ -110,8 +110,8 @@ class TweakEpubAction(InterfaceAction):
|
|||||||
_('The book must be in the %s formats to tweak.'
|
_('The book must be in the %s formats to tweak.'
|
||||||
'\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))),
|
'\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))),
|
||||||
show=True)
|
show=True)
|
||||||
|
from calibre.gui2.tweak_book import tprefs
|
||||||
if len(tweakable_fmts) > 1:
|
if len(tweakable_fmts) > 1:
|
||||||
from calibre.gui2.tweak_book import tprefs
|
|
||||||
if tprefs['choose_tweak_fmt']:
|
if tprefs['choose_tweak_fmt']:
|
||||||
d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui)
|
d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui)
|
||||||
if d.exec_() != d.Accepted:
|
if d.exec_() != d.Accepted:
|
||||||
@ -131,6 +131,12 @@ class TweakEpubAction(InterfaceAction):
|
|||||||
' library maintenance.') % fmt, show=True)
|
' library maintenance.') % fmt, show=True)
|
||||||
tweak = 'ebook-tweak'
|
tweak = 'ebook-tweak'
|
||||||
self.gui.setCursor(Qt.BusyCursor)
|
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:
|
try:
|
||||||
self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(args=[tweak, path]))
|
self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(args=[tweak, path]))
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
@ -17,6 +17,7 @@ tprefs.defaults['editor_tab_stop_width'] = 2
|
|||||||
tprefs.defaults['preview_refresh_time'] = 2
|
tprefs.defaults['preview_refresh_time'] = 2
|
||||||
tprefs.defaults['choose_tweak_fmt'] = True
|
tprefs.defaults['choose_tweak_fmt'] = True
|
||||||
tprefs.defaults['tweak_fmt_order'] = ['EPUB', 'AZW3']
|
tprefs.defaults['tweak_fmt_order'] = ['EPUB', 'AZW3']
|
||||||
|
tprefs.defaults['update_metadata_from_calibre'] = True
|
||||||
|
|
||||||
_current_container = None
|
_current_container = None
|
||||||
|
|
||||||
|
@ -178,6 +178,13 @@ class IntegrationSettings(BasicSettings):
|
|||||||
self.l = l = QFormLayout(self)
|
self.l = l = QFormLayout(self)
|
||||||
self.setLayout(l)
|
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 = self('choose_tweak_fmt')
|
||||||
ask.setText(_('Ask which format to tweak if more than one format is available for the book'))
|
ask.setText(_('Ask which format to tweak if more than one format is available for the book'))
|
||||||
l.addRow(ask)
|
l.addRow(ask)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user