#NEWLINE# def ToMessage( self , fields = None ) : #NEWLINE# #INDENT# 'Converts an entity to an ProtoRPC message.\n\n    Uses the fields list passed in to create a ProtoRPC message class and then\n    converts the relevant fields from the entity using ToValue.\n\n    Args:\n      fields: Optional fields, defaults to None. Passed to ProtoModel to\n          create a ProtoRPC message class for the message.\n\n    Returns:\n      The ProtoRPC message created using the values from the entity and the\n          fields provided for the message class.\n\n    Raises:\n      TypeError: if a repeated field has a value which is not a tuple or list.\n    ' #NEWLINE# proto_model = self . ProtoModel ( fields = fields ) #NEWLINE# proto_args = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for field in proto_model . all_fields ( ) : #NEWLINE# #INDENT# name = field . name #NEWLINE# value_property = _VerifyProperty ( self . __class__ , name ) #NEWLINE# value = getattr ( self , value_property . _code_name ) #NEWLINE# if ( value is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if field . repeated : #NEWLINE# #INDENT# if ( not isinstance ( value , ( list , tuple ) ) ) : #NEWLINE# #INDENT# error_msg = ( 'Property %s is a repeated field and its value should be a list or tuple. Received: %s' % ( name , value ) ) #NEWLINE# raise TypeError ( error_msg ) #NEWLINE# #UNINDENT# to_add = [ ToValue ( value_property , element ) for element in value ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# to_add = ToValue ( value_property , value ) #NEWLINE# #UNINDENT# proto_args [ name ] = to_add #NEWLINE# #UNINDENT# return proto_model ( ** proto_args )