<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import sys <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> try : <EOL> from io import StringIO as BytesIO <EOL> except ImportError : <EOL> from io import StringIO as BytesIO <EOL> import copyreg as copyreg <EOL> else : <EOL> from io import BytesIO <EOL> import copyreg <EOL> import struct <EOL> import weakref <EOL> from google . protobuf . internal import containers <EOL> from google . protobuf . internal import decoder <EOL> from google . protobuf . internal import encoder <EOL> from google . protobuf . internal import enum_type_wrapper <EOL> from google . protobuf . internal import message_listener as message_listener_mod <EOL> from google . protobuf . internal import type_checkers <EOL> from google . protobuf . internal import wire_format <EOL> from google . protobuf import descriptor as descriptor_mod <EOL> from google . protobuf import message as message_mod <EOL> from google . protobuf import text_format <EOL> _FieldDescriptor = descriptor_mod . FieldDescriptor <EOL> def NewMessage ( bases , descriptor , dictionary ) : <EOL> _AddClassAttributesForNestedExtensions ( descriptor , dictionary ) <EOL> _AddSlots ( descriptor , dictionary ) <EOL> return bases <EOL> def InitMessage ( descriptor , cls ) : <EOL> cls . _decoders_by_tag = { } <EOL> cls . _extensions_by_name = { } <EOL> cls . _extensions_by_number = { } <EOL> if ( descriptor . has_options and <EOL> descriptor . GetOptions ( ) . message_set_wire_format ) : <EOL> cls . _decoders_by_tag [ decoder . MESSAGE_SET_ITEM_TAG ] = ( <EOL> decoder . MessageSetItemDecoder ( cls . _extensions_by_number ) ) <EOL> for field in descriptor . fields : <EOL> _AttachFieldHelpers ( cls , field ) <EOL> _AddEnumValues ( descriptor , cls ) <EOL> _AddInitMethod ( descriptor , cls ) <EOL> _AddPropertiesForFields ( descriptor , cls ) <EOL> _AddPropertiesForExtensions ( descriptor , cls ) <EOL> _AddStaticMethods ( cls ) <EOL> _AddMessageMethods ( descriptor , cls ) <EOL> _AddPrivateHelperMethods ( descriptor , cls ) <EOL> copyreg . pickle ( cls , lambda obj : ( cls , ( ) , obj . __getstate__ ( ) ) ) <EOL> def _PropertyName ( proto_field_name ) : <EOL> """<STR_LIT>""" <EOL> return proto_field_name <EOL> def _VerifyExtensionHandle ( message , extension_handle ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( extension_handle , _FieldDescriptor ) : <EOL> raise KeyError ( '<STR_LIT>' % <EOL> extension_handle ) <EOL> if not extension_handle . is_extension : <EOL> raise KeyError ( '<STR_LIT>' % extension_handle . full_name ) <EOL> if not extension_handle . containing_type : <EOL> raise KeyError ( '<STR_LIT>' <EOL> % extension_handle . full_name ) <EOL> if extension_handle . containing_type is not message . DESCRIPTOR : <EOL> raise KeyError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( extension_handle . full_name , <EOL> extension_handle . containing_type . full_name , <EOL> message . DESCRIPTOR . full_name ) ) <EOL> def _AddSlots ( message_descriptor , dictionary ) : <EOL> """<STR_LIT>""" <EOL> dictionary [ '<STR_LIT>' ] = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def _IsMessageSetExtension ( field ) : <EOL> return ( field . is_extension and <EOL> field . containing_type . has_options and <EOL> field . containing_type . GetOptions ( ) . message_set_wire_format and <EOL> field . type == _FieldDescriptor . TYPE_MESSAGE and <EOL> field . message_type == field . extension_scope and <EOL> field . label == _FieldDescriptor . LABEL_OPTIONAL ) <EOL> def _AttachFieldHelpers ( cls , field_descriptor ) : <EOL> is_repeated = ( field_descriptor . label == _FieldDescriptor . LABEL_REPEATED ) <EOL> is_packed = ( field_descriptor . has_options and <EOL> field_descriptor . GetOptions ( ) . packed ) <EOL> if _IsMessageSetExtension ( field_descriptor ) : <EOL> field_encoder = encoder . MessageSetItemEncoder ( field_descriptor . number ) <EOL> sizer = encoder . MessageSetItemSizer ( field_descriptor . number ) <EOL> else : <EOL> field_encoder = type_checkers . TYPE_TO_ENCODER [ field_descriptor . type ] ( <EOL> field_descriptor . number , is_repeated , is_packed ) <EOL> sizer = type_checkers . TYPE_TO_SIZER [ field_descriptor . type ] ( <EOL> field_descriptor . number , is_repeated , is_packed ) <EOL> field_descriptor . _encoder = field_encoder <EOL> field_descriptor . _sizer = sizer <EOL> field_descriptor . _default_constructor = _DefaultValueConstructorForField ( <EOL> field_descriptor ) <EOL> def AddDecoder ( wiretype , is_packed ) : <EOL> tag_bytes = encoder . TagBytes ( field_descriptor . number , wiretype ) <EOL> cls . _decoders_by_tag [ tag_bytes ] = ( <EOL> type_checkers . TYPE_TO_DECODER [ field_descriptor . type ] ( <EOL> field_descriptor . number , is_repeated , is_packed , <EOL> field_descriptor , field_descriptor . _default_constructor ) ) <EOL> AddDecoder ( type_checkers . FIELD_TYPE_TO_WIRE_TYPE [ field_descriptor . type ] , <EOL> False ) <EOL> if is_repeated and wire_format . IsTypePackable ( field_descriptor . type ) : <EOL> AddDecoder ( wire_format . WIRETYPE_LENGTH_DELIMITED , True ) <EOL> def _AddClassAttributesForNestedExtensions ( descriptor , dictionary ) : <EOL> extension_dict = descriptor . extensions_by_name <EOL> for extension_name , extension_field in list ( extension_dict . items ( ) ) : <EOL> assert extension_name not in dictionary <EOL> dictionary [ extension_name ] = extension_field <EOL> def _AddEnumValues ( descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> for enum_type in descriptor . enum_types : <EOL> setattr ( cls , enum_type . name , enum_type_wrapper . EnumTypeWrapper ( enum_type ) ) <EOL> for enum_value in enum_type . values : <EOL> setattr ( cls , enum_value . name , enum_value . number ) <EOL> def _DefaultValueConstructorForField ( field ) : <EOL> """<STR_LIT>""" <EOL> if field . label == _FieldDescriptor . LABEL_REPEATED : <EOL> if field . has_default_value and field . default_value != [ ] : <EOL> raise ValueError ( '<STR_LIT>' % ( <EOL> field . default_value ) ) <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> message_type = field . message_type <EOL> def MakeRepeatedMessageDefault ( message ) : <EOL> return containers . RepeatedCompositeFieldContainer ( <EOL> message . _listener_for_children , field . message_type ) <EOL> return MakeRepeatedMessageDefault <EOL> else : <EOL> type_checker = type_checkers . GetTypeChecker ( field ) <EOL> def MakeRepeatedScalarDefault ( message ) : <EOL> return containers . RepeatedScalarFieldContainer ( <EOL> message . _listener_for_children , type_checker ) <EOL> return MakeRepeatedScalarDefault <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> message_type = field . message_type <EOL> def MakeSubMessageDefault ( message ) : <EOL> result = message_type . _concrete_class ( ) <EOL> result . _SetListener ( message . _listener_for_children ) <EOL> return result <EOL> return MakeSubMessageDefault <EOL> def MakeScalarDefault ( message ) : <EOL> return field . default_value <EOL> return MakeScalarDefault <EOL> def _AddInitMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> fields = message_descriptor . fields <EOL> def init ( self , ** kwargs ) : <EOL> self . _cached_byte_size = <NUM_LIT:0> <EOL> self . _cached_byte_size_dirty = len ( kwargs ) > <NUM_LIT:0> <EOL> self . _fields = { } <EOL> self . _oneofs = { } <EOL> self . _unknown_fields = ( ) <EOL> self . _is_present_in_parent = False <EOL> self . _listener = message_listener_mod . NullMessageListener ( ) <EOL> self . _listener_for_children = _Listener ( self ) <EOL> for field_name , field_value in list ( kwargs . items ( ) ) : <EOL> field = _GetFieldByName ( message_descriptor , field_name ) <EOL> if field is None : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( message_descriptor . name , field_name ) ) <EOL> if field . label == _FieldDescriptor . LABEL_REPEATED : <EOL> copy = field . _default_constructor ( self ) <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> for val in field_value : <EOL> copy . add ( ) . MergeFrom ( val ) <EOL> else : <EOL> copy . extend ( field_value ) <EOL> self . _fields [ field ] = copy <EOL> elif field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> copy = field . _default_constructor ( self ) <EOL> copy . MergeFrom ( field_value ) <EOL> self . _fields [ field ] = copy <EOL> else : <EOL> setattr ( self , field_name , field_value ) <EOL> init . __module__ = None <EOL> init . __doc__ = None <EOL> cls . __init__ = init <EOL> def _GetFieldByName ( message_descriptor , field_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return message_descriptor . fields_by_name [ field_name ] <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' % field_name ) <EOL> def _AddPropertiesForFields ( descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> for field in descriptor . fields : <EOL> _AddPropertiesForField ( field , cls ) <EOL> if descriptor . is_extendable : <EOL> cls . Extensions = property ( lambda self : _ExtensionDict ( self ) ) <EOL> def _AddPropertiesForField ( field , cls ) : <EOL> """<STR_LIT>""" <EOL> assert _FieldDescriptor . MAX_CPPTYPE == <NUM_LIT:10> <EOL> constant_name = field . name . upper ( ) + "<STR_LIT>" <EOL> setattr ( cls , constant_name , field . number ) <EOL> if field . label == _FieldDescriptor . LABEL_REPEATED : <EOL> _AddPropertiesForRepeatedField ( field , cls ) <EOL> elif field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> _AddPropertiesForNonRepeatedCompositeField ( field , cls ) <EOL> else : <EOL> _AddPropertiesForNonRepeatedScalarField ( field , cls ) <EOL> def _AddPropertiesForRepeatedField ( field , cls ) : <EOL> """<STR_LIT>""" <EOL> proto_field_name = field . name <EOL> property_name = _PropertyName ( proto_field_name ) <EOL> def getter ( self ) : <EOL> field_value = self . _fields . get ( field ) <EOL> if field_value is None : <EOL> field_value = field . _default_constructor ( self ) <EOL> field_value = self . _fields . setdefault ( field , field_value ) <EOL> return field_value <EOL> getter . __module__ = None <EOL> getter . __doc__ = '<STR_LIT>' % proto_field_name <EOL> def setter ( self , new_value ) : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % proto_field_name ) <EOL> doc = '<STR_LIT>' % proto_field_name <EOL> setattr ( cls , property_name , property ( getter , setter , doc = doc ) ) <EOL> def _AddPropertiesForNonRepeatedScalarField ( field , cls ) : <EOL> """<STR_LIT>""" <EOL> proto_field_name = field . name <EOL> property_name = _PropertyName ( proto_field_name ) <EOL> type_checker = type_checkers . GetTypeChecker ( field ) <EOL> default_value = field . default_value <EOL> valid_values = set ( ) <EOL> def getter ( self ) : <EOL> return self . _fields . get ( field , default_value ) <EOL> getter . __module__ = None <EOL> getter . __doc__ = '<STR_LIT>' % proto_field_name <EOL> def field_setter ( self , new_value ) : <EOL> self . _fields [ field ] = type_checker . CheckValue ( new_value ) <EOL> if not self . _cached_byte_size_dirty : <EOL> self . _Modified ( ) <EOL> if field . containing_oneof is not None : <EOL> def setter ( self , new_value ) : <EOL> field_setter ( self , new_value ) <EOL> self . _UpdateOneofState ( field ) <EOL> else : <EOL> setter = field_setter <EOL> setter . __module__ = None <EOL> setter . __doc__ = '<STR_LIT>' % proto_field_name <EOL> doc = '<STR_LIT>' % proto_field_name <EOL> setattr ( cls , property_name , property ( getter , setter , doc = doc ) ) <EOL> def _AddPropertiesForNonRepeatedCompositeField ( field , cls ) : <EOL> """<STR_LIT>""" <EOL> proto_field_name = field . name <EOL> property_name = _PropertyName ( proto_field_name ) <EOL> message_type = field . message_type <EOL> def getter ( self ) : <EOL> field_value = self . _fields . get ( field ) <EOL> if field_value is None : <EOL> field_value = message_type . _concrete_class ( ) <EOL> field_value . _SetListener ( <EOL> _OneofListener ( self , field ) <EOL> if field . containing_oneof is not None <EOL> else self . _listener_for_children ) <EOL> field_value = self . _fields . setdefault ( field , field_value ) <EOL> return field_value <EOL> getter . __module__ = None <EOL> getter . __doc__ = '<STR_LIT>' % proto_field_name <EOL> def setter ( self , new_value ) : <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % proto_field_name ) <EOL> doc = '<STR_LIT>' % proto_field_name <EOL> setattr ( cls , property_name , property ( getter , setter , doc = doc ) ) <EOL> def _AddPropertiesForExtensions ( descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> extension_dict = descriptor . extensions_by_name <EOL> for extension_name , extension_field in list ( extension_dict . items ( ) ) : <EOL> constant_name = extension_name . upper ( ) + "<STR_LIT>" <EOL> setattr ( cls , constant_name , extension_field . number ) <EOL> def _AddStaticMethods ( cls ) : <EOL> def RegisterExtension ( extension_handle ) : <EOL> extension_handle . containing_type = cls . DESCRIPTOR <EOL> _AttachFieldHelpers ( cls , extension_handle ) <EOL> actual_handle = cls . _extensions_by_number . setdefault ( <EOL> extension_handle . number , extension_handle ) <EOL> if actual_handle is not extension_handle : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( extension_handle . full_name , actual_handle . full_name , <EOL> cls . DESCRIPTOR . full_name , extension_handle . number ) ) <EOL> cls . _extensions_by_name [ extension_handle . full_name ] = extension_handle <EOL> handle = extension_handle <EOL> if _IsMessageSetExtension ( handle ) : <EOL> cls . _extensions_by_name [ <EOL> extension_handle . message_type . full_name ] = extension_handle <EOL> cls . RegisterExtension = staticmethod ( RegisterExtension ) <EOL> def FromString ( s ) : <EOL> message = cls ( ) <EOL> message . MergeFromString ( s ) <EOL> return message <EOL> cls . FromString = staticmethod ( FromString ) <EOL> def _IsPresent ( item ) : <EOL> """<STR_LIT>""" <EOL> if item [ <NUM_LIT:0> ] . label == _FieldDescriptor . LABEL_REPEATED : <EOL> return bool ( item [ <NUM_LIT:1> ] ) <EOL> elif item [ <NUM_LIT:0> ] . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> return item [ <NUM_LIT:1> ] . _is_present_in_parent <EOL> else : <EOL> return True <EOL> def _AddListFieldsMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def ListFields ( self ) : <EOL> all_fields = [ item for item in list ( self . _fields . items ( ) ) if _IsPresent ( item ) ] <EOL> all_fields . sort ( key = lambda item : item [ <NUM_LIT:0> ] . number ) <EOL> return all_fields <EOL> cls . ListFields = ListFields <EOL> def _AddHasFieldMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> singular_fields = { } <EOL> for field in message_descriptor . fields : <EOL> if field . label != _FieldDescriptor . LABEL_REPEATED : <EOL> singular_fields [ field . name ] = field <EOL> for field in message_descriptor . oneofs : <EOL> singular_fields [ field . name ] = field <EOL> def HasField ( self , field_name ) : <EOL> try : <EOL> field = singular_fields [ field_name ] <EOL> except KeyError : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % field_name ) <EOL> if isinstance ( field , descriptor_mod . OneofDescriptor ) : <EOL> try : <EOL> return HasField ( self , self . _oneofs [ field ] . name ) <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> value = self . _fields . get ( field ) <EOL> return value is not None and value . _is_present_in_parent <EOL> else : <EOL> return field in self . _fields <EOL> cls . HasField = HasField <EOL> def _AddClearFieldMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def ClearField ( self , field_name ) : <EOL> try : <EOL> field = message_descriptor . fields_by_name [ field_name ] <EOL> except KeyError : <EOL> try : <EOL> field = message_descriptor . oneofs_by_name [ field_name ] <EOL> if field in self . _oneofs : <EOL> field = self . _oneofs [ field ] <EOL> else : <EOL> return <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' % field_name ) <EOL> if field in self . _fields : <EOL> del self . _fields [ field ] <EOL> if self . _oneofs . get ( field . containing_oneof , None ) is field : <EOL> del self . _oneofs [ field . containing_oneof ] <EOL> self . _Modified ( ) <EOL> cls . ClearField = ClearField <EOL> def _AddClearExtensionMethod ( cls ) : <EOL> """<STR_LIT>""" <EOL> def ClearExtension ( self , extension_handle ) : <EOL> _VerifyExtensionHandle ( self , extension_handle ) <EOL> if extension_handle in self . _fields : <EOL> del self . _fields [ extension_handle ] <EOL> self . _Modified ( ) <EOL> cls . ClearExtension = ClearExtension <EOL> def _AddClearMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def Clear ( self ) : <EOL> self . _fields = { } <EOL> self . _unknown_fields = ( ) <EOL> self . _Modified ( ) <EOL> cls . Clear = Clear <EOL> def _AddHasExtensionMethod ( cls ) : <EOL> """<STR_LIT>""" <EOL> def HasExtension ( self , extension_handle ) : <EOL> _VerifyExtensionHandle ( self , extension_handle ) <EOL> if extension_handle . label == _FieldDescriptor . LABEL_REPEATED : <EOL> raise KeyError ( '<STR_LIT>' % extension_handle . full_name ) <EOL> if extension_handle . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> value = self . _fields . get ( extension_handle ) <EOL> return value is not None and value . _is_present_in_parent <EOL> else : <EOL> return extension_handle in self . _fields <EOL> cls . HasExtension = HasExtension <EOL> def _AddEqualsMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def __eq__ ( self , other ) : <EOL> if ( not isinstance ( other , message_mod . Message ) or <EOL> other . DESCRIPTOR != self . DESCRIPTOR ) : <EOL> return False <EOL> if self is other : <EOL> return True <EOL> if not self . ListFields ( ) == other . ListFields ( ) : <EOL> return False <EOL> unknown_fields = list ( self . _unknown_fields ) <EOL> unknown_fields . sort ( ) <EOL> other_unknown_fields = list ( other . _unknown_fields ) <EOL> other_unknown_fields . sort ( ) <EOL> return unknown_fields == other_unknown_fields <EOL> cls . __eq__ = __eq__ <EOL> def _AddStrMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return text_format . MessageToString ( self ) <EOL> cls . __str__ = __str__ <EOL> def _AddUnicodeMethod ( unused_message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def __unicode__ ( self ) : <EOL> return text_format . MessageToString ( self , as_utf8 = True ) . decode ( '<STR_LIT:utf-8>' ) <EOL> cls . __unicode__ = __unicode__ <EOL> def _AddSetListenerMethod ( cls ) : <EOL> """<STR_LIT>""" <EOL> def SetListener ( self , listener ) : <EOL> if listener is None : <EOL> self . _listener = message_listener_mod . NullMessageListener ( ) <EOL> else : <EOL> self . _listener = listener <EOL> cls . _SetListener = SetListener <EOL> def _BytesForNonRepeatedElement ( value , field_number , field_type ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fn = type_checkers . TYPE_TO_BYTE_SIZE_FN [ field_type ] <EOL> return fn ( field_number , value ) <EOL> except KeyError : <EOL> raise message_mod . EncodeError ( '<STR_LIT>' % field_type ) <EOL> def _AddByteSizeMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def ByteSize ( self ) : <EOL> if not self . _cached_byte_size_dirty : <EOL> return self . _cached_byte_size <EOL> size = <NUM_LIT:0> <EOL> for field_descriptor , field_value in self . ListFields ( ) : <EOL> size += field_descriptor . _sizer ( field_value ) <EOL> for tag_bytes , value_bytes in self . _unknown_fields : <EOL> size += len ( tag_bytes ) + len ( value_bytes ) <EOL> self . _cached_byte_size = size <EOL> self . _cached_byte_size_dirty = False <EOL> self . _listener_for_children . dirty = False <EOL> return size <EOL> cls . ByteSize = ByteSize <EOL> def _AddSerializeToStringMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def SerializeToString ( self ) : <EOL> errors = [ ] <EOL> if not self . IsInitialized ( ) : <EOL> raise message_mod . EncodeError ( <EOL> '<STR_LIT>' % ( <EOL> self . DESCRIPTOR . full_name , '<STR_LIT:U+002C>' . join ( self . FindInitializationErrors ( ) ) ) ) <EOL> return self . SerializePartialToString ( ) <EOL> cls . SerializeToString = SerializeToString <EOL> def _AddSerializePartialToStringMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def SerializePartialToString ( self ) : <EOL> out = BytesIO ( ) <EOL> self . _InternalSerialize ( out . write ) <EOL> return out . getvalue ( ) <EOL> cls . SerializePartialToString = SerializePartialToString <EOL> def InternalSerialize ( self , write_bytes ) : <EOL> for field_descriptor , field_value in self . ListFields ( ) : <EOL> field_descriptor . _encoder ( write_bytes , field_value ) <EOL> for tag_bytes , value_bytes in self . _unknown_fields : <EOL> write_bytes ( tag_bytes ) <EOL> write_bytes ( value_bytes ) <EOL> cls . _InternalSerialize = InternalSerialize <EOL> def _AddMergeFromStringMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def MergeFromString ( self , serialized ) : <EOL> length = len ( serialized ) <EOL> try : <EOL> if self . _InternalParse ( serialized , <NUM_LIT:0> , length ) != length : <EOL> raise message_mod . DecodeError ( '<STR_LIT>' ) <EOL> except ( IndexError , TypeError ) : <EOL> raise message_mod . DecodeError ( '<STR_LIT>' ) <EOL> except struct . error as e : <EOL> raise message_mod . DecodeError ( e ) <EOL> return length <EOL> cls . MergeFromString = MergeFromString <EOL> local_ReadTag = decoder . ReadTag <EOL> local_SkipField = decoder . SkipField <EOL> decoders_by_tag = cls . _decoders_by_tag <EOL> def InternalParse ( self , buffer , pos , end ) : <EOL> self . _Modified ( ) <EOL> field_dict = self . _fields <EOL> unknown_field_list = self . _unknown_fields <EOL> while pos != end : <EOL> ( tag_bytes , new_pos ) = local_ReadTag ( buffer , pos ) <EOL> field_decoder = decoders_by_tag . get ( tag_bytes ) <EOL> if field_decoder is None : <EOL> value_start_pos = new_pos <EOL> new_pos = local_SkipField ( buffer , new_pos , end , tag_bytes ) <EOL> if new_pos == - <NUM_LIT:1> : <EOL> return pos <EOL> if not unknown_field_list : <EOL> unknown_field_list = self . _unknown_fields = [ ] <EOL> unknown_field_list . append ( ( tag_bytes , buffer [ value_start_pos : new_pos ] ) ) <EOL> pos = new_pos <EOL> else : <EOL> pos = field_decoder ( buffer , new_pos , end , self , field_dict ) <EOL> return pos <EOL> cls . _InternalParse = InternalParse <EOL> def _AddIsInitializedMethod ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> required_fields = [ field for field in message_descriptor . fields <EOL> if field . label == _FieldDescriptor . LABEL_REQUIRED ] <EOL> def IsInitialized ( self , errors = None ) : <EOL> """<STR_LIT>""" <EOL> for field in required_fields : <EOL> if ( field not in self . _fields or <EOL> ( field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE and <EOL> not self . _fields [ field ] . _is_present_in_parent ) ) : <EOL> if errors is not None : <EOL> errors . extend ( self . FindInitializationErrors ( ) ) <EOL> return False <EOL> for field , value in list ( self . _fields . items ( ) ) : <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> if field . label == _FieldDescriptor . LABEL_REPEATED : <EOL> for element in value : <EOL> if not element . IsInitialized ( ) : <EOL> if errors is not None : <EOL> errors . extend ( self . FindInitializationErrors ( ) ) <EOL> return False <EOL> elif value . _is_present_in_parent and not value . IsInitialized ( ) : <EOL> if errors is not None : <EOL> errors . extend ( self . FindInitializationErrors ( ) ) <EOL> return False <EOL> return True <EOL> cls . IsInitialized = IsInitialized <EOL> def FindInitializationErrors ( self ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> for field in required_fields : <EOL> if not self . HasField ( field . name ) : <EOL> errors . append ( field . name ) <EOL> for field , value in self . ListFields ( ) : <EOL> if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> if field . is_extension : <EOL> name = "<STR_LIT>" % field . full_name <EOL> else : <EOL> name = field . name <EOL> if field . label == _FieldDescriptor . LABEL_REPEATED : <EOL> for i in range ( len ( value ) ) : <EOL> element = value [ i ] <EOL> prefix = "<STR_LIT>" % ( name , i ) <EOL> sub_errors = element . FindInitializationErrors ( ) <EOL> errors += [ prefix + error for error in sub_errors ] <EOL> else : <EOL> prefix = name + "<STR_LIT:.>" <EOL> sub_errors = value . FindInitializationErrors ( ) <EOL> errors += [ prefix + error for error in sub_errors ] <EOL> return errors <EOL> cls . FindInitializationErrors = FindInitializationErrors <EOL> def _AddMergeFromMethod ( cls ) : <EOL> LABEL_REPEATED = _FieldDescriptor . LABEL_REPEATED <EOL> CPPTYPE_MESSAGE = _FieldDescriptor . CPPTYPE_MESSAGE <EOL> def MergeFrom ( self , msg ) : <EOL> if not isinstance ( msg , cls ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , type ( msg ) . __name__ ) ) <EOL> assert msg is not self <EOL> self . _Modified ( ) <EOL> fields = self . _fields <EOL> for field , value in list ( msg . _fields . items ( ) ) : <EOL> if field . label == LABEL_REPEATED : <EOL> field_value = fields . get ( field ) <EOL> if field_value is None : <EOL> field_value = field . _default_constructor ( self ) <EOL> fields [ field ] = field_value <EOL> field_value . MergeFrom ( value ) <EOL> elif field . cpp_type == CPPTYPE_MESSAGE : <EOL> if value . _is_present_in_parent : <EOL> field_value = fields . get ( field ) <EOL> if field_value is None : <EOL> field_value = field . _default_constructor ( self ) <EOL> fields [ field ] = field_value <EOL> field_value . MergeFrom ( value ) <EOL> else : <EOL> self . _fields [ field ] = value <EOL> if msg . _unknown_fields : <EOL> if not self . _unknown_fields : <EOL> self . _unknown_fields = [ ] <EOL> self . _unknown_fields . extend ( msg . _unknown_fields ) <EOL> cls . MergeFrom = MergeFrom <EOL> def _AddWhichOneofMethod ( message_descriptor , cls ) : <EOL> def WhichOneof ( self , oneof_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> field = message_descriptor . oneofs_by_name [ oneof_name ] <EOL> except KeyError : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % oneof_name ) <EOL> nested_field = self . _oneofs . get ( field , None ) <EOL> if nested_field is not None and self . HasField ( nested_field . name ) : <EOL> return nested_field . name <EOL> else : <EOL> return None <EOL> cls . WhichOneof = WhichOneof <EOL> def _AddMessageMethods ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> _AddListFieldsMethod ( message_descriptor , cls ) <EOL> _AddHasFieldMethod ( message_descriptor , cls ) <EOL> _AddClearFieldMethod ( message_descriptor , cls ) <EOL> if message_descriptor . is_extendable : <EOL> _AddClearExtensionMethod ( cls ) <EOL> _AddHasExtensionMethod ( cls ) <EOL> _AddClearMethod ( message_descriptor , cls ) <EOL> _AddEqualsMethod ( message_descriptor , cls ) <EOL> _AddStrMethod ( message_descriptor , cls ) <EOL> _AddUnicodeMethod ( message_descriptor , cls ) <EOL> _AddSetListenerMethod ( cls ) <EOL> _AddByteSizeMethod ( message_descriptor , cls ) <EOL> _AddSerializeToStringMethod ( message_descriptor , cls ) <EOL> _AddSerializePartialToStringMethod ( message_descriptor , cls ) <EOL> _AddMergeFromStringMethod ( message_descriptor , cls ) <EOL> _AddIsInitializedMethod ( message_descriptor , cls ) <EOL> _AddMergeFromMethod ( cls ) <EOL> _AddWhichOneofMethod ( message_descriptor , cls ) <EOL> def _AddPrivateHelperMethods ( message_descriptor , cls ) : <EOL> """<STR_LIT>""" <EOL> def Modified ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _cached_byte_size_dirty : <EOL> self . _cached_byte_size_dirty = True <EOL> self . _listener_for_children . dirty = True <EOL> self . _is_present_in_parent = True <EOL> self . _listener . Modified ( ) <EOL> def _UpdateOneofState ( self , field ) : <EOL> """<STR_LIT>""" <EOL> other_field = self . _oneofs . setdefault ( field . containing_oneof , field ) <EOL> if other_field is not field : <EOL> del self . _fields [ other_field ] <EOL> self . _oneofs [ field . containing_oneof ] = field <EOL> cls . _Modified = Modified <EOL> cls . SetInParent = Modified <EOL> cls . _UpdateOneofState = _UpdateOneofState <EOL> class _Listener ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent_message ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( parent_message , weakref . ProxyType ) : <EOL> self . _parent_message_weakref = parent_message <EOL> else : <EOL> self . _parent_message_weakref = weakref . proxy ( parent_message ) <EOL> self . dirty = False <EOL> def Modified ( self ) : <EOL> if self . dirty : <EOL> return <EOL> try : <EOL> self . _parent_message_weakref . _Modified ( ) <EOL> except ReferenceError : <EOL> pass <EOL> class _OneofListener ( _Listener ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent_message , field ) : <EOL> """<STR_LIT>""" <EOL> super ( _OneofListener , self ) . __init__ ( parent_message ) <EOL> self . _field = field <EOL> def Modified ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _parent_message_weakref . _UpdateOneofState ( self . _field ) <EOL> super ( _OneofListener , self ) . Modified ( ) <EOL> except ReferenceError : <EOL> pass <EOL> class _ExtensionDict ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , extended_message ) : <EOL> """<STR_LIT>""" <EOL> self . _extended_message = extended_message <EOL> def __getitem__ ( self , extension_handle ) : <EOL> """<STR_LIT>""" <EOL> _VerifyExtensionHandle ( self . _extended_message , extension_handle ) <EOL> result = self . _extended_message . _fields . get ( extension_handle ) <EOL> if result is not None : <EOL> return result <EOL> if extension_handle . label == _FieldDescriptor . LABEL_REPEATED : <EOL> result = extension_handle . _default_constructor ( self . _extended_message ) <EOL> elif extension_handle . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : <EOL> result = extension_handle . message_type . _concrete_class ( ) <EOL> try : <EOL> result . _SetListener ( self . _extended_message . _listener_for_children ) <EOL> except ReferenceError : <EOL> pass <EOL> else : <EOL> return extension_handle . default_value <EOL> result = self . _extended_message . _fields . setdefault ( <EOL> extension_handle , result ) <EOL> return result <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , self . __class__ ) : <EOL> return False <EOL> my_fields = self . _extended_message . ListFields ( ) <EOL> other_fields = other . _extended_message . ListFields ( ) <EOL> my_fields = [ field for field in my_fields if field . is_extension ] <EOL> other_fields = [ field for field in other_fields if field . is_extension ] <EOL> return my_fields == other_fields <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __hash__ ( self ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __setitem__ ( self , extension_handle , value ) : <EOL> """<STR_LIT>""" <EOL> _VerifyExtensionHandle ( self . _extended_message , extension_handle ) <EOL> if ( extension_handle . label == _FieldDescriptor . LABEL_REPEATED or <EOL> extension_handle . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % extension_handle . full_name ) <EOL> type_checker = type_checkers . GetTypeChecker ( <EOL> extension_handle ) <EOL> self . _extended_message . _fields [ extension_handle ] = ( <EOL> type_checker . CheckValue ( value ) ) <EOL> self . _extended_message . _Modified ( ) <EOL> def _FindExtensionByName ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _extended_message . _extensions_by_name . get ( name , None ) </s>
