Add defaut value to formatted mapping dict.

This mimics dict get method behavior.
This commit is contained in:
Pierre Rust 2014-05-21 00:01:20 +02:00
parent 9901d6a4eb
commit 4aa626d263
2 changed files with 7 additions and 4 deletions

View file

@ -590,13 +590,16 @@ class FormattedItemMapping(dbcore.db.FormattedMapping):
if key in Album.item_keys or key not in item._fields.keys():
self.album_keys.append(key)
def get(self, key):
def get(self, key, default=None):
if key in self.album_keys:
return self.album._get_formatted(key, self.for_path)
elif key in self.model_keys:
return self.model._get_formatted(key, self.for_path)
else:
raise KeyError(key)
if default is None:
raise KeyError(key)
else:
return default
def __getitem__(self, key):
value = self.get(key)

View file

@ -576,8 +576,8 @@ def _field_diff(field, old, new):
return None
# Get formatted values for output.
oldstr = old.formatted.get(field) or u''
newstr = new.formatted.get(field) or u''
oldstr = old.formatted.get(field, u'')
newstr = new.formatted.get(field, u'')
# For strings, highlight changes. For others, colorize the whole
# thing.