Replace SingletonQuery with BooleanQuery on computed singleton field.

Updated use in command parsing and mbsync plugin.
This commit is contained in:
Kyle Konrad 2014-04-11 22:20:04 -07:00
parent 271a1627a5
commit d044e1773a
2 changed files with 11 additions and 27 deletions

View file

@ -58,22 +58,6 @@ class PathQuery(dbcore.FieldQuery):
(file_blob, dir_pat)
class SingletonQuery(dbcore.Query):
"""Matches either singleton or non-singleton items."""
def __init__(self, sense):
self.sense = sense
def clause(self):
if self.sense:
return "album_id ISNULL", ()
else:
return "NOT album_id ISNULL", ()
def match(self, item):
return (not item.album_id) == self.sense
# Library-specific field types.
@ -278,7 +262,9 @@ class Item(LibModel):
@classmethod
def _getters(cls):
return plugins.item_field_getters()
getters = plugins.item_field_getters()
getters['singleton'] = Item.singleton
return getters
@classmethod
def from_path(cls, path):
@ -305,8 +291,13 @@ class Item(LibModel):
super(Item, self).__setitem__(key, value)
def singleton(self):
"""Returns a boolean indicating whether this item is a singleton
(doesn't belong to an album)"""
return self.album_id is None
def update(self, values):
"""Sett all key/value pairs in the mapping. If mtime is
"""Set all key/value pairs in the mapping. If mtime is
specified, it is not reset (as it might otherwise be).
"""
super(Item, self).update(values)
@ -932,13 +923,6 @@ def construct_query_part(query_part, model_cls):
return query_class(pattern)
key = key.lower()
# Singleton query (not a real field).
if key == 'singleton':
return SingletonQuery(util.str2bool(pattern))
# Other field.
else:
return query_class(key.lower(), pattern, key in model_cls._fields)

View file

@ -49,7 +49,7 @@ def mbsync_singletons(lib, query, move, pretend, write):
"""Synchronize matching singleton items.
"""
singletons_query = library.get_query(query, library.Item)
singletons_query.subqueries.append(library.SingletonQuery(True))
singletons_query.subqueries.append(library.BooleanQuery('singleton', True))
for s in lib.items(singletons_query):
if not s.mb_trackid:
log.info(u'Skipping singleton {0}: has no mb_trackid'