mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix some bugs found after or introduced by trunk merges
This commit is contained in:
parent
f0ab741444
commit
9155f838f1
@ -109,7 +109,8 @@ COPYABLE_METADATA_FIELDS = SOCIAL_METADATA_FIELDS.union(
|
|||||||
CALIBRE_METADATA_FIELDS) - \
|
CALIBRE_METADATA_FIELDS) - \
|
||||||
frozenset(['title', 'title_sort', 'authors',
|
frozenset(['title', 'title_sort', 'authors',
|
||||||
'author_sort', 'author_sort_map' 'comments',
|
'author_sort', 'author_sort_map' 'comments',
|
||||||
'cover_data', 'tags', 'language', 'lpath'])
|
'cover_data', 'tags', 'language', 'lpath',
|
||||||
|
'size'])
|
||||||
|
|
||||||
SERIALIZABLE_FIELDS = SOCIAL_METADATA_FIELDS.union(
|
SERIALIZABLE_FIELDS = SOCIAL_METADATA_FIELDS.union(
|
||||||
USER_METADATA_FIELDS).union(
|
USER_METADATA_FIELDS).union(
|
||||||
|
@ -201,6 +201,11 @@ class Metadata(object):
|
|||||||
Merge the information in C{other} into self. In case of conflicts, the information
|
Merge the information in C{other} into self. In case of conflicts, the information
|
||||||
in C{other} takes precedence, unless the information in other is NULL.
|
in C{other} takes precedence, unless the information in other is NULL.
|
||||||
'''
|
'''
|
||||||
|
def copy_not_none(dest, src, attr):
|
||||||
|
v = getattr(src, attr, None)
|
||||||
|
if v is not None:
|
||||||
|
setattr(dest, attr, copy.deepcopy(v))
|
||||||
|
|
||||||
if other.title and other.title != _('Unknown'):
|
if other.title and other.title != _('Unknown'):
|
||||||
self.title = other.title
|
self.title = other.title
|
||||||
if hasattr(other, 'title_sort'):
|
if hasattr(other, 'title_sort'):
|
||||||
@ -220,21 +225,19 @@ class Metadata(object):
|
|||||||
self.tags = other.tags
|
self.tags = other.tags
|
||||||
self.cover_data = getattr(other, 'cover_data', '')
|
self.cover_data = getattr(other, 'cover_data', '')
|
||||||
self.set_all_user_metadata(other.get_all_user_metadata(make_copy=True))
|
self.set_all_user_metadata(other.get_all_user_metadata(make_copy=True))
|
||||||
self.comments = getattr(other, 'comments', '')
|
copy_not_none(self, other, 'lpath')
|
||||||
self.language = getattr(other, 'language', None)
|
copy_not_none(self, other, 'size')
|
||||||
lpath = getattr(other, 'lpath', None)
|
copy_not_none(self, other, 'comments')
|
||||||
if lpath is not None:
|
# language is handled below
|
||||||
self.lpath = lpath
|
|
||||||
else:
|
else:
|
||||||
for attr in COPYABLE_METADATA_FIELDS:
|
for attr in COPYABLE_METADATA_FIELDS:
|
||||||
if hasattr(other, attr):
|
if hasattr(other, attr):
|
||||||
|
copy_not_none(self, other, attr)
|
||||||
val = getattr(other, attr)
|
val = getattr(other, attr)
|
||||||
if val is not None:
|
if val is not None:
|
||||||
setattr(self, attr, copy.deepcopy(val))
|
setattr(self, attr, copy.deepcopy(val))
|
||||||
|
|
||||||
if other.tags:
|
if other.tags:
|
||||||
self.tags += list(set(self.tags + other.tags))
|
self.tags += list(set(self.tags + other.tags))
|
||||||
|
|
||||||
if getattr(other, 'cover_data', False):
|
if getattr(other, 'cover_data', False):
|
||||||
other_cover = other.cover_data[-1]
|
other_cover = other.cover_data[-1]
|
||||||
self_cover = self.cover_data[-1] if self.cover_data else ''
|
self_cover = self.cover_data[-1] if self.cover_data else ''
|
||||||
@ -242,13 +245,11 @@ class Metadata(object):
|
|||||||
if not other_cover: other_cover = ''
|
if not other_cover: other_cover = ''
|
||||||
if len(other_cover) > len(self_cover):
|
if len(other_cover) > len(self_cover):
|
||||||
self.cover_data = other.cover_data
|
self.cover_data = other.cover_data
|
||||||
|
|
||||||
if getattr(other, 'user_metadata_keys', None):
|
if getattr(other, 'user_metadata_keys', None):
|
||||||
for x in other.user_metadata_keys:
|
for x in other.user_metadata_keys:
|
||||||
meta = other.get_user_metadata(x, make_copy=True)
|
meta = other.get_user_metadata(x, make_copy=True)
|
||||||
if meta is not None:
|
if meta is not None:
|
||||||
self.set_user_metadata(x, meta) # get... did the deepcopy
|
self.set_user_metadata(x, meta) # get... did the deepcopy
|
||||||
|
|
||||||
my_comments = getattr(self, 'comments', '')
|
my_comments = getattr(self, 'comments', '')
|
||||||
other_comments = getattr(other, 'comments', '')
|
other_comments = getattr(other, 'comments', '')
|
||||||
if not my_comments:
|
if not my_comments:
|
||||||
@ -257,10 +258,9 @@ class Metadata(object):
|
|||||||
other_comments = ''
|
other_comments = ''
|
||||||
if len(other_comments.strip()) > len(my_comments.strip()):
|
if len(other_comments.strip()) > len(my_comments.strip()):
|
||||||
self.comments = other_comments
|
self.comments = other_comments
|
||||||
|
other_lang = getattr(other, 'language', None)
|
||||||
other_lang = getattr(other, 'language', None)
|
if other_lang and other_lang.lower() != 'und':
|
||||||
if other_lang and other_lang.lower() != 'und':
|
self.language = other_lang
|
||||||
self.language = other_lang
|
|
||||||
|
|
||||||
def format_series_index(self, val=None):
|
def format_series_index(self, val=None):
|
||||||
from calibre.ebooks.metadata import fmt_sidx
|
from calibre.ebooks.metadata import fmt_sidx
|
||||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, traceback, cStringIO, re
|
import os, traceback, cStringIO, re, string
|
||||||
|
|
||||||
from calibre.utils.config import Config, StringConfig, tweaks
|
from calibre.utils.config import Config, StringConfig, tweaks
|
||||||
from calibre.utils.filenames import shorten_components_to, supports_long_names, \
|
from calibre.utils.filenames import shorten_components_to, supports_long_names, \
|
||||||
@ -98,17 +98,20 @@ def preprocess_template(template):
|
|||||||
template = template.decode(preferred_encoding, 'replace')
|
template = template.decode(preferred_encoding, 'replace')
|
||||||
return template
|
return template
|
||||||
|
|
||||||
|
class SafeFormat(string.Formatter):
|
||||||
|
'''
|
||||||
|
Provides a format function that substitutes '' for any missing value
|
||||||
|
'''
|
||||||
|
def get_value(self, key, args, kwargs):
|
||||||
|
try:
|
||||||
|
return kwargs[key]
|
||||||
|
except:
|
||||||
|
return ''
|
||||||
|
safe_formatter = SafeFormat()
|
||||||
|
|
||||||
def safe_format(x, format_args):
|
def safe_format(x, format_args):
|
||||||
try:
|
ans = safe_formatter.vformat(x, [], format_args).strip()
|
||||||
ans = x.format(**format_args).strip()
|
return re.sub(r'\s+', ' ', ans)
|
||||||
return re.sub(r'\s+', ' ', ans)
|
|
||||||
except IndexError: # Thrown if user used [] and index is out of bounds
|
|
||||||
pass
|
|
||||||
except AttributeError: # Thrown if user used a non existing attribute
|
|
||||||
pass
|
|
||||||
except KeyError: # Thrown if user used custom field w/value None
|
|
||||||
pass
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
||||||
sanitize_func=ascii_filename, replace_whitespace=False,
|
sanitize_func=ascii_filename, replace_whitespace=False,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user