Skip to content

Commit

Permalink
Implement dsc ls command (list releases)
Browse files Browse the repository at this point in the history
- Basically a start to replace `dsc search`
- but most of all a way fo finally create filtered lists of releases
- Super simple query language: field=value
- If field= is missing, asssume title=
- Simple tabulate table layout
- Fixme: All releases is still old "All releases search results table"
  • Loading branch information
Johannes Tiefenbacher committed Oct 27, 2024
1 parent 6baea86 commit 6af7e8b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
12 changes: 11 additions & 1 deletion discodos/cmd23/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/usr/bin/env python
from discodos.config import Config
from discodos.cmd23 import helper, import_, mix, search, setup, stats, suggest
from discodos.cmd23 import (
helper,
import_,
mix,
search,
setup,
stats,
suggest,
ls,
)
import logging
import click

Expand Down Expand Up @@ -38,3 +47,4 @@ def main_cmd(context, verbose_count, offline_mode):
main_cmd.add_command(suggest.suggest_cmd)
main_cmd.add_command(stats.stats_cmd)
main_cmd.add_command(setup.setup_cmd)
main_cmd.add_command(ls.ls_cmd)
47 changes: 47 additions & 0 deletions discodos/cmd23/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import click
import logging

from discodos.ctrls import Coll_ctrl_cli

log = logging.getLogger('discodos')


@click.command(
context_settings=dict(
ignore_unknown_options=True,
# allow_extra_args=True,
),
name="ls",
)
@click.argument("search_terms", metavar="SEARCH_TERMS", nargs=-1)
@click.pass_obj
def ls_cmd(helper, search_terms):
"""Searches and lists collection items - offline only!.
"""
def update_user_interaction_helper(user):
user.WANTS_ONLINE = False
user.WANTS_TO_SEARCH_FOR_RELEASE = True
if not search_terms:
user.WANTS_TO_LIST_ALL_RELEASES = True
user.WANTS_TO_SEARCH_FOR_RELEASE = False
return user

user = update_user_interaction_helper(helper)
log.info("user.WANTS_ONLINE: %s", user.WANTS_ONLINE)
coll_ctrl = Coll_ctrl_cli(
False, user, user.conf.discogs_token, user.conf.discogs_appid,
user.conf.discobase, user.conf.musicbrainz_user,
user.conf.musicbrainz_password)

try: # Catch when `=` character is missing and assume title column
search_key_value = dict([
item.split("=") if "=" in item else ["title", item]
for item in search_terms
])
except ValueError as error:
coll_ctrl.cli.p(error)

if user.WANTS_TO_LIST_ALL_RELEASES:
coll_ctrl.view_all_releases()
else:
coll_ctrl.ls_releases(search_key_value)
18 changes: 18 additions & 0 deletions discodos/ctrls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,3 +1148,21 @@ def view_stats(self):
self.collection.stats_tracks_bpm_brainz(),
self.collection.stats_tracks_bpm_manual(),
)

def ls_releases(self, search_terms):
"""search_terms is a key value dict: eg: d_artist: artistname"""

search_results = []
self.cli.p('Searching database for: {}'.format(search_terms))
try:
search_results = self.collection.key_value_search_releases(
search_key_value=search_terms
)
except Exception as error:
self.cli.p(error)

if not search_results:
self.cli.p('Nothing found.')
else:
self.cli.p('Found releases:')
self.cli.tab_ls_releases(search_results)
27 changes: 27 additions & 0 deletions discodos/model_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ def get_all_db_releases(self, orderby='d_artist, discogs_title'):
'release', orderby=orderby
)

def key_value_search_releases(self, search_key_value=None, orderby='d_artist, discogs_title'):
replace_cols = {
"artist": "d_artist",
"title": "discogs_title",
"id": "discogs_id",
"cat": "d_catno",
}
where = " AND ".join(
[
f'{replace_cols.get(k, k)} LIKE "%{v}%"'
for k, v in search_key_value.items()
]
)

return self._select_simple(
[
"discogs_id",
"d_catno",
"d_artist",
"discogs_title",
"in_d_collection",
"sold",
],
"release", fetchone=False, orderby=orderby, condition=where,
)


def search_release_online(self, id_or_title):
try:
if is_number(id_or_title):
Expand Down
9 changes: 9 additions & 0 deletions discodos/view_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ def tab_online_search_results(self, _result_list):
'format': 'Format'
}))

def tab_ls_releases(self, _result_list):
self.p(
tab(
_result_list,
tablefmt="simple",
headers=["ID", "Cat. #", "Artist", "Title", "D. Coll.", "Sold"],
)
)

def online_search_results_tracklist(self, _tracklist):
for tr in _tracklist:
try:
Expand Down

0 comments on commit 6af7e8b

Please sign in to comment.