1111from requests .exceptions import (
1212 ConnectionError as requests__ConnectionError , Timeout as requests__Timeout )
1313from requests_html import Element
14+ from urllib .parse import unquote_plus
1415
1516from .buildupdater import get_boff_spec , load_build , load_skill_pages
1617from .constants import (
1920 STARSHIP_TRAIT_QUERY_URL , TRAIT_QUERY_URL , WIKI_IMAGE_URL )
2021from .iofunc import (
2122 auto_backup_cargo_file , browse_path , cache_cargo_data , copy_file , download_image ,
22- fetch_html , get_asset_path , get_cached_cargo_data , get_cargo_data , get_downloaded_images ,
23- image , load_image , load_json , read_env_file , retrieve_image , store_json , store_to_cache )
23+ download_images_fast , fetch_html , get_asset_path , get_cached_cargo_data , get_cargo_data ,
24+ get_downloaded_images , image , load_image , load_json , read_env_file , retrieve_image ,
25+ store_json , store_to_cache )
2426from .splash import enter_splash , exit_splash , splash_text
2527from .textedit import (
2628 create_equipment_tooltip , create_trait_tooltip , dewikify , parse_wikitext ,
@@ -347,7 +349,8 @@ def download_images(self, threaded_worker: ThreadObject):
347349 no_retry_images .add (img )
348350 else :
349351 self .cache .images_failed .pop (img )
350- images = self .cache .images_set - no_retry_images - get_downloaded_images (self )
352+ images = self .cache .images_set - no_retry_images - get_downloaded_images (
353+ Path (self .config ['config_subfolders' ]['images' ]))
351354 img_folder = self .config ['config_subfolders' ]['images' ]
352355
353356 images_to_download = images - self .cache .boff_abilities ['all' ].keys ()
@@ -378,16 +381,16 @@ def cache_doff_single(self, cache: dict, doff: dict):
378381 cache [doff ['spec' ]][doff ['description' ]] = doff
379382
380383
381- def cache_skills (self ):
384+ def cache_skills (skill_cache : dict [ str , dict ], app_directory : str ):
382385 """
383386 Loads skills into cache.
384387 """
385- space_skill_data = load_json (get_asset_path ('space_skills.json' , self . app_dir ))
386- self . cache . skills ['space' ] = space_skill_data ['space' ]
387- self . cache . skills ['space_unlocks' ] = space_skill_data ['space_unlocks' ]
388- ground_skill_data = load_json (get_asset_path ('ground_skills.json' , self . app_dir ))
389- self . cache . skills ['ground' ] = ground_skill_data ['ground' ]
390- self . cache . skills ['ground_unlocks' ] = ground_skill_data ['ground_unlocks' ]
388+ space_skill_data = load_json (get_asset_path ('space_skills.json' , app_directory ))
389+ skill_cache ['space' ] = space_skill_data ['space' ]
390+ skill_cache ['space_unlocks' ] = space_skill_data ['space_unlocks' ]
391+ ground_skill_data = load_json (get_asset_path ('ground_skills.json' , app_directory ))
392+ skill_cache ['ground' ] = ground_skill_data ['ground' ]
393+ skill_cache ['ground_unlocks' ] = ground_skill_data ['ground_unlocks' ]
391394
392395
393396def autosave (self ):
@@ -1053,14 +1056,74 @@ class ThisIsTerribleError(RuntimeError):
10531056 store_json (self .cache .boff_abilities , filepath )
10541057
10551058
1056- def build_cache (config_path : Path ) -> int :
1059+ def get_icon_set (cargo_dir : Path ) -> set [str ]:
1060+ """
1061+ Creates set of all required icons from cargo data and required static images.
1062+
1063+ Parameters:
1064+ - :param cargo_dir: path to cargo data directory
1065+ """
1066+ images_set = set ()
1067+ equipment_cargo_data = load_json (str (cargo_dir / 'equipment.json' ))
1068+ equipment_types = set (EQUIPMENT_TYPES .keys ())
1069+ elite_hangar = {
1070+ 'Hangar - Elite Federation Mission Scout Ships' ,
1071+ 'Hangar - Elite Valor Fighters'
1072+ }
1073+ for item in equipment_cargo_data :
1074+ if item ['type' ] in equipment_types :
1075+ if item ['type' ] == 'Hangar Bay' and item ['name' ] not in elite_hangar and (
1076+ item ['name' ].startswith ('Hangar - Advanced' )
1077+ or item ['name' ].startswith ('Hangar - Elite' )):
1078+ continue
1079+ images_set .add (sanitize_equipment_name (item ['name' ]))
1080+ trait_cargo_data = load_json (str (cargo_dir / 'traits.json' ))
1081+ for trait in trait_cargo_data :
1082+ if trait ['type' ] != 'doff' and trait ['type' ] != 'boff' and trait ['name' ] is not None :
1083+ if trait ['icon_name' ] is None :
1084+ images_set .add (trait ['name' ])
1085+ else :
1086+ images_set .add (trait ['icon_name' ])
1087+ shiptrait_cargo_data = load_json (str (cargo_dir / 'starship_traits.json' ))
1088+ images_set |= set (ship_trait ['name' ] for ship_trait in shiptrait_cargo_data )
1089+ return images_set
1090+
1091+
1092+ def get_skill_icons (skill_cache : dict [str , dict ]) -> set [str ]:
1093+ """
1094+ """
1095+ icons = set ()
1096+ for rank_group in skill_cache ['space' ]:
1097+ for skill_group in rank_group :
1098+ for skill_node in skill_group ['nodes' ]:
1099+ icons .add (skill_node ['image' ])
1100+ for skill_group in skill_cache ['ground' ]:
1101+ for skill_node in skill_group ['nodes' ]:
1102+ icons .add (skill_node ['image' ])
1103+ return icons
1104+
1105+
1106+ def get_boff_icons (boff_cache : dict [str , dict ]) -> set [str ]:
1107+ """
1108+ """
1109+ return set (boff_cache ['all' ].keys ())
1110+
1111+
1112+ def get_ship_icons (ship_list : list [dict [str ]]) -> set [str ]:
1113+ """
1114+ """
1115+ return set (ship ['image' ][5 :] for ship in ship_list )
1116+
1117+
1118+ def build_cache (app_dir : Path ) -> int :
10571119 """
10581120 Builds cache in config folder indicated by `config_path`. Returns status: success: `0`,
10591121 failure: `1`
10601122
10611123 Parameters:
10621124 - :param config_path: path to build cache into
10631125 """
1126+ config_path = app_dir / '.config'
10641127 env_variables = read_env_file (config_path / '.env' , ['SETS_CF_CLEARANCE' , 'SETS_USER_AGENT' ])
10651128 requests_session = Session ()
10661129 if 'SETS_CF_CLEARANCE' in env_variables :
@@ -1077,6 +1140,32 @@ def build_cache(config_path: Path) -> int:
10771140 cargo_dir / 'starship_traits.json' , STARSHIP_TRAIT_QUERY_URL , requests_session ))
10781141 success .append (cache_cargo_data (cargo_dir / 'modifiers.json' , MODIFIER_QUERY , requests_session ))
10791142 success .append (cache_cargo_data (cargo_dir / 'doffs.json' , DOFF_QUERY_URL , requests_session ))
1143+
1144+ image_dir = config_path / 'images'
1145+ downloaded_images = get_downloaded_images (image_dir )
1146+ ultimate_icons = {'Focused Frenzy' , 'Probability Manipulation' , 'EPS Corruption' }
1147+ images_set = (get_icon_set (cargo_dir ) | ultimate_icons ) - downloaded_images
1148+ if len (images_set ) > 0 :
1149+ download_images_fast (list (images_set ), env_variables , image_dir )
1150+ skill_cache = dict ()
1151+ cache_skills (skill_cache , app_dir )
1152+ skill_images = get_skill_icons (skill_cache ) - downloaded_images
1153+ if len (skill_images ) > 0 :
1154+ download_images_fast (list (skill_images ), env_variables , image_dir , image_suffix = '.png' )
1155+ boff_cache = load_json (cargo_dir / 'boff_abilities.json' )
1156+ boff_images = get_boff_icons (boff_cache ) - downloaded_images
1157+ if len (boff_images ) > 0 :
1158+ download_images_fast (
1159+ list (boff_images ), env_variables , image_dir , image_suffix = '_icon_(Federation).png' )
1160+
1161+ downloaded_ship_images = set (
1162+ map (lambda x : unquote_plus (x ), os .listdir (str (config_path / 'ship_images' ))))
1163+ ship_list = load_json (str (cargo_dir / 'ship_list.json' ))
1164+ ship_images = get_ship_icons (ship_list ) - downloaded_ship_images
1165+ if len (ship_images ) > 0 :
1166+ download_images_fast (
1167+ list (ship_images ), env_variables , config_path / 'ship_images' , image_suffix = '' )
1168+
10801169 if False in success :
10811170 return 1
10821171 return 0
0 commit comments