Fix some bugs found after or introduced by trunk merges

This commit is contained in:
Charles Haley 2010-09-08 19:39:57 +01:00
parent f0ab741444
commit 9155f838f1
3 changed files with 29 additions and 25 deletions

View File

@ -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(

View File

@ -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,7 +258,6 @@ 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

View File

@ -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
def safe_format(x, format_args): class SafeFormat(string.Formatter):
'''
Provides a format function that substitutes '' for any missing value
'''
def get_value(self, key, args, kwargs):
try: try:
ans = x.format(**format_args).strip() return kwargs[key]
return re.sub(r'\s+', ' ', ans) except:
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 '' return ''
safe_formatter = SafeFormat()
def safe_format(x, format_args):
ans = safe_formatter.vformat(x, [], format_args).strip()
return re.sub(r'\s+', ' ', ans)
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,