<s> import importlib <EOL> import os <EOL> import six <EOL> from facebookads . utils import api_utils <EOL> from facebookads . exceptions import ( <EOL> FacebookBadParameterTypeException , <EOL> ) <EOL> class TypeChecker : <EOL> """<STR_LIT>""" <EOL> primitive_types = set ( [ "<STR_LIT>" , "<STR_LIT:int>" , "<STR_LIT:bool>" , "<STR_LIT:string>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:float>" ] ) <EOL> def __init__ ( self , type_check_info , type_check_enum ) : <EOL> self . _type_check_info = type_check_info <EOL> self . _enum_data = type_check_enum <EOL> def is_primitive_type ( self , type ) : <EOL> return ( type in self . primitive_types ) or ( type in self . _enum_data ) <EOL> def convert_string_to_prim_type ( self , primitive_type , value ) : <EOL> if primitive_type in self . _enum_data : <EOL> return value <EOL> elif primitive_type in ( "<STR_LIT>" , "<STR_LIT:int>" ) : <EOL> return int ( value ) <EOL> elif primitive_type == "<STR_LIT:bool>" : <EOL> if value in ( "<STR_LIT:false>" , "<STR_LIT:0>" , "<STR_LIT:null>" ) : <EOL> return False <EOL> return True <EOL> elif primitive_type == "<STR_LIT:float>" : <EOL> return float ( value ) <EOL> elif primitive_type == "<STR_LIT>" : <EOL> return value <EOL> elif primitive_type == "<STR_LIT:string>" : <EOL> return str ( value ) <EOL> elif primitive_type == "<STR_LIT>" : <EOL> return value <EOL> else : <EOL> raise FacebookBadParameterTypeException ( '<STR_LIT>' + <EOL> value . __class__ . __name__ + '<STR_LIT>' + str ( primitive_type ) ) <EOL> def get_type ( self , param ) : <EOL> if param not in self . _type_check_info : <EOL> return None <EOL> return self . _type_check_info [ param ] <EOL> def is_valid_key ( self , param ) : <EOL> return param in self . _type_check_info <EOL> def is_valid_pair ( self , param , value ) : <EOL> if self . is_valid_key ( param ) : <EOL> value_type = self . _type_check_info [ param ] <EOL> return self . is_type ( value_type , value ) <EOL> else : <EOL> return True <EOL> def is_type ( self , value_type , value , allow_dict_as_obj = True ) : <EOL> if value is None or value_type is None : <EOL> return True <EOL> if value_type in self . _enum_data : <EOL> return value in self . _enum_data [ value_type ] <EOL> if value_type == '<STR_LIT:file>' : <EOL> return os . path . isfile ( value ) <EOL> if value_type == '<STR_LIT:list>' : <EOL> return isinstance ( value , list ) <EOL> if isinstance ( value , dict ) and value_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return True <EOL> if isinstance ( value , bool ) : <EOL> return value_type in [ '<STR_LIT:bool>' ] <EOL> if isinstance ( value , six . string_types ) : <EOL> if value_type in [ '<STR_LIT:string>' , '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' ] : <EOL> return True <EOL> elif value_type == '<STR_LIT:bool>' and value in [ '<STR_LIT:true>' , '<STR_LIT:false>' ] : <EOL> return True <EOL> elif value_type in [ '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT:float>' ] : <EOL> return value . isdigit ( ) <EOL> else : <EOL> return False <EOL> if isinstance ( value , ( int , float ) ) : <EOL> return value_type in [ '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT:float>' , '<STR_LIT:string>' , '<STR_LIT>' ] <EOL> if self . is_type_collection ( value_type , '<STR_LIT:list>' ) : <EOL> if not isinstance ( value , list ) : <EOL> return False <EOL> sub_type = self . get_type_from_collection ( value_type , '<STR_LIT:list>' ) [ <NUM_LIT:0> ] <EOL> return all ( [ self . is_type ( sub_type , item ) for item in value ] ) <EOL> if self . is_type_collection ( value_type , '<STR_LIT>' ) : <EOL> if not isinstance ( value , dict ) : <EOL> return False <EOL> sub_types = self . get_type_from_collection ( value_type , '<STR_LIT>' ) <EOL> sub_type_key = sub_types [ <NUM_LIT:0> ] <EOL> sub_type_value = sub_types [ <NUM_LIT:1> ] <EOL> return all ( [ self . is_type ( sub_type_key , k ) and <EOL> self . is_type ( sub_type_value , v ) for k , v in value . items ( ) ] ) <EOL> if ( type ( value ) . __name__ == value_type or <EOL> hasattr ( value , '<STR_LIT>' + value_type ) ) : <EOL> return True <EOL> if allow_dict_as_obj and isinstance ( value , dict ) : <EOL> return self . _type_is_ad_object ( value_type ) <EOL> return False <EOL> def is_type_collection ( self , value_type , collection_name ) : <EOL> return collection_name == value_type [ : len ( collection_name ) ] <EOL> def get_type_from_collection ( self , value_type , collection_name ) : <EOL> return [ s . strip ( ) for s in <EOL> value_type [ len ( collection_name ) : ] . strip ( "<STR_LIT>" ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> def is_list_param ( self , param ) : <EOL> if self . is_valid_key ( param ) : <EOL> return self . is_type_collection ( self . _type_check_info [ param ] , "<STR_LIT:list>" ) <EOL> return False <EOL> def is_map_param ( self , param ) : <EOL> if self . is_valid_key ( param ) : <EOL> return self . is_type_collection ( self . _type_check_info [ param ] , "<STR_LIT>" ) <EOL> return False <EOL> def is_file_param ( self , param ) : <EOL> if param == "<STR_LIT:filename>" : <EOL> return True <EOL> if self . is_valid_key ( param ) : <EOL> return self . _type_check_info [ param ] == "<STR_LIT:file>" <EOL> return False <EOL> def get_typed_value ( self , key , value ) : <EOL> if not self . is_valid_key ( key ) : <EOL> return value <EOL> field_type = self . get_type ( key ) <EOL> if self . is_type ( field_type , value , allow_dict_as_obj = False ) : <EOL> return value <EOL> if self . is_primitive_type ( field_type ) and isinstance ( value , six . text_type ) : <EOL> typed_value = self . convert_string_to_prim_type ( field_type , value ) <EOL> elif self . is_type_collection ( field_type , "<STR_LIT:list>" ) : <EOL> sub_type = self . get_type_from_collection ( field_type , "<STR_LIT:list>" ) [ <NUM_LIT:0> ] <EOL> if isinstance ( value , list ) : <EOL> typed_value = [ self . get_typed_value ( sub_type , v ) for v in value ] <EOL> else : <EOL> typed_value = [ self . get_typed_value ( sub_type , value ) ] <EOL> elif self . is_type_collection ( field_type , "<STR_LIT>" ) : <EOL> sub_types = self . get_type_from_collection ( field_type , "<STR_LIT>" ) <EOL> if len ( sub_types ) == <NUM_LIT:2> : <EOL> sub_type_key = sub_types [ <NUM_LIT:0> ] <EOL> sub_type_value = sub_types [ <NUM_LIT:1> ] <EOL> else : <EOL> sub_type_key = '<STR_LIT:string>' <EOL> sub_type_value = sub_types [ <NUM_LIT:0> ] <EOL> typed_value = dict ( <EOL> ( self . get_typed_value ( sub_type_key , k ) , <EOL> self . get_typed_value ( sub_type_value , v ) ) <EOL> for ( k , v ) in value . items ( ) <EOL> ) <EOL> elif isinstance ( value , dict ) : <EOL> try : <EOL> typed_value = self . _create_field_object ( field_type , value ) <EOL> except : <EOL> typed_value = value <EOL> else : <EOL> typed_value = value <EOL> if not self . is_type ( field_type , typed_value ) : <EOL> api_utils . warning ( '<STR_LIT>' + key + '<STR_LIT>' + <EOL> '<STR_LIT>' + field_type + '<STR_LIT>' + str ( type ( typed_value ) ) ) <EOL> return typed_value <EOL> def _create_field_object ( self , field_type , data = None ) : <EOL> mod = importlib . import_module ( <EOL> "<STR_LIT>" + field_type . lower ( ) ) <EOL> if mod is not None and hasattr ( mod , field_type ) : <EOL> obj = ( getattr ( mod , field_type ) ) ( ) <EOL> if data is not None : <EOL> obj . _set_data ( data ) <EOL> return obj <EOL> return None <EOL> def _type_is_ad_object ( self , value_type ) : <EOL> try : <EOL> mod = importlib . import_module ( <EOL> "<STR_LIT>" + value_type . lower ( ) ) <EOL> return mod is not None <EOL> except : <EOL> return False </s>
