Q,Qc@sVdZdZddlmZddlmZddlmZdefdYZdS( sProvides DescriptorPool to use as a container for proto2 descriptors. The DescriptorPool is used in conjection with a DescriptorDatabase to maintain a collection of protocol buffer descriptors for use when dynamically creating message types at runtime. For most applications protocol buffers should be used via modules generated by the protocol buffer compiler tool. This should only be used when the type of protocol buffers used in an application or library cannot be predetermined. Below is a straightforward example on how to use this class: pool = DescriptorPool() file_descriptor_protos = [ ... ] for file_descriptor_proto in file_descriptor_protos: pool.Add(file_descriptor_proto) my_message_descriptor = pool.FindMessageTypeByName('some.package.MessageType') The message descriptor can be used in conjunction with the message_factory module in order to create a protocol buffer class that can be encoded and decoded. s"matthewtoia@google.com (Matt Toia)i(tdescriptor_pb2(t descriptor(tdescriptor_databasetDescriptorPoolcBseZdZddZdZdZdZdZdZ dZ ddddZ ddddd Z e d Zd Zd Zd ZdZdZdZRS(sGA collection of protobufs dynamically constructed by descriptor protos.cCs7tj|_||_i|_i|_i|_dS(sInitializes a Pool of proto buffs. The descriptor_db argument to the constructor is provided to allow specialized file descriptor proto lookup code to be triggered on demand. An example would be an implementation which will read and compile a file specified in a call to FindFileByName() and not require the call to Add() at all. Results from this database will be cached internally here as well. Args: descriptor_db: A secondary source of file descriptors. N(RtDescriptorDatabaset _internal_dbt_descriptor_dbt _descriptorst_enum_descriptorst_file_descriptors(tselft descriptor_db((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt__init__@s    cCs|jj|dS(sAdds the FileDescriptorProto and its types to this pool. Args: file_desc_proto: The FileDescriptorProto to add. N(RtAdd(R tfile_desc_proto((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR SscCsvy|jj|}Wn7tk rO}|jrF|jj|}qP|nX|sitd|n|j|S(sGets a FileDescriptor by file name. Args: file_name: The path to the file to get a descriptor for. Returns: A FileDescriptor for the named file. Raises: KeyError: if the file can not be found in the pool. sCannot find a file named %s(RtFindFileByNametKeyErrorRt!_ConvertFileProtoToFileDescriptor(R t file_namet file_prototerror((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR\s   cCsvy|jj|}Wn7tk rO}|jrF|jj|}qP|nX|sitd|n|j|S(sGets the FileDescriptor for the file containing the specified symbol. Args: symbol: The name of the symbol to search for. Returns: A FileDescriptor that contains the specified symbol. Raises: KeyError: if the file can not be found in the pool. s Cannot find a file containing %s(RtFindFileContainingSymbolRRR(R tsymbolRR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRts   cCs9|jd}||jkr.|j|n|j|S(sLoads the named descriptor from the pool. Args: full_name: The full name of the descriptor to load. Returns: The descriptor for the named type. t.(tlstripRR(R t full_name((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindMessageTypeByNames cCs9|jd}||jkr.|j|n|j|S(sLoads the named enum descriptor from the pool. Args: full_name: The full name of the enum descriptor to load. Returns: The enum descriptor for the named type. R(RRR(R R((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindEnumTypeByNames c sC|j|jkr5tjd|jd|jd|jd|j}i}t|j|}x|D]}|j |j}t j j |j }d|j}|dfdt|j|j|} |j| |jfd| Dt|j|j|} |j| |jfd| DqgWx<|jD]1} |j| |j||} | |j| jsc3s'|]\}}||fVqdS(N((R$R%R&(R#(sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pys sN(RR RtFileDescriptorRRtSerializeToStringtlistt_GetDepsRRtFileDescriptorProtot FromStringRt_ExtractSymbolst message_typetupdatet _ExtractEnumst enum_typet_ConvertMessageDescriptortmessage_types_by_namet_ConvertEnumDescriptortNonet_ExtractMessagest_SetFieldTypesR (R Rtfile_descriptortscopet dependenciest dependencytdep_desct dep_protoRtsymbolsR.t message_descR1t desc_prototdesc((R#R"sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRsH            !  cCs3|r!dj||jf}n |j}|dkr?d}n |j}|dkr]i}ng|jD]}|j||||^qg}g|jD]!}|j|||d|^q} gt|jD]!\} } |j | || ^q} gt|j D]!\} } |j | |t ^q}g|j D]}|j |jf^q4}|rat }nt}tjd|jd|d|ddd| d|d| d |d |jd |d |d |dddd}x|jD]}||_qWx|jD]}||_qW|||j<||d|<||j|<|S(soAdds the proto to the pool in the specified package. Args: desc_proto: The descriptor_pb2.DescriptorProto protobuf message. package: The package the proto should be located in. file_desc: The file containing this message. scope: Dict mapping short and full symbols to message and enum types. Returns: The added descriptor. RRRtfilenametcontaining_typetfieldst nested_typest enum_typest extensionsRt is_extendabletextension_rangestfiletserialized_starttserialized_endN(tjoinRR5t nested_typeR2R1R4t enumeratetfieldt_MakeFieldDescriptort extensiontTruetextension_rangetstarttendtFalseRt DescriptorRRERCRFR(R R@Rt file_descR9t desc_nameRtnestedtenumtenumstindexRPRDRRRGtrRIRHRA((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR2sV      +.44(         c Cs|r!dj||jf}n |j}|d kr?d }n |j}gt|jD]\}} |j| |^qX} tjd|jd|d|d|d| d|d|j} | ||j<| |d |<| |j |<| S( sMake a protobuf EnumDescriptor given an EnumDescriptorProto protobuf. Args: enum_proto: The descriptor_pb2.EnumDescriptorProto protobuf message. package: Optional package name for the new message EnumDescriptor. file_desc: The file containing the enum descriptor. containing_type: The type containing this enum. scope: Scope containing available types. Returns: The added descriptor RRRRBRJtvaluesRCRs.%sN( RMRR5ROtvaluet_MakeEnumValueDescriptorRtEnumDescriptorRR( R t enum_protoRRYRCR9t enum_nameRR^RaR`RA((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR4(s&    1   cCs|r!dj||jf}n |j}tjd|jd|d|d|jd|jddddd dd dd |jd td dd|ddd|j S(shCreates a field descriptor from a FieldDescriptorProto. For message and enum type fields, this method will do a look up in the pool for the appropriate descriptor for that type. If it is unavailable, it will fall back to the _source function to create it. If this type is still unavailable, construction will fail. Args: field_proto: The proto describing the field. message_name: The name of the containing message. index: Index of the field is_extension: Indication that this field is for an extension. Returns: An initialized FieldDescriptor object RRRR^tnumberttypetcpp_typeR.R1RCtlabelthas_default_valuet default_valuet is_extensiontextension_scopeRN( RMRRtFieldDescriptorRfRgR5RiRWR(R t field_protot message_nameR^RlR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRQOs&      cCs||j}xNt|j|jD]7\}}|jrg|j}||krZd|}n||}nd}|jdst|tj rtj j |_ qtj j |_ ntj j|j |_|j tj j ks|j tj jkr||_n|j tj j kr!||_n|jtj jkrKt|_g|_n|jdr<t|_|j tj jks|j tj jkrt|j|_qN|j tj jkr|j|_qN|j tj jkr|jjdk|_qN|j tj j kr'|jj|jj |_qNt!|j|_nt|_d|_|j |_ q#Wx!|j"D]}|j#||qhWdS(sSets the field's type, cpp_type, message_type and enum_type. Args: desc_proto: The message descriptor to update. scope: Enclosing scope of available types. RRgRkttrueN($RtzipRPRDt type_nameR5tHasFieldt isinstanceRRXRnt TYPE_MESSAGERgt TYPE_ENUMtProtoTypeToCppProtoTypeRht TYPE_GROUPR.R1RitLABEL_REPEATEDRWt has_defaultRkRSt TYPE_DOUBLEt TYPE_FLOATtfloatt TYPE_STRINGt TYPE_BOOLtlowertvalues_by_nameR^tintRNR7(R R@R9RARot field_descRsRN((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR7ysP "              c Cs1tjd|jd|d|jd|jddS(sCreates a enum value descriptor object from a enum value proto. Args: value_proto: The proto describing the enum value. index: The index of the enum value. Returns: An initialized EnumValueDescriptor object. RR^RfRRgN(RtEnumValueDescriptorRRfRR5(R t value_protoR^((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRbs    ccsx|D]}|r.dj||jf}n |j}|j|}||fVx"|j|j|D] }|VqgWx"|j|j|D] }|VqWqWdS(sPulls out all the symbols from descriptor protos. Args: desc_protos: The protos to extract symbols from. package: The package containing the descriptor type. Yields: A two element tuple of the type name and descriptor object. RN(RMRRR-RNR0R1(R t desc_protosRR@RpR?R((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR-s    ccsYxR|D]J}|r.dj||jf}n |j}|j|}||fVqWdS(sPulls out all the symbols from enum protos. Args: enum_protos: The protos to extract symbols from. package: The package containing the enum type. Yields: A two element tuple of the type name and enum descriptor object. RN(RMRR(R t enum_protosRRdRet enum_desc((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR0s  ccs<x5|D]-}|Vx|j|jD] }|Vq%WqWdS(sPulls out all the message protos from descriptos. Args: desc_protos: The protos to extract symbols from. Yields: Descriptor protos. N(R6RN(R RR@tmessage((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR6s ccs`xY|jD]N}|j|}tjj|j}|Vx|j|D] }|VqIWq WdS(sRecursively finds dependencies for file protos. Args: file_proto: The proto to get dependencies from. Yields: Each direct and indirect dependency. N(R;RRR+R,RR*(R RR;R<R=t parent_dep((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR*s   N(t__name__t __module__t__doc__R5R R RRRRRR2R4RWRQR7RbR-R0R6R*(((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR=s&      < A' ) <    N(Rt __author__tgoogle.protobufRRRtobjectR(((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt4s