Try to import metadata from metadata.opf if it exists in the same directory as the book being added to the library.

This commit is contained in:
Kovid Goyal 2008-02-13 15:35:12 +00:00
parent d7c0d543fe
commit d1053b7bb5
3 changed files with 16 additions and 5 deletions

View File

@ -21,16 +21,20 @@ from libprs500.ebooks.metadata.pdf import get_metadata as pdf_metadata
from libprs500.ebooks.metadata.lit import get_metadata as lit_metadata
from libprs500.ebooks.metadata.epub import get_metadata as epub_metadata
from libprs500.ebooks.metadata.html import get_metadata as html_metadata
from libprs500.ebooks.metadata.opf import OPFReader
from libprs500.ebooks.metadata.rtf import set_metadata as set_rtf_metadata
from libprs500.ebooks.lrf.meta import set_metadata as set_lrf_metadata
from libprs500.ebooks.metadata import MetaInformation
def get_metadata(stream, stream_type='lrf'):
def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False):
if stream_type: stream_type = stream_type.lower()
if stream_type in ('html', 'html', 'xhtml', 'xhtm'):
stream_type = 'html'
if use_libprs_metadata and hasattr(stream, 'name'):
mi = libprs_metadata(stream.name)
if mi is not None:
return mi
try:
func = eval(stream_type + '_metadata')
mi = func(stream)
@ -78,4 +82,11 @@ def metadata_from_filename(name):
if not mi.title:
mi.title = name
return mi
def libprs_metadata(name):
if os.access(name, os.R_OK):
name = os.path.abspath(name)
f = open(name, 'rb')
opf = OPFReader(f, os.path.dirname(name))
if opf.libprs_id is not None:
return MetaInformation(opf, None)

View File

@ -459,7 +459,7 @@ class OPF(MetaInformation):
class OPFReader(OPF):
def __init__(self, stream, dir=os.getcwd()):
def __init__(self, stream, dir=os.getcwdu()):
manage = False
if not hasattr(stream, 'read'):
manage = True

View File

@ -298,7 +298,7 @@ class Main(MainWindow, Ui_MainWindow):
format = os.path.splitext(book)[1]
format = format[1:] if format else None
stream = open(book, 'rb')
mi = get_metadata(stream, stream_type=format)
mi = get_metadata(stream, stream_type=format, use_libprs_metadata=True)
if not mi.title:
mi.title = os.path.splitext(os.path.basename(book))[0]
formats.append(format)