Skip to content

Commit

Permalink
Minor changes from review (mostly style)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRust committed Jul 24, 2014
1 parent d168c62 commit 3130a6a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
13 changes: 5 additions & 8 deletions beets/dbcore/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def __iter__(self):
# Slow sort. Must build the full list first.
objects = []
for row in self.rows:
obj = self._generate_results(row)
obj = self._make_model(row)
# check the predicate if any
if not self.query or self.query.match(obj):
objects.append(obj)
Expand All @@ -518,12 +518,12 @@ def __iter__(self):
yield o
else:
for row in self.rows:
obj = self._generate_results(row)
obj = self._make_model(row)
# check the predicate if any
if not self.query or self.query.match(obj):
yield obj

def _generate_results(self, row):
def _make_model(self, row):
# Get the flexible attributes for the object.
with self.db.transaction() as tx:
flex_rows = tx.query(
Expand Down Expand Up @@ -771,15 +771,12 @@ def _fetch(self, model_cls, query, sort_order=None):
Sort object.
"""

sql, subvals, slow_query, slow_sort = build_sql(model_cls, query,
sort_order)
sql, subvals, query, sort = build_sql(model_cls, query, sort_order)

with self.transaction() as tx:
rows = tx.query(sql, subvals)

return Results(model_cls, rows, self,
None if not slow_query else query,
None if not slow_sort else sort_order)
return Results(model_cls, rows, self, query, sort)

def _get(self, model_cls, id):
"""Get a Model object by its id or None if the id does not
Expand Down
15 changes: 9 additions & 6 deletions beets/dbcore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,31 +713,34 @@ def sort(self, items):

def build_sql(model_cls, query, sort):
""" Generate a sql statement (and the values that must be injected into it)
from a query, sort and a model class.
from a query, sort and a model class. Query and sort objects are returned
only for slow query and slow sort operation.
"""
where, subvals = query.clause()
slow_query = where is None
if where is not None:
query = None

if not sort:
sort_select = ""
sort_union = ""
sort_order = ""
slow_sort = False
sort = None
elif isinstance(sort, basestring):
sort_select = ""
sort_union = ""
sort_order = " ORDER BY {0}".format(sort) \
if sort else ""
slow_sort = False
sort = None
elif isinstance(sort, Sort):
select_clause = sort.select_clause()
sort_select = " ,{0} ".format(select_clause) \
if select_clause else ""
sort_union = sort.union_clause()
slow_sort = sort.is_slow()
order_clause = sort.order_clause()
sort_order = " ORDER BY {0}".format(order_clause) \
if order_clause else ""
if sort.is_slow():
sort = None

sql = ("SELECT {table}.* {sort_select} FROM {table} {sort_union} WHERE "
"{query_clause} {sort_order}").format(
Expand All @@ -748,4 +751,4 @@ def build_sql(model_cls, query, sort):
sort_order=sort_order
)

return sql, subvals, slow_query, slow_sort
return sql, subvals, query, sort
8 changes: 4 additions & 4 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def destination(self, fragment=False, basedir=None, platform=None,
for query, path_format in path_formats:
if query == PF_KEY_DEFAULT:
continue
(query, _) = get_query(query, type(self))
(query, _) = get_query_sort(query, type(self))
if query.match(self):
# The query matches the item! Use the corresponding path
# format.
Expand Down Expand Up @@ -887,7 +887,7 @@ def store(self):

# Query construction helper.

def get_query(val, model_cls):
def get_query_sort(val, model_cls):
"""Take a value which may be None, a query string, a query string
list, or a Query object, and return a suitable Query object and Sort
object.
Expand Down Expand Up @@ -1016,8 +1016,8 @@ def _fetch(self, model_cls, query, sort_order=None):
"""Parse a query and fetch. If a order specification is present in the
query string the sort_order argument is ignored.
"""
(query, sort) = get_query(query, model_cls)
sort = sort_order if sort is None else sort
query, sort = get_query_sort(query, model_cls)
sort = sort or sort_order

return super(Library, self)._fetch(
model_cls, query, sort
Expand Down
6 changes: 3 additions & 3 deletions beetsplug/ihate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import logging
from beets.plugins import BeetsPlugin
from beets.importer import action
from beets.library import get_query
from beets.library import get_query_sort
from beets.library import Item
from beets.library import Album

Expand Down Expand Up @@ -57,9 +57,9 @@ def do_i_hate_this(cls, task, action_patterns):
for query_string in action_patterns:
query = None
if task.is_album:
(query, _) = get_query(query_string, Album)
(query, _) = get_query_sort(query_string, Album)
else:
(query, _) = get_query(query_string, Item)
(query, _) = get_query_sort(query_string, Item)
if any(query.match(item) for item in task.imported_items()):
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion beetsplug/smartplaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _items_for_query(lib, playlist, album=False):
query_strings = [query_strings]
model = library.Album if album else library.Item
query = dbcore.OrQuery(
[library.get_query(q, model)[0] for q in query_strings]
[library.get_query_sort(q, model)[0] for q in query_strings]
)

# Execute query, depending on type.
Expand Down

0 comments on commit 3130a6a

Please sign in to comment.