Skip to content

Commit f70ddfb

Browse files
committed
deduplicate albums() and items() methods
This also drops the keyword arguments to these methods, which were vestigial.
1 parent 0e042d3 commit f70ddfb

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

beets/importer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _duplicate_check(lib, task):
6565

6666
found_albums = []
6767
cur_paths = set(i.path for i in task.items if i)
68-
for album_cand in lib.albums(artist=artist):
68+
for album_cand in lib.albums(library.MatchQuery('albumartist', artist)):
6969
if album_cand.album == album:
7070
# Check whether the album is identical in contents, in which
7171
# case it is not a duplicate (will be replaced).
@@ -83,7 +83,11 @@ def _item_duplicate_check(lib, task):
8383
artist, title = task.chosen_ident()
8484

8585
found_items = []
86-
for other_item in lib.items(artist=artist, title=title):
86+
query = library.AndQuery((
87+
library.MatchQuery('artist', artist),
88+
library.MatchQuery('title', title),
89+
))
90+
for other_item in lib.items(query):
8791
# Existing items not considered duplicates.
8892
if other_item.path == task.item.path:
8993
continue
@@ -296,7 +300,7 @@ def choose_match(self, task):
296300

297301
def resolve_duplicate(self, task):
298302
raise NotImplementedError
299-
303+
300304
def choose_item(self, task):
301305
raise NotImplementedError
302306

@@ -533,7 +537,7 @@ def read_tasks(session):
533537
if resume_dir:
534538

535539
# Either accept immediately or prompt for input to decide.
536-
if _resume() == True:
540+
if _resume() is True:
537541
do_resume = True
538542
log.warn('Resuming interrupted import of %s' % path)
539543
else:
@@ -746,7 +750,9 @@ def apply_choices(session):
746750
# when the last item is removed.
747751
task.replaced_items = defaultdict(list)
748752
for item in items:
749-
dup_items = session.lib.items(library.MatchQuery('path', item.path))
753+
dup_items = session.lib.items(
754+
library.MatchQuery('path', item.path)
755+
)
750756
for dup_item in dup_items:
751757
task.replaced_items[item].append(dup_item)
752758
log.debug('replacing item %i: %s' %

beets/library.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,58 +1508,42 @@ def move(self, item, copy=False, basedir=None,
15081508

15091509
# Querying.
15101510

1511-
def albums(self, query=None, artist=None):
1512-
"""Returns a sorted list of Album objects, possibly filtered
1513-
by an artist name or an arbitrary query. Unqualified query
1514-
string terms only match fields that apply at an album
1515-
granularity: artist, album, and genre.
1511+
def _fetch(self, model_cls, order_by, query):
1512+
"""Fetch the objects of type `model_cls` matching the given
1513+
query. The query may be given as a string, string sequence, a
1514+
Query object, or None (to fetch everything).
15161515
"""
1517-
query = get_query(query, True)
1518-
if artist is not None:
1519-
# "Add" the artist to the query.
1520-
query = AndQuery((query, MatchQuery('albumartist', artist)))
1516+
query = get_query(query, model_cls is Album)
15211517

15221518
where, subvals = query.clause()
15231519
with self.transaction() as tx:
15241520
rows = tx.query(
1525-
"SELECT * FROM albums WHERE {0} "
1526-
"ORDER BY {1}, album".format(
1527-
where or '1',
1528-
_orelse("albumartist_sort", "albumartist"),
1521+
"SELECT * FROM {table} WHERE {where} "
1522+
"ORDER BY {order_by}".format(
1523+
table=model_cls._table,
1524+
where=where or '1',
1525+
order_by=order_by
15291526
),
1530-
subvals,
1527+
subvals
15311528
)
15321529

1533-
return Results(Album, rows, self, None if where else query)
1534-
1535-
def items(self, query=None, artist=None, album=None, title=None):
1536-
"""Returns a sequence of the items matching the given artist,
1537-
album, title, and query (if present). Sorts in such a way as to
1538-
group albums appropriately. Unqualified query string terms only
1539-
match intuitively relevant fields: artist, album, genre, title,
1540-
and comments.
1541-
"""
1542-
queries = [get_query(query, False)]
1543-
if artist is not None:
1544-
queries.append(MatchQuery('artist', artist))
1545-
if album is not None:
1546-
queries.append(MatchQuery('album', album))
1547-
if title is not None:
1548-
queries.append(MatchQuery('title', title))
1549-
query = AndQuery(queries)
1550-
where, subvals = query.clause()
1530+
return Results(model_cls, rows, self, None if where else query)
15511531

1552-
with self.transaction() as tx:
1553-
rows = tx.query(
1554-
"SELECT * FROM items WHERE {0} "
1555-
"ORDER BY {1}, album, disc, track".format(
1556-
where or '1',
1557-
_orelse("artist_sort", "artist"),
1558-
),
1559-
subvals
1560-
)
1532+
def albums(self, query=None):
1533+
"""Get a sorted list of Album objects matching the given query.
1534+
"""
1535+
order = '{0}, album'.format(
1536+
_orelse("albumartist_sort", "albumartist")
1537+
)
1538+
return self._fetch(Album, order, query)
15611539

1562-
return Results(Item, rows, self, None if where else query)
1540+
def items(self, query=None):
1541+
"""Get a sorted list of Item objects matching the given query.
1542+
"""
1543+
order = '{0}, album'.format(
1544+
_orelse("artist_sort", "artist")
1545+
)
1546+
return self._fetch(Item, order, query)
15631547

15641548

15651549
# Convenience accessors.
@@ -1932,7 +1916,7 @@ def tmpl_aunique(self, keys=None, disam=None):
19321916
for key in keys:
19331917
value = getattr(album, key)
19341918
subqueries.append(MatchQuery(key, value))
1935-
albums = self.lib.albums(query=AndQuery(subqueries))
1919+
albums = self.lib.albums(AndQuery(subqueries))
19361920

19371921
# If there's only one album to matching these details, then do
19381922
# nothing.

beetsplug/random.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def random_item(lib, opts, args):
3232
template = Template(fmt) if fmt else None
3333

3434
if opts.album:
35-
objs = list(lib.albums(query=query))
35+
objs = list(lib.albums(query))
3636
else:
37-
objs = list(lib.items(query=query))
37+
objs = list(lib.items(query))
3838

3939
if opts.equal_chance:
4040
# Group the objects by artist so we can sample from them.

0 commit comments

Comments
 (0)