mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #7245 (Defer Metadata reading until after on_import plugins run)
This commit is contained in:
parent
b5d013ac91
commit
c9a0e00e45
@ -292,7 +292,7 @@ class RTFMetadataReader(MetadataReaderPlugin):
|
|||||||
def get_metadata(self, stream, ftype):
|
def get_metadata(self, stream, ftype):
|
||||||
from calibre.ebooks.metadata.rtf import get_metadata
|
from calibre.ebooks.metadata.rtf import get_metadata
|
||||||
return get_metadata(stream)
|
return get_metadata(stream)
|
||||||
|
|
||||||
class SNBMetadataReader(MetadataReaderPlugin):
|
class SNBMetadataReader(MetadataReaderPlugin):
|
||||||
|
|
||||||
name = 'Read SNB metadata'
|
name = 'Read SNB metadata'
|
||||||
|
@ -21,51 +21,62 @@ def debug(*args):
|
|||||||
prints(*args)
|
prints(*args)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
def read_metadata_(task, tdir, notification=lambda x,y:x):
|
def serialize_metadata_for(formats, tdir, id_):
|
||||||
from calibre.ebooks.metadata.meta import metadata_from_formats
|
from calibre.ebooks.metadata.meta import metadata_from_formats
|
||||||
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
||||||
|
mi = metadata_from_formats(formats)
|
||||||
|
mi.cover = None
|
||||||
|
cdata = None
|
||||||
|
if mi.cover_data:
|
||||||
|
cdata = mi.cover_data[-1]
|
||||||
|
mi.cover_data = None
|
||||||
|
if not mi.application_id:
|
||||||
|
mi.application_id = '__calibre_dummy__'
|
||||||
|
with open(os.path.join(tdir, '%s.opf'%id_), 'wb') as f:
|
||||||
|
f.write(metadata_to_opf(mi))
|
||||||
|
if cdata:
|
||||||
|
with open(os.path.join(tdir, str(id_)), 'wb') as f:
|
||||||
|
f.write(cdata)
|
||||||
|
|
||||||
|
def read_metadata_(task, tdir, notification=lambda x,y:x):
|
||||||
from calibre.customize.ui import run_plugins_on_import
|
from calibre.customize.ui import run_plugins_on_import
|
||||||
for x in task:
|
for x in task:
|
||||||
try:
|
try:
|
||||||
id, formats = x
|
id_, formats = x
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
if isinstance(formats, basestring): formats = [formats]
|
if isinstance(formats, basestring): formats = [formats]
|
||||||
mi = metadata_from_formats(formats)
|
|
||||||
mi.cover = None
|
|
||||||
cdata = None
|
|
||||||
if mi.cover_data:
|
|
||||||
cdata = mi.cover_data[-1]
|
|
||||||
mi.cover_data = None
|
|
||||||
if not mi.application_id:
|
|
||||||
mi.application_id = '__calibre_dummy__'
|
|
||||||
with open(os.path.join(tdir, '%s.opf'%id), 'wb') as f:
|
|
||||||
f.write(metadata_to_opf(mi))
|
|
||||||
if cdata:
|
|
||||||
with open(os.path.join(tdir, str(id)), 'wb') as f:
|
|
||||||
f.write(cdata)
|
|
||||||
import_map = {}
|
import_map = {}
|
||||||
|
fmts = []
|
||||||
for format in formats:
|
for format in formats:
|
||||||
nfp = run_plugins_on_import(format)
|
nfp = run_plugins_on_import(format)
|
||||||
if nfp is None:
|
if not nfp or not os.access(nfp, os.R_OK):
|
||||||
nfp = format
|
nfp = format
|
||||||
nfp = os.path.abspath(nfp)
|
nfp = os.path.abspath(nfp)
|
||||||
|
fmts.append(nfp)
|
||||||
|
|
||||||
|
serialize_metadata_for(fmts, tdir, id_)
|
||||||
|
|
||||||
|
for format, nfp in zip(formats, fmts):
|
||||||
if isinstance(nfp, unicode):
|
if isinstance(nfp, unicode):
|
||||||
nfp.encode(filesystem_encoding)
|
nfp.encode(filesystem_encoding)
|
||||||
x = lambda j : os.path.abspath(os.path.normpath(os.path.normcase(j)))
|
x = lambda j : os.path.abspath(os.path.normpath(os.path.normcase(j)))
|
||||||
if x(nfp) != x(format) and os.access(nfp, os.R_OK|os.W_OK):
|
if x(nfp) != x(format) and os.access(nfp, os.R_OK|os.W_OK):
|
||||||
fmt = os.path.splitext(format)[1].replace('.', '').lower()
|
fmt = os.path.splitext(format)[1].replace('.', '').lower()
|
||||||
nfmt = os.path.splitext(nfp)[1].replace('.', '').lower()
|
nfmt = os.path.splitext(nfp)[1].replace('.', '').lower()
|
||||||
dest = os.path.join(tdir, '%s.%s'%(id, nfmt))
|
dest = os.path.join(tdir, '%s.%s'%(id_, nfmt))
|
||||||
shutil.copyfile(nfp, dest)
|
shutil.copyfile(nfp, dest)
|
||||||
import_map[fmt] = dest
|
import_map[fmt] = dest
|
||||||
os.remove(nfp)
|
os.remove(nfp)
|
||||||
if import_map:
|
if import_map:
|
||||||
with open(os.path.join(tdir, str(id)+'.import'), 'wb') as f:
|
with open(os.path.join(tdir, str(id_)+'.import'), 'wb') as f:
|
||||||
for fmt, nfp in import_map.items():
|
for fmt, nfp in import_map.items():
|
||||||
f.write(fmt+':'+nfp+'\n')
|
f.write(fmt+':'+nfp+'\n')
|
||||||
notification(0.5, id)
|
notification(0.5, id_)
|
||||||
except:
|
except:
|
||||||
import traceback
|
import traceback
|
||||||
with open(os.path.join(tdir, '%s.error'%id), 'wb') as f:
|
with open(os.path.join(tdir, '%s.error'%id_), 'wb') as f:
|
||||||
f.write(traceback.format_exc())
|
f.write(traceback.format_exc())
|
||||||
|
|
||||||
class Progress(object):
|
class Progress(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user