From 44ea89ef63cd1eb77639ec1a88f99491cc91cd3a Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 7 Feb 2020 09:17:26 +0100 Subject: [PATCH] adhere to comments --- src/galaxy/api/plugin.py | 48 ++++++++---------------- src/galaxy/api/types.py | 8 +--- tests/test_subscriptions.py | 73 ++----------------------------------- 3 files changed, 22 insertions(+), 107 deletions(-) diff --git a/src/galaxy/api/plugin.py b/src/galaxy/api/plugin.py index b97072e..84f1c0b 100644 --- a/src/galaxy/api/plugin.py +++ b/src/galaxy/api/plugin.py @@ -43,7 +43,7 @@ class Importer: notification_failure, notification_finished, complete, - yielding=False + is_generator=False ): self._task_manager = task_manger self._name = name @@ -55,16 +55,13 @@ class Importer: self._complete = complete self._import_in_progress = False - self._yielding = yielding + self._is_generator = is_generator async def _import_element(self, id_, context_): try: - if self._yielding: + if self._is_generator: async for element in self._get(id_, context_): self._notification_success(id_, element) - if element is None: - logger.debug("None element yielded, import finished") - break else: element = await self._get(id_, context_) self._notification_success(id_, element) @@ -195,7 +192,7 @@ class Plugin: self._subscription_games_import_failure, self._subscription_games_import_finished, self.subscription_games_import_complete, - yielding=True + is_generator=True ) # internal @@ -1101,21 +1098,10 @@ class Plugin: async def get_subscriptions(self) -> List[Subscription]: """Override this method to return a list of available - Subscriptions available on platform. + Subscriptions available on platform. This method is called by the GOG Galaxy Client. Example of possible override of the method: - - .. code-block:: python - :linenos: - - async def get_subscriptions(self, game_id): - subs = [] - platform_subs_info = await self.retrieve_platform_subs_info() - for sub_info in platform_subs_info: - subs.append(Subscription(subscription_name=sub_info['name'], owned=sub_info['is_owned'])) - return subs - """ raise NotImplementedError() @@ -1126,31 +1112,29 @@ class Plugin: """Override this method to prepare context for :meth:`get_subscription_games` Default implementation returns None. - :param subscription_names: the names of the subscriptions for which subscriptions games are imported + :param subscription_names: the names of the subscriptions' for which subscriptions games are imported :return: context """ return None - async def get_subscription_games(self, subscription_name: str, context: Any) -> AsyncGenerator[List[SubscriptionGame],None]: - """Override this method to return SubscriptionGames for a given subscription. - This method should `yield` results from a list of SubscriptionGames + async def get_subscription_games(self, subscription_name: str, context: Any) -> AsyncGenerator[List[SubscriptionGame], None]: + """Override this method to provide SubscriptionGames for a given subscription. + This method should `yield` a list of SubscriptionGames -> yield [sub_games] - Both this method and get_subscriptions are required to be overridden + Both this method and :meth:`get_subscriptions` are required to be overridden for the ImportSubscriptionGames feature to be recognized :param context: the value returned from :meth:`prepare_subscription_games_context` - :return yield List of subscription games. + :return a generator object that yields SubscriptionGames .. code-block:: python :linenos: - async def get_sub_games(sub_name: str, context: Any): - for i in range(10): - try: - games_page = await _get_subs_from_backend(sub_name, i) - except KeyError: - print('no more chunk pages for', sub_name) - return + async def get_subscription_games(subscription_name: str, context: Any): + while True: + games_page = await self._get_subscriptions_from_backend(subscription_name, i) + if not games_pages: + yield None yield [SubGame(game['game_id'], game['game_title']) for game in games_page] """ diff --git a/src/galaxy/api/types.py b/src/galaxy/api/types.py index f98557d..75fcd1c 100644 --- a/src/galaxy/api/types.py +++ b/src/galaxy/api/types.py @@ -219,7 +219,7 @@ class UserPresence: @dataclass class Subscription: - """Information about a subscription. If a subscription is not owned no end_time should be specified. + """Information about a subscription. :param subscription_name: name of the subscription, will also be used as its identifier. :param owned: whether the subscription is owned or not, None if unknown. @@ -229,10 +229,6 @@ class Subscription: owned: Optional[bool] = None end_time: Optional[int] = None - def __post_init__(self): - if not self.owned: - assert self.end_time is None, "Subscriptions not owned but end time specified." \ - "Specify end time for owned subscriptions only" @dataclass class SubscriptionGame: @@ -241,7 +237,7 @@ class SubscriptionGame: :param game_title: title of the game :param game_id: id of the game :param start_time: unix timestamp of when the game has been added to subscription - :param end_time: unix timestamp of when the game is removed from subscription. + :param end_time: unix timestamp of when the game will be removed from subscription. """ game_title: str game_id: str diff --git a/tests/test_subscriptions.py b/tests/test_subscriptions.py index 4f5d02d..4da441e 100644 --- a/tests/test_subscriptions.py +++ b/tests/test_subscriptions.py @@ -77,11 +77,6 @@ async def test_get_subscriptions_failure_generic(plugin, read, write, error, cod } ] -@pytest.mark.asyncio -async def test_subscription_assert_failure(): - with pytest.raises(AssertionError): - Subscription("test", False, 123) - @pytest.mark.asyncio async def test_get_subscription_games_success(plugin, read, write): plugin.prepare_subscription_games_context.return_value = async_return_value(5) @@ -153,7 +148,7 @@ async def test_get_subscription_games_success(plugin, read, write): ] @pytest.mark.asyncio -async def test_get_subscription_games_success_none_yield(plugin, read, write): +async def test_get_subscription_games_success_empty(plugin, read, write): plugin.prepare_subscription_games_context.return_value = async_return_value(5) request = { "jsonrpc": "2.0", @@ -195,66 +190,6 @@ async def test_get_subscription_games_success_none_yield(plugin, read, write): } ] -@pytest.mark.asyncio -async def test_get_subscription_games_success_yield_mix(plugin, read, write): - plugin.prepare_subscription_games_context.return_value = async_return_value(5) - request = { - "jsonrpc": "2.0", - "id": "3", - "method": "start_subscription_games_import", - "params": { - "subscription_names": ["sub_a"] - } - } - read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)] - - async def sub_games(): - games = [ - SubscriptionGame(game_title="game A", game_id="game_A")] - yield games - yield None - - plugin.get_subscription_games.return_value = sub_games() - await plugin.run() - plugin.prepare_subscription_games_context.assert_called_with(["sub_a"]) - plugin.get_subscription_games.assert_called_with("sub_a", 5) - plugin.subscription_games_import_complete.asert_called_with() - - assert get_messages(write) == [ - { - "jsonrpc": "2.0", - "id": "3", - "result": None - }, - { - "jsonrpc": "2.0", - "method": "subscription_games_import_success", - "params": { - "subscription_name": "sub_a", - "subscription_games": [ - { - "game_title": "game A", - "game_id": "game_A" - }, - ] - } - }, - { - "jsonrpc": "2.0", - "method": "subscription_games_import_success", - "params": { - "subscription_name": "sub_a", - "subscription_games": None - } - }, - { - "jsonrpc": "2.0", - "method": "subscription_games_import_finished", - "params": None - } - ] - - @pytest.mark.asyncio @pytest.mark.parametrize("exception,code,message", [ (BackendError, 4, "Backend error"), @@ -305,9 +240,9 @@ async def test_get_subscription_games_error(exception, code, message, plugin, re @pytest.mark.asyncio async def test_prepare_get_subscription_games_context_error(plugin, read, write): request_id = "31415" - error_details = "Unexpected syntax" - error_message, error_code = FailedParsingManifest().message, FailedParsingManifest().code - plugin.prepare_subscription_games_context.side_effect = FailedParsingManifest(error_details) + error_details = "Unexpected backend error" + error_message, error_code = BackendError().message, BackendError().code + plugin.prepare_subscription_games_context.side_effect = BackendError(error_details) request = { "jsonrpc": "2.0", "id": request_id,