1515from sc2reader import utils
1616from sc2reader import log_utils
1717from sc2reader import readers , data
18- from sc2reader .objects import Player , Observer , Team , PlayerSummary , Graph , DepotHash
18+ from sc2reader .objects import Player , Observer , Team , PlayerSummary , Graph , DepotFile
1919from sc2reader .constants import REGIONS , LOCALIZED_RACES , GAME_SPEED_FACTOR , GAME_SPEED_CODES , RACE_CODES , PLAYER_TYPE_CODES , TEAM_COLOR_CODES , GAME_FORMAT_CODES , GAME_TYPE_CODES , DIFFICULTY_CODES
2020
2121
@@ -29,7 +29,8 @@ def real_type(teams):
2929
3030
3131class Resource (object ):
32- def __init__ (self , file_object , filename = None , ** options ):
32+ def __init__ (self , file_object , filename = None , factory = None , ** options ):
33+ self .factory = factory
3334 self .opt = utils .AttributeDict (options )
3435 self .logger = log_utils .get_logger (self .__class__ )
3536 self .filename = filename or getattr (file_object ,'name' ,'Unavailable' )
@@ -619,6 +620,17 @@ def get_url(cls, gateway, map_hash):
619620 else :
620621 return None
621622
623+ class Localization (Resource ):
624+
625+ def __init__ (self , s2ml_file , ** options ):
626+ super (Localization , self ).__init__ (s2ml_file , ** options )
627+ self .mapping = dict ()
628+ xml = ElementTree .parse (s2ml_file )
629+ for entry in xml .findall ('e' ):
630+ self .mapping [int (entry .attrib ['id' ])] = entry .text
631+
632+ def __getitem__ (self , key ):
633+ return self .mapping [key ]
622634
623635class GameSummary (Resource ):
624636
@@ -710,9 +722,7 @@ def __init__(self, summary_file, filename=None, **options):
710722 # The s2gs file also keeps reference to a series of s2mv files
711723 # Some of these appear to be encoded bytes and others appear to be
712724 # the preview images that authors may bundle with their maps.
713- self .s2mv_urls = [str (DepotHash (file_hash )) for file_hash in self .parts [0 ][6 ][7 ]]
714-
715-
725+ self .s2mv_urls = [str (DepotFile (file_hash )) for file_hash in self .parts [0 ][6 ][7 ]]
716726
717727 def load_translations (self ):
718728 # This section of the file seems to map numerical ids to their
@@ -758,7 +768,7 @@ def load_translations(self):
758768 files = list ()
759769 for file_hash in localization [1 ]:
760770 if file_hash [:4 ] != '\x00 \x00 \x00 \x00 ' :
761- files .append (DepotHash (file_hash ))
771+ files .append (DepotFile (file_hash ))
762772 self .localization_urls [language ] = files
763773
764774 # Grab the gateway from the one of the files
@@ -772,15 +782,12 @@ def load_translations(self):
772782 # For now we'll only do this for english localizations.
773783 self .lang_sheets = dict ()
774784 self .translations = dict ()
775- for lang ,urls in self .localization_urls .items ():
785+ for lang , files in self .localization_urls .items ():
776786 if lang != 'enUS' : continue
777787
778788 sheets = dict ()
779- for sheet , url in enumerate (urls ):
780- print "Opening " , str (url )
781- xml = ElementTree .parse (urllib2 .urlopen (str (url )))
782- translation = dict ((int (e .attrib ['id' ]),e .text ) for e in xml .findall ('e' ))
783- sheets [sheet ] = translation
789+ for sheet , depot_file in enumerate (files ):
790+ sheets [sheet ] = self .factory .load_localization (depot_file )
784791
785792 translation = dict ()
786793 for uid , (sheet , item ) in self .id_map .items ():
@@ -917,8 +924,6 @@ def load_players(self):
917924 player = PlayerSummary (struct [0 ][0 ])
918925 stats = self .player_stats [index ]
919926 settings = self .player_settings [index ]
920- print settings
921- print stats
922927 player .is_ai = not isinstance (struct [0 ][1 ], dict )
923928 if not player .is_ai :
924929 player .region = self .region
0 commit comments