Skip to content

Commit

Permalink
Make model helper collect artist models
Browse files Browse the repository at this point in the history
Refs: #143
  • Loading branch information
orontee committed Jul 12, 2023
1 parent 884e87b commit be14609
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion argos/controllers/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def complete_album_description(self, message: Message) -> None:

length_acc = LengthAcc()
metadata_collector = AlbumMetadataCollector()
parsed_tracks = self.helper.parse_tracks(
parsed_tracks = self._helper.parse_tracks(
tracks_dto, visitors=[length_acc, metadata_collector]
).get(album_uri, [])

Expand Down
4 changes: 2 additions & 2 deletions argos/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
if TYPE_CHECKING:
from argos.app import Application

from argos.controllers.helper import ModelHelper
from argos.http import MopidyHTTPClient
from argos.message import Message, MessageType
from argos.model import Model
from argos.model.helper import ModelHelper
from argos.notify import Notifier


Expand All @@ -36,7 +36,7 @@ def __init__(
self._notifier: Notifier = application.props.notifier
self._settings: Gio.Settings = application.props.settings

self.helper = ModelHelper()
self._helper: ModelHelper = self._model.helper

def send_message(
self, message_type: MessageType, data: Optional[Dict[str, Any]] = None
Expand Down
4 changes: 2 additions & 2 deletions argos/controllers/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async def _complete_albums(
)

LOGGER.debug("Parsing albums tracks")
parsed_tracks = self.helper.parse_tracks(
parsed_tracks = self._helper.parse_tracks(
directory_tracks_dto, visitors=[length_acc, metadata_collector]
)

Expand Down Expand Up @@ -438,7 +438,7 @@ async def _complete_tracks(

LOGGER.debug("Parsing tracks")
parsed_tracks: List[TrackModel] = []
for tracks in self.helper.parse_tracks(directory_tracks_dto).values():
for tracks in self._helper.parse_tracks(directory_tracks_dto).values():
for track in tracks:
track_uri = track.uri
if images is not None and len(images.get(track_uri, [])) > 0:
Expand Down
4 changes: 2 additions & 2 deletions argos/controllers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async def _complete_playlist_from(
params=track_uris,
)
parsed_tracks: List[TrackModel] = []
for tracks in self.helper.parse_tracks(
for tracks in self._helper.parse_tracks(
found_tracks_dto, visitors=[PlaylistTrackNameFix(playlist_dto)]
).values():
parsed_tracks += tracks
Expand Down Expand Up @@ -269,7 +269,7 @@ async def __complete_history_playlist(self) -> None:
)

parsed_history_tracks_with_duplicates: List[TrackModel] = []
parsed_history_tracks: Dict[str, List[TrackModel]] = self.helper.parse_tracks(
parsed_history_tracks: Dict[str, List[TrackModel]] = self._helper.parse_tracks(
history_tracks_dto
)
for history_item in history:
Expand Down
2 changes: 1 addition & 1 deletion argos/controllers/tracklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def get_tracklist(self, message: Message) -> None:

tl_tracks = (
[
self.helper.convert_tl_track(tl_track_dto)
self._helper.convert_tl_track(tl_track_dto)
for tl_track_dto in tl_tracks_dto
]
if tl_tracks_dto is not None
Expand Down
2 changes: 1 addition & 1 deletion argos/controllers/visitors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Visitors for tracks DTOs.
See ``argos.controllers.helper.ModelHelper.parse_tracks()``.
See ``argos.model.helper.ModelHelper.parse_tracks()``.
"""
import logging
Expand Down
41 changes: 32 additions & 9 deletions argos/controllers/helper.py → argos/model/helper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from collections import defaultdict
from itertools import chain
from typing import Callable, Dict, List, Mapping, Optional, Sequence

from argos.dto import AlbumDTO, ArtistDTO, TlTrackDTO, TrackDTO
from argos.model import AlbumModel, ArtistModel, TracklistTrackModel, TrackModel
from argos.model.album import AlbumModel
from argos.model.artist import ArtistModel
from argos.model.track import TrackModel
from argos.model.tracklist import TracklistTrackModel


class ModelHelper:
Expand All @@ -12,27 +16,43 @@ class ModelHelper:
"""

def __init__(self):
self._artists: Dict[str, ArtistModel] = {}

def convert_album(self, album_dto: AlbumDTO) -> AlbumModel:
for artist_dto in album_dto.artists:
self.convert_artist(artist_dto)

album = AlbumModel(
uri=album_dto.uri,
name=album_dto.name,
num_tracks=album_dto.num_tracks,
num_discs=album_dto.num_discs,
date=album_dto.date,
)
# TODO album dto has artists

return album

def convert_artist(self, artist_dto: ArtistDTO) -> ArtistModel:
artist = ArtistModel(
uri=artist_dto.uri,
name=artist_dto.name,
sortname=artist_dto.name,
artist_mbid=artist_dto.musicbrainz_id,
)
return artist
if artist_dto.uri not in self._artists:
artist = ArtistModel(
uri=artist_dto.uri,
name=artist_dto.name,
sortname=artist_dto.name,
artist_mbid=artist_dto.musicbrainz_id,
)
self._artists[artist_dto.uri] = artist

return self._artists[artist_dto.uri]

def convert_track(self, track_dto: TrackDTO) -> TrackModel:
for artist_dto in chain(
track_dto.artists,
track_dto.composers,
track_dto.performers,
):
self.convert_artist(artist_dto)

track = TrackModel(
uri=track_dto.uri,
name=track_dto.name,
Expand Down Expand Up @@ -83,3 +103,6 @@ def parse_tracks(
parsed_tracks[uri].append(self.convert_track(track_dto))

return parsed_tracks

def get_artist(self, uri: str) -> Optional[ArtistModel]:
return self._artists.get(uri)
9 changes: 8 additions & 1 deletion argos/model/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import random
import threading
from datetime import datetime
from functools import partial
Expand All @@ -15,13 +14,15 @@
compare_albums_by_name_func,
compare_albums_by_publication_date_func,
)
from argos.model.artist import ArtistModel
from argos.model.backends import (
GenericBackend,
MopidyBackend,
MopidyBandcampBackend,
MopidyPodcastBackend,
)
from argos.model.directory import DirectoryModel, compare_directories_func
from argos.model.helper import ModelHelper
from argos.model.library import LibraryModel
from argos.model.mixer import MixerModel
from argos.model.playback import PlaybackModel
Expand Down Expand Up @@ -61,6 +62,7 @@ class Model(WithThreadSafePropertySetter, GObject.Object):
tracklist: TracklistModel
playlists: Gio.ListStore
backends: Gio.ListStore
helper: ModelHelper

def __init__(self, application: "Application", *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -80,6 +82,8 @@ def __init__(self, application: "Application", *args, **kwargs):
self.backends.append(GenericBackend())
# Must be the last one!

self.helper = ModelHelper()

application._nm.connect("network-changed", self._on_nm_network_changed)

self.playback.connect(
Expand Down Expand Up @@ -390,6 +394,9 @@ def _complete_playlist_description() -> None:
def get_album(self, uri: str) -> Optional[AlbumModel]:
return self.library.get_album(uri)

def get_artist(self, uri: str) -> Optional[ArtistModel]:
return self.helper.get_artist(uri)

def get_directory(self, uri: Optional[str]) -> Optional[DirectoryModel]:
return self.library.get_directory(uri)

Expand Down
2 changes: 1 addition & 1 deletion tests/controllers/test_visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import unittest
from copy import copy

from argos.controllers.helper import ModelHelper
from argos.controllers.visitors import (
AlbumMetadataCollector,
LengthAcc,
PlaylistTrackNameFix,
)
from argos.dto import PlaylistDTO, TrackDTO, cast_seq_of
from argos.model.helper import ModelHelper


def load_json_data(filename: str):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import pathlib
import unittest
from unittest.mock import Mock, call
from unittest.mock import Mock

from argos.controllers.helper import ModelHelper
from argos.dto import TrackDTO
from argos.model.helper import ModelHelper
from argos.model.track import TrackModel


Expand Down

0 comments on commit be14609

Please sign in to comment.