<s> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> import plistlib <EOL> from uuid import uuid4 <EOL> class Payload ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , payload_type , identifier , uuid = None , version = <NUM_LIT:1> , ** kwargs ) : <EOL> self . payload = { <EOL> '<STR_LIT>' : payload_type , <EOL> '<STR_LIT>' : identifier , <EOL> '<STR_LIT>' : uuid if uuid else str ( uuid4 ( ) ) . upper ( ) , <EOL> '<STR_LIT>' : version , <EOL> } <EOL> self . payload . update ( kwargs ) <EOL> def generate_dict ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . payload . copy ( ) <EOL> @ classmethod <EOL> def from_dict ( cls , payload_dict ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in payload_dict : <EOL> raise KeyError ( '<STR_LIT>' ) <EOL> pldcls = find_payload_class ( payload_dict [ '<STR_LIT>' ] ) <EOL> if pldcls : <EOL> pld = pldcls . __new__ ( pldcls ) <EOL> else : <EOL> pld = Payload . __new__ ( Payload ) <EOL> pld . payload = payload_dict <EOL> return pld <EOL> def get_uuid ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . payload [ '<STR_LIT>' ] <EOL> def set_uuid ( self , uuid = None ) : <EOL> self . payload [ '<STR_LIT>' ] = uuid if uuid else str ( uuid4 ( ) ) . upper ( ) <EOL> return self . get_uuid ( ) <EOL> def get_identifier ( self ) : <EOL> return self . payload [ '<STR_LIT>' ] <EOL> def __repr__ ( self ) : <EOL> if self . __class__ . __name__ != Payload . __name__ : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . payload [ '<STR_LIT>' ] , self . payload [ '<STR_LIT>' ] ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . payload [ '<STR_LIT>' ] , self . payload [ '<STR_LIT>' ] , self . payload [ '<STR_LIT>' ] ) <EOL> def find_payload_class ( payload_type ) : <EOL> '''<STR_LIT>''' <EOL> subclasses = set ( ) <EOL> work = [ Payload ] <EOL> while work : <EOL> parent_subclass = work . pop ( ) <EOL> for child_subclass in parent_subclass . __subclasses__ ( ) : <EOL> if child_subclass not in subclasses : <EOL> if hasattr ( child_subclass , '<STR_LIT>' ) and child_subclass . payload_type == payload_type : <EOL> return child_subclass <EOL> subclasses . add ( child_subclass ) <EOL> work . append ( child_subclass ) <EOL> return None <EOL> class Profile ( Payload ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , identifier , uuid = None , version = <NUM_LIT:1> , ** kwargs ) : <EOL> Payload . __init__ ( self , '<STR_LIT>' , identifier , uuid , version , ** kwargs ) <EOL> self . payloads = [ ] <EOL> def append_payload ( self , payload ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in self . payload . keys ( ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . payloads . append ( payload ) <EOL> def remove_payload ( self , payload ) : <EOL> self . payloads . remove ( payload ) <EOL> def generate_dict ( self ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in self . payload . keys ( ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> dict_copy = self . payload . copy ( ) <EOL> dict_copy [ '<STR_LIT>' ] = [ ] <EOL> for payload in self . payloads : <EOL> dict_copy [ '<STR_LIT>' ] . append ( payload . generate_dict ( ) ) <EOL> return dict_copy <EOL> @ classmethod <EOL> def from_dict ( cls , payload_dict ) : <EOL> '''<STR_LIT>''' <EOL> prof = Profile . __new__ ( Profile ) <EOL> prof . payloads = [ ] <EOL> payload_dict_no_payloads = payload_dict . copy ( ) <EOL> if '<STR_LIT>' in payload_dict_no_payloads : <EOL> del payload_dict_no_payloads [ '<STR_LIT>' ] <EOL> prof . payload = payload_dict_no_payloads <EOL> if '<STR_LIT>' in payload_dict : <EOL> for pld in payload_dict [ '<STR_LIT>' ] : <EOL> new_pld = Payload . from_dict ( pld ) <EOL> prof . append_payload ( new_pld ) <EOL> return prof <EOL> def generate_plist ( self ) : <EOL> '''<STR_LIT>''' <EOL> return plistlib . writePlistToString ( self . generate_dict ( ) ) <EOL> @ classmethod <EOL> def from_plist ( cls , plist ) : <EOL> '''<STR_LIT>''' <EOL> return cls . from_dict ( plistlib . readPlistFromString ( plist ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . payload [ '<STR_LIT>' ] , self . payload [ '<STR_LIT>' ] , len ( self . payloads ) ) </s>
