less code to specify sql and query for Type

Declarative class members instead of super calls. Wins every time.
This commit is contained in:
Adrian Sampson 2014-01-26 21:17:55 -08:00
parent f29fbe47da
commit fbed9552ea
2 changed files with 21 additions and 23 deletions

View file

@ -27,13 +27,14 @@ class Type(object):
information about how to store the value in the database, query,
format, and parse a given field.
"""
def __init__(self, sql, query):
"""Create a type. `sql` is the SQLite column type for the value.
`query` is the `Query` subclass to be used when querying the
field.
"""
self.sql = sql
self.query = query
sql = None
"""The SQLite column type for the value.
"""
query = None
"""The `Query` subclass to be used when querying the field.
"""
def format(self, value):
"""Given a value of this type, produce a Unicode string
@ -55,8 +56,8 @@ class Type(object):
class Integer(Type):
"""A basic integer type.
"""
def __init__(self, sql='INTEGER', query=query.NumericQuery):
super(Integer, self).__init__(sql, query)
sql = u'INTEGER'
query = query.NumericQuery
def format(self, value):
return unicode(value or 0)
@ -73,7 +74,6 @@ class PaddedInt(Integer):
padded with zeroes.
"""
def __init__(self, digits):
super(PaddedInt, self).__init__()
self.digits = digits
def format(self, value):
@ -85,7 +85,6 @@ class ScaledInt(Integer):
constant and adds a suffix. Good for units with large magnitudes.
"""
def __init__(self, unit, suffix=u''):
super(ScaledInt, self).__init__()
self.unit = unit
self.suffix = suffix
@ -96,15 +95,14 @@ class ScaledInt(Integer):
class Id(Integer):
"""An integer used as the row key for a SQLite table.
"""
def __init__(self):
super(Id, self).__init__('INTEGER PRIMARY KEY')
sql = u'INTEGER PRIMARY KEY'
class Float(Type):
"""A basic floating-point type.
"""
def __init__(self, sql='REAL', query=query.NumericQuery):
super(Float, self).__init__(sql, query)
sql = u'REAL'
query = query.NumericQuery
def format(self, value):
return u'{0:.1f}'.format(value or 0.0)
@ -119,8 +117,8 @@ class Float(Type):
class String(Type):
"""A Unicode string type.
"""
def __init__(self, sql='TEXT', query=query.SubstringQuery):
super(String, self).__init__(sql, query)
sql = u'TEXT'
query = query.SubstringQuery
def format(self, value):
return unicode(value) if value else u''
@ -132,8 +130,8 @@ class String(Type):
class Boolean(Type):
"""A boolean type.
"""
def __init__(self, sql='INTEGER', query=query.BooleanQuery):
return super(Boolean, self).__init__(sql, query)
sql = u'INTEGER'
query = query.BooleanQuery
def format(self, value):
return unicode(bool(value))

View file

@ -78,8 +78,8 @@ class SingletonQuery(dbcore.Query):
class DateType(types.Type):
def __init__(self):
super(DateType, self).__init__('REAL', dbcore.query.NumericQuery)
sql = u'REAL'
query = dbcore.query.NumericQuery
def format(self, value):
return time.strftime(beets.config['time_format'].get(unicode),
@ -90,8 +90,8 @@ class DateType(types.Type):
class PathType(types.Type):
def __init__(self):
super(PathType, self).__init__('BLOB', PathQuery)
sql = u'BLOB'
query = PathQuery
def format(self, value):
return util.displayable_path(value)