Skip to content

Commit 0c51ff2

Browse files
author
unknown
committed
adhere to comments, move importers to seperate module
1 parent cd452b8 commit 0c51ff2

2 files changed

Lines changed: 94 additions & 104 deletions

File tree

src/galaxy/api/importer.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import asyncio
2+
import logging
3+
from galaxy.api.jsonrpc import ApplicationError
4+
from galaxy.api.errors import ImportInProgress, UnknownError
5+
6+
logger = logging.getLogger(__name__)
7+
8+
9+
class Importer:
10+
def __init__(
11+
self,
12+
task_manger,
13+
name,
14+
get,
15+
prepare_context,
16+
notification_success,
17+
notification_failure,
18+
notification_finished,
19+
complete,
20+
):
21+
self._task_manager = task_manger
22+
self._name = name
23+
self._get = get
24+
self._prepare_context = prepare_context
25+
self._notification_success = notification_success
26+
self._notification_failure = notification_failure
27+
self._notification_finished = notification_finished
28+
self._complete = complete
29+
30+
self._import_in_progress = False
31+
32+
async def _import_element(self, id_, context_):
33+
try:
34+
element = await self._get(id_, context_)
35+
self._notification_success(id_, element)
36+
except ApplicationError as error:
37+
self._notification_failure(id_, error)
38+
except asyncio.CancelledError:
39+
pass
40+
except Exception:
41+
logger.exception("Unexpected exception raised in %s importer", self._name)
42+
self._notification_failure(id_, UnknownError())
43+
44+
async def _import_elements(self, ids_, context_):
45+
try:
46+
imports = [self._import_element(id_, context_) for id_ in ids_]
47+
await asyncio.gather(*imports)
48+
self._notification_finished()
49+
self._complete()
50+
except asyncio.CancelledError:
51+
logger.debug("Importing %s cancelled", self._name)
52+
finally:
53+
self._import_in_progress = False
54+
55+
async def start(self, ids):
56+
if self._import_in_progress:
57+
raise ImportInProgress()
58+
59+
self._import_in_progress = True
60+
try:
61+
context = await self._prepare_context(ids)
62+
self._task_manager.create_task(
63+
self._import_elements(ids, context),
64+
"{} import".format(self._name),
65+
handle_exceptions=False
66+
)
67+
except:
68+
self._import_in_progress = False
69+
raise
70+
71+
72+
class CollectionImporter(Importer):
73+
def __init__(self, notification_partialy_finished, *args):
74+
super().__init__(*args)
75+
self._notification_partial_finished = notification_partialy_finished
76+
77+
async def _import_element(self, id_, context_):
78+
try:
79+
async for element in self._get(id_, context_):
80+
self._notification_success(id_, element)
81+
except ApplicationError as error:
82+
self._notification_failure(id_, error)
83+
except asyncio.CancelledError:
84+
pass
85+
except Exception:
86+
logger.exception("Unexpected exception raised in %s importer", self._name)
87+
self._notification_failure(id_, UnknownError())
88+
finally:
89+
self._notification_partial_finished(id_)

src/galaxy/api/plugin.py

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
from typing import Any, Dict, List, Optional, Set, Union, AsyncGenerator
88

99
from galaxy.api.consts import Feature, OSCompatibility
10-
from galaxy.api.errors import ImportInProgress, UnknownError
1110
from galaxy.api.jsonrpc import ApplicationError, Connection
1211
from galaxy.api.types import (
1312
Achievement, Authentication, Game, GameLibrarySettings, GameTime, LocalGame, NextStep, UserInfo, UserPresence,
1413
Subscription, SubscriptionGame
1514
)
1615
from galaxy.task_manager import TaskManager
16+
from galaxy.api.importer import Importer, CollectionImporter
1717

1818
logger = logging.getLogger(__name__)
1919

2020

21+
2122
class 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-
13535
class 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

Comments
 (0)