ó
X¾÷Xc           @   s›   d  Z  y d d l m Z Wn! e k
 r= d d l m Z n Xd d l Z d d l Z d d l m Z d d l m	 Z	 d „  Z
 d d d „ Z d „  Z d S(	   s   Dynamic Protobuf class creator.iÿÿÿÿ(   t   OrderedDictN(   t   descriptor_pb2(   t   message_factoryc         C   s%   |  j  j | ƒ } |  j | ƒ } | S(   s8  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.
  (   t   poolt   FindMessageTypeByNamet   GetPrototype(   t   factoryt	   full_namet   proto_descriptort	   proto_cls(    (    s?   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/proto_builder.pyt   _GetMessageFromFactory,   s    c   
      C   sN  t  j d | ƒ } | d k	 rL y t | | ƒ } | SWqL t k
 rH qL Xn  |  j ƒ  } t |  t ƒ sv t | ƒ } n  t	 j
 ƒ  } xF | D]> \ } } | j | j d ƒ ƒ | j t | ƒ j d ƒ ƒ q‰ W| j ƒ  d }	 | d k r%d | j ƒ  } y t | | ƒ } | SWq%t k
 r!q%Xn  | j j t |	 | | ƒ ƒ t | | ƒ S(   sê  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   s   utf-8s   .protos6   net.proto2.python.public.proto_builder.AnonymousProto_N(   R   t   MessageFactoryt   NoneR
   t   KeyErrort   itemst
   isinstanceR    t   sortedt   hashlibt   sha1t   updatet   encodet   strt	   hexdigestR   t   Addt   _MakeFileDescriptorProto(
   t   fieldsR   R   R   R	   t   field_itemst   fields_hasht   f_namet   f_typet   proto_file_name(    (    s?   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/proto_builder.pyt   MakeSimpleProtoClass<   s4     	c         C   sÌ   | j  d d ƒ \ } } t j ƒ  } t j j | j d d ƒ |  ƒ | _ | | _ | j	 j
 ƒ  } | | _ x\ t | d ƒ D]K \ } \ } }	 | j j
 ƒ  }
 | |
 _ | |
 _ t j j |
 _ |	 |
 _ qy W| S(   sA   Populate FileDescriptorProto for MessageFactory's DescriptorPool.t   .i   t   /(   t   rsplitR   t   FileDescriptorProtot   ost   patht   joint   replacet   namet   packaget   message_typet   addt	   enumeratet   fieldt   numbert   FieldDescriptorProtot   LABEL_OPTIONALt   labelt   type(   R   R   R   R)   R(   t
   file_protot
   desc_protot   f_numberR   R   t   field_proto(    (    s?   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/proto_builder.pyR   t   s    $		"		(   t   __doc__t   collectionsR    t   ImportErrort   ordereddictR   R$   t   google.protobufR   R   R
   R   R   R   (    (    (    s?   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/proto_builder.pyt   <module>   s   	8