ó
Q÷,Qc           @   sV   d  Z  d Z d d l m Z d d l m Z d d l m Z d e f d „  ƒ  YZ d S(	   s®  Provides 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ÿÿÿÿ(   t   descriptor_pb2(   t
   descriptor(   t   descriptor_databaset   DescriptorPoolc           B   s¹   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d d d d „ Z d d d d d	 „ Z e d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sG   A collection of protobufs dynamically constructed by descriptor protos.c         C   s7   t  j ƒ  |  _ | |  _ i  |  _ i  |  _ i  |  _ d S(   sî  Initializes 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(   R   t   DescriptorDatabaset   _internal_dbt   _descriptor_dbt   _descriptorst   _enum_descriptorst   _file_descriptors(   t   selft   descriptor_db(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt   __init__@   s
    			c         C   s   |  j  j | ƒ d S(   s   Adds the FileDescriptorProto and its types to this pool.

    Args:
      file_desc_proto: The FileDescriptorProto to add.
    N(   R   t   Add(   R
   t   file_desc_proto(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR   S   s    c         C   sv   y |  j  j | ƒ } Wn7 t k
 rO } |  j rF |  j j | ƒ } qP | ‚ n X| si t d | ƒ ‚ n  |  j | ƒ S(   sò   Gets 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.
    s   Cannot find a file named %s(   R   t   FindFileByNamet   KeyErrorR   t!   _ConvertFileProtoToFileDescriptor(   R
   t	   file_namet
   file_protot   error(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR   \   s    	
c         C   sv   y |  j  j | ƒ } Wn7 t k
 rO } |  j rF |  j j | ƒ } qP | ‚ n X| si t d | ƒ ‚ n  |  j | ƒ S(   s  Gets 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(   R   t   FindFileContainingSymbolR   R   R   (   R
   t   symbolR   R   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR   t   s    	
c         C   s9   | j  d ƒ } | |  j k r. |  j | ƒ n  |  j | S(   sª   Loads 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   .(   t   lstripR   R   (   R
   t	   full_name(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt   FindMessageTypeByNameŒ   s    
c         C   s9   | j  d ƒ } | |  j k r. |  j | ƒ n  |  j | S(   s¹   Loads 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   (   R   R   R   (   R
   R   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt   FindEnumTypeByName›   s    
c      	      sC  | j  |  j k r5t j d | j  d | j d | j d | j ƒ  ƒ } i  } t |  j | ƒ ƒ } xå | D]Ý } |  j	 | j  ƒ } t
 j j | j ƒ } d | j } | d ‰ ‡ f d †  ‰  t |  j | j | ƒ ƒ }	 | j |	 ƒ | j ‡  f d †  |	 Dƒ ƒ t |  j | j | ƒ ƒ }	 | j |	 ƒ | j ‡  f d †  |	 Dƒ ƒ qg Wx< | j D]1 }
 |  j |
 | j | | ƒ } | | j | j  <qRWx- | j D]" } |  j | | j | d	 | ƒ q‘Wx* |  j | j ƒ D] } |  j | | ƒ qÊWx. | j D]# } | | j  } | | j | j  <qîW|  j | ƒ | |  j | j  <n  |  j | j  S(
   sD  Creates a FileDescriptor from a proto or returns a cached copy.

    This method also has the side effect of loading all the symbols found in
    the file into the appropriate dictionaries in the pool.

    Args:
      file_proto: The proto to convert.

    Returns:
      A FileDescriptor matching the passed in proto.
    t   namet   packaget   optionst   serialized_pbR   c            s!   |  j  ˆ  ƒ r |  t ˆ  ƒ S|  S(   N(   t
   startswitht   len(   R   (   t   package_prefix(    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt   _strip_packageÇ   s    c         3   s'   |  ] \ } } ˆ  | ƒ | f Vq d  S(   N(    (   t   .0t   kt   v(   R#   (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pys	   <genexpr>Î   s    c         3   s'   |  ] \ } } ˆ  | ƒ | f Vq d  S(   N(    (   R$   R%   R&   (   R#   (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pys	   <genexpr>Ò   s    N(   R   R	   R   t   FileDescriptorR   R   t   SerializeToStringt   listt   _GetDepsR   R    t   FileDescriptorProtot
   FromStringR   t   _ExtractSymbolst   message_typet   updatet   _ExtractEnumst	   enum_typet   _ConvertMessageDescriptort   message_types_by_namet   _ConvertEnumDescriptort   Nonet   _ExtractMessagest   _SetFieldTypesR   (   R
   R   t   file_descriptort   scopet   dependenciest
   dependencyt   dep_desct	   dep_protoR   t   symbolsR.   t   message_descR1   t
   desc_protot   desc(    (   R#   R"   sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR   ª   sH    					
!c         C   s3  | r! d j  | | j f ƒ } n	 | j } | d k r? d } n	 | j } | d k r] i  } n  g  | j D] } |  j | | | | ƒ ^ qg } g  | j D]! } |  j | | | d | ƒ ^ q• }	 g  t | j ƒ D]! \ }
 } |  j	 | | |
 ƒ ^ qÌ } g  t | j
 ƒ D]! \ }
 } |  j	 | | t ƒ ^ q} g  | j D] } | j | j f ^ q4} | rat } n t } t j d | j d | d | d d d | d | d |	 d	 | d
 | j d | d | d | d d d d ƒ } x | j D] } | | _ q×Wx | j D] } | | _ qôW| | | j <| | d | <| |  j | <| S(   so  Adds 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.
    R   R   R   t   filenamet   containing_typet   fieldst   nested_typest
   enum_typest
   extensionsR   t   is_extendablet   extension_rangest   filet   serialized_startt   serialized_endN(   t   joinR   R5   t   nested_typeR2   R1   R4   t	   enumeratet   fieldt   _MakeFieldDescriptort	   extensiont   Truet   extension_ranget   startt   endt   FalseR   t
   DescriptorR   RE   RC   RF   R   (   R
   R@   R   t	   file_descR9   t	   desc_nameR   t   nestedt   enumt   enumst   indexRP   RD   RR   RG   t   rRI   RH   RA   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR2   æ   sV    				+.44(					c         C   sä   | r! d j  | | j f ƒ } n	 | j } | d
 k r? d
 } n	 | j } g  t | j ƒ D] \ } }	 |  j |	 | ƒ ^ qX }
 t j d | j d | d | d | d |
 d | d | j ƒ } | | | j <| | d	 | <| |  j	 | <| S(   s±  Make 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
    R   R   R   RB   RJ   t   valuesRC   R   s   .%sN(
   RM   R   R5   RO   t   valuet   _MakeEnumValueDescriptorR   t   EnumDescriptorR   R   (   R
   t
   enum_protoR   RY   RC   R9   t	   enum_nameR   R^   Ra   R`   RA   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR4   (  s&    			1c         C   s   | r! d j  | | j f ƒ } n	 | j } t j d | j d | d | d | j d | j d d d d d	 d d
 d d | j d t d d d | d d d | j	 ƒ S(   sh  Creates 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
    R   R   R   R^   t   numbert   typet   cpp_typeR.   R1   RC   t   labelt   has_default_valuet   default_valuet   is_extensiont   extension_scopeR   N(
   RM   R   R   t   FieldDescriptorRf   Rg   R5   Ri   RW   R   (   R
   t   field_protot   message_nameR^   Rl   R   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRQ   O  s&    						c         C   s†  | | j  } xNt | j | j ƒ D]7\ } } | j rg | j } | | k rZ d | } n  | | } n d } | j d ƒ s² t | t j	 ƒ r  t j
 j | _ q² t j
 j | _ n  t j
 j | j ƒ | _ | j t j
 j k sô | j t j
 j k r | | _ n  | j t j
 j k r!| | _ n  | j t j
 j k rKt | _ g  | _ n| j d ƒ r<t | _ | j t j
 j k s| j t j
 j k r¢t | j ƒ | _ qN| j t j
 j k rÆ| j | _ qN| j t j
 j k rö| j j ƒ  d k | _ qN| j t j
 j k r'| j j | j j  | _ qNt! | j ƒ | _ n t | _ d | _ | j | _ q# Wx! | j" D] } |  j# | | ƒ qhWd S(   s±   Sets 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.
    R   Rg   Rk   t   trueN($   R   t   zipRP   RD   t	   type_nameR5   t   HasFieldt
   isinstanceR   RX   Rn   t   TYPE_MESSAGERg   t	   TYPE_ENUMt   ProtoTypeToCppProtoTypeRh   t
   TYPE_GROUPR.   R1   Ri   t   LABEL_REPEATEDRW   t   has_defaultRk   RS   t   TYPE_DOUBLEt
   TYPE_FLOATt   floatt   TYPE_STRINGt	   TYPE_BOOLt   lowert   values_by_nameR^   t   intRN   R7   (   R
   R@   R9   RA   Ro   t
   field_descRs   RN   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR7   y  sP    "								c         C   s1   t  j d | j d | d | j d | j d d ƒ S(   sð   Creates 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.
    R   R^   Rf   R   Rg   N(   R   t   EnumValueDescriptorR   Rf   R   R5   (   R
   t   value_protoR^   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyRb   µ  s    				c         c   s£   xœ | D]” } | r. d j  | | j f ƒ } n	 | j } |  j | ƒ } | | f Vx" |  j | j | ƒ D] } | Vqg Wx" |  j | j | ƒ D] } | VqŒ Wq Wd S(   s  Pulls 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.
    R   N(   RM   R   R   R-   RN   R0   R1   (   R
   t   desc_protosR   R@   Rp   R?   R   (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR-   Ç  s    
		c         c   sY   xR | D]J } | r. d j  | | j f ƒ } n	 | j } |  j | ƒ } | | f Vq Wd S(   sû   Pulls 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.
    R   N(   RM   R   R   (   R
   t   enum_protosR   Rd   Re   t	   enum_desc(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR0   Ý  s    	c         c   s<   x5 | D]- } | Vx |  j  | j ƒ D] } | Vq% Wq Wd S(   sž   Pulls out all the message protos from descriptos.

    Args:
      desc_protos: The protos to extract symbols from.

    Yields:
      Descriptor protos.
    N(   R6   RN   (   R
   R‡   R@   t   message(    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR6   ð  s    
c         c   s`   xY | j  D]N } |  j | ƒ } t j j | j ƒ } | Vx |  j | ƒ D] } | VqI Wq
 Wd S(   s­   Recursively finds dependencies for file protos.

    Args:
      file_proto: The proto to get dependencies from.

    Yields:
      Each direct and indirect dependency.
    N(   R;   R   R    R+   R,   R   R*   (   R
   R   R;   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__R5   R   R   R   R   R   R   R   R2   R4   RW   RQ   R7   Rb   R-   R0   R6   R*   (    (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyR   =   s&   							<A')	<				N(   RŽ   t
   __author__t   google.protobufR    R   R   t   objectR   (    (    (    sC   /usr/lib/python2.7/site-packages/google/protobuf/descriptor_pool.pyt   <module>4   s
   