When adding PRC/MOBI files that are actually Topaz files, change detected file type to Topaz

This commit is contained in:
Kovid Goyal 2010-02-26 14:54:15 -07:00
parent 37db157f85
commit 8a930b53af
5 changed files with 50 additions and 41 deletions

View File

@ -235,6 +235,8 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
with plugin:
try:
nfp = plugin.run(path_to_file)
if not nfp:
nfp = path_to_file
except:
print 'Running file type plugin %s failed with traceback:'%plugin.name
traceback.print_exc()

View File

@ -129,3 +129,11 @@ def render_html(path_to_html, width=590, height=750):
del loop
return renderer
def check_ebook_format(stream, current_guess):
ans = current_guess
if current_guess.lower() in ('prc', 'mobi', 'azw', 'azw1'):
stream.seek(0)
if stream.read(3) == 'TPZ':
ans = 'tpz'
return ans

View File

@ -798,7 +798,6 @@ def get_metadata(stream):
from calibre.utils.logging import Log
log = Log()
mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')])
try:
mh = MetadataHeader(stream, log)
if mh.title and mh.title != _('Unknown'):
mi.title = mh.title
@ -835,7 +834,4 @@ def get_metadata(stream):
obuf = cStringIO.StringIO()
im.convert('RGB').save(obuf, format='JPEG')
mi.cover_data = ('jpg', obuf.getvalue())
except:
log.filter_level = Log.DEBUG
log.exception('Failed to read MOBI metadata')
return mi

View File

@ -34,7 +34,7 @@ from calibre.customize.ui import run_plugins_on_import
from calibre.utils.filenames import ascii_filename
from calibre.utils.date import utcnow, now as nowf, utcfromtimestamp
from calibre.ebooks import BOOK_EXTENSIONS
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
if iswindows:
import calibre.utils.winshell as winshell
@ -993,7 +993,9 @@ class LibraryDatabase2(LibraryDatabase):
path=None, notify=True):
npath = self.run_import_plugins(fpath, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper()
return self.add_format(index, format, open(npath, 'rb'),
stream = open(npath, 'rb')
format = check_ebook_format(stream, format)
return self.add_format(index, format, stream,
index_is_id=index_is_id, path=path, notify=notify)
def add_format(self, index, format, stream, index_is_id=False, path=None, notify=True):
@ -1596,6 +1598,7 @@ class LibraryDatabase2(LibraryDatabase):
npath = self.run_import_plugins(path, format)
format = os.path.splitext(npath)[-1].lower().replace('.', '').upper()
stream = open(npath, 'rb')
format = check_ebook_format(stream, format)
self.add_format(id, format, stream, index_is_id=True)
stream.close()
self.conn.commit()