Skip to content

Commit

Permalink
library: remove compatibility code for model v1
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Jan 16, 2024
1 parent e7994ff commit f14f3f4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 84 deletions.
10 changes: 4 additions & 6 deletions feeluown/gui/components/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from feeluown.excs import ProviderIOError
from feeluown.utils.aio import run_fn, run_afn
from feeluown.player import SongRadio
from feeluown.library import SongProtocol, VideoModel, SupportsSongMV
from feeluown.library import SongProtocol, VideoModel

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -108,11 +108,9 @@ def mv_fetched_cb(future):
if data['mvs'] is None and self._fetching_mv is False:
logger.debug('fetch song.mv for actions')
song = data['song']
provider = self._app.library.get(song.source)
if provider is not None and isinstance(provider, SupportsSongMV):
self._fetching_mv = True
task = run_fn(provider.song_get_mv, song)
task.add_done_callback(mv_fetched_cb)
self._fetching_mv = True
task = run_fn(self._app.library.song_get_mv, song)
task.add_done_callback(mv_fetched_cb)

def _hover_artists(self, action, data):
# pylint: disable=unnecessary-direct-lambda-call
Expand Down
3 changes: 1 addition & 2 deletions feeluown/gui/page_containers/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from feeluown.utils.reader import wrap
from feeluown.media import Media, MediaType
from feeluown.excs import ProviderIOError
from feeluown.library import ModelState, NotSupported, ModelFlags, ModelType
from feeluown.library import ModelState, NotSupported, ModelType

from feeluown.gui.helpers import BgTransparentMixin, \
disconnect_slots_if_has, fetch_cover_wrapper
Expand Down Expand Up @@ -502,7 +502,6 @@ async def _on_songs_table_activated(self, index):
song = await aio.run_in_executor(
None, self._app.library.song_upgrade, song)
except NotSupported as e:
assert ModelFlags.v2 & song.meta.flags
self._app.show_msg(f'资源提供方不支持该功能: {str(e)}')
logger.info(f'provider:{song.source} does not support song_get')
song.state = ModelState.cant_upgrade
Expand Down
7 changes: 3 additions & 4 deletions feeluown/gui/pages/song_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,9 @@ async def maybe_show_song_pic(self, song, album):
album.cover,
reverse(album) + '/cover')
else:
if ModelFlags.v2 in song.meta.flags:
aio.run_afn(self.cover_label.show_cover,
song.pic_url,
reverse(song) + '/pic_url')
aio.run_afn(self.cover_label.show_cover,
song.pic_url,
reverse(song) + '/pic_url')

def resizeEvent(self, e: QResizeEvent) -> None:
margins = self.layout().contentsMargins()
Expand Down
84 changes: 29 additions & 55 deletions feeluown/library/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import logging
import warnings
from functools import partial
from typing import cast, Optional, Union, TypeVar, Type, Callable, Any
from typing import Optional, Union, TypeVar, Type, Callable, Any

from feeluown.media import Media
from feeluown.utils import aio
from feeluown.utils.aio import run_fn
from feeluown.utils.dispatch import Signal
from feeluown.utils.reader import create_reader
from .base import SearchType, ModelType
Expand Down Expand Up @@ -136,7 +137,11 @@ def err_provider_not_support_flag(pid, model_type, op):


class Library:
"""音乐库,管理资源提供方以及资源"""
"""音乐库,管理资源提供方以及资源
.. versionchanged:: 4.0
Never raise ProviderNotFound error.
"""

def __init__(self, providers_standby=None):
"""
Expand Down Expand Up @@ -250,9 +255,7 @@ async def a_list_song_standby_v2(self, song,
async def prepare_media(standby, policy):
media = None
try:
media = await aio.run_in_executor(None,
self.song_prepare_media,
standby, policy)
await run_fn(self.song_prepare_media, standby, policy)
except MediaNotFound:
pass
except: # noqa
Expand Down Expand Up @@ -358,23 +361,12 @@ def song_upgrade(self, song: BriefSongProtocol) -> SongProtocol:
def song_prepare_media(self, song: BriefSongProtocol, policy) -> Media:
provider = self.get(song.source)
if provider is None:
# FIXME: raise ProviderNotfound
raise MediaNotFound(f'provider:{song.source} not found')
if song.meta.flags & MF.v2:
support_or_raise(provider, SupportsSongMultiQuality)
provider = cast(SupportsSongMultiQuality, provider)
raise MediaNotFound(f'provider({song.source}) not found')
media = None
if isinstance(provider, SupportsSongMultiQuality):
media, _ = provider.song_select_media(song, policy)
else:
if song.meta.support_multi_quality:
media, _ = song.select_media(policy) # type: ignore
else:
url = song.url # type: ignore
if url:
media = Media(url)
else:
raise MediaNotFound
if not media:
raise MediaNotFound
raise MediaNotFound('provider returns empty media')
return media

def song_prepare_mv_media(self, song: BriefSongProtocol, policy) -> Media:
Expand All @@ -386,15 +378,14 @@ def song_prepare_mv_media(self, song: BriefSongProtocol, policy) -> Media:
if mv is not None:
media = self.video_prepare_media(mv, policy)
return media
raise MediaNotFound
raise MediaNotFound('provider returns empty media')

def song_get_mv(self, song: BriefSongProtocol) -> Optional[VideoProtocol]:
"""Get the MV model of a song.
:raises NotSupported:
:raises ProviderNotFound:
"""
provider = self.get_or_raise(song.source)
provider = self.get(song.source)
if isinstance(provider, SupportsSongMV):
mv = provider.song_get_mv(song)
else:
Expand Down Expand Up @@ -571,24 +562,18 @@ def model_get_cover(self, model):
:param model: model which has a 'cover' field.
:return: cover url if exists, else ''.
"""
if MF.v2 in model.meta.flags:
if MF.normal not in model.meta.flags:
try:
um = self._model_upgrade(model)
except (ResourceNotFound, NotSupported):
return ''
else:
um = model
# FIXME: remove this hack lator.
if ModelType(model.meta.model_type) is ModelType.artist:
cover = um.pic_url
else:
cover = um.cover
if MF.normal not in model.meta.flags:
try:
um = self._model_upgrade(model)
except (ResourceNotFound, NotSupported):
return ''
else:
cover = model.cover
# Check if cover is a media object.
if cover and not isinstance(cover, str):
cover = cover.url
um = model
# FIXME: remove this hack lator.
if ModelType(model.meta.model_type) is ModelType.artist:
cover = um.pic_url
else:
cover = um.cover
return cover

def _model_upgrade(self, model):
Expand All @@ -604,10 +589,6 @@ def _model_upgrade(self, model):
Raise ModelNotFound if the model does not exist.
Before ModelCannotUpgrade was raised.
"""
# Upgrade model in v1 way if it is a v1 model.
if MF.v2 not in model.meta.flags:
return self._model_upgrade_in_v1_way(model)

# Return model directly if it is already a normal model.
if MF.normal in model.meta.flags:
return model
Expand Down Expand Up @@ -643,18 +624,11 @@ def video_prepare_media(self, video: BriefVideoProtocol, policy) -> Media:
:param video: either a v1 MvModel or a v2 (Brief)VideoModel.
"""
provider = self.get_or_raise(video.source)
if video.meta.flags & MF.v2:
# provider MUST has multi_quality flag for video
assert isinstance(provider, SupportsVideoMultiQuality)
media, _ = provider.video_select_media(video, policy)
else:
# V1 VideoModel has attribute `media`
if video.meta.support_multi_quality:
media, _ = video.select_media(policy) # type: ignore
else:
media = video.media # type: ignore
# provider MUST has multi_quality flag for video
assert isinstance(provider, SupportsVideoMultiQuality)
media, _ = provider.video_select_media(video, policy)
if not media:
raise MediaNotFound
raise MediaNotFound('provider returns empty media')
return media

# --------
Expand Down
4 changes: 2 additions & 2 deletions feeluown/player/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from feeluown.utils.utils import DedupList
from feeluown.player import Metadata, MetadataFields
from feeluown.library import (
MediaNotFound, SongProtocol, ModelType, NotSupported, ResourceNotFound
MediaNotFound, SongProtocol, ModelType, NotSupported, ResourceNotFound,
)
from feeluown.media import Media
from feeluown.library import reverse
Expand Down Expand Up @@ -480,7 +480,7 @@ async def a_set_current_song(self, song):
await self.a_set_current_song_children(song)
return

logger.info(f'{song_str} has no valid media, mark it as bad')
logger.info(f'no media found for {song_str} due to {e}, mark it as bad')
self.mark_as_bad(song)
except ProviderIOError as e:
# FIXME: This may cause infinite loop when the prepare media always fails
Expand Down
25 changes: 10 additions & 15 deletions feeluown/serializers/model_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from feeluown.library import AbstractProvider
from feeluown.library import (
ModelFlags,

BaseModel,
SongModel,
ArtistModel,
Expand All @@ -23,19 +21,16 @@ def _get_items(self, model):
# initialize fields that need to be serialized
# if as_line option is set, we always use fields_display
if self.opt_as_line or self.opt_brief:
if ModelFlags.v2 in model.meta.flags:
modelcls = type(model)
fields = [field for field in model.__fields__
if field not in BaseModel.__fields__]
# Include properties.
pydantic_fields = ("__values__", "fields", "__fields_set__",
"model_computed_fields", "model_extra",
"model_fields_set")
fields += [prop for prop in dir(modelcls)
if isinstance(getattr(modelcls, prop), property)
and prop not in pydantic_fields]
else:
fields = model.meta.fields_display
modelcls = type(model)
fields = [field for field in model.__fields__
if field not in BaseModel.__fields__]
# Include properties.
pydantic_fields = ("__values__", "fields", "__fields_set__",
"model_computed_fields", "model_extra",
"model_fields_set")
fields += [prop for prop in dir(modelcls)
if isinstance(getattr(modelcls, prop), property)
and prop not in pydantic_fields]
else:
fields = self._declared_fields
items = [("provider", model.source),
Expand Down

0 comments on commit f14f3f4

Please sign in to comment.