#NEWLINE# def _MergeMessage( node , source , destination , replace_message , replace_repeated ) : #NEWLINE# #INDENT# 'Merge all fields specified by a sub-tree from source to destination.' #NEWLINE# source_descriptor = source . DESCRIPTOR #NEWLINE# for name in node : #NEWLINE# #INDENT# child = node [ name ] #NEWLINE# field = source_descriptor . fields_by_name [ name ] #NEWLINE# if ( field is None ) : #NEWLINE# #INDENT# raise ValueError ( "Error: Can't find field {0} in message {1}." . format ( name , source_descriptor . full_name ) ) #NEWLINE# #UNINDENT# if child : #NEWLINE# #INDENT# if ( ( field . label == FieldDescriptor . LABEL_REPEATED ) or ( field . cpp_type != FieldDescriptor . CPPTYPE_MESSAGE ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Error: Field {0} in message {1} is not a singular message field and cannot have sub-fields.' . format ( name , source_descriptor . full_name ) ) #NEWLINE# #UNINDENT# _MergeMessage ( child , getattr ( source , name ) , getattr ( destination , name ) , replace_message , replace_repeated ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( field . label == FieldDescriptor . LABEL_REPEATED ) : #NEWLINE# #INDENT# if replace_repeated : #NEWLINE# #INDENT# destination . ClearField ( _StrConvert ( name ) ) #NEWLINE# #UNINDENT# repeated_source = getattr ( source , name ) #NEWLINE# repeated_destination = getattr ( destination , name ) #NEWLINE# if ( field . cpp_type == FieldDescriptor . CPPTYPE_MESSAGE ) : #NEWLINE# #INDENT# for item in repeated_source : #NEWLINE# #INDENT# repeated_destination . add ( ) . MergeFrom ( item ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# repeated_destination . extend ( repeated_source ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( field . cpp_type == FieldDescriptor . CPPTYPE_MESSAGE ) : #NEWLINE# #INDENT# if replace_message : #NEWLINE# #INDENT# destination . ClearField ( _StrConvert ( name ) ) #NEWLINE# #UNINDENT# if source . HasField ( name ) : #NEWLINE# #INDENT# getattr ( destination , name ) . MergeFrom ( getattr ( source , name ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# setattr ( destination , name , getattr ( source , name ) )