3 SÉLjRã @s|dZyddlmZWn ek r4ddlmZYnXddlZddlZddlmZddlm Z dd„Z d dd „Z d d „Z dS) zDynamic Protobuf class creator.é)Ú OrderedDictN)Údescriptor_pb2)Úmessage_factorycCs|jj|ƒ}|j|ƒ}|S)a8Get a proto class from the MessageFactory by name. Args: factory: a MessageFactory instance. full_name: str, the fully qualified name of the proto type. Returns: A class, for the type identified by full_name. Raises: KeyError, if the proto is not found in the factory's descriptor pool. )ÚpoolZFindMessageTypeByNameZ GetPrototype)ÚfactoryÚ full_nameZproto_descriptorÚ proto_cls©r ú#/usr/lib/python3.6/proto_builder.pyÚ_GetMessageFromFactory,s  r c Csòtj|d�}|dk r8yt||ƒ}|Stk r6YnX|jƒ}t|tƒsRt|ƒ}tj ƒ}x4|D],\}}|j |j dƒƒ|j t |ƒj dƒƒq`W|j ƒd} |dkrÔd|j ƒ}yt||ƒ}|Stk rÒYnX|jjt| ||ƒƒt||ƒS)aêCreate a Protobuf class whose fields are basic types. Note: this doesn't validate field names! Args: fields: dict of {name: field_type} mappings for each field in the proto. If this is an OrderedDict the order will be maintained, otherwise the fields will be sorted by name. full_name: optional str, the fully-qualified name of the proto type. pool: optional DescriptorPool instance. Returns: a class, the new protobuf class with a FileDescriptor. )rNzutf-8z.protoz6net.proto2.python.public.proto_builder.AnonymousProto_)rZMessageFactoryr ÚKeyErrorÚitemsÚ isinstancerÚsortedÚhashlibZsha1ÚupdateÚencodeÚstrZ hexdigestrZAddÚ_MakeFileDescriptorProto) ZfieldsrrrrÚ field_itemsZ fields_hashÚf_nameÚf_typeÚproto_file_namer r r ÚMakeSimpleProtoClass<s4      rc CsŒ|jddƒ\}}tjƒ}tjj|jddƒ|ƒ|_||_|j j ƒ}||_x@t |dƒD]2\}\}} |j j ƒ} || _|| _ tjj| _| | _qRW|S)zAPopulate FileDescriptorProto for MessageFactory's DescriptorPool.Ú.éú/)ÚrsplitrZFileDescriptorProtoÚosÚpathÚjoinÚreplaceÚnameÚpackageZ message_typeÚaddÚ enumerateZfieldZnumberZFieldDescriptorProtoZLABEL_OPTIONALZlabelÚtype) rrrr#r"Z file_protoZ desc_protoZf_numberrrZ field_protor r r rts    r)NN) Ú__doc__Ú collectionsrÚ ImportErrorZ ordereddictrrZgoogle.protobufrrr rrr r r r Ús   8