Skip to content

Commit

Permalink
library: remove NotSupported exception (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven authored Jan 20, 2024
1 parent 356c577 commit a9f1258
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 69 deletions.
38 changes: 9 additions & 29 deletions feeluown/excs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ class ReadFailed(ProviderIOError):


class ResourceNotFound(LibraryException):
pass


class ProviderAlreadyRegistered(LibraryException):
pass


class ModelNotFound(ResourceNotFound):
"""Model is not found
For example, a model identifier is invalid.
.. versionadded:: 3.7.7
"""
class Reason(Enum):
"""
.. versionadded:: v4.0
Expand All @@ -75,23 +61,17 @@ def __init__(self, *args, reason=Reason.not_found, **kwargs) -> None:
self.reason = reason


class NotSupported(LibraryException):
"""Provider does not support the operation
"""
class ProviderAlreadyRegistered(LibraryException):
pass

def __init__(self, *args, provider=None, **kwargs):
self.provider = provider

@classmethod
def create_by_p_p(cls, provider, protocol_cls):
if isinstance(provider, str):
pid = provider
else:
if hasattr(provider, 'meta'):
pid = provider.meta.identifier
else:
pid = provider.identifier
return cls(f'provider:{pid} does not support {protocol_cls.__name__}')
class ModelNotFound(ResourceNotFound):
"""Model is not found
For example, a model identifier is invalid.
.. versionadded:: 3.7.7
"""


class MediaNotFound(ResourceNotFound):
Expand Down
12 changes: 4 additions & 8 deletions feeluown/gui/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from feeluown.utils.reader import AsyncReader, Reader
from feeluown.utils.typing_ import Protocol
from feeluown.excs import ProviderIOError, ResourceNotFound
from feeluown.library import NotSupported, ModelType, BaseModel
from feeluown.library import ModelNotFound, ModelType, BaseModel
from feeluown.library import reverse


Expand Down Expand Up @@ -494,7 +494,7 @@ async def fetch_song_pic_url(model, cb):
if not is_v2_model:
try:
upgraded_song = await run_fn(library.song_upgrade, model)
except NotSupported:
except ModelNotFound:
cb(None)
else:
await fetch_song_pic_from_album(upgraded_song.album, cb)
Expand All @@ -514,7 +514,7 @@ async def fetch_song_pic_url(model, cb):
# Image is not in cache.
try:
upgraded_song = await run_fn(library.song_upgrade, model)
except (NotSupported, ResourceNotFound):
except ResourceNotFound:
cb(None)
else:
# Try to fetch with pic_url first.
Expand Down Expand Up @@ -542,11 +542,7 @@ async def fetch_other_model_cover(model, cb):
cb(content)
return

try:
img_url = await run_fn(library.model_get_cover, model)
except NotSupported:
img_url = ''

img_url = await run_fn(library.model_get_cover, model)
return await fetch_image_with_cb(img_uid, img_url, cb)

async def fetch_model_cover(model, cb):
Expand Down
4 changes: 2 additions & 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, ModelType
from feeluown.library import ModelState, ModelType, ModelNotFound

from feeluown.gui.helpers import BgTransparentMixin, \
disconnect_slots_if_has, fetch_cover_wrapper
Expand Down Expand Up @@ -501,7 +501,7 @@ async def _on_songs_table_activated(self, index):
try:
song = await aio.run_in_executor(
None, self._app.library.song_upgrade, song)
except NotSupported as e:
except ModelNotFound as e:
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
6 changes: 3 additions & 3 deletions feeluown/gui/pages/song_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from feeluown.excs import ResourceNotFound
from feeluown.library import (
SupportsSongHotComments, SupportsSongSimilar, SupportsSongWebUrl,
NotSupported, ModelFlags
ModelFlags,
)
from feeluown.player import Lyric
from feeluown.library import reverse, resolve
Expand Down Expand Up @@ -63,7 +63,7 @@ async def render(req, **kwargs): # pylint: disable=too-many-locals,too-many-bra

try:
upgraded_song = await aio.run_fn(app.library.song_upgrade, song)
except (NotSupported, ResourceNotFound):
except ResourceNotFound:
upgraded_song = None
if upgraded_song is not None:
song = upgraded_song
Expand All @@ -81,7 +81,7 @@ async def render(req, **kwargs): # pylint: disable=too-many-locals,too-many-bra
if album is not None:
try:
upgraded_album = await aio.run_fn(app.library.album_upgrade, song.album)
except (NotSupported, ResourceNotFound):
except ResourceNotFound:
upgraded_album = None
else:
upgraded_album = None
Expand Down
2 changes: 1 addition & 1 deletion feeluown/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
fmt_artists_names, AlbumType, SimpleSearchResult, \
get_modelcls_by_type, \
V2SupportedModelTypes
from .excs import NotSupported, NoUserLoggedIn, ModelNotFound, \
from .excs import NoUserLoggedIn, ModelNotFound, \
ProviderAlreadyExists, ResourceNotFound, MediaNotFound
from .provider_protocol import *
from .uri import (
Expand Down
1 change: 0 additions & 1 deletion feeluown/library/excs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# FIXME: ProviderAlreadyExists should be renamed to ProviderAlreadyRegistered
ProviderAlreadyRegistered as ProviderAlreadyExists,
ModelNotFound,
NotSupported,
NoUserLoggedIn,
MediaNotFound,
) # noqa
17 changes: 2 additions & 15 deletions feeluown/library/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
from feeluown.utils.dispatch import Signal
from .base import SearchType, ModelType
from .provider import Provider
from .excs import (
NotSupported, MediaNotFound, ProviderAlreadyExists,
ModelNotFound, ResourceNotFound
)
from .excs import MediaNotFound, ProviderAlreadyExists, ModelNotFound, ResourceNotFound
from .flags import Flags as PF
from .models import (
ModelFlags as MF, BaseModel,
Expand Down Expand Up @@ -68,14 +65,6 @@ def duration_ms_to_duration(ms):
return score


def err_provider_not_support_flag(pid, model_type, op):
op_str = str(op)
if op is PF.get:
op_str = 'get'
mtype_str = str(ModelType(model_type))
return NotSupported(f"provider:{pid} does't support '{op_str}' for {mtype_str}")


class Library:
"""Resource entrypoints."""

Expand Down Expand Up @@ -346,7 +335,6 @@ def model_get(self, pid, mtype, mid):
:param mid: model id
:return: model
:raise NotSupported: provider has not .get for this model type
:raise ResourceNotFound: model does not exist
"""
provider = self.get(pid)
Expand All @@ -363,7 +351,7 @@ def model_get_cover(self, model):
if MF.normal not in model.meta.flags:
try:
um = self._model_upgrade(model)
except (ResourceNotFound, NotSupported):
except ResourceNotFound:
return ''
else:
um = model
Expand All @@ -377,7 +365,6 @@ def model_get_cover(self, model):
def _model_upgrade(self, model):
"""Upgrade a model to normal model.
:raises NotSupported: provider does't impl SupportGetProtocol for the model type
:raises ModelNotFound: the model does not exist
Note you may catch ResourceNotFound exception to simplify your code.
Expand Down
3 changes: 1 addition & 2 deletions feeluown/library/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from .base import ModelType
from .models import V2SupportedModelTypes
from .flags import Flags
from .excs import MediaNotFound, ModelNotFound, NoUserLoggedIn, \
NotSupported # noqa
from .excs import MediaNotFound, ModelNotFound, NoUserLoggedIn # noqa


class AbstractProvider(ABC):
Expand Down
10 changes: 5 additions & 5 deletions feeluown/player/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from enum import IntEnum, Enum
from typing import Optional, TYPE_CHECKING

from feeluown.excs import ProviderIOError
from feeluown.excs import ModelNotFound, ProviderIOError
from feeluown.utils import aio
from feeluown.utils.aio import run_fn, run_afn
from feeluown.utils.dispatch import Signal
from feeluown.utils.utils import DedupList
from feeluown.player import Metadata, MetadataFields
from feeluown.library import (
MediaNotFound, SongModel, ModelType, NotSupported, ResourceNotFound,
MediaNotFound, SongModel, ModelType, ResourceNotFound,
)
from feeluown.media import Media
from feeluown.library import reverse
Expand Down Expand Up @@ -575,7 +575,7 @@ async def _prepare_metadata_for_song(self, song):
})
try:
song = await aio.run_fn(self._app.library.song_upgrade, song)
except (NotSupported, ResourceNotFound):
except ResourceNotFound:
return metadata
except: # noqa
logger.exception(f"fetching song's meta failed, song:'{song.title_display}'")
Expand All @@ -586,7 +586,7 @@ async def _prepare_metadata_for_song(self, song):
if song.album is not None:
try:
album = await aio.run_fn(self._app.library.album_upgrade, song.album)
except (NotSupported, ResourceNotFound):
except ResourceNotFound:
pass
except: # noqa
logger.warning(
Expand Down Expand Up @@ -618,7 +618,7 @@ async def _prepare_metadata_for_video(self, video):
})
try:
video = await aio.run_fn(self._app.library.video_upgrade, video)
except NotSupported as e:
except ModelNotFound as e:
logger.warning(f"can't get cover of video due to {str(e)}")
else:
metadata[MetadataFields.artwork] = video.cover
Expand Down
4 changes: 1 addition & 3 deletions feeluown/server/handlers/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from feeluown.utils.utils import to_readall_reader
from feeluown.utils.router import Router, NotFound
from feeluown.library import NotSupported, ResourceNotFound
from feeluown.library import ResourceNotFound
from feeluown.library import NS_TYPE_MAP
from feeluown.library import ModelType

Expand Down Expand Up @@ -46,8 +46,6 @@ def handle(self, cmd):
raise HandlerException(f'path {path} not found') from None
except ResourceNotFound as e:
raise HandlerException(str(e)) from e
except NotSupported as e:
raise HandlerException(str(e)) from None
return rv


Expand Down

0 comments on commit a9f1258

Please sign in to comment.