mirror of
https://github.com/beetbox/beets.git
synced 2026-02-12 18:31:48 +01:00
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.)
This commit is contained in:
parent
d091c7e5b4
commit
c64e9ed789
1 changed files with 15 additions and 18 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue