mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Handle periods better when reading metadata from filenames
This commit is contained in:
parent
2708065870
commit
b23b32a7ea
@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
import os, re, collections
|
import os, re, collections
|
||||||
|
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
|
|
||||||
from calibre.ebooks.metadata.opf2 import OPF
|
from calibre.ebooks.metadata.opf2 import OPF
|
||||||
|
|
||||||
from calibre.customize.ui import get_file_type_metadata, set_file_type_metadata
|
from calibre.customize.ui import get_file_type_metadata, set_file_type_metadata
|
||||||
@ -37,18 +37,18 @@ def metadata_from_formats(formats):
|
|||||||
mi2 = opf_metadata(opf)
|
mi2 = opf_metadata(opf)
|
||||||
if mi2 is not None and mi2.title:
|
if mi2 is not None and mi2.title:
|
||||||
return mi2
|
return mi2
|
||||||
|
|
||||||
for path, ext in zip(formats, extensions):
|
for path, ext in zip(formats, extensions):
|
||||||
with open(path, 'rb') as stream:
|
with open(path, 'rb') as stream:
|
||||||
try:
|
try:
|
||||||
newmi = get_metadata(stream, stream_type=ext,
|
newmi = get_metadata(stream, stream_type=ext,
|
||||||
use_libprs_metadata=True)
|
use_libprs_metadata=True)
|
||||||
mi.smart_update(newmi)
|
mi.smart_update(newmi)
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
if getattr(mi, 'application_id', None) is not None:
|
if getattr(mi, 'application_id', None) is not None:
|
||||||
return mi
|
return mi
|
||||||
|
|
||||||
if not mi.title:
|
if not mi.title:
|
||||||
mi.title = _('Unknown')
|
mi.title = _('Unknown')
|
||||||
if not mi.authors:
|
if not mi.authors:
|
||||||
@ -64,20 +64,20 @@ def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False):
|
|||||||
stream_type = 'mobi'
|
stream_type = 'mobi'
|
||||||
if stream_type in ('odt', 'ods', 'odp', 'odg', 'odf'):
|
if stream_type in ('odt', 'ods', 'odp', 'odg', 'odf'):
|
||||||
stream_type = 'odt'
|
stream_type = 'odt'
|
||||||
|
|
||||||
opf = None
|
opf = None
|
||||||
if hasattr(stream, 'name'):
|
if hasattr(stream, 'name'):
|
||||||
c = os.path.splitext(stream.name)[0]+'.opf'
|
c = os.path.splitext(stream.name)[0]+'.opf'
|
||||||
if os.access(c, os.R_OK):
|
if os.access(c, os.R_OK):
|
||||||
opf = opf_metadata(os.path.abspath(c))
|
opf = opf_metadata(os.path.abspath(c))
|
||||||
|
|
||||||
if use_libprs_metadata and getattr(opf, 'application_id', None) is not None:
|
if use_libprs_metadata and getattr(opf, 'application_id', None) is not None:
|
||||||
return opf
|
return opf
|
||||||
|
|
||||||
mi = MetaInformation(None, None)
|
mi = MetaInformation(None, None)
|
||||||
if prefs['read_file_metadata']:
|
if prefs['read_file_metadata']:
|
||||||
mi = get_file_type_metadata(stream, stream_type)
|
mi = get_file_type_metadata(stream, stream_type)
|
||||||
|
|
||||||
name = os.path.basename(getattr(stream, 'name', ''))
|
name = os.path.basename(getattr(stream, 'name', ''))
|
||||||
base = metadata_from_filename(name)
|
base = metadata_from_filename(name)
|
||||||
if base.title == os.path.splitext(name)[0] and base.authors is None:
|
if base.title == os.path.splitext(name)[0] and base.authors is None:
|
||||||
@ -98,17 +98,17 @@ def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False):
|
|||||||
base.smart_update(mi)
|
base.smart_update(mi)
|
||||||
if opf is not None:
|
if opf is not None:
|
||||||
base.smart_update(opf)
|
base.smart_update(opf)
|
||||||
|
|
||||||
return base
|
return base
|
||||||
|
|
||||||
def set_metadata(stream, mi, stream_type='lrf'):
|
def set_metadata(stream, mi, stream_type='lrf'):
|
||||||
if stream_type:
|
if stream_type:
|
||||||
stream_type = stream_type.lower()
|
stream_type = stream_type.lower()
|
||||||
set_file_type_metadata(stream, mi, stream_type)
|
set_file_type_metadata(stream, mi, stream_type)
|
||||||
|
|
||||||
|
|
||||||
def metadata_from_filename(name, pat=None):
|
def metadata_from_filename(name, pat=None):
|
||||||
name = os.path.splitext(name)[0]
|
name = name.rpartition('.')[0]
|
||||||
mi = MetaInformation(None, None)
|
mi = MetaInformation(None, None)
|
||||||
if pat is None:
|
if pat is None:
|
||||||
pat = re.compile(prefs.get('filename_pattern'))
|
pat = re.compile(prefs.get('filename_pattern'))
|
||||||
@ -161,7 +161,7 @@ def opf_metadata(opfpath):
|
|||||||
mi = MetaInformation(opf)
|
mi = MetaInformation(opf)
|
||||||
if hasattr(opf, 'cover') and opf.cover:
|
if hasattr(opf, 'cover') and opf.cover:
|
||||||
cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
|
cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
|
||||||
if os.access(cpath, os.R_OK):
|
if os.access(cpath, os.R_OK):
|
||||||
fmt = cpath.rpartition('.')[-1]
|
fmt = cpath.rpartition('.')[-1]
|
||||||
data = open(cpath, 'rb').read()
|
data = open(cpath, 'rb').read()
|
||||||
mi.cover_data = (fmt, data)
|
mi.cover_data = (fmt, data)
|
||||||
|
@ -530,6 +530,7 @@ class build_windows(VMInstaller):
|
|||||||
self.run_windows_install_jammer(installer)
|
self.run_windows_install_jammer(installer)
|
||||||
return os.path.basename(installer)
|
return os.path.basename(installer)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
def run_windows_install_jammer(self, installer):
|
def run_windows_install_jammer(self, installer):
|
||||||
ibp = os.path.abspath('installer/windows')
|
ibp = os.path.abspath('installer/windows')
|
||||||
sys.path.insert(0, ibp)
|
sys.path.insert(0, ibp)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user