<s> from api import * <EOL> from codegen_core import * <EOL> __author__ = '<STR_LIT>' <EOL> def get_function_name ( prefix , data_type , media_type , api ) : <EOL> name = '<STR_LIT>' <EOL> if isinstance ( data_type , ContainerTypeRef ) : <EOL> child_type = api . get_type_by_name ( data_type . type . get_reference_name ( ) ) <EOL> name += prefix + '<STR_LIT:_>' + data_type . container <EOL> name = get_function_name ( name , child_type , media_type , api ) <EOL> else : <EOL> name += prefix + '<STR_LIT:_>' + sanitize ( data_type . name ) + '<STR_LIT:_>' + media_type <EOL> return name <EOL> def sanitize ( string ) : <EOL> temp_name = '<STR_LIT>' . join ( [ i for i in string if i . isalnum ( ) or i == '<STR_LIT:_>' ] ) <EOL> if not temp_name : <EOL> temp_name = '<STR_LIT>' <EOL> elif temp_name [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> temp_name = '<STR_LIT>' + temp_name <EOL> return temp_name <EOL> def get_serializer ( media_type , classes , class_name_mappings ) : <EOL> if media_type == '<STR_LIT>' : <EOL> return JSONSerializer ( classes , class_name_mappings ) <EOL> elif media_type == '<STR_LIT>' : <EOL> return FormSerializer ( classes , class_name_mappings ) <EOL> else : <EOL> return UnsupportedSerializer ( classes , class_name_mappings , media_type ) <EOL> class AbstractSerializer : <EOL> def __init__ ( self , classes , class_name_mappings ) : <EOL> self . classes = classes <EOL> self . class_name_mappings = class_name_mappings <EOL> def generate_serializers ( self , data_type , api ) : <EOL> raise NotImplementedError <EOL> def generate_deserializers ( self , data_type , api ) : <EOL> raise NotImplementedError <EOL> def generate_final_serializer ( self ) : <EOL> raise NotImplementedError <EOL> def get_class_parameter ( self , data_type , key ) : <EOL> class_name = self . class_name_mappings [ data_type ] <EOL> for clazz in self . classes : <EOL> if clazz . name == class_name : <EOL> constructor = clazz . get_constructor ( ) <EOL> return constructor . get_mapping ( key ) <EOL> return None <EOL> class UnsupportedSerializer ( AbstractSerializer ) : <EOL> def __init__ ( self , classes , class_name_mappings , media_type ) : <EOL> AbstractSerializer . __init__ ( self , classes , class_name_mappings ) <EOL> self . media_type = media_type <EOL> def generate_final_serializer ( self ) : <EOL> sm = StaticMethod ( '<STR_LIT>' + self . media_type ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> return sm <EOL> def generate_serializers ( self , data_type , api ) : <EOL> function_name = get_function_name ( '<STR_LIT>' , data_type , self . media_type , api ) <EOL> sm = StaticMethod ( function_name ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> return [ sm , self . generate_final_serializer ( ) ] <EOL> def generate_deserializers ( self , data_type , api ) : <EOL> function_name = get_function_name ( '<STR_LIT>' , data_type , self . media_type , api ) <EOL> sm = StaticMethod ( function_name ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> return [ sm ] <EOL> class JSONSerializer ( AbstractSerializer ) : <EOL> def __init__ ( self , classes , class_name_mappings ) : <EOL> AbstractSerializer . __init__ ( self , classes , class_name_mappings ) <EOL> def generate_final_serializer ( self ) : <EOL> sm = StaticMethod ( '<STR_LIT>' ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> return sm <EOL> def generate_serializers ( self , data_type , api ) : <EOL> serializers = list ( ) <EOL> function_name = get_function_name ( '<STR_LIT>' , data_type , '<STR_LIT>' , api ) <EOL> sm = StaticMethod ( function_name ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> if isinstance ( data_type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> elif isinstance ( data_type , ContainerTypeRef ) : <EOL> if isinstance ( data_type . type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> else : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> child_serializers = self . generate_serializers ( data_type . type , api ) <EOL> serializers . extend ( child_serializers ) <EOL> sm . add_line ( '<STR_LIT>' + child_serializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> elif isinstance ( data_type , NamedTypeDef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> for field in data_type . fields : <EOL> field_type = field . type . type <EOL> key = field . name <EOL> param = self . get_class_parameter ( data_type . name , key ) <EOL> if isinstance ( field_type , PrimitiveTypeRef ) : <EOL> value = '<STR_LIT>' + param <EOL> elif isinstance ( field_type , ContainerTypeRef ) and isinstance ( field_type . type , PrimitiveTypeRef ) : <EOL> value = '<STR_LIT>' + param <EOL> elif isinstance ( field_type , TypeDef ) : <EOL> named_type = NamedTypeDef ( name = data_type . name + '<STR_LIT:_>' + field . name , <EOL> fields = field_type . fields ) <EOL> child_serializers = self . generate_serializers ( named_type , api ) <EOL> serializers . extend ( child_serializers ) <EOL> value = child_serializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' + param + '<STR_LIT:)>' <EOL> else : <EOL> child_serializers = self . generate_serializers ( field_type , api ) <EOL> serializers . extend ( child_serializers ) <EOL> value = child_serializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' + param + '<STR_LIT:)>' <EOL> if field . optional : <EOL> sm . add_line ( '<STR_LIT>' + param + '<STR_LIT::>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' + key + '<STR_LIT>' + value ) <EOL> sm . dedent ( ) <EOL> else : <EOL> sm . add_line ( '<STR_LIT>' + key + '<STR_LIT>' + value ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> else : <EOL> actual_type = api . get_type_by_name ( data_type . get_reference_name ( ) ) <EOL> serializers . extend ( self . generate_serializers ( actual_type , api ) ) <EOL> serializers . append ( self . generate_final_serializer ( ) ) <EOL> return serializers <EOL> def generate_deserializers ( self , data_type , api ) : <EOL> deserializers = list ( ) <EOL> function_name = get_function_name ( '<STR_LIT>' , data_type , '<STR_LIT>' , api ) <EOL> sm = StaticMethod ( function_name ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> if isinstance ( data_type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> deserializers . append ( sm ) <EOL> elif isinstance ( data_type , ContainerTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> if isinstance ( data_type . type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> else : <EOL> child_deserializers = self . generate_deserializers ( data_type . type , api ) <EOL> deserializers . extend ( child_deserializers ) <EOL> sm . add_line ( '<STR_LIT>' + child_deserializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> if data_type . container == '<STR_LIT:list>' : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> else : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> deserializers . append ( sm ) <EOL> elif isinstance ( data_type , NamedTypeDef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> if data_type . name in PrimitiveTypeRef . PRIMITIVES : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> elif data_type . name == '<STR_LIT>' : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> else : <EOL> fields = data_type . fields <EOL> constructor = '<STR_LIT>' + self . class_name_mappings [ data_type . name ] + '<STR_LIT:(>' <EOL> i = <NUM_LIT:0> <EOL> for field in fields : <EOL> if i > <NUM_LIT:0> : <EOL> constructor += '<STR_LIT:U+002CU+0020>' <EOL> i += <NUM_LIT:1> <EOL> field_type = field . type . type <EOL> param = self . get_class_parameter ( data_type . name , field . name ) <EOL> if isinstance ( field_type , PrimitiveTypeRef ) : <EOL> sm . add_line ( param + '<STR_LIT>' + field . name + '<STR_LIT>' ) <EOL> elif isinstance ( field_type , ContainerTypeRef ) and isinstance ( field_type . type , PrimitiveTypeRef ) : <EOL> sm . add_line ( param + '<STR_LIT>' + field . name + '<STR_LIT>' ) <EOL> elif isinstance ( field_type , TypeDef ) : <EOL> named_type = NamedTypeDef ( name = data_type . name + '<STR_LIT:_>' + field . name , <EOL> fields = field_type . fields ) <EOL> child_deserializers = self . generate_deserializers ( named_type , api ) <EOL> deserializers . extend ( child_deserializers ) <EOL> sm . add_line ( param + '<STR_LIT>' + child_deserializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' + field . name + '<STR_LIT>' ) <EOL> else : <EOL> child_deserializers = self . generate_deserializers ( field_type , api ) <EOL> deserializers . extend ( child_deserializers ) <EOL> sm . add_line ( param + '<STR_LIT>' + child_deserializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' + field . name + '<STR_LIT>' ) <EOL> constructor += param + '<STR_LIT:=>' + param <EOL> constructor += '<STR_LIT:)>' <EOL> sm . add_line ( constructor ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> deserializers . append ( sm ) <EOL> else : <EOL> actual_type = api . get_type_by_name ( data_type . get_reference_name ( ) ) <EOL> deserializers . extend ( self . generate_deserializers ( actual_type , api ) ) <EOL> return deserializers <EOL> class FormSerializer ( AbstractSerializer ) : <EOL> def __init__ ( self , classes , class_name_mappings ) : <EOL> AbstractSerializer . __init__ ( self , classes , class_name_mappings ) <EOL> def generate_final_serializer ( self ) : <EOL> sm = StaticMethod ( '<STR_LIT>' ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> return sm <EOL> def generate_serializers ( self , data_type , api ) : <EOL> serializers = list ( ) <EOL> function_name = get_function_name ( '<STR_LIT>' , data_type , '<STR_LIT>' , api ) <EOL> sm = StaticMethod ( function_name ) <EOL> sm . arguments . append ( MethodArgument ( '<STR_LIT>' ) ) <EOL> if isinstance ( data_type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> elif isinstance ( data_type , ContainerTypeRef ) : <EOL> if isinstance ( data_type . type , PrimitiveTypeRef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> else : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> sm . indent ( ) <EOL> child_serializers = self . generate_serializers ( data_type . type , api ) <EOL> serializers . extend ( child_serializers ) <EOL> sm . add_line ( '<STR_LIT>' + child_serializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' ) <EOL> sm . dedent ( ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> elif isinstance ( data_type , NamedTypeDef ) : <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> for field in data_type . fields : <EOL> field_type = field . type . type <EOL> key = field . name <EOL> param = self . get_class_parameter ( data_type . name , key ) <EOL> if isinstance ( field_type , PrimitiveTypeRef ) : <EOL> value = '<STR_LIT>' + param <EOL> elif isinstance ( field_type , ContainerTypeRef ) and isinstance ( field_type . type , PrimitiveTypeRef ) : <EOL> value = '<STR_LIT>' + param <EOL> else : <EOL> child_serializers = self . generate_serializers ( field_type , api ) <EOL> serializers . extend ( child_serializers ) <EOL> value = child_serializers [ - <NUM_LIT:1> ] . name + '<STR_LIT>' + param + '<STR_LIT:)>' <EOL> if field . optional : <EOL> sm . add_line ( '<STR_LIT>' + param + '<STR_LIT::>' ) <EOL> sm . indent ( ) <EOL> sm . add_line ( '<STR_LIT>' + key + '<STR_LIT>' + value ) <EOL> sm . dedent ( ) <EOL> else : <EOL> sm . add_line ( '<STR_LIT>' + key + '<STR_LIT>' + value ) <EOL> sm . add_line ( '<STR_LIT>' ) <EOL> serializers . append ( sm ) <EOL> else : <EOL> actual_type = api . get_type_by_name ( data_type . get_reference_name ( ) ) <EOL> serializers . extend ( self . generate_serializers ( actual_type , api ) ) <EOL> serializers . append ( self . generate_final_serializer ( ) ) <EOL> return serializers </s>
