@@ -31,6 +31,62 @@ def dict_factory(elements):
3131 return super ().default (o )
3232
3333
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 start (self , ids ):
58+ if self ._import_in_progress :
59+ raise ImportInProgress ()
60+
61+ context = await self ._prepare_context (ids )
62+
63+ async def import_element (id_ , context_ ):
64+ try :
65+ element = await self ._get (id_ , context_ )
66+ self ._notification_success (id_ , element )
67+ except ApplicationError as error :
68+ self ._notification_failure (id_ , error )
69+ except Exception :
70+ logger .exception ("Unexpected exception raised in %s importer" , self ._name )
71+ self ._notification_failure (id_ , UnknownError ())
72+
73+ async def import_elements (ids_ , context_ ):
74+ try :
75+ imports = [import_element (id_ , context_ ) for id_ in ids_ ]
76+ await asyncio .gather (* imports )
77+ finally :
78+ self ._notification_finished ()
79+ self ._import_in_progress = False
80+ self ._complete ()
81+
82+ self ._task_manager .create_task (
83+ import_elements (ids , context ),
84+ "{} import" .format (self ._name ),
85+ handle_exceptions = False
86+ )
87+ self ._import_in_progress = True
88+
89+
3490class Plugin :
3591 """Use and override methods of this class to create a new platform integration."""
3692
@@ -48,17 +104,62 @@ def __init__(self, platform, version, reader, writer, handshake_token):
48104 encoder = JSONEncoder ()
49105 self ._connection = Connection (self ._reader , self ._writer , encoder )
50106
51- self ._achievements_import_in_progress = False
52- self ._game_times_import_in_progress = False
53- self ._game_library_settings_import_in_progress = False
54- self ._os_compatibility_import_in_progress = False
55- self ._user_presence_import_in_progress = False
56-
57107 self ._persistent_cache = dict ()
58108
59109 self ._internal_task_manager = TaskManager ("plugin internal" )
60110 self ._external_task_manager = TaskManager ("plugin external" )
61111
112+ self ._achievements_importer = Importer (
113+ self ._external_task_manager ,
114+ "achievements" ,
115+ self .get_unlocked_achievements ,
116+ self .prepare_achievements_context ,
117+ self ._game_achievements_import_success ,
118+ self ._game_achievements_import_failure ,
119+ self ._achievements_import_finished ,
120+ self .achievements_import_complete
121+ )
122+ self ._game_time_importer = Importer (
123+ self ._external_task_manager ,
124+ "game times" ,
125+ self .get_game_time ,
126+ self .prepare_game_times_context ,
127+ self ._game_time_import_success ,
128+ self ._game_time_import_failure ,
129+ self ._game_times_import_finished ,
130+ self .game_times_import_complete
131+ )
132+ self ._game_library_settings_importer = Importer (
133+ self ._external_task_manager ,
134+ "game library settings" ,
135+ self .get_game_library_settings ,
136+ self .prepare_game_library_settings_context ,
137+ self ._game_library_settings_import_success ,
138+ self ._game_library_settings_import_failure ,
139+ self ._game_library_settings_import_finished ,
140+ self .game_library_settings_import_complete
141+ )
142+ self ._os_compatibility_importer = Importer (
143+ self ._external_task_manager ,
144+ "os compatibility" ,
145+ self .get_os_compatibility ,
146+ self .prepare_os_compatibility_context ,
147+ self ._os_compatibility_import_success ,
148+ self ._os_compatibility_import_failure ,
149+ self ._os_compatibility_import_finished ,
150+ self .os_compatibility_import_complete
151+ )
152+ self ._user_presence_importer = Importer (
153+ self ._external_task_manager ,
154+ "users presence" ,
155+ self .get_user_presence ,
156+ self .prepare_user_presence_context ,
157+ self ._user_presence_import_success ,
158+ self ._user_presence_import_failure ,
159+ self ._user_presence_import_finished ,
160+ self .user_presence_import_complete
161+ )
162+
62163 # internal
63164 self ._register_method ("shutdown" , self ._shutdown , internal = True )
64165 self ._register_method ("get_capabilities" , self ._get_capabilities , internal = True , immediate = True )
@@ -435,7 +536,7 @@ def update_user_presence(self, user_id: str, user_presence: UserPresence) -> Non
435536 }
436537 )
437538
438- def _game_time_import_success (self , game_time : GameTime ) -> None :
539+ def _game_time_import_success (self , game_id : str , game_time : GameTime ) -> None :
439540 params = {"game_time" : game_time }
440541 self ._connection .send_notification ("game_time_import_success" , params )
441542
@@ -449,7 +550,7 @@ def _game_time_import_failure(self, game_id: str, error: ApplicationError) -> No
449550 def _game_times_import_finished (self ) -> None :
450551 self ._connection .send_notification ("game_times_import_finished" , None )
451552
452- def _game_library_settings_import_success (self , game_library_settings : GameLibrarySettings ) -> None :
553+ def _game_library_settings_import_success (self , game_id : str , game_library_settings : GameLibrarySettings ) -> None :
453554 params = {"game_library_settings" : game_library_settings }
454555 self ._connection .send_notification ("game_library_settings_import_success" , params )
455556
@@ -636,36 +737,7 @@ async def get_owned_games(self):
636737 raise NotImplementedError ()
637738
638739 async def _start_achievements_import (self , game_ids : List [str ]) -> None :
639- if self ._achievements_import_in_progress :
640- raise ImportInProgress ()
641-
642- context = await self .prepare_achievements_context (game_ids )
643-
644- async def import_game_achievements (game_id , context_ ):
645- try :
646- achievements = await self .get_unlocked_achievements (game_id , context_ )
647- self ._game_achievements_import_success (game_id , achievements )
648- except ApplicationError as error :
649- self ._game_achievements_import_failure (game_id , error )
650- except Exception :
651- logger .exception ("Unexpected exception raised in import_game_achievements" )
652- self ._game_achievements_import_failure (game_id , UnknownError ())
653-
654- async def import_games_achievements (game_ids_ , context_ ):
655- try :
656- imports = [import_game_achievements (game_id , context_ ) for game_id in game_ids_ ]
657- await asyncio .gather (* imports )
658- finally :
659- self ._achievements_import_finished ()
660- self ._achievements_import_in_progress = False
661- self .achievements_import_complete ()
662-
663- self ._external_task_manager .create_task (
664- import_games_achievements (game_ids , context ),
665- "unlocked achievements import" ,
666- handle_exceptions = False
667- )
668- self ._achievements_import_in_progress = True
740+ await self ._achievements_importer .start (game_ids )
669741
670742 async def prepare_achievements_context (self , game_ids : List [str ]) -> Any :
671743 """Override this method to prepare context for get_unlocked_achievements.
@@ -800,36 +872,7 @@ async def get_friends(self):
800872 raise NotImplementedError ()
801873
802874 async def _start_game_times_import (self , game_ids : List [str ]) -> None :
803- if self ._game_times_import_in_progress :
804- raise ImportInProgress ()
805-
806- context = await self .prepare_game_times_context (game_ids )
807-
808- async def import_game_time (game_id , context_ ):
809- try :
810- game_time = await self .get_game_time (game_id , context_ )
811- self ._game_time_import_success (game_time )
812- except ApplicationError as error :
813- self ._game_time_import_failure (game_id , error )
814- except Exception :
815- logger .exception ("Unexpected exception raised in import_game_time" )
816- self ._game_time_import_failure (game_id , UnknownError ())
817-
818- async def import_game_times (game_ids_ , context_ ):
819- try :
820- imports = [import_game_time (game_id , context_ ) for game_id in game_ids_ ]
821- await asyncio .gather (* imports )
822- finally :
823- self ._game_times_import_finished ()
824- self ._game_times_import_in_progress = False
825- self .game_times_import_complete ()
826-
827- self ._external_task_manager .create_task (
828- import_game_times (game_ids , context ),
829- "game times import" ,
830- handle_exceptions = False
831- )
832- self ._game_times_import_in_progress = True
875+ await self ._game_time_importer .start (game_ids )
833876
834877 async def prepare_game_times_context (self , game_ids : List [str ]) -> Any :
835878 """Override this method to prepare context for get_game_time.
@@ -858,36 +901,7 @@ def game_times_import_complete(self) -> None:
858901 """
859902
860903 async def _start_game_library_settings_import (self , game_ids : List [str ]) -> None :
861- if self ._game_library_settings_import_in_progress :
862- raise ImportInProgress ()
863-
864- context = await self .prepare_game_library_settings_context (game_ids )
865-
866- async def import_game_library_settings (game_id , context_ ):
867- try :
868- game_library_settings = await self .get_game_library_settings (game_id , context_ )
869- self ._game_library_settings_import_success (game_library_settings )
870- except ApplicationError as error :
871- self ._game_library_settings_import_failure (game_id , error )
872- except Exception :
873- logger .exception ("Unexpected exception raised in import_game_library_settings" )
874- self ._game_library_settings_import_failure (game_id , UnknownError ())
875-
876- async def import_game_library_settings_set (game_ids_ , context_ ):
877- try :
878- imports = [import_game_library_settings (game_id , context_ ) for game_id in game_ids_ ]
879- await asyncio .gather (* imports )
880- finally :
881- self ._game_library_settings_import_finished ()
882- self ._game_library_settings_import_in_progress = False
883- self .game_library_settings_import_complete ()
884-
885- self ._external_task_manager .create_task (
886- import_game_library_settings_set (game_ids , context ),
887- "game library settings import" ,
888- handle_exceptions = False
889- )
890- self ._game_library_settings_import_in_progress = True
904+ await self ._game_library_settings_importer .start (game_ids )
891905
892906 async def prepare_game_library_settings_context (self , game_ids : List [str ]) -> Any :
893907 """Override this method to prepare context for get_game_library_settings.
@@ -916,37 +930,7 @@ def game_library_settings_import_complete(self) -> None:
916930 """
917931
918932 async def _start_os_compatibility_import (self , game_ids : List [str ]) -> None :
919- if self ._os_compatibility_import_in_progress :
920- raise ImportInProgress ()
921-
922- context = await self .prepare_os_compatibility_context (game_ids )
923-
924- async def import_os_compatibility (game_id , context_ ):
925- try :
926- os_compatibility = await self .get_os_compatibility (game_id , context_ )
927- self ._os_compatibility_import_success (game_id , os_compatibility )
928- except ApplicationError as error :
929- self ._os_compatibility_import_failure (game_id , error )
930- except Exception :
931- logger .exception ("Unexpected exception raised in import_os_compatibility" )
932- self ._os_compatibility_import_failure (game_id , UnknownError ())
933-
934- async def import_os_compatibility_set (game_ids_ , context_ ):
935- try :
936- await asyncio .gather (* [
937- import_os_compatibility (game_id , context_ ) for game_id in game_ids_
938- ])
939- finally :
940- self ._os_compatibility_import_finished ()
941- self ._os_compatibility_import_in_progress = False
942- self .os_compatibility_import_complete ()
943-
944- self ._external_task_manager .create_task (
945- import_os_compatibility_set (game_ids , context ),
946- "game OS compatibility import" ,
947- handle_exceptions = False
948- )
949- self ._os_compatibility_import_in_progress = True
933+ await self ._os_compatibility_importer .start (game_ids )
950934
951935 async def prepare_os_compatibility_context (self , game_ids : List [str ]) -> Any :
952936 """Override this method to prepare context for get_os_compatibility.
@@ -972,37 +956,7 @@ def os_compatibility_import_complete(self) -> None:
972956 """Override this method to handle operations after OS compatibility import is finished (like updating cache)."""
973957
974958 async def _start_user_presence_import (self , user_id_list : List [str ]) -> None :
975- if self ._user_presence_import_in_progress :
976- raise ImportInProgress ()
977-
978- context = await self .prepare_user_presence_context (user_id_list )
979-
980- async def import_user_presence (user_id , context_ ) -> None :
981- try :
982- self ._user_presence_import_success (user_id , await self .get_user_presence (user_id , context_ ))
983- except ApplicationError as error :
984- self ._user_presence_import_failure (user_id , error )
985- except Exception :
986- logger .exception ("Unexpected exception raised in import_user_presence" )
987- self ._user_presence_import_failure (user_id , UnknownError ())
988-
989- async def import_user_presence_set (user_id_list_ , context_ ) -> None :
990- try :
991- await asyncio .gather (* [
992- import_user_presence (user_id , context_ )
993- for user_id in user_id_list_
994- ])
995- finally :
996- self ._user_presence_import_finished ()
997- self ._user_presence_import_in_progress = False
998- self .user_presence_import_complete ()
999-
1000- self ._external_task_manager .create_task (
1001- import_user_presence_set (user_id_list , context ),
1002- "user presence import" ,
1003- handle_exceptions = False
1004- )
1005- self ._user_presence_import_in_progress = True
959+ await self ._user_presence_importer .start (user_id_list )
1006960
1007961 async def prepare_user_presence_context (self , user_id_list : List [str ]) -> Any :
1008962 """Override this method to prepare context for get_user_presence.
0 commit comments