Duac@sdZdZddlZddlmZddlmZddlmZejZdZdZ d Z d e fd YZ d Z erejjZn e Zd ZdS(sJProvides 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. If you want to get a Python class for the specified proto, use the helper functions inside google.protobuf.message_factory directly instead of this class. s"matthewtoia@google.com (Matt Toia)iN(t descriptor(tdescriptor_database(t text_encodingcCs |jdS(sLRemove leading period from fully-qualified type name. Due to b/13860351 in descriptor_database.py, types in the root namespace are generated with a leading period. This function removes that prefix. Args: name: A str, the fully-qualified symbol name. Returns: A str, the normalized fully-qualified symbol name. t.(tlstrip(tname((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt_NormalizeFullyQualifiedNameFs cCs|jdr|jSdSdS(sCReturns the value of the field `options`, or None if it is not set.toptionsN(tHasFieldRtNone(tdescriptor_proto((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt_OptionsOrNoneUscCsO|joN|jjoN|jjjoN|jtjjkoN|j tjj kS(N( t is_extensiontcontaining_typet has_optionst GetOptionstmessage_set_wire_formatttypeRtFieldDescriptort TYPE_MESSAGEtlabeltLABEL_OPTIONAL(tfield((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt_IsMessageSetExtension]s   tDescriptorPoolcBsaeZdZer!d"dZnd"dZdZdZdZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZd"d"d"d"dZd"d"d"d"dZedZdZdZ dZ!dZ"dZ#dZ$d Z%d!Z&RS(#sGA collection of protobufs dynamically constructed by descriptor protos.cCstjj|S(N(Rt_messageR(tclst descriptor_db((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt__new__jscCsmtj|_||_i|_i|_i|_i|_i|_t j t |_ t j t |_ 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_service_descriptorst_file_descriptorst_toplevel_extensionst collectionst defaultdicttdictt_extensions_by_namet_extensions_by_number(tselfR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt__init__ns       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,scCs3ddlm}|jj|}|j|dS(sAdds the FileDescriptorProto and its types to this pool. Args: serialized_file_desc_proto: A bytes string, serialization of the FileDescriptorProto to add. i(tdescriptor_pb2N(tgoogle.protobufR.tFileDescriptorProtot FromStringR,(R*tserialized_file_desc_protoR.R-((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytAddSerializedFiles   cCsEt|tjs!tdn||j|j<|j|jdS(sAdds a Descriptor to the pool, non-recursively. If the Descriptor contains nested messages or enums, the caller must explicitly register them. This method also registers the FileDescriptor associated with the message. Args: desc: A Descriptor. s+Expected instance of descriptor.Descriptor.N(t isinstanceRt Descriptort TypeErrorR t full_namet_AddFileDescriptortfile(R*tdesc((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt AddDescriptors cCsEt|tjs!tdn||j|j<|j|jdS(sAdds an EnumDescriptor to the pool. This method also registers the FileDescriptor associated with the enum. Args: enum_desc: An EnumDescriptor. s/Expected instance of descriptor.EnumDescriptor.N(R4RtEnumDescriptorR6R!R7R8R9(R*t enum_desc((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytAddEnumDescriptors cCs5t|tjs!tdn||j|jt|}|jd\}}}|j|}|j|S(sLoads the named field descriptor from the pool. Args: full_name: The full name of the field descriptor to load. Returns: The field descriptor for the named field. Raises: KeyError: if the field cannot be found in the pool. R(RRSRTtfields_by_name(R*R7RWRXt field_nametmessage_descriptor((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindFieldByNames cCs>t|}|jd\}}}|j|}|j|S(sLoads the named oneof descriptor from the pool. Args: full_name: The full name of the oneof descriptor to load. Returns: The oneof descriptor for the named oneof. Raises: KeyError: if the oneof cannot be found in the pool. R(RRSRTtoneofs_by_name(R*R7RWRXt oneof_nameR_((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindOneofByNames cCst|}y|j|SWntk r.nX|jd\}}}y|j|}Wn tk r||j|}nX|j|S(sLoads the named extension descriptor from the pool. Args: full_name: The full name of the extension descriptor to load. Returns: A FieldDescriptor, describing the named extension. Raises: KeyError: if the extension cannot be found in the pool. R(RR$RDRSRTRRRU(R*R7RWRXRYtscope((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindExtensionByNames   cCs|j||S(sGets the extension of the specified message with the specified number. Extensions have to be registered to this pool by calling AddExtensionDescriptor. Args: message_descriptor: descriptor of the extended message. number: integer, number of the extension field. Returns: A FieldDescriptor describing the extension. Raises: KeyError: when no extension with the given number is known for the specified message. (R)(R*R_RC((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindExtensionByNumberscCst|j|jS(s!Gets all the known extension of a given message. Extensions have to be registered to this pool by calling AddExtensionDescriptor. Args: message_descriptor: descriptor of the extended message. Returns: A list of FieldDescriptor describing the extensions. (tlistR)tvalues(R*R_((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindAllExtensionss cCs6t|}||jkr+|j|n|j|S(sLoads the named service descriptor from the pool. Args: full_name: The full name of the service descriptor to load. Returns: The service descriptor for the named service. Raises: KeyError: if the service cannot be found in the pool. (RR"RR(R*R7((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytFindServiceByNames cCsvy|jj|}Wn7tk rO}|jrF|jj|}qP|nX|sitd|n|j|S(s"Finds the file in descriptor DB 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 cannot be found in the descriptor database. s Cannot find a file containing %s(RR[RDRRN(R*RVRPRQ((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRRs   cCs|j|jkrt|j|j}g|jD]}|j|^q4}g|jD]}||^qY}tjd|d|jd|j d|j dt |d|j d|d|}i}xM|D]E} |j |j| jj|j d | jjDqWxB|jD]7} |j| |j |||j } | |j| j,sR tN(&RR#Rgt_GetDepst dependencyRMtpublic_dependencyRRLRlRmR tSerializeToStringtupdatet_ExtractSymbolstmessage_types_by_nameRhtenum_types_by_nameRFt_ConvertMessageDescriptort enum_typet_ConvertEnumDescriptorR t enumerateRGt_MakeFieldDescriptortTruet_GetTypeFromScopetextendeeR t _SetFieldTypeRUt_SetAllFieldTypesRqtservicet_MakeServiceDescriptortservices_by_nameR,(R*RPt built_depstnt direct_depstit public_depstfile_descriptorRdRvRFt message_descR~tindextextension_prototextension_desct desc_prototdesc_proto_prefixR:t service_proto((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRNsf %                 c!Cs|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 ||| |dt ^q }gt|j D]B\} }t j|jdj||jf| dg|j^qI}g|jD]}|j|jf^q}|rt }nt}t jd|jd|d|ddd| d|d |d | d |d t|d |d|d|ddddd|}x|jD]}||_qMWx|jD]} || _qjWxat|jD]P\}}|jdr|j}||jj| |||| |_qqW||t|<||j|<|S(sAdds 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. syntax: string indicating syntax of the file ("proto2" or "proto3") Returns: The added descriptor. RR RR7tfilenameR tfieldstoneofst nested_typest enum_typest extensionsRt is_extendabletextension_rangesR9tserialized_starttserialized_endRmt oneof_indexN( tjoinRR t nested_typeR}R~RRRRRGRt oneof_declRtOneofDescriptorRtextension_rangetstarttendtFalseR5R RR RRRRtappendtcontaining_oneofRqR (R*RRlRJRdRmt desc_nameROtnestedRstenumsRRRRGRR:RtrRRt field_indext field_descR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR}]sf      ..7=U(         c Cs|r!dj||jf}n |j}|d kr?d }n |j}gt|jD]\}} |j| |^qX} tjd|jd|d|d|d| d|dt|} | |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 RRR7RR9RhR Rs.%sN( RRR Rtvaluet_MakeEnumValueDescriptorRR<R R!( R*t enum_protoRlRJR Rdt enum_nameRORRRhR:((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRs$    1 c!Cs|r!dj||jf}n |j}tjd|jd|d|d|jd|jddddd dd dd |jd td dd|dddt |d|S(sCreates 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 file_desc: The file containing the field descriptor. is_extension: Indication that this field is for an extension. Returns: An initialized FieldDescriptor object RRR7RRCRtcpp_typeRFR~R Rthas_default_valuet default_valueR RBRR9N( RRRRRCRR RRR (R*t field_protoRWRRJR R7((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRs(       c Cst|}|j||j|}|dkrBt|j}ndj||jg}x9t|j|jD]"\}}|j||||qpWxTt|j|j D]=\}} |j||j || _ |j|| ||qWx$|j D]} |j || |qWdS(sSets all the descriptor's fields's types. This method also sets the containing types on any extensions. Args: package: The current package of desc_proto. desc_proto: The message descriptor to update. scope: Enclosing scope of available types. RN(RqRRRtzipRRRRGRRR RR( R*RlRRdt main_desctnested_packageRRRRR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRs  "cCs|jr$|j||j|}nd}|jdsot|tjr]tjj|_ qotjj |_ ntjj |j |_ |j tjjks|j tjj kr||_n|j tjj kr||_n|jtjjkrt|_g|_n|jdr&t|_|j tjjksJ|j tjjkr_t|j|_q|j tjjkr|j|_q|j tjjkr|jjdk|_q|j tjj kr|jj|jj|_q|j tjjkrtj |j|_qt!|j|_nt|_|j tjjksY|j tjjkred|_n|j tjjkrd|_ny|j tjjkrt|_nX|j tjj kr|jj"dj|_n*|j tjjkrd|_n d|_|j |_ dS( sSets the field's type, cpp_type, message_type and enum_type. Args: field_proto: Data about the field in proto format. field_desc: The descriptor to modiy. package: The package the field's container is in. scope: Enclosing scope of available types. RRttrueguiRtN(#t type_nameRR RR4RR5RRRt TYPE_ENUMtProtoTypeToCppProtoTypeRt TYPE_GROUPRFR~RtLABEL_REPEATEDRRRRt TYPE_DOUBLEt TYPE_FLOATtfloatt TYPE_STRINGt TYPE_BOOLtlowertvalues_by_nameRCt TYPE_BYTESRt CUnescapetintRh(R*RRRlRdR:((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR s^              c Cs4tjd|jd|d|jdt|ddS(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. RRRCRRN(RtEnumValueDescriptorRRCR R (R*t value_protoR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRfs    c Cs|r!dj||jf}n |j}gt|jD]'\}}|j|||||^q:} tjd|jd|d|d| dt|d|} | |j|<| S(sMake a protobuf ServiceDescriptor given a ServiceDescriptorProto. Args: service_proto: The descriptor_pb2.ServiceDescriptorProto protobuf message. service_index: The index of the service in the File. scope: Dict mapping short and full symbols to message and enum types. package: Optional package name for the new message EnumDescriptor. file_desc: The file containing the service descriptor. Returns: The added descriptor. RRR7RtmethodsRR9( RRRtmethodt_MakeMethodDescriptorRR?R R"( R*Rt service_indexRdRlRJt service_nameRt method_protoRR:((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRxs :   c Csdj||jf}|j||j|}|j||j|}tjd|jd|d|dd d|d|dt|S( sCreates a method descriptor from a MethodDescriptorProto. Args: method_proto: The proto describing the method. service_name: The name of the containing service. package: Optional package name to look up for types. scope: Scope containing available types. index: Index of the method in the service. Returns: An initialized MethodDescriptor object. RRR7Rtcontaining_servicet input_typet output_typeRN( RRRRRRtMethodDescriptorR R ( R*RRRlRdRR7RR((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRsccssxl|D]d}t|j|fVx|j|jD] }|Vq4Wx%|jD]}t|j|fVqMWqWdS(sPulls out all the symbols from descriptor protos. Args: descriptors: The messages to extract descriptors from. Yields: A two element tuple of the type name and descriptor object. N(RqR7RzRR(R*t descriptorsR:RVRs((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRzs  ccsBx;|D]3}|j|}|Vx|jD] }|Vq+WqWdS(sRecursively finds dependencies for file protos. Args: dependencies: The names of the files being depended on. Yields: Each direct and indirect dependency. N(RMRo(R*RoRvtdep_desct parent_dep((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRus cCsr||krjt|jd}xF|rfdj||g}||krV|}Pq$|jdq$Wn||S(s?Finds a given type name in the current scope. Args: package: The package the proto should be located in. type_name: The name of the type to be found in the scope. scope: Dict mapping short and full symbols to message and enum types. Returns: The descriptor for the requested type. Ri(RqtsplitRtpop(R*RlRRdt componentstpossible_match((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRs   N('t__name__t __module__t__doc__t_USE_C_DESCRIPTORSR RR+R,R3R;R>RARIRKR8RMR[RTR\R`RcReRfRiRjRRRNR}RRRRRRRRRzRuR(((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyResJ     *   0          UP& + F    cCs|jdr|Sd|S(NRs.%s(t startswith(R((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRqscCstS(N(t_DEFAULT(((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pytDefaults(Rt __author__R%R/RRRRRR RtobjectRRqRt default_poolRR(((sC/usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt8s$