Skip to content

Commit 4ee4169

Browse files
committed
simplify get_query using class attributes
1 parent f70ddfb commit 4ee4169

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

beets/library.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,9 @@
163163
}
164164
SQLITE_KEY_TYPE = 'INTEGER PRIMARY KEY'
165165

166-
# Default search fields for various granularities.
167-
ARTIST_DEFAULT_FIELDS = ('artist',)
166+
# Default search fields for each model.
168167
ALBUM_DEFAULT_FIELDS = ('album', 'albumartist', 'genre')
169-
ITEM_DEFAULT_FIELDS = ARTIST_DEFAULT_FIELDS + ALBUM_DEFAULT_FIELDS + \
170-
('title', 'comments')
168+
ITEM_DEFAULT_FIELDS = ALBUM_DEFAULT_FIELDS + ('artist', 'title', 'comments')
171169

172170
# Special path format key.
173171
PF_KEY_DEFAULT = 'default'
@@ -350,6 +348,11 @@ class LibModel(FlexModel):
350348
strings.
351349
"""
352350

351+
_search_fields = ()
352+
"""The fields that should be queried by default by unqualified query
353+
terms.
354+
"""
355+
353356
def __init__(self, lib=None, **values):
354357
self._lib = lib
355358
super(LibModel, self).__init__(**values)
@@ -424,6 +427,7 @@ class Item(LibModel):
424427
_fields = ITEM_KEYS
425428
_table = 'items'
426429
_flex_table = 'item_attributes'
430+
_search_fields = ITEM_DEFAULT_FIELDS
427431

428432
@classmethod
429433
def from_path(cls, path):
@@ -610,7 +614,7 @@ class Query(object):
610614
def clause(self):
611615
"""Generate an SQLite expression implementing the query.
612616
Return a clause string, a sequence of substitution values for
613-
the clause, and a Query object representing the "remainder"
617+
the clause, and a Query object representing the "remainder"
614618
Returns (clause, subvals) where clause is a valid sqlite
615619
WHERE clause implementing the query and subvals is a list of
616620
items to be substituted for ?s in the clause.
@@ -1101,18 +1105,13 @@ def construct_query_part(query_part, default_fields, all_keys):
11011105
else:
11021106
return query_class(key.lower(), pattern, key in all_keys)
11031107

1104-
def get_query(val, album=False):
1108+
def get_query(val, model_cls):
11051109
"""Takes a value which may be None, a query string, a query string
1106-
list, or a Query object, and returns a suitable Query object. album
1107-
determines whether the query is to match items or albums.
1110+
list, or a Query object, and returns a suitable Query object.
1111+
`model_cls` is the subclass of LibModel indicating which entity this
1112+
is a query for (i.e., Album or Item) and is used to determine which
1113+
fields are searched.
11081114
"""
1109-
if album:
1110-
default_fields = ALBUM_DEFAULT_FIELDS
1111-
all_keys = ALBUM_KEYS
1112-
else:
1113-
default_fields = ITEM_DEFAULT_FIELDS
1114-
all_keys = ITEM_KEYS
1115-
11161115
# Convert a single string into a list of space-separated
11171116
# criteria.
11181117
if isinstance(val, basestring):
@@ -1121,7 +1120,8 @@ def get_query(val, album=False):
11211120
if val is None:
11221121
return TrueQuery()
11231122
elif isinstance(val, list) or isinstance(val, tuple):
1124-
return AndQuery.from_strings(val, default_fields, all_keys)
1123+
return AndQuery.from_strings(val, model_cls._search_fields,
1124+
model_cls._fields)
11251125
elif isinstance(val, Query):
11261126
return val
11271127
else:
@@ -1513,7 +1513,7 @@ def _fetch(self, model_cls, order_by, query):
15131513
query. The query may be given as a string, string sequence, a
15141514
Query object, or None (to fetch everything).
15151515
"""
1516-
query = get_query(query, model_cls is Album)
1516+
query = get_query(query, model_cls)
15171517

15181518
where, subvals = query.clause()
15191519
with self.transaction() as tx:
@@ -1608,6 +1608,7 @@ class Album(LibModel):
16081608
_fields = ALBUM_KEYS
16091609
_table = 'albums'
16101610
_flex_table = 'album_attributes'
1611+
_search_fields = ALBUM_DEFAULT_FIELDS
16111612

16121613
def __setitem__(self, key, value):
16131614
"""Set the value of an album attribute."""

beetsplug/mbsync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _print_and_apply_changes(lib, item, old_data, move, pretend, write):
6161
def mbsync_singletons(lib, query, move, pretend, write):
6262
"""Synchronize matching singleton items.
6363
"""
64-
singletons_query = library.get_query(query, False)
64+
singletons_query = library.get_query(query, library.Item)
6565
singletons_query.subqueries.append(library.SingletonQuery(True))
6666
for s in lib.items(singletons_query):
6767
if not s.mb_trackid:

0 commit comments

Comments
 (0)