77from typing import Any , Dict , List , Optional , Set , Union , AsyncGenerator
88
99from galaxy .api .consts import Feature , OSCompatibility
10- from galaxy .api .errors import ImportInProgress , UnknownError
1110from galaxy .api .jsonrpc import ApplicationError , Connection
1211from galaxy .api .types import (
1312 Achievement , Authentication , Game , GameLibrarySettings , GameTime , LocalGame , NextStep , UserInfo , UserPresence ,
1413 Subscription , SubscriptionGame
1514)
1615from galaxy .task_manager import TaskManager
16+ from galaxy .api .importer import Importer , CollectionImporter
1717
1818logger = logging .getLogger (__name__ )
1919
2020
21+
2122class JSONEncoder (json .JSONEncoder ):
2223 def default (self , o ): # pylint: disable=method-hidden
2324 if dataclasses .is_dataclass (o ):
@@ -31,107 +32,6 @@ def dict_factory(elements):
3132 return super ().default (o )
3233
3334
34- class Importer :
35- def __init__ (
36- self ,
37- task_manger ,
38- name ,
39- get ,
40- prepare_context ,
41- notification_success ,
42- notification_failure ,
43- notification_finished ,
44- complete ,
45- ):
46- self ._task_manager = task_manger
47- self ._name = name
48- self ._get = get
49- self ._prepare_context = prepare_context
50- self ._notification_success = notification_success
51- self ._notification_failure = notification_failure
52- self ._notification_finished = notification_finished
53- self ._complete = complete
54-
55- self ._import_in_progress = False
56-
57- async def _import_element (self , id_ , context_ ):
58- try :
59- element = await self ._get (id_ , context_ )
60- self ._notification_success (id_ , element )
61- except ApplicationError as error :
62- self ._notification_failure (id_ , error )
63- except asyncio .CancelledError :
64- pass
65- except Exception :
66- logger .exception ("Unexpected exception raised in %s importer" , self ._name )
67- self ._notification_failure (id_ , UnknownError ())
68-
69- async def _import_elements (self , ids_ , context_ ):
70- try :
71- imports = [self ._import_element (id_ , context_ ) for id_ in ids_ ]
72- await asyncio .gather (* imports )
73- self ._notification_finished ()
74- self ._complete ()
75- except asyncio .CancelledError :
76- logger .debug ("Importing %s cancelled" , self ._name )
77- finally :
78- self ._import_in_progress = False
79-
80- async def start (self , ids ):
81- if self ._import_in_progress :
82- raise ImportInProgress ()
83-
84- self ._import_in_progress = True
85- try :
86- context = await self ._prepare_context (ids )
87- self ._task_manager .create_task (
88- self ._import_elements (ids , context ),
89- "{} import" .format (self ._name ),
90- handle_exceptions = False
91- )
92- except :
93- self ._import_in_progress = False
94- raise
95-
96-
97- class SubscriptionGamesImporter (Importer ):
98- def __init__ (
99- self ,
100- task_manger ,
101- name ,
102- get ,
103- prepare_context ,
104- notification_success ,
105- notification_failure ,
106- notification_finished ,
107- notification_partial_finished ,
108- complete
109- ):
110- super (SubscriptionGamesImporter , self ).__init__ (task_manger ,
111- name ,
112- get ,
113- prepare_context ,
114- notification_success ,
115- notification_failure ,
116- notification_finished ,
117- complete )
118- self ._notification_partial_finished = notification_partial_finished
119-
120- async def _import_element (self , id_ , context_ ):
121- try :
122- async for element in self ._get (id_ , context_ ):
123- self ._notification_success (id_ , element )
124- except ApplicationError as error :
125- self ._notification_failure (id_ , error )
126- except asyncio .CancelledError :
127- pass
128- except Exception :
129- logger .exception ("Unexpected exception raised in %s importer" , self ._name )
130- self ._notification_failure (id_ , UnknownError ())
131- finally :
132- self ._notification_partial_finished (id_ )
133-
134-
13535class Plugin :
13636 """Use and override methods of this class to create a new platform integration."""
13737
@@ -214,15 +114,16 @@ def __init__(self, platform, version, reader, writer, handshake_token):
214114 self ._local_size_import_finished ,
215115 self .local_size_import_complete
216116 )
217- self ._subscription_games_importer = SubscriptionGamesImporter (
117+ self ._subscription_games_importer = CollectionImporter (
118+ self ._subscriptions_games_partial_import_finished ,
119+
218120 self ._external_task_manager ,
219121 "subscription games" ,
220122 self .get_subscription_games ,
221123 self .prepare_subscription_games_context ,
222124 self ._subscription_games_import_success ,
223125 self ._subscription_games_import_failure ,
224126 self ._subscription_games_import_finished ,
225- self ._subscriptions_games_partial_import_finished ,
226127 self .subscription_games_import_complete
227128 )
228129
0 commit comments