<s> """<STR_LIT>""" <EOL> import sys , subprocess , datetime , os . path , dateutil . parser <EOL> import re , types , time , getopt , csv , codecs <EOL> from io import StringIO <EOL> import xml . etree . ElementTree as ET <EOL> try : <EOL> import json <EOL> except : <EOL> import simplejson as json <EOL> try : <EOL> import requests <EOL> except : <EOL> import urllib , types <EOL> import urllib2 as requests <EOL> setattr ( requests , '<STR_LIT:content>' , '<STR_LIT>' ) <EOL> def get ( self , urlstr , params = { } ) : <EOL> if ( params ) : <EOL> urlstr += "<STR_LIT>" % urllib . urlencode ( params ) <EOL> self . content = self . urlopen ( urlstr ) . read ( ) <EOL> return self <EOL> requests . get = types . MethodType ( get , requests ) <EOL> asd_db = { } <EOL> model_db = { } <EOL> def init_asd_db ( ) : <EOL> global asd_db <EOL> if ( not asd_db ) : <EOL> try : <EOL> response = requests . get ( '<STR_LIT>' ) <EOL> for model , val in [ model_str . strip ( ) . split ( '<STR_LIT::>' ) for model_str in response . content . split ( '<STR_LIT:\n>' ) if model_str . strip ( ) ] : <EOL> asd_db [ model ] = val <EOL> except : <EOL> asd_db = { } <EOL> def init_model_db ( ) : <EOL> global model_db <EOL> if ( not model_db ) : <EOL> try : <EOL> model_file = open ( os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , '<STR_LIT>' ) , '<STR_LIT:r>' ) <EOL> model_db = json . load ( model_file ) <EOL> model_file . close ( ) <EOL> except : <EOL> model_db = { } <EOL> def blank_machine_dict ( ) : <EOL> return { u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' } <EOL> def apple_year_offset ( dateobj , years = <NUM_LIT:0> ) : <EOL> mod_time = dateobj . timetuple ( ) <EOL> mod_time = time . struct_time ( tuple ( [ mod_time [ <NUM_LIT:0> ] + years ] ) + mod_time [ <NUM_LIT:1> : ] ) <EOL> return datetime . datetime . fromtimestamp ( int ( time . mktime ( mod_time ) ) ) <EOL> def offline_snippet_lookup ( serial ) : <EOL> global model_db <EOL> init_model_db ( ) <EOL> if ( len ( serial ) == <NUM_LIT:11> ) : <EOL> snippet = serial [ - <NUM_LIT:3> : ] <EOL> elif ( len ( serial ) == <NUM_LIT:12> ) : <EOL> snippet = serial [ - <NUM_LIT:4> : ] <EOL> elif ( <NUM_LIT:2> < len ( serial ) < <NUM_LIT:5> ) : <EOL> snippet = serial <EOL> else : <EOL> return None <EOL> return model_db . get ( snippet . upper ( ) , None ) <EOL> def online_snippet_lookup ( serial ) : <EOL> if len ( serial ) == <NUM_LIT:11> : <EOL> snippet = serial [ - <NUM_LIT:3> : ] <EOL> elif len ( serial ) == <NUM_LIT:12> : <EOL> snippet = serial [ - <NUM_LIT:4> : ] <EOL> elif <NUM_LIT:2> < len ( serial ) < <NUM_LIT:5> : <EOL> snippet = serial <EOL> else : <EOL> return None <EOL> try : <EOL> prod_xml = requests . get ( '<STR_LIT>' , params = { '<STR_LIT>' : snippet , '<STR_LIT>' : '<STR_LIT>' } ) . content <EOL> prod_descr = ET . fromstring ( prod_xml ) . find ( '<STR_LIT>' ) . text <EOL> except : <EOL> return none <EOL> return prod_descr <EOL> def online_asd_version ( prod_descr ) : <EOL> global asd_db <EOL> init_asd_db ( ) <EOL> try : <EOL> return asd_db . get ( prod_descr , u'<STR_LIT>' ) <EOL> except : <EOL> return u'<STR_LIT>' <EOL> def offline_estimated_manufacture ( serial ) : <EOL> est_date = '<STR_LIT>' <EOL> if <NUM_LIT:10> < len ( serial ) < <NUM_LIT> : <EOL> if len ( serial ) == <NUM_LIT:11> : <EOL> year = serial [ <NUM_LIT:2> ] . lower ( ) <EOL> est_year = <NUM_LIT> + '<STR_LIT>' . index ( year ) <EOL> week = int ( serial [ <NUM_LIT:3> : <NUM_LIT:5> ] ) - <NUM_LIT:1> <EOL> year_time = datetime . date ( year = est_year , month = <NUM_LIT:1> , day = <NUM_LIT:1> ) <EOL> if ( week ) : <EOL> week_dif = datetime . timedelta ( weeks = week ) <EOL> year_time += week_dif <EOL> est_date = u'<STR_LIT>' + year_time . strftime ( '<STR_LIT>' ) <EOL> else : <EOL> alpha_year = '<STR_LIT>' <EOL> year = serial [ <NUM_LIT:3> ] . lower ( ) <EOL> est_year = <NUM_LIT> + ( alpha_year . index ( year ) / <NUM_LIT:2> ) <EOL> est_half = alpha_year . index ( year ) % <NUM_LIT:2> <EOL> week = serial [ <NUM_LIT:4> ] . lower ( ) <EOL> alpha_week = '<STR_LIT>' <EOL> est_week = alpha_week . index ( week ) + ( est_half * <NUM_LIT> ) - <NUM_LIT:1> <EOL> year_time = datetime . date ( year = int ( est_year ) , month = <NUM_LIT:1> , day = <NUM_LIT:1> ) <EOL> if ( est_week ) : <EOL> week_dif = datetime . timedelta ( weeks = est_week ) <EOL> year_time += week_dif <EOL> est_date = u'<STR_LIT>' + year_time . strftime ( '<STR_LIT>' ) <EOL> return est_date <EOL> def offline_estimated_applecare_end_date ( details ) : <EOL> manu_date = details [ u'<STR_LIT>' ] <EOL> prod_descr = details [ u'<STR_LIT>' ] <EOL> iOS_device = re . compile ( '<STR_LIT>' ) <EOL> if ( iOS_device . match ( prod_descr ) ) : <EOL> return u'<STR_LIT>' + apple_year_offset ( dateutil . parser . parse ( manu_date ) , <NUM_LIT:2> ) . strftime ( '<STR_LIT>' ) <EOL> else : <EOL> return u'<STR_LIT>' + apple_year_offset ( dateutil . parser . parse ( manu_date ) , <NUM_LIT:3> ) . strftime ( '<STR_LIT>' ) <EOL> def offline_estimated_warranty_end_date ( details ) : <EOL> manu_date = details [ u'<STR_LIT>' ] <EOL> return u'<STR_LIT>' + apple_year_offset ( dateutil . parser . parse ( manu_date ) , <NUM_LIT:1> ) . strftime ( '<STR_LIT>' ) <EOL> def warranty_generator ( serials ) : <EOL> for serial in serials : <EOL> if not isinstance ( serial , str ) : <EOL> for result in warranty_generator ( * serial ) : <EOL> yield result <EOL> else : <EOL> prod_dict = blank_machine_dict ( ) <EOL> prod_dict [ '<STR_LIT>' ] = serial <EOL> try : <EOL> prod_descr = offline_snippet_lookup ( prod_dict [ '<STR_LIT>' ] ) <EOL> except : <EOL> prod_descr = None <EOL> if ( not prod_descr ) : <EOL> try : <EOL> prod_descr = online_snippet_lookup ( prod_dict [ '<STR_LIT>' ] ) <EOL> except : <EOL> prod_descr = None <EOL> if ( not prod_descr ) : <EOL> prod_dict [ u'<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> prod_dict [ u'<STR_LIT>' ] = '<STR_LIT>' + prod_descr <EOL> except : <EOL> prod_dict [ u'<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> prod_dict [ u'<STR_LIT>' ] = offline_estimated_manufacture ( serial ) <EOL> except : <EOL> prod_dict [ u'<STR_LIT>' ] = u'<STR_LIT>' <EOL> if ( prod_dict [ u'<STR_LIT>' ] ) : <EOL> prod_dict [ u'<STR_LIT>' ] = u'<STR_LIT>' + prod_dict [ u'<STR_LIT>' ] <EOL> prod_dict [ u'<STR_LIT>' ] = offline_estimated_warranty_end_date ( prod_dict ) <EOL> prod_dict [ u'<STR_LIT>' ] = offline_estimated_applecare_end_date ( prod_dict ) <EOL> if ( datetime . datetime . now ( ) > dateutil . parser . parse ( prod_dict [ u'<STR_LIT>' ] ) ) : <EOL> prod_dict [ u'<STR_LIT>' ] = u'<STR_LIT>' <EOL> elif ( datetime . datetime . now ( ) > dateutil . parser . parse ( prod_dict [ u'<STR_LIT>' ] ) ) : <EOL> prod_dict [ u'<STR_LIT>' ] = u'<STR_LIT>' <EOL> else : <EOL> prod_dict [ u'<STR_LIT>' ] = u'<STR_LIT>' <EOL> yield prod_dict <EOL> def warranty ( * serials ) : <EOL> if not serials : <EOL> return None <EOL> results = list ( warranty_generator ( serials ) ) <EOL> if ( len ( serials ) == <NUM_LIT:1> ) and ( len ( results ) == <NUM_LIT:1> ) : <EOL> if ( hasattr ( serials [ <NUM_LIT:0> ] , "<STR_LIT>" ) and hasattr ( serials [ <NUM_LIT:0> ] , "<STR_LIT>" ) and not hasattr ( serials [ <NUM_LIT:0> ] , "<STR_LIT>" ) ) : <EOL> return results [ <NUM_LIT:0> ] <EOL> return results <EOL> online_warranty_generator = warranty_generator <EOL> online_warranty = warranty <EOL> offline_warranty_generator = warranty_generator <EOL> offline_warranty = warranty <EOL> def my_serial ( ) : <EOL> return [ ( subprocess . Popen ( "<STR_LIT>" , shell = True , stdout = subprocess . PIPE ) . communicate ( ) [ <NUM_LIT:0> ] . strip ( ) . decode ( "<STR_LIT>" ) ) ] <EOL> class UnicodeWriter : <EOL> def __init__ ( self , f , dialect = csv . excel , encoding = "<STR_LIT:utf-8>" , ** kwds ) : <EOL> self . queue = StringIO . StringIO ( ) <EOL> self . writer = csv . writer ( self . queue , dialect = dialect , ** kwds ) <EOL> self . stream = f <EOL> self . encoder = codecs . getincrementalencoder ( encoding ) ( ) <EOL> def writerow ( self , row ) : <EOL> temp = [ ] <EOL> for s in row : <EOL> if ( type ( s ) == types . IntType ) : <EOL> temp . append ( str ( s ) ) <EOL> else : <EOL> temp . append ( s ) <EOL> self . writer . writerow ( [ s . encode ( "<STR_LIT:utf-8>" ) for s in temp ] ) <EOL> data = self . queue . getvalue ( ) <EOL> data = data . decode ( "<STR_LIT:utf-8>" ) <EOL> data = self . encoder . encode ( data ) <EOL> self . stream . write ( data ) <EOL> self . queue . truncate ( <NUM_LIT:0> ) <EOL> def writerows ( self , rows ) : <EOL> for row in rows : <EOL> self . writerow ( row ) <EOL> def usage ( ) : <EOL> print ( __doc__ ) <EOL> sys . exit ( ) <EOL> def main ( ) : <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> except getopt . GetoptError as err : <EOL> print ( str ( err ) ) <EOL> usage ( ) <EOL> outfile , format = None , None <EOL> for o , a in opts : <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> usage ( ) <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> try : <EOL> f = open ( a , "<STR_LIT:rb>" ) <EOL> except : <EOL> print ( "<STR_LIT>" , a ) <EOL> usage ( ) <EOL> args = [ line for line in f . read ( ) . splitlines ( ) if line . strip ( ) ] <EOL> f . close ( ) <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> outfile = a <EOL> elif o in ( "<STR_LIT:-c>" , "<STR_LIT>" ) : <EOL> format = "<STR_LIT>" <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> format = "<STR_LIT>" <EOL> serials = args <EOL> warranty_dicts = [ ] <EOL> print ( offline_estimated_manufacture ( "<STR_LIT>" ) ) <EOL> for serial in ( serials or my_serial ( ) ) : <EOL> results = warranty ( serial ) <EOL> if type ( results ) == dict : <EOL> results = [ results ] <EOL> warranty_dicts . extend ( results ) <EOL> csv . register_dialect ( '<STR_LIT>' , delimiter = '<STR_LIT:\t>' ) <EOL> csv . register_dialect ( '<STR_LIT>' , delimiter = '<STR_LIT:U+002C>' ) <EOL> if ( not format ) : <EOL> plain_format = "<STR_LIT>" <EOL> for result in warranty_dicts : <EOL> plain_format += "<STR_LIT>" % ( u'<STR_LIT>' , result [ u'<STR_LIT>' ] ) <EOL> plain_format += "<STR_LIT>" % ( u'<STR_LIT>' , result [ u'<STR_LIT>' ] ) <EOL> for key , val in sorted ( result . items ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> if ( key not in ( u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ) ) : <EOL> plain_format += "<STR_LIT>" % ( key , val ) <EOL> plain_format += "<STR_LIT>" % ( u'<STR_LIT>' , result [ u'<STR_LIT>' ] ) <EOL> if ( not outfile ) : <EOL> sys . stdout . write ( plain_format ) <EOL> else : <EOL> open ( outfile , "<STR_LIT:wb>" ) . write ( plain_format ) <EOL> elif ( format in [ '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> if ( not outfile ) : <EOL> outfile = sys . stdout <EOL> else : <EOL> outfile = open ( outfile , "<STR_LIT:wb>" ) <EOL> dialect = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ format ] <EOL> writer = UnicodeWriter ( outfile , dialect = dialect ) <EOL> sample_machine = blank_machine_dict ( ) <EOL> header_row = [ u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> for key , val in sorted ( sample_machine . items ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> if ( key not in ( u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ) ) : <EOL> header_row . append ( key ) <EOL> header_row . append ( u'<STR_LIT>' ) <EOL> writer . writerow ( header_row ) <EOL> for result in warranty_dicts : <EOL> row_data = [ result [ u'<STR_LIT>' ] ] <EOL> row_data . append ( result [ u'<STR_LIT>' ] ) <EOL> for key , val in sorted ( result . items ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> if ( key not in ( u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ) ) : <EOL> row_data . append ( val ) <EOL> row_data . append ( result [ u'<STR_LIT>' ] ) <EOL> writer . writerow ( row_data ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
