<s> from __future__ import absolute_import , print_function , unicode_literals , division <EOL> import binascii <EOL> import os <EOL> import json <EOL> from datetime import timedelta , datetime <EOL> from sc2reader . log_utils import loggable <EOL> from sc2reader . exceptions import MPQError <EOL> from sc2reader . constants import COLOR_CODES , COLOR_CODES_INV <EOL> class DepotFile ( object ) : <EOL> """<STR_LIT>""" <EOL> url_template = '<STR_LIT>' <EOL> def __init__ ( self , bytes ) : <EOL> self . server = bytes [ <NUM_LIT:4> : <NUM_LIT:8> ] . decode ( '<STR_LIT:utf-8>' ) . strip ( '<STR_LIT>' ) <EOL> if self . server == '<STR_LIT>' : <EOL> self . server = '<STR_LIT>' <EOL> self . hash = binascii . b2a_hex ( bytes [ <NUM_LIT:8> : ] ) . decode ( '<STR_LIT:utf8>' ) <EOL> self . type = bytes [ <NUM_LIT:0> : <NUM_LIT:4> ] . decode ( '<STR_LIT:utf8>' ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . url_template . format ( self . server , self . hash , self . type ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . url ) <EOL> def __str__ ( self ) : <EOL> return self . url <EOL> def windows_to_unix ( windows_time ) : <EOL> return int ( ( windows_time - <NUM_LIT> ) / <NUM_LIT:10> ** <NUM_LIT:7> ) <EOL> @ loggable <EOL> class Color ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None , r = <NUM_LIT:0> , g = <NUM_LIT:0> , b = <NUM_LIT:0> , a = <NUM_LIT:255> ) : <EOL> if name : <EOL> if name not in COLOR_CODES_INV : <EOL> self . logger . warn ( "<STR_LIT>" + name ) <EOL> hexstr = COLOR_CODES_INV . get ( name , '<STR_LIT>' ) <EOL> self . r = int ( hexstr [ <NUM_LIT:0> : <NUM_LIT:2> ] , <NUM_LIT:16> ) <EOL> self . g = int ( hexstr [ <NUM_LIT:2> : <NUM_LIT:4> ] , <NUM_LIT:16> ) <EOL> self . b = int ( hexstr [ <NUM_LIT:4> : <NUM_LIT:6> ] , <NUM_LIT:16> ) <EOL> self . a = <NUM_LIT:255> <EOL> self . name = name <EOL> else : <EOL> self . r = r <EOL> self . g = g <EOL> self . b = b <EOL> self . a = a <EOL> if self . hex not in COLOR_CODES : <EOL> self . logger . warn ( "<STR_LIT>" + self . hex ) <EOL> self . name = COLOR_CODES . get ( self . hex , self . hex ) <EOL> @ property <EOL> def rgba ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . r , self . g , self . b , self . a ) <EOL> @ property <EOL> def hex ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def get_real_type ( teams ) : <EOL> team_sizes = [ len ( team . players ) for team in teams ] <EOL> if len ( team_sizes ) > <NUM_LIT:2> and sum ( team_sizes ) == len ( team_sizes ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT:v>" . join ( str ( size ) for size in sorted ( team_sizes ) ) <EOL> def extract_data_file ( data_file , archive ) : <EOL> def recovery_attempt ( ) : <EOL> try : <EOL> return archive . read_file ( data_file ) <EOL> except Exception : <EOL> return None <EOL> try : <EOL> try : <EOL> file_data = archive . read_file ( data_file , force_decompress = True ) <EOL> except Exception as e : <EOL> file_data = recovery_attempt ( ) <EOL> if file_data is None : <EOL> raise <EOL> return file_data <EOL> except Exception as e : <EOL> raise MPQError ( "<STR_LIT>" . format ( data_file ) , e ) <EOL> def get_files ( path , exclude = list ( ) , depth = - <NUM_LIT:1> , followlinks = False , extension = None , ** extras ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( path ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( path ) ) <EOL> if extension : <EOL> type_check = lambda path : os . path . splitext ( path ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] . lower ( ) == extension . lower ( ) <EOL> else : <EOL> type_check = lambda n : True <EOL> if os . path . isfile ( path ) : <EOL> if type_check ( path ) : <EOL> yield path <EOL> else : <EOL> pass <EOL> else : <EOL> for root , directories , filenames in os . walk ( path , followlinks = followlinks ) : <EOL> for directory in list ( directories ) : <EOL> if directory in exclude or depth == <NUM_LIT:0> : <EOL> directories . remove ( directory ) <EOL> for filename in filter ( type_check , filenames ) : <EOL> yield os . path . join ( root , filename ) <EOL> depth -= <NUM_LIT:1> <EOL> class Length ( timedelta ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def hours ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . seconds // <NUM_LIT> <EOL> @ property <EOL> def mins ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . seconds // <NUM_LIT> % <NUM_LIT> <EOL> @ property <EOL> def secs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . seconds % <NUM_LIT> <EOL> def __str__ ( self ) : <EOL> if self . hours : <EOL> return "<STR_LIT>" . format ( self . hours , self . mins , self . secs ) <EOL> else : <EOL> return "<STR_LIT>" . format ( self . mins , self . secs ) <EOL> class JSONDateEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , datetime ) : <EOL> return obj . strftime ( "<STR_LIT>" ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> def toJSON ( replay , ** user_options ) : <EOL> options = dict ( cls = JSONDateEncoder ) <EOL> options . update ( user_options ) <EOL> return json . dumps ( toDict ( replay ) , ** options ) <EOL> def toDict ( replay ) : <EOL> observers = list ( ) <EOL> for observer in replay . observers : <EOL> messages = list ( ) <EOL> for message in getattr ( observer , '<STR_LIT>' , list ( ) ) : <EOL> messages . append ( { <EOL> '<STR_LIT:time>' : message . time . seconds , <EOL> '<STR_LIT:text>' : message . text , <EOL> '<STR_LIT>' : message . to_all <EOL> } ) <EOL> observers . append ( { <EOL> '<STR_LIT:name>' : getattr ( observer , '<STR_LIT:name>' , None ) , <EOL> '<STR_LIT>' : getattr ( observer , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : messages , <EOL> } ) <EOL> players = list ( ) <EOL> for player in replay . players : <EOL> messages = list ( ) <EOL> for message in player . messages : <EOL> messages . append ( { <EOL> '<STR_LIT:time>' : message . time . seconds , <EOL> '<STR_LIT:text>' : message . text , <EOL> '<STR_LIT>' : message . to_all <EOL> } ) <EOL> players . append ( { <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : player . color . __dict__ if hasattr ( player , '<STR_LIT>' ) else None , <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT:name>' : getattr ( player , '<STR_LIT:name>' , None ) , <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT:result>' : getattr ( player , '<STR_LIT:result>' , None ) , <EOL> '<STR_LIT:type>' : getattr ( player , '<STR_LIT:type>' , None ) , <EOL> '<STR_LIT>' : getattr ( player , '<STR_LIT>' , None ) , <EOL> '<STR_LIT:url>' : getattr ( player , '<STR_LIT:url>' , None ) , <EOL> '<STR_LIT>' : messages , <EOL> } ) <EOL> return { <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT:date>' : getattr ( replay , '<STR_LIT:date>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT:type>' : getattr ( replay , '<STR_LIT:type>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , False ) , <EOL> '<STR_LIT:filename>' : getattr ( replay , '<STR_LIT:filename>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( getattr ( replay , '<STR_LIT>' , None ) , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : players , <EOL> '<STR_LIT>' : observers , <EOL> '<STR_LIT>' : getattr ( getattr ( replay , '<STR_LIT>' , None ) , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : getattr ( replay , '<STR_LIT>' , None ) <EOL> } </s>
