<s> """<STR_LIT>""" <EOL> import json <EOL> from cafe . engine . models . base import ( <EOL> AutoMarshallingModel , AutoMarshallingListModel ) <EOL> class DeserializationError ( Exception ) : <EOL> pass <EOL> class EventBaseModel ( AutoMarshallingModel ) : <EOL> """<STR_LIT>""" <EOL> obj_model_key = None <EOL> kwarg_map = { } <EOL> optional_kwargs = [ ] <EOL> strict_checking = True <EOL> def __init__ ( self , kwargs ) : <EOL> super ( EventBaseModel , self ) . __init__ ( ) <EOL> for var in kwargs : <EOL> if ( var != '<STR_LIT>' and not var . startswith ( '<STR_LIT:_>' ) and <EOL> not hasattr ( self , var ) ) : <EOL> setattr ( self , var , kwargs . get ( var ) ) <EOL> @ classmethod <EOL> def _map_values_to_kwargs ( cls , deserialized_obj ) : <EOL> """<STR_LIT>""" <EOL> cls . _validate_attributes ( deserialized_obj ) <EOL> kwargs = { } <EOL> for local_kw , deserialized_obj_kw in cls . kwarg_map . iteritems ( ) : <EOL> kwargs [ local_kw ] = deserialized_obj . get ( deserialized_obj_kw , None ) <EOL> if not cls . strict_checking : <EOL> for key in cls . _extra_args : <EOL> kwargs [ key ] = deserialized_obj [ key ] <EOL> return cls ( ** kwargs ) <EOL> @ classmethod <EOL> def _validate_attributes ( cls , deserialized_obj ) : <EOL> """<STR_LIT>""" <EOL> expected = set ( cls . kwarg_map . values ( ) ) <EOL> actual = set ( deserialized_obj . keys ( ) ) <EOL> missing_args = list ( expected . difference ( actual ) ) <EOL> cls . _extra_args = list ( actual . difference ( expected ) ) <EOL> missing_args = [ arg for arg in missing_args <EOL> if arg not in cls . optional_kwargs ] <EOL> cls . _extra_args = [ arg for arg in cls . _extra_args <EOL> if arg not in cls . optional_kwargs ] <EOL> msg = '<STR_LIT>' <EOL> if cls . _extra_args and cls . strict_checking : <EOL> msg = '<STR_LIT>' . format ( <EOL> msg = msg , attrs = cls . _extra_args ) <EOL> if missing_args : <EOL> msg = '<STR_LIT>' . format ( <EOL> msg = msg , attrs = missing_args ) <EOL> if msg : <EOL> raise DeserializationError ( msg ) <EOL> @ classmethod <EOL> def _json_to_obj ( cls , serialized_str ) : <EOL> """<STR_LIT>""" <EOL> json_dict = json . loads ( serialized_str ) <EOL> model_dict = json_dict . get ( cls . obj_model_key ) or json_dict <EOL> return cls . _dict_to_obj ( model_dict ) <EOL> @ classmethod <EOL> def _dict_to_obj ( cls , json_dict ) : <EOL> """<STR_LIT>""" <EOL> return cls . _map_values_to_kwargs ( json_dict ) <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self . kwarg_map . keys ( ) : <EOL> value = getattr ( self , key ) <EOL> if value : <EOL> return value . is_empty ( ) if hasattr ( value , '<STR_LIT>' ) else False <EOL> return True <EOL> class EventBaseListModel ( AutoMarshallingListModel ) : <EOL> """<STR_LIT>""" <EOL> list_model_key = None <EOL> ObjectModel = None <EOL> @ classmethod <EOL> def _json_to_obj ( cls , serialized_str ) : <EOL> """<STR_LIT>""" <EOL> json_dict = json . loads ( serialized_str ) <EOL> cls . _validate_attributes ( json_dict ) <EOL> list_of_dicts = json_dict . get ( cls . list_model_key ) <EOL> return cls . _list_to_obj ( list_of_dicts ) <EOL> @ classmethod <EOL> def _list_to_obj ( cls , list_of_dicts ) : <EOL> """<STR_LIT>""" <EOL> obj_list = cls ( ) <EOL> for obj_dict in list_of_dicts : <EOL> obj_list . append ( cls . ObjectModel . _dict_to_obj ( obj_dict ) ) <EOL> return obj_list <EOL> @ classmethod <EOL> def _validate_attributes ( cls , json_dict ) : <EOL> """<STR_LIT>""" <EOL> if len ( json_dict ) > <NUM_LIT:1> : <EOL> extras = [ key for key in json_dict . keys ( ) <EOL> if key != cls . list_model_key ] <EOL> raise DeserializationError ( <EOL> '<STR_LIT>' . format ( <EOL> extras ) ) <EOL> elif len ( json_dict ) < <NUM_LIT:1> : <EOL> raise DeserializationError ( <EOL> '<STR_LIT>' . format ( cls . list_model_key ) ) </s>
