3
(hR                 @   s|   d Z yddlmZ W n  ek
r4   ddlmZ Y nX ddlZddlZddlmZ ddlm	Z	 dd Z
ddd	Zd
d ZdS )zDynamic Protobuf class creator.    )OrderedDictN)descriptor_pb2)message_factoryc             C   s   | j j|}| j|}|S )a8  Get 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.
  )poolZFindMessageTypeByNameZGetPrototype)factory	full_nameZproto_descriptor	proto_cls r	   C/tmp/pip-install-q3hcpn_q/protobuf/google/protobuf/proto_builder.py_GetMessageFromFactory,   s    
r   c       
      C   s   t j|d}|dk	r8yt||}|S  tk
r6   Y nX | j }t| tsRt|}tj	 }x4|D ],\}}|j
|jd |j
t|jd q`W |j d }	|dkrd|j  }yt||}|S  tk
r   Y nX |jjt|	|| 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.
  )r   Nzutf-8z.protoz6net.proto2.python.public.proto_builder.AnonymousProto_)r   ZMessageFactoryr   KeyErroritems
isinstancer   sortedhashlibsha1updateencodestr	hexdigestr   Add_MakeFileDescriptorProto)
fieldsr   r   r   r   field_itemsZfields_hashf_namef_typeproto_file_namer	   r	   r
   MakeSimpleProtoClass<   s4    



r   c             C   s   |j dd\}}tj }tjj|jdd| |_||_|j	j
 }||_x@t|dD ]2\}\}}	|jj
 }
||
_||
_tjj|
_|	|
_qRW |S )zAPopulate FileDescriptorProto for MessageFactory's DescriptorPool..   /)rsplitr   ZFileDescriptorProtoospathjoinreplacenamepackageZmessage_typeadd	enumeratefieldnumberZFieldDescriptorProtoZLABEL_OPTIONALlabeltype)r   r   r   r'   r&   Z
file_protoZ
desc_protoZf_numberr   r   Zfield_protor	   r	   r
   r   t   s    



r   )NN)__doc__collectionsr   ImportErrorZordereddictr   r"   Zgoogle.protobufr   r   r   r   r   r	   r	   r	   r
   <module>   s   
8