3 SÉLjöáã@sŠdZdZddlmZddlZddlZddlZddlZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZejZdZGdd„deƒZdd„Zdd„Zdd„Z dd„Z!dd„Z"dd„Z#d d!„Z$d"d#„Z%d$d%„Z&d&d'„Z'd(d)„Z(d*d+„Z)d,d-„Z*d.d/„Z+d0d1„Z,d2d3„Z-d4d5„Z.d6d7„Z/d8d9„Z0d:d;„Z1dd?„Z3d@dA„Z4dBZ5dCZ6dDdE„Z7dFdG„Z8dHdI„Z9dJdK„Z:dLdM„Z;dNdO„ZdTdU„Z?dVdW„Z@dXdY„ZAdZd[„ZBd\d]„ZCd^d_„ZDd`da„ZEdbdc„ZFddde„ZGdfdg„ZHdhdi„ZIdjdk„ZJdldm„ZKdndo„ZLdpdq„ZMGdrds„dseNƒZOGdtdu„dueOƒZPGdvdw„dweNƒZQdS)xa Contains a metaclass and helper functions used to create protocol message classes from Descriptor objects at runtime. Recall that a metaclass is the "type" of a class. (A class is to a metaclass what an instance is to a class.) In this case, we use the GeneratedProtocolMessageType metaclass to inject all the useful functionality into the classes output by the protocol compiler at compile-time. The upshot of all this is that the real implementation details for ALL pure-Python protocol buffers are *here in this file*. z#robinson@google.com (Will Robinson)é)ÚBytesION)Úapi_implementation)Ú containers)Údecoder)Úencoder)Úenum_type_wrapper)Úmessage_listener)Ú type_checkers)Úwell_known_types)Ú wire_format)Ú descriptor)Úmessage)Ú text_formatzgoogle.protobuf.Anycs0eZdZdZdZ‡fdd„Z‡fdd„Z‡ZS)ÚGeneratedProtocolMessageTypeaQMetaclass for protocol message classes created at runtime from Descriptors. We add implementations for all methods described in the Message class. We also create properties to allow getting/setting all fields in the protocol message. Finally, we create slots to prevent users from accidentally "setting" nonexistent fields in the protocol message, which then wouldn't get serialized / deserialized properly. The protocol compiler currently uses this metaclass to create protocol message classes at runtime. Clients can also manually create their own classes at runtime, as in this example: mydescriptor = Descriptor(.....) factory = symbol_database.Default() factory.pool.AddDescriptor(mydescriptor) MyProtoClass = factory.GetPrototype(mydescriptor) myproto_instance = MyProtoClass() myproto.foo_field = 23 ... Ú DESCRIPTORcsZ|tj}|jtjkr(|tj|jf7}t||ƒt||ƒtt|ƒ}|j||||ƒ}|S)aCustom allocation for runtime-generated class types. We override __new__ because this is apparently the only place where we can meaningfully set __slots__ on the class we're creating(?). (The interplay between metaclasses and slots is not very well-documented). Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. Returns: Newly-allocated class. ) rÚ_DESCRIPTOR_KEYÚ full_namer ZWKTBASESÚ&_AddClassAttributesForNestedExtensionsÚ _AddSlotsÚsuperÚ__new__)ÚclsÚnameÚbasesÚ dictionaryr Ú superclassÚ new_class)Ú __class__©ú$/usr/lib/python3.6/python_message.pyris     z$GeneratedProtocolMessageType.__new__cs¶|tj}i|_|jr6|jƒjr6tj|ƒdf|jtj<x|j D]}t ||ƒq>W||_ t ||ƒt ||ƒt||ƒt||ƒt|ƒt||ƒt||ƒtt|ƒ}|j|||ƒdS)a Here we perform the majority of our work on the class. We add enum getters, an __init__ method, implementations of all Message methods, and properties for all fields in the protocol type. Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. N)rrÚ_decoders_by_tagÚ has_optionsÚ GetOptionsÚmessage_set_wire_formatrZMessageSetItemDecoderZMESSAGE_SET_ITEM_TAGÚfieldsÚ_AttachFieldHelpersÚ_concrete_classÚ_AddEnumValuesÚ_AddInitMethodÚ_AddPropertiesForFieldsÚ_AddPropertiesForExtensionsÚ_AddStaticMethodsÚ_AddMessageMethodsÚ_AddPrivateHelperMethodsrÚ__init__)rrrrr Úfieldr)rrrr.ˆs"          z%GeneratedProtocolMessageType.__init__)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr.Ú __classcell__rr)rrrNs rcCs|S)aReturns the name of the public property attribute which clients can use to get and (in some cases) set the value of a protocol message field. Args: proto_field_name: The protocol message field name, exactly as it appears (or would appear) in a .proto file. r)Úproto_field_namerrrÚ _PropertyName¹sr6cCsjt|tƒstd|ƒ‚|js*td|jƒ‚|js>td|jƒ‚|j|jk rftd|j|jj|jjfƒ‚dS)z0Verify that the given extension handle is valid.z3HasExtension() expects an extension handle, got: %sz"%s" is not an extension.z""%s" is missing a containing_type.zKExtension "%s" extends message type "%s", but this message is of type "%s".N)Ú isinstanceÚ_FieldDescriptorÚKeyErrorÚ is_extensionrÚcontaining_typer)r Úextension_handlerrrÚ_VerifyExtensionHandleÖs   r=c Csddddddddd g |d <d S) a Adds a __slots__ entry to dictionary, containing the names of all valid attributes for this message type. Args: message_descriptor: A Descriptor instance describing this message type. dictionary: Class dictionary to which we'll add a '__slots__' entry. Ú_cached_byte_sizeÚ_cached_byte_size_dirtyÚ_fieldsÚ_unknown_fieldsÚ_is_present_in_parentÚ _listenerÚ_listener_for_childrenÚ __weakref__Ú_oneofsÚ __slots__Nr)Úmessage_descriptorrrrrrìsrcCs2|jo0|jjo0|jjƒjo0|jtjko0|jtj kS)N) r:r;r!r"r#Útyper8Ú TYPE_MESSAGEÚlabelZLABEL_OPTIONAL)r/rrrÚ_IsMessageSetExtensionÿs   rLcCs |jtjko|jjo|jjƒjS)N)rIr8rJÚ message_typer!r"Ú map_entry)r/rrrÚ _IsMapFields rOcCs|jjd}|jtjkS)NÚvalue)rMÚfields_by_nameÚcpp_typer8ÚCPPTYPE_MESSAGE)r/Z value_typerrrÚ_IsMessageMapField s rTcs@ˆjtjk‰ˆotjˆjƒ}|s&d}nFˆjjdkrDˆjo@ˆj ƒj }n(ˆjodˆj ƒj dƒodˆj ƒj dk}| }t ˆƒ‰ˆr”t jˆƒ}t jˆtˆƒƒ}nNtˆƒr¶t jˆjƒ}t jˆjƒ}n,tjˆjˆjˆ|ƒ}tjˆjˆjˆ|ƒ}|ˆ_|ˆ_tˆƒˆ_‡‡‡‡fdd„}|tjˆjdƒˆ�r.AddDecoderT)rKr8ÚLABEL_REPEATEDr ZIsTypePackablerIr;Úsyntaxr!r"rUÚHasFieldrOrZ MapEncoderZMapSizerrTrLZMessageSetItemEncoderrVZMessageSetItemSizerr ZTYPE_TO_ENCODERZ TYPE_TO_SIZERÚ_encoderÚ_sizerÚ _DefaultValueConstructorForFieldrYZFIELD_TYPE_TO_WIRE_TYPEZWIRETYPE_LENGTH_DELIMITED)rr]Z is_packablerZZhas_packed_falseZ field_encoderZsizerr`r)rr]r^r_rr%sB            r%cCs(|j}x|jƒD]\}}|||<qWdS)N)Úextensions_by_nameÚitems)r rÚextension_dictÚextension_nameÚextension_fieldrrrrUsrcCsHxB|jD]8}t||jtj|ƒƒx|jD]}t||j|jƒq(WqWdS)aSets class-level attributes for all enum fields defined in this message. Also exporting a class-level object that can name enum values. Args: descriptor: Descriptor object for this message type. cls: Class we're constructing for this message type. N)Z enum_typesÚsetattrrrZEnumTypeWrapperÚvaluesrV)r rÚ enum_typeZ enum_valuerrrr'\s  r'csvˆjtjkrtdˆjƒ‚ˆjj}tj|dƒ‰|d‰t ˆƒrT‡‡‡fdd„}|Stjˆƒ‰‡‡‡fdd„}|SdS)Nz&map_entry set on non-repeated field %sÚkeyrPcstj|jˆjˆˆjƒS)N)rZ MessageMaprDrM)r )r/Ú key_checkerÚ value_fieldrrÚMakeMessageMapDefaultts z:_GetInitializeDefaultForMap..MakeMessageMapDefaultcstj|jˆˆˆjƒS)N)rZ ScalarMaprDrM)r )r/rpÚ value_checkerrrÚMakePrimitiveMapDefault{sz<_GetInitializeDefaultForMap..MakePrimitiveMapDefault) rKr8raÚ ValueErrorrrMrQr ÚGetTypeCheckerrT)r/rQrrrtr)r/rprsrqrrXks   rXcsªtˆƒrtˆƒSˆjtjkrvˆjr:ˆjgkr:tdˆjƒ‚ˆjtj kr\ˆj ‰‡fdd„}|St j ˆƒ‰‡fdd„}|Sˆjtj kršˆj ‰‡‡fdd„}|S‡fdd „}|S) a‡Returns a function which returns a default value for a field. Args: field: FieldDescriptor object for this field. The returned function has one argument: message: Message instance containing this field, or a weakref proxy of same. That function in turn returns a default value for this field. The default value may refer back to |message| via a weak reference. z/Repeated field default value not empty list: %scstj|jˆjƒS)N)rZRepeatedCompositeFieldContainerrDrM)r )r/rrÚMakeRepeatedMessageDefaultšszD_DefaultValueConstructorForField..MakeRepeatedMessageDefaultcstj|jˆƒS)N)rZRepeatedScalarFieldContainerrD)r )Ú type_checkerrrÚMakeRepeatedScalarDefault szC_DefaultValueConstructorForField..MakeRepeatedScalarDefaultcs,ˆjƒ}|jˆjdk r t|ˆƒn|jƒ|S)N)r&Ú _SetListenerrWÚ_OneofListenerrD)r Úresult)r/rMrrÚMakeSubMessageDefault¨s z?_DefaultValueConstructorForField..MakeSubMessageDefaultcsˆjS)N)Ú default_value)r )r/rrÚMakeScalarDefault±sz;_DefaultValueConstructorForField..MakeScalarDefault) rOrXrKr8raZhas_default_valuer~rurRrSrMr rv)r/rwryr}rr)r/rMrxrrf�s&        rfcCsZtjƒd}t|jƒdkrtd|j|fƒ‚YnX|S)aConvert a string or integer enum value to an integer. If the value is a string, it is converted to the enum value in enum_type with the same name. If the value is not a string, it's returned as-is. (No conversion or bounds-checking is done.) z Enum type %s: unknown label "%s")r7rˆZ string_typesZvalues_by_namerVr9rur)rnrPrrrÚ_GetIntegerEnumValueÆs  z,_AddInitMethod.._GetIntegerEnumValuec s"d|_t|ƒdk|_i|_i|_f|_d|_tjƒ|_ t |ƒ|_ �xÚ|j ƒD�]Ì\}}t ˆ|ƒ‰ˆdkrztdˆj|fƒ‚|dkr„qLˆjtjk�rXˆj|ƒ}ˆjtjk�r tˆƒrètˆƒrÜx*|D]}||j||ƒqÀWn |j|ƒn6xb|D],}t|tƒ�r |jf|Žqî|jƒj|ƒqîWn,ˆjtjk�rB‡‡fdd„|Dƒ}|j|ƒ||jˆ<qLˆjtjk�r̈j|ƒ}|}t|tƒ�rŽˆjj f|Ž}y|j|ƒWn"tk �r¾t!ˆj|ƒYnX||jˆ<qLˆjtjk�r戈j"|ƒ}yt#|||ƒWqLtk �rt!ˆj|ƒYqLXqLWdS)NrFz,%s() got an unexpected keyword argument '%s'csg|]}ˆˆj|ƒ‘qSr)rn)Ú.0Úval)rŒr/rrú üsz0_AddInitMethod..init..)$r>r„r?r@rFrArBÚmessage_listener_modÚNullMessageListenerrCÚ _ListenerrDrhÚ_GetFieldByNamer†rrKr8rarYrRrSrOrTÚ MergeFromÚupdater7ÚdictÚaddZ CPPTYPE_ENUMÚextendrMr&r‹rnrl)ÚselfÚkwargsr‰Ú field_valueÚcopyrorŽZnew_val)rŒrH)r/rÚinitÕs`               z_AddInitMethod..initN)r1r3r.)rHrr�r)rŒrHrr(Ãs =r(c Cs6y |j|Stk r0td|j|fƒ‚YnXdS)zóReturns a field descriptor by field name. Args: message_descriptor: A Descriptor describing all fields in message. field_name: The name of the field to retrieve. Returns: The field descriptor associated with the field name. z&Protocol message %s has no "%s" field.N)rQr9rur)rHr‰rrrr“s  r“cCs2x|jD]}t||ƒqW|jr.tdd„ƒ|_dS)z=Adds properties for all fields in this protocol message type.cSst|ƒS)N)Ú_ExtensionDict)r™rrrÚ/sz)_AddPropertiesForFields..N)r$Ú_AddPropertiesForFieldÚ is_extendableÚpropertyZ Extensions)r rr/rrrr)'s r)cCsZ|jjƒd}t|||jƒ|jtjkr4t||ƒn"|jtj krLt ||ƒn t ||ƒdS)a#Adds a public property for a protocol message field. Clients can use this property to get and (in the case of non-repeated scalar fields) directly set the value of a protocol message field. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. Ú _FIELD_NUMBERN) rÚupperrlrVrKr8raÚ_AddPropertiesForRepeatedFieldrRrSÚ*_AddPropertiesForNonRepeatedCompositeFieldÚ'_AddPropertiesForNonRepeatedScalarField)r/rÚ constant_namerrrr 2s    r csXˆj‰tˆƒ}‡fdd„}d|_dˆ|_‡fdd„}dˆ}t||t|||d�ƒdS) aAdds a public property for a "repeated" protocol message field. Clients can use this property to get the value of the field, which will be either a _RepeatedScalarFieldContainer or _RepeatedCompositeFieldContainer (see below). Note that when clients add values to these containers, we perform type-checking in the case of repeated scalar fields, and we also set any necessary "has" bits as a side-effect. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. cs0|jjˆƒ}|dkr,ˆj|ƒ}|jjˆ|ƒ}|S)N)r@ÚgetrYÚ setdefault)r™r›)r/rrÚgetter\s   z._AddPropertiesForRepeatedField..getterNzGetter for %s.cstdˆƒ‚dS)NzIAssignment not allowed to repeated field "%s" in protocol message object.)ÚAttributeError)r™Ú new_value)r5rrÚsetterosz._AddPropertiesForRepeatedField..setterz/Magic attribute generated for "%s" proto field.)Údoc)rr6r1r3rlr¢)r/rÚ property_namer«r®r¯r)r/r5rr¥Ks   r¥c s¶ˆj}t|ƒ}tjˆƒ‰ˆj‰tƒ}ˆjjdk}‡‡fdd„}d|_d||_ |oXˆj ‰‡‡‡fdd„‰ˆj r€‡‡fdd „}nˆ}d|_d ||_ d |}t ||t |||d �ƒdS) a Adds a public property for a nonrepeated, scalar protocol message field. Clients can use this property to get and directly set the value of the field. Note that when the client sets the value of a field by using this property, all necessary "has" bits are set as a side-effect, and we also perform type-checking. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. Úproto3cs|jjˆˆƒS)N)r@r©)r™)r~r/rrr«‰sz7_AddPropertiesForNonRepeatedScalarField..getterNzGetter for %s.cs@ˆj|ƒ}ˆr$| r$|jjˆdƒn ||jˆ<|js<|jƒdS)N)Ú CheckValuer@Úpopr?Ú _Modified)r™r­)Úclear_when_set_to_defaultr/rxrrÚ field_setter’s    z=_AddPropertiesForNonRepeatedScalarField..field_settercsˆ||ƒ|jˆƒdS)N)Ú_UpdateOneofState)r™r­)r/r¶rrr®¡s z7_AddPropertiesForNonRepeatedScalarField..setterzSetter for %s.z/Magic attribute generated for "%s" proto field.)r¯) rr6r rvr~Úsetr;rbr1r3rWrlr¢) r/rr5r°Z valid_valuesÚ is_proto3r«r®r¯r)rµr~r/r¶rxrr§ws$      r§csXˆj‰tˆƒ}‡fdd„}d|_dˆ|_‡fdd„}dˆ}t||t|||d�ƒdS) aNAdds a public property for a nonrepeated, composite protocol message field. A composite field is a "group" or "message" field. Clients can use this property to get the value of the field, but cannot assign to the property directly. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. cs0|jjˆƒ}|dkr,ˆj|ƒ}|jjˆ|ƒ}|S)N)r@r©rYrª)r™r›)r/rrr«¿s   z:_AddPropertiesForNonRepeatedCompositeField..getterNzGetter for %s.cstdˆƒ‚dS)NzJAssignment not allowed to composite field "%s" in protocol message object.)r¬)r™r­)r5rrr®Òsz:_AddPropertiesForNonRepeatedCompositeField..setterz/Magic attribute generated for "%s" proto field.)r¯)rr6r1r3rlr¢)r/rr°r«r®r¯r)r/r5rr¦¯s    r¦cCsd|j}x.|jƒD]"\}}|jƒd}t|||jƒqW|jdk r`|jj}|j||_|j||_dS)z=Adds properties for all fields in this protocol message type.r£N) rgrhr¤rlrVÚfileÚpoolÚ_extensions_by_numberÚ_extensions_by_name)r rrirjrkr¨r»rrrr*Ûs   r*cs0‡fdd„}t|ƒˆ_‡fdd„}t|ƒˆ_dS)Ncs&ˆj|_ˆjjjj|ƒtˆ|ƒdS)N)rr;rºr»ZAddExtensionDescriptorr%)r<)rrrÚRegisterExtensionìsz,_AddStaticMethods..RegisterExtensioncsˆƒ}|j|ƒ|S)N)ÚMergeFromString)Úsr )rrrÚ FromStringós z%_AddStaticMethods..FromString)Ú staticmethodr¾rÁ)rr¾rÁr)rrr+ês   r+cCs>|djtjkrt|dƒS|djtjkr6|djSdSdS)zˆGiven a (FieldDescriptor, value) tuple from _fields, return true if the value should be included in the list returned by ListFields().rr€TN)rKr8raÚboolrRrSrB)ÚitemrrrÚ _IsPresentús   rÅcCsdd„}||_dS)z Helper for _AddMessageMethods().cSs(dd„|jjƒDƒ}|jdd„d�|S)NcSsg|]}t|ƒr|‘qSr)rÅ)r�rÄrrrr� sz<_AddListFieldsMethod..ListFields..cSs |djS)Nr)rV)rÄrrrrŸ sz:_AddListFieldsMethod..ListFields..)ro)r@rhÚsort)r™Z all_fieldsrrrÚ ListFields sz(_AddListFieldsMethod..ListFieldsN)rÇ)rHrrÇrrrÚ_AddListFieldsMethodsrÈz:Protocol message has no non-repeated submessage field "%s"z/Protocol message has no non-repeated field "%s"cs”|jdk}|rtnt‰i‰x@|jD]6}|jtjkr4q"|rN|jtjkrN|j rNq"|ˆ|j <q"W|szx|j D]}|ˆ|j <qhW‡‡‡fdd„‰ˆ|_ dS)z Helper for _AddMessageMethods().r±cs˜y ˆ|}Wn tk r,tˆ|ƒ‚YnXt|tjƒrdyˆ||j|jƒStk r`dSXn0|jtj krŠ|j j |ƒ}|dk oˆ|j S||j kSdS)NF) r9rur7Údescriptor_modZOneofDescriptorrFrrRr8rSr@r©rB)r™r‰r/rP)rcÚ error_msgÚhassable_fieldsrrrc(s    z$_AddHasFieldMethod..HasFieldN) rbÚ_Proto3HasErrorÚ_Proto2HasErrorr$rKr8rarRrSrWrZoneofsrc)rHrr¹r/Zoneofr)rcrÊrËrÚ_AddHasFieldMethods     rÎcs‡fdd„}||_dS)z Helper for _AddMessageMethods().csÎyˆj|}Wndtk rry(ˆj|}||jkr@|j|}ndSWn&tk rltdˆj|fƒ‚YnXYnX||jkrÂt|j|dƒrœ|j|jƒ|j|=|jj |j dƒ|krÂ|j|j =|j ƒdS)Nz(Protocol message %s() has no "%s" field.ÚInvalidateIterators) rQr9Úoneofs_by_namerFrurr@ÚhasattrrÏr©rWr´)r™r‰r/)rHrrÚ ClearField?s$     z(_AddClearFieldMethod..ClearFieldN)rÒ)rHrrÒr)rHrÚ_AddClearFieldMethod=s !rÓcCsdd„}||_dS)z Helper for _AddMessageMethods().cSs(t||ƒ||jkr|j|=|jƒdS)N)r=r@r´)r™r<rrrÚClearExtensiones  z0_AddClearExtensionMethod..ClearExtensionN)rÔ)rrÔrrrÚ_AddClearExtensionMethodcsrÕcCsdd„}||_dS)z Helper for _AddMessageMethods().cSsXt||ƒ|jtjkr$td|jƒ‚|jtjkrJ|jj |ƒ}|dk oH|j S||jkSdS)Nz"%s" is repeated.) r=rKr8rar9rrRrSr@r©rB)r™r<rPrrrÚ HasExtensionqs    z,_AddHasExtensionMethod..HasExtensionN)rÖ)rrÖrrrÚ_AddHasExtensionMethodos r×cCshddlm}|jƒ}|j}|s"dS|jdƒd}|jj|ƒ}|dkrHdS|j|ƒ}|ƒ}|j|j ƒ|S)atUnpacks Any message and returns the unpacked message. This internal method is different from public Any Unpack method which takes the target message as argument. _InternalUnpackAny method does not have target message type and need to find the message type in descriptor pool. Args: msg: An Any message to be unpacked. Returns: The unpacked message. r)Úsymbol_databaseNú/r€éÿÿÿÿ) Úgoogle.protobufrØZDefaultÚtype_urlÚsplitr»ZFindMessageTypeByNameZ GetPrototypeZParseFromStringrP)ÚmsgrØÚfactoryrÜZ type_namer Z message_classr rrrÚ_InternalUnpackAny}s    ràcCsdd„}||_dS)z Helper for _AddMessageMethods().cSs–t|tjƒ s|j|jkrdS||kr*dS|jjtkrVt|ƒ}t|ƒ}|rV|rV||kS|jƒ|jƒksjdSt|j ƒ}|j ƒt|j ƒ}|j ƒ||kS)NFT) r7Ú message_modZMessagerrÚ_AnyFullTypeNameràrÇÚlistrArÆ)r™ÚotherZany_aZany_bZunknown_fieldsZother_unknown_fieldsrrrÚ__eq__§s"    z _AddEqualsMethod..__eq__N)rå)rHrrårrrÚ_AddEqualsMethod¥sræcCsdd„}||_dS)z Helper for _AddMessageMethods().cSs tj|ƒS)N)rÚMessageToString)r™rrrÚ__str__Åsz_AddStrMethod..__str__N)rè)rHrrèrrrÚ _AddStrMethodÃsrécCsdd„}||_dS)z Helper for _AddMessageMethods().cSs tj|ƒS)N)rrç)r™rrrÚ__repr__Ìsz _AddReprMethod..__repr__N)rê)rHrrêrrrÚ_AddReprMethodÊsrëcCsdd„}||_dS)z Helper for _AddMessageMethods().cSstj|dd�jdƒS)NT)Zas_utf8zutf-8)rrçÚdecode)r™rrrÚ __unicode__Ôsz&_AddUnicodeMethod..__unicode__N)rí)Zunused_message_descriptorrrírrrÚ_AddUnicodeMethodÑsrîc Cs<ytj|}|||ƒStk r6tjd|ƒ‚YnXdS)a Returns the number of bytes needed to serialize a non-repeated element. The returned byte count includes space for tag information and any other additional space associated with serializing value. Args: value: Value we're serializing. field_number: Field number of this value. (Since the field number is stored as part of a varint-encoded tag, this has an impact on the total bytes required to serialize the value). field_type: The type of the field. One of the TYPE_* constants within FieldDescriptor. zUnrecognized field type: %dN)r ZTYPE_TO_BYTE_SIZE_FNr9ráÚ EncodeError)rPZ field_numberZ field_typeÚfnrrrÚ_BytesForNonRepeatedElementÙs   rñcCsdd„}||_dS)z Helper for _AddMessageMethods().cSs®|js |jSd}|j}|jƒjrJ|jdj|jƒ}||jdj|jƒ7}nLx"|j ƒD]\}}||j|ƒ7}qTWx&|j D]\}}|t |ƒt |ƒ7}qvW||_d|_d|j _ |S)NrrorPF)r?r>rr"rNrQrerorPrÇrAr„rDÚdirty)r™Úsizer r]r›r[Ú value_bytesrrrÚByteSizeðs z$_AddByteSizeMethod..ByteSizeN)rõ)rHrrõrrrÚ_AddByteSizeMethodísröcCsdd„}||_dS)z Helper for _AddMessageMethods().c[s8g}|jƒs,tjd|jjdj|jƒƒfƒ‚|jf|ŽS)Nz)Message %s is missing required fields: %sú,)Ú IsInitializedrárïrrÚjoinÚFindInitializationErrorsÚSerializePartialToString)r™ršÚerrorsrrrÚSerializeToString s z6_AddSerializeToStringMethod..SerializeToStringN)rý)rHrrýrrrÚ_AddSerializeToStringMethodsrþcCs"dd„}||_ddd„}||_dS)z Helper for _AddMessageMethods().c[stƒ}|j|jf|Ž|jƒS)N)rÚ_InternalSerializeÚwriteÚgetvalue)r™ršÚoutrrrrûszD_AddSerializePartialToStringMethod..SerializePartialToStringNcSs¤|dkrtjƒ}nt|ƒ}|j}|jƒjrX|jdj||j|ƒ|jdj||j |ƒnHx"|j ƒD]\}}|j|||ƒqbWx"|j D]\}}||ƒ||ƒq„WdS)NrorP) rZ)IsPythonDefaultSerializationDeterministicrÃrr"rNrQrdrorPrÇrA)r™Z write_bytesZ deterministicr r]r›r[rôrrrÚInternalSerializes     z=_AddSerializePartialToStringMethod..InternalSerialize)N)rûrÿ)rHrrûrrrrÚ"_AddSerializePartialToStringMethods rcsFdd„}||_tj‰tj‰|j‰|jdk‰‡‡‡‡fdd„}||_dS)z Helper for _AddMessageMethods().cSs|t|ƒ}y |j|d|ƒ|kr&tjdƒ‚WnNttfk rJtjdƒ‚Yn.tjk rv}ztj|ƒ‚WYdd}~XnX|S)NrzUnexpected end-group tag.zTruncated message.)r„Ú_InternalParseráZ DecodeErrorÚ IndexErrorr†ÚstructÚerror)r™Z serializedZlengthÚerrrr¿8sz2_AddMergeFromStringMethod..MergeFromStringr±c sÀ|jƒ|j}|j}x¦||krºˆ||ƒ\}}ˆj|dƒ\}} |dkrš|} ˆ||||ƒ}|dkrb|Sˆ sptjƒr”|s~g}|_|j||| |…fƒ|}q||||||ƒ}| r|j| ƒqW|S)Nr€)NNrÚ)r´r@rAr©rZ&GetPythonProto3PreserveUnknownsDefaultÚappendr·) r™ÚbufferÚposÚendZ field_dictZunknown_field_listr[Znew_posr\Z field_descZvalue_start_pos)Údecoders_by_tagr¹Ú local_ReadTagÚlocal_SkipFieldrrÚ InternalParseLs,  z0_AddMergeFromStringMethod..InternalParseN)r¿rZReadTagZ SkipFieldr rbr)rHrr¿rr)rr¹rrrÚ_AddMergeFromStringMethod6s  rcs:dd„|jDƒ‰d‡fdd„ }||_‡fdd„}||_dS) z[Adds the IsInitialized and FindInitializationError methods to the protocol message class.cSsg|]}|jtjkr|‘qSr)rKr8ZLABEL_REQUIRED)r�r/rrrr�ksz+_AddIsInitializedMethod..NcsöxJˆD]B}||jks.|jtjkr|j|j r|dk rD|j|jƒƒdSqWx¤t|jjƒƒD]’\}}|jtjkr\|j tj krÄ|j j r’|j j ƒjr’q\xZ|D]&}|jƒs˜|dk rº|j|jƒƒdSq˜Wq\|jr\|jƒ r\|dk rê|j|jƒƒdSq\WdS)aChecks if all required fields of a message are set. Args: errors: A list which, if provided, will be populated with the field paths of all missing required fields. Returns: True iff the specified message has all required fields set. NFT)r@rRr8rSrBr˜rúrãrhrKrarMr!r"rNrø)r™rür/rPÚelement)Úrequired_fieldsrrrøns.        z._AddIsInitializedMethod..IsInitializedc s>g}x$ˆD]}|j|jƒs |j|jƒq W�x |jƒD�]þ\}}|jtjkr6|jr^d|j}n|j}t |ƒr¸t |ƒr¶x@|D]6}||}d||f‰|j ƒ}|‡fdd„|Dƒ7}qzWnq6|j tj k�rxntt|ƒƒD]6}||}d||f‰|j ƒ}|‡fdd„|Dƒ7}qÔWq6|d‰|j ƒ}|‡fdd„|Dƒ7}q6W|S) zÆFinds required fields which are not initialized. Returns: A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz". z(%s)z%s[%s].csg|] }ˆ|‘qSrr)r�r)Úprefixrrr�²szM_AddIsInitializedMethod..FindInitializationErrors..z%s[%d].csg|] }ˆ|‘qSrr)r�r)rrrr�»sÚ.csg|] }ˆ|‘qSrr)r�r)rrrr�¿s)rcrr rÇrRr8rSr:rrOrTrúrKraÚranger„) r™rür/rPrrorZ sub_errorsÚi)r)rrrú—s6       z9_AddIsInitializedMethod..FindInitializationErrors)N)r$rørú)rHrrørúr)rrÚ_AddIsInitializedMethodgs ' ,rcs&tj‰tj‰‡‡‡fdd„}|ˆ_dS)Ncsþt|ˆƒs tdˆj|jjfƒ‚|jƒ|j}xª|jjƒD]œ\}}|jˆkr||j|ƒ}|dkrp|j |ƒ}|||<|j |ƒq:|j ˆkr¼|j rÖ|j|ƒ}|dkr°|j |ƒ}|||<|j |ƒq:||j|<|j r:|j|ƒq:W|jrú|jsìg|_|jj|jƒdS)NzLParameter to MergeFrom() must be instance of same class: expected %s got %s.)r7r†r0rr´r@rhrKr©rYr”rRrBrWr·rAr˜)r™rÞr$r/rPr›)rSrarrrr”Ês6          z&_AddMergeFromMethod..MergeFrom)r8rarSr”)rr”r)rSrarrÚ_AddMergeFromMethodÆs%rcs‡fdd„}||_dS)Nc s`yˆj|}Wn tk r.td|ƒ‚YnX|jj|dƒ}|dk rX|j|jƒrX|jSdSdS)zDReturns the name of the currently set field inside a oneof, or None.z)Protocol message has no oneof "%s" field.N)rÐr9rurFr©rcr)r™Z oneof_namer/Z nested_field)rHrrÚ WhichOneofósz(_AddWhichOneofMethod..WhichOneof)r)rHrrr)rHrÚ_AddWhichOneofMethodòs rcCsdd„}||_dS)NcSst|ƒf|jƒfS)N)rIÚ __getstate__)r™rrrÚ __reduce__sz$_AddReduceMethod..__reduce__)r)rrrrrÚ_AddReduceMethodsrcCsi|_f|_i|_|jƒdS)N)r@rArFr´)r™rrrÚ_Clear sr cCsXg|_xL|jƒD]@\}}|jtjkr|jtjkrHx|D] }|jƒq6Wq|jƒqWdS)N)rArÇrRr8rSrKraÚDiscardUnknownFields)r™r/rPZ sub_messagerrrÚ_DiscardUnknownFieldss   r"cCs|dkrtjƒ|_n||_dS)N)r�r‘rC)r™Zlistenerrrrrzs rzcCs¾t||ƒt||ƒt||ƒ|jr4t|ƒt|ƒt||ƒt||ƒt||ƒt ||ƒt ||ƒt ||ƒt ||ƒt ||ƒt||ƒt|ƒt||ƒt|ƒt|_t|_t|_dS)z3Adds implementations of all Message methods to cls.N)rÈrÎrÓr¡rÕr×rærérërîrörþrrrrrrr ZClearr"r!rz)rHrrrrr,$s*             r,cCs&dd„}dd„}||_||_||_dS)z5Adds implementation of private helper methods to cls.cSs(|js$d|_d|j_d|_|jjƒdS)zwSets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change. TN)r?rDròrBrCÚModified)r™rrrr#As z*_AddPrivateHelperMethods..ModifiedcSs0|jj|j|ƒ}||k r,|j|=||j|j<dS)zÈSets field as the active field in its containing oneof. Will also delete currently active field in the oneof, if it is different from the argument. Does not mark the message as modified. N)rFrªrWr@)r™r/Z other_fieldrrrr·Psz3_AddPrivateHelperMethods.._UpdateOneofStateN)r´Z SetInParentr·)rHrr#r·rrrr->s  r-c@s eZdZdZdd„Zdd„ZdS)r’a0MessageListener implementation that a parent message registers with its child message. In order to support semantics like: foo.bar.baz.qux = 23 assert foo.HasField('bar') ...child objects must have back references to their parents. This helper class is at the heart of this support. cCs*t|tjƒr||_n tj|ƒ|_d|_dS)z�Args: parent_message: The message whose _Modified() method we should call when we receive Modified() messages. FN)r7ÚweakrefÚ ProxyTypeÚ_parent_message_weakrefÚproxyrò)r™Úparent_messagerrrr.ns  z_Listener.__init__c Cs2|jr dSy|jjƒWntk r,YnXdS)N)ròr&r´ÚReferenceError)r™rrrr#�s z_Listener.ModifiedN)r0r1r2r3r.r#rrrrr’`s r’cs,eZdZdZ‡fdd„Z‡fdd„Z‡ZS)r{zCSpecial listener implementation for setting composite oneof fields.cstt|ƒj|ƒ||_dS)zËArgs: parent_message: The message whose _Modified() method we should call when we receive Modified() messages. field: The descriptor of the field being set in the parent message. N)rr{r.Ú_field)r™r(r/)rrrr.‘sz_OneofListener.__init__c s:y |jj|jƒtt|ƒjƒWntk r4YnXdS)zEAlso updates the state of the containing oneof in the parent message.N)r&r·r*rr{r#r))r™)rrrr#šs z_OneofListener.Modified)r0r1r2r3r.r#r4rr)rrr{Žs r{c@sPeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dS)ržz«Dict-like container for supporting an indexable "Extensions" field on proto instances. Note that in all cases we expect extension handles to be FieldDescriptors. cCs ||_dS)zMextended_message: Message instance for which we are the Extensions dict. N)Ú_extended_message)r™Zextended_messagerrrr.°sz_ExtensionDict.__init__c Csšt|j|ƒ|jjj|ƒ}|dk r&|S|jtjkr@|j|jƒ}nF|jtj kr€|j j ƒ}y|j |jj ƒWq†tk r|Yq†Xn|jS|jjj||ƒ}|S)z8Returns the current value of the given extension handle.N)r=r+r@r©rKr8rarYrRrSrMr&rzrDr)r~rª)r™r<r|rrrÚ __getitem__¶s     z_ExtensionDict.__getitem__cCsHt||jƒsdS|jjƒ}|jjƒ}dd„|Dƒ}dd„|Dƒ}||kS)NFcSsg|]}|jr|‘qSr)r:)r�r/rrrr�ßsz)_ExtensionDict.__eq__..cSsg|]}|jr|‘qSr)r:)r�r/rrrr�às)r7rr+rÇ)r™räZ my_fieldsZ other_fieldsrrrrå×s   z_ExtensionDict.__eq__cCs ||k S)Nr)r™rärrrÚ__ne__äsz_ExtensionDict.__ne__cCs tdƒ‚dS)Nzunhashable object)r†)r™rrrÚ__hash__çsz_ExtensionDict.__hash__cCs\t|j|ƒ|jtjks$|jtjkr2td|jƒ‚t j |ƒ}|j |ƒ|jj |<|jj ƒdS)zlIf extension_handle specifies a non-repeated, scalar extension field, sets the value of that field. zKCannot assign to extension "%s" because it is a repeated or composite type.N)r=r+rKr8rarRrSr†rr rvr²r@r´)r™r<rPrxrrrÚ __setitem__îs     z_ExtensionDict.__setitem__cCs|jjj|dƒS)z™Tries to find a known extension with the specified name. Args: name: Extension full name. Returns: Extension field descriptor. N)r+r½r©)r™rrrrÚ_FindExtensionByNames z#_ExtensionDict._FindExtensionByNamecCs|jjj|dƒS)zœTries to find a known extension with the field number. Args: number: Extension field number. Returns: Extension field descriptor. N)r+r¼r©)r™rVrrrÚ_FindExtensionByNumbers z%_ExtensionDict._FindExtensionByNumberN) r0r1r2r3r.r,rår-r.r/r0r1rrrrrž§s!  rž)Rr3Ú __author__Úiorrr‚r$rˆZgoogle.protobuf.internalrrrrrrr�r r r rÛr rÉr rárZFieldDescriptorr8rârIrr6r=rrLrOrTr%rr'rXrfr‹r(r“r)r r¥r§r¦r*r+rÅrÈrÌrÍrÎrÓrÕr×ràrærérërîrñrörþrrrrrrr r"rzr,r-Úobjectr’r{ržrrrrÚ1s�             kC7 T ,8,  *& ( 1_, ".