From c64e9ed7898e9bbfbb832f4d387c8e53421fedd8 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 11 Mar 2014 22:09:22 -0700 Subject: [PATCH] MediaFile: remove out_type from StorageStyle This eliminates a little bit of state redundancy in which StorageStyles needed to know about their field's externally visible type. Now the MediaField itself is *solely* responsible for external-type-related conversions; StorageStyles need only worry about their *internal* types. (I hope I didn't mess up any essential design decisions, @geigerzaehler.) --- beets/mediafile.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/beets/mediafile.py b/beets/mediafile.py index 212b1b21f..339d22bbc 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -430,9 +430,6 @@ class StorageStyle(object): """Convert the external Python value to a type that is suitable for storing in a Mutagen file object. """ - if value is None: - value = self._none_value() - if isinstance(value, float) and self.as_type is unicode: value = u'{0:.{1}f}'.format(value, self.float_places) value = self.as_type(value) @@ -452,18 +449,6 @@ class StorageStyle(object): return value - # Utility. - - def _none_value(self): - if self.out_type == int: - return 0 - elif self.out_type == float: - return 0.0 - elif self.out_type == bool: - return False - elif self.out_type == unicode: - return u'' - class ListStorageStyle(StorageStyle): """Abstract storage style that provides access to lists. @@ -931,8 +916,6 @@ class MediaField(object): """ self.out_type = kwargs.get('out_type', unicode) self._styles = styles - for style in styles: - style.out_type = self.out_type def styles(self, mediafile): """Yields the list of storage styles of this field that can @@ -950,11 +933,25 @@ class MediaField(object): break return _safe_cast(self.out_type, out) - def __set__(self, mediafile, value): + if value is None: + value = self._none_value() for style in self.styles(mediafile): style.set(mediafile.mgfile, value) + def _none_value(self): + """Get an appropriate "null" value for this field's type. This + is used internally when setting the field to None. + """ + if self.out_type == int: + return 0 + elif self.out_type == float: + return 0.0 + elif self.out_type == bool: + return False + elif self.out_type == unicode: + return u'' + class ListMediaField(MediaField): """Property descriptor that retrieves a list of multiple values from