3
\^                 @   s  d Z dZddlZddlZddlmZ dZej dkr\ddlZddl	Z	ddl
mZ eeddZG d	d
 d
eZG dd deZerG dd deZneZG dd deZej ZG dd dejeZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZ d%d& Z!d'd( Z"d)d* Z#d+d, Z$d1d/d0Z%dS )2zDescriptors essentially contain exactly the information found in a .proto
file, in types that make this information accessible in Python.
z#robinson@google.com (Will Robinson)    N)api_implementationFcpp)_message_USE_C_DESCRIPTORSc               @   s   e Zd ZdZdS )ErrorzBase error for this module.N)__name__
__module____qualname____doc__ r   r   >/tmp/pip-build-8app2_gc/protobuf/google/protobuf/descriptor.pyr   3   s   r   c               @   s   e Zd ZdZdS )TypeTransformationErrorzHError transforming between python proto type and corresponding C++ type.N)r   r   r	   r
   r   r   r   r   r   7   s   r   c                   s   e Zd Z fddZ  ZS )DescriptorMetaclassc                s(   t t| j|rdS t|| jr$dS dS )NTF)superr   __instancecheck__
isinstance_C_DESCRIPTOR_CLASS)clsobj)	__class__r   r   r   A   s
    z%DescriptorMetaclass.__instancecheck__)r   r   r	   r   __classcell__r   r   )r   r   r   @   s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_Lockz>Wrapper class of threading.Lock(), which is allowed by 'with'.c             C   s   t j| }tj |_|S )N)object__new__	threadingLock_lock)r   selfr   r   r   r   O   s    

z_Lock.__new__c             C   s   | j j  d S )N)r   acquire)r   r   r   r   	__enter__T   s    z_Lock.__enter__c             C   s   | j j  d S )N)r   release)r   exc_type	exc_valueexc_tbr   r   r   __exit__W   s    z_Lock.__exit__N)r   r   r	   r
   r   r   r$   r   r   r   r   r   L   s   r   c               @   s0   e Zd ZdZerf Zdd Zdd Zdd ZdS )	DescriptorBasea  Descriptors base class.

  This class is the base of all descriptor classes. It provides common options
  related functionality.

  Attributes:
    has_options:  True if the descriptor has non-default options.  Usually it
        is not necessary to read this -- just call GetOptions() which will
        happily return the default instance.  However, it's sometimes useful
        for efficiency, and also useful inside the protobuf implementation to
        avoid some bootstrapping issues.
  c             C   s(   || _ || _|| _|dk	p |dk	| _dS )zInitialize the descriptor given its options message and the name of the
    class of the options message. The name of the class is required in case
    the options message is None and has to be created.
    N)_options_options_class_name_serialized_optionshas_options)r   optionsserialized_optionsoptions_class_namer   r   r   __init__r   s    zDescriptorBase.__init__c             C   s   || _ || _|dk	| _dS )zSets the descriptor's options

    This function is used in generated proto2 files to update descriptor
    options. It must not be used outside proto2.
    N)r&   r'   r)   )r   r*   r,   r   r   r   _SetOptions~   s    zDescriptorBase._SetOptionsc             C   s   | j r| j S ddlm} yt|| j}W n" tk
rJ   td| j Y nX t, | jdkrf| | _ nt	| | j| _ | j S Q R X dS )zRetrieves descriptor options.

    This method returns the options set or creates the default options for the
    descriptor.
    r   )descriptor_pb2zUnknown options class name %s!N)
r&   google.protobufr/   getattrr'   AttributeErrorRuntimeErrorr   r(   _ParseOptions)r   r/   Zoptions_classr   r   r   
GetOptions   s    


zDescriptorBase.GetOptionsN)	r   r   r	   r
   r   r   r-   r.   r5   r   r   r   r   r%   ^   s   r%   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )_NestedDescriptorBasez0Common class for descriptors that can be nested.Nc
       
         s<   t t| j||	| || _|| _|| _|| _|| _|| _dS )a  Constructor.

    Args:
      options: Protocol message options or None
        to use default message options.
      options_class_name: (str) The class name of the above options.

      name: (str) Name of this protocol message type.
      full_name: (str) Fully-qualified name of this protocol message type,
        which will include protocol "package" name and the name of any
        enclosing types.
      file: (FileDescriptor) Reference to file info.
      containing_type: if provided, this is a nested descriptor, with this
        descriptor as parent, otherwise None.
      serialized_start: The start index (inclusive) in block in the
        file.serialized_pb that describes this descriptor.
      serialized_end: The end index (exclusive) in block in the
        file.serialized_pb that describes this descriptor.
      serialized_options: Protocol message serilized options or None.
    N)	r   r6   r-   name	full_namefilecontaining_type_serialized_start_serialized_end)
r   r*   r,   r7   r8   r9   r:   serialized_startserialized_endr+   )r   r   r   r-      s    

z_NestedDescriptorBase.__init__c             C   sF   | j dk	r:| jdk	r:| jdk	r:|j| j j| j| j  ntddS )zCopies this to the matching proto in descriptor_pb2.

    Args:
      proto: An empty proto instance from descriptor_pb2.

    Raises:
      Error: If self couldnt be serialized, due to to few constructor arguments.
    Nz*Descriptor does not contain serialization.)r9   r;   r<   ParseFromStringserialized_pbr   )r   protor   r   r   CopyToProto   s    	



z!_NestedDescriptorBase.CopyToProto)NNN)r   r   r	   r
   r-   rB   r   r   r   )r   r   r6      s    "r6   c            	       sV   e Zd ZdZer ejZdddZd fdd	Z	e
dd	 Zd
d Z fddZ  ZS )
Descriptorai	  Descriptor for a protocol message type.

  A Descriptor instance has the following attributes:

    name: (str) Name of this protocol message type.
    full_name: (str) Fully-qualified name of this protocol message type,
      which will include protocol "package" name and the name of any
      enclosing types.

    containing_type: (Descriptor) Reference to the descriptor of the
      type containing us, or None if this is top-level.

    fields: (list of FieldDescriptors) Field descriptors for all
      fields in this type.
    fields_by_number: (dict int -> FieldDescriptor) Same FieldDescriptor
      objects as in |fields|, but indexed by "number" attribute in each
      FieldDescriptor.
    fields_by_name: (dict str -> FieldDescriptor) Same FieldDescriptor
      objects as in |fields|, but indexed by "name" attribute in each
      FieldDescriptor.
    fields_by_camelcase_name: (dict str -> FieldDescriptor) Same
      FieldDescriptor objects as in |fields|, but indexed by
      "camelcase_name" attribute in each FieldDescriptor.

    nested_types: (list of Descriptors) Descriptor references
      for all protocol message types nested within this one.
    nested_types_by_name: (dict str -> Descriptor) Same Descriptor
      objects as in |nested_types|, but indexed by "name" attribute
      in each Descriptor.

    enum_types: (list of EnumDescriptors) EnumDescriptor references
      for all enums contained within this type.
    enum_types_by_name: (dict str ->EnumDescriptor) Same EnumDescriptor
      objects as in |enum_types|, but indexed by "name" attribute
      in each EnumDescriptor.
    enum_values_by_name: (dict str -> EnumValueDescriptor) Dict mapping
      from enum value name to EnumValueDescriptor for that value.

    extensions: (list of FieldDescriptor) All extensions defined directly
      within this message type (NOT within a nested type).
    extensions_by_name: (dict, string -> FieldDescriptor) Same FieldDescriptor
      objects as |extensions|, but indexed by "name" attribute of each
      FieldDescriptor.

    is_extendable:  Does this type define any extension ranges?

    oneofs: (list of OneofDescriptor) The list of descriptors for oneof fields
      in this message.
    oneofs_by_name: (dict str -> OneofDescriptor) Same objects as in |oneofs|,
      but indexed by "name" attribute.

    file: (FileDescriptor) Reference to file descriptor.
  NTc             C   s   t jj  t jj|S )N)r   Message_CheckCalledFromGeneratedFiledefault_poolZFindMessageTypeByName)r   r7   r8   filenamer:   fieldsnested_types
enum_types
extensionsr*   r+   is_extendableextension_rangesoneofsr9   r=   r>   syntaxr   r   r   r     s    
zDescriptor.__new__c                sl  t t| j|	d|||||||
d	 || _x| jD ]
}| |_q0W tdd |D | _tdd |D | _d| _|| _	x|D ]
}| |_qxW tdd |D | _
|| _x| jD ]
}| |_qW tdd |D | _td	d |D | _|| _x| jD ]
}| |_qW td
d |D | _|| _|| _|dk	r(|ng | _tdd | jD | _x| jD ]}| |_qLW |pdd| _dS )zArguments to __init__() are as described in the description
    of Descriptor fields above.

    Note that filename is an obsolete argument, that is not used anymore.
    Please use file.name to access this as an attribute.
    ZMessageOptions)r=   r>   r+   c             s   s   | ]}|j |fV  qd S )N)number).0fr   r   r   	<genexpr>>  s    z&Descriptor.__init__.<locals>.<genexpr>c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   rR   r   r   r   rS   ?  s    Nc             s   s   | ]}|j |fV  qd S )N)r7   )rQ   tr   r   r   rS   E  s    c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   rT   r   r   r   rS   J  s    c             s   s$   | ]}|j D ]}|j|fV  qqd S )N)valuesr7   )rQ   rT   vr   r   r   rS   L  s    c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   rR   r   r   r   rS   Q  s    c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   or   r   r   rS   U  s    proto2)r   rC   r-   rH   r:   dictZfields_by_numberZfields_by_name_fields_by_camelcase_namerI   Znested_types_by_namerJ   enum_types_by_nameZenum_values_by_namerK   extension_scopeextensions_by_namerL   rM   rN   Zoneofs_by_namerO   )r   r7   r8   rG   r:   rH   rI   rJ   rK   r*   r+   rL   rM   rN   r9   r=   r>   rO   fieldnested_type	enum_type	extensionZoneof)r   r   r   r-   %  s>    







zDescriptor.__init__c             C   s&   | j d kr tdd | jD | _ | j S )Nc             s   s   | ]}|j |fV  qd S )N)camelcase_name)rQ   rR   r   r   r   rS   ^  s    z6Descriptor.fields_by_camelcase_name.<locals>.<genexpr>)rZ   rY   rH   )r   r   r   r   fields_by_camelcase_nameZ  s    
z#Descriptor.fields_by_camelcase_namec             C   s   | j | j| jS )as  Returns the string name of an enum value.

    This is just a small helper method to simplify a common operation.

    Args:
      enum: string name of the Enum.
      value: int, value of the enum.

    Returns:
      string name of the enum value.

    Raises:
      KeyError if either the Enum doesn't exist or the value is not a valid
        value for the enum.
    )r[   values_by_numberr7   )r   enumvaluer   r   r   EnumValueNamea  s    zDescriptor.EnumValueNamec                s   t t| j| dS )zvCopies this to a descriptor_pb2.DescriptorProto.

    Args:
      proto: An empty descriptor_pb2.DescriptorProto.
    N)r   rC   rB   )r   rA   )r   r   r   rB   s  s    zDescriptor.CopyToProto)	NNTNNNNNN)	NNTNNNNNN)r   r   r	   r
   r   r   rC   r   r   r-   propertyrc   rg   rB   r   r   r   )r   r   rC      s    6    
    0rC   c            $       s0  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd	Zd
ZdZ dZ!eeeeeeeeeeeeeee	eeeeeeeeee
eeeeeeeee ee iZ"dZ#dZ$dZ%dZ&d"Z'dZ(dZ)e*re+j,Z-d#ddZ.d$ fdd	Z/e0dd Z1e2dd  Z3  Z4S )%FieldDescriptora	  Descriptor for a single field in a .proto file.

  A FieldDescriptor instance has the following attributes:

    name: (str) Name of this field, exactly as it appears in .proto.
    full_name: (str) Name of this field, including containing scope.  This is
      particularly relevant for extensions.
    camelcase_name: (str) Camelcase name of this field.
    index: (int) Dense, 0-indexed index giving the order that this
      field textually appears within its message in the .proto file.
    number: (int) Tag number declared for this field in the .proto file.

    type: (One of the TYPE_* constants below) Declared type.
    cpp_type: (One of the CPPTYPE_* constants below) C++ type used to
      represent this field.

    label: (One of the LABEL_* constants below) Tells whether this
      field is optional, required, or repeated.
    has_default_value: (bool) True if this field has a default value defined,
      otherwise false.
    default_value: (Varies) Default value of this field.  Only
      meaningful for non-repeated scalar fields.  Repeated fields
      should always set this to [], and non-repeated composite
      fields should always set this to None.

    containing_type: (Descriptor) Descriptor of the protocol message
      type that contains this field.  Set by the Descriptor constructor
      if we're passed into one.
      Somewhat confusingly, for extension fields, this is the
      descriptor of the EXTENDED message, not the descriptor
      of the message containing this field.  (See is_extension and
      extension_scope below).
    message_type: (Descriptor) If a composite field, a descriptor
      of the message type contained in this field.  Otherwise, this is None.
    enum_type: (EnumDescriptor) If this field contains an enum, a
      descriptor of that enum.  Otherwise, this is None.

    is_extension: True iff this describes an extension field.
    extension_scope: (Descriptor) Only meaningful if is_extension is True.
      Gives the message that immediately contains this extension field.
      Will be None iff we're a top-level (file-level) extension field.

    options: (descriptor_pb2.FieldOptions) Protocol message field options or
      None to use default field options.

    containing_oneof: (OneofDescriptor) If the field is a member of a oneof
      union, contains its descriptor. Otherwise, None.

    file: (FileDescriptor) Reference to file descriptor.
                          	   
                              i8J  iN  NTc             C   s*   t jj  |rt jj|S t jj|S d S )N)r   rD   rE   rF   FindExtensionByNameFindFieldByName)r   r7   r8   indexrP   typecpp_typelabeldefault_valuemessage_typer`   r:   is_extensionr\   r*   r+   has_default_valuecontaining_oneof	json_namer9   r   r   r   r     s    
zFieldDescriptor.__new__c                s   t t| j||d || _|| _|| _d| _|dkr@t|| _n|| _|| _	|| _
|| _|| _|| _|| _|| _|| _|	| _|
| _|| _|| _|| _tj dkr|rtjj|| _qtjj|| _nd| _dS )a  The arguments are as described in the description of FieldDescriptor
    attributes above.

    Note that containing_type may be None, and may be set later if necessary
    (to deal with circular references between message types, for example).
    Likewise for extension_scope.
    ZFieldOptionsNr   )r   ri   r-   r7   r8   r9   _camelcase_name_ToJsonNamer   r   rP   r   r   r   r   r   r:   r   r`   r   r\   r   r   Typer   rF   r}   Z_cdescriptorr~   )r   r7   r8   r   rP   r   r   r   r   r   r`   r:   r   r\   r*   r+   r   r   r   r9   )r   r   r   r-     s6    

zFieldDescriptor.__init__c             C   s   | j d krt| j| _ | j S )N)r   _ToCamelCaser7   )r   r   r   r   rb   D  s    
zFieldDescriptor.camelcase_namec             C   s0   y
t j|  S  tk
r*   td|  Y nX dS )a  Converts from a Python proto type to a C++ Proto Type.

    The Python ProtocolBuffer classes specify both the 'Python' datatype and the
    'C++' datatype - and they're not the same. This helper method should
    translate from one to another.

    Args:
      proto_type: the Python proto type (descriptor.FieldDescriptor.TYPE_*)
    Returns:
      descriptor.FieldDescriptor.CPPTYPE_*, the C++ type.
    Raises:
      TypeTransformationError: when the Python proto type isn't known.
    zUnknown proto_type: %sN)ri   _PYTHON_TO_CPP_PROTO_TYPE_MAPKeyErrorr   )Z
proto_typer   r   r   ProtoTypeToCppProtoTypeJ  s    
z'FieldDescriptor.ProtoTypeToCppProtoTypei    i)NNTNNN)NNTNNN)5r   r   r	   r
   ZTYPE_DOUBLEZ
TYPE_FLOATZ
TYPE_INT64ZTYPE_UINT64Z
TYPE_INT32ZTYPE_FIXED64ZTYPE_FIXED32Z	TYPE_BOOLZTYPE_STRINGZ
TYPE_GROUPZTYPE_MESSAGEZ
TYPE_BYTESZTYPE_UINT32Z	TYPE_ENUMZTYPE_SFIXED32ZTYPE_SFIXED64ZTYPE_SINT32ZTYPE_SINT64ZMAX_TYPEZCPPTYPE_INT32ZCPPTYPE_INT64ZCPPTYPE_UINT32ZCPPTYPE_UINT64ZCPPTYPE_DOUBLEZCPPTYPE_FLOATZCPPTYPE_BOOLZCPPTYPE_ENUMZCPPTYPE_STRINGZCPPTYPE_MESSAGEZMAX_CPPTYPEr   ZLABEL_OPTIONALZLABEL_REQUIREDZLABEL_REPEATEDZ	MAX_LABELZMAX_FIELD_NUMBERZFIRST_RESERVED_FIELD_NUMBERZLAST_RESERVED_FIELD_NUMBERr   r   ri   r   r   r-   rh   rb   staticmethodr   r   r   r   )r   r   ri     s   3   
	   'ri   c                   sB   e Zd ZdZer ejZd	ddZd
 fdd	Z	 fddZ
  ZS )EnumDescriptora  Descriptor for an enum defined in a .proto file.

  An EnumDescriptor instance has the following attributes:

    name: (str) Name of the enum type.
    full_name: (str) Full name of the type, including package name
      and any enclosing type(s).

    values: (list of EnumValueDescriptors) List of the values
      in this enum.
    values_by_name: (dict str -> EnumValueDescriptor) Same as |values|,
      but indexed by the "name" field of each EnumValueDescriptor.
    values_by_number: (dict int -> EnumValueDescriptor) Same as |values|,
      but indexed by the "number" field of each EnumValueDescriptor.
    containing_type: (Descriptor) Descriptor of the immediate containing
      type of this enum, or None if this is an enum defined at the
      top level in a .proto file.  Set by Descriptor's constructor
      if we're passed into one.
    file: (FileDescriptor) Reference to file descriptor.
    options: (descriptor_pb2.EnumOptions) Enum options message or
      None to use default enum options.
  Nc             C   s   t jj  t jj|S )N)r   rD   rE   rF   ZFindEnumTypeByName)r   r7   r8   rG   rU   r:   r*   r+   r9   r=   r>   r   r   r   r   {  s    
zEnumDescriptor.__new__c                sn   t t| j|d|||||	|
|d	 || _x| jD ]
}| |_q0W tdd |D | _tdd t|D | _dS )zArguments are as described in the attribute description above.

    Note that filename is an obsolete argument, that is not used anymore.
    Please use file.name to access this as an attribute.
    ZEnumOptions)r=   r>   r+   c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   rV   r   r   r   rS     s    z*EnumDescriptor.__init__.<locals>.<genexpr>c             s   s   | ]}|j |fV  qd S )N)rP   )rQ   rV   r   r   r   rS     s    N)	r   r   r-   rU   r   rY   Zvalues_by_namereversedrd   )r   r7   r8   rG   rU   r:   r*   r+   r9   r=   r>   rf   )r   r   r   r-     s    	



zEnumDescriptor.__init__c                s   t t| j| dS )z~Copies this to a descriptor_pb2.EnumDescriptorProto.

    Args:
      proto: An empty descriptor_pb2.EnumDescriptorProto.
    N)r   r   rB   )r   rA   )r   r   r   rB     s    zEnumDescriptor.CopyToProto)NNNNNN)NNNNNN)r   r   r	   r
   r   r   r   r   r   r-   rB   r   r   r   )r   r   r   _  s     
  r   c                   s6   e Zd ZdZer ejZdddZd fdd	Z	  Z
S )	EnumValueDescriptora,  Descriptor for a single value within an enum.

    name: (str) Name of this value.
    index: (int) Dense, 0-indexed index giving the order that this
      value appears textually within its enum in the .proto file.
    number: (int) Actual number assigned to this enum value.
    type: (EnumDescriptor) EnumDescriptor to which this value
      belongs.  Set by EnumDescriptor's constructor if we're
      passed into one.
    options: (descriptor_pb2.EnumValueOptions) Enum value options message or
      None to use default enum value options options.
  Nc             C   s   t jj  d S )N)r   rD   rE   )r   r7   r   rP   r   r*   r+   r   r   r   r     s    
zEnumValueDescriptor.__new__c                s0   t t| j||d || _|| _|| _|| _dS )z>Arguments are as described in the attribute description above.ZEnumValueOptionsN)r   r   r-   r7   r   rP   r   )r   r7   r   rP   r   r*   r+   )r   r   r   r-     s    

zEnumValueDescriptor.__init__)NNN)NNN)r   r   r	   r
   r   r   r   r   r   r-   r   r   r   )r   r   r     s    
	 r   c                   s6   e Zd ZdZer ejZdddZd fdd	Z	  Z
S )	OneofDescriptora	  Descriptor for a oneof field.

    name: (str) Name of the oneof field.
    full_name: (str) Full name of the oneof field, including package name.
    index: (int) 0-based index giving the order of the oneof field inside
      its containing type.
    containing_type: (Descriptor) Descriptor of the protocol message
      type that contains this field.  Set by the Descriptor constructor
      if we're passed into one.
    fields: (list of FieldDescriptor) The list of field descriptors this
      oneof can contain.
  Nc             C   s   t jj  t jj|S )N)r   rD   rE   rF   ZFindOneofByName)r   r7   r8   r   r:   rH   r*   r+   r   r   r   r     s    
zOneofDescriptor.__new__c                s6   t t| j||d || _|| _|| _|| _|| _dS )z>Arguments are as described in the attribute description above.ZOneofOptionsN)r   r   r-   r7   r8   r   r:   rH   )r   r7   r8   r   r:   rH   r*   r+   )r   r   r   r-     s    

zOneofDescriptor.__init__)NN)NN)r   r   r	   r
   r   r   r   r   r   r-   r   r   r   )r   r   r     s    
 r   c                   sJ   e Zd ZdZer ejZdddZd fdd	Z	dd Z
 fd	d
Z  ZS )ServiceDescriptora  Descriptor for a service.

    name: (str) Name of the service.
    full_name: (str) Full name of the service, including package name.
    index: (int) 0-indexed index giving the order that this services
      definition appears withing the .proto file.
    methods: (list of MethodDescriptor) List of methods provided by this
      service.
    methods_by_name: (dict str -> MethodDescriptor) Same MethodDescriptor
      objects as in |methods_by_name|, but indexed by "name" attribute in each
      MethodDescriptor.
    options: (descriptor_pb2.ServiceOptions) Service options message or
      None to use default service options.
    file: (FileDescriptor) Reference to file info.
  Nc
       
      C   s   t jj  t jj|S )N)r   rD   rE   rF   ZFindServiceByName)
r   r7   r8   r   methodsr*   r+   r9   r=   r>   r   r   r   r     s    
zServiceDescriptor.__new__c
                s\   t t| j|d|||d ||	|d	 || _|| _tdd |D | _x| jD ]
}
| |
_qJW d S )NZServiceOptions)r=   r>   r+   c             s   s   | ]}|j |fV  qd S )N)r7   )rQ   mr   r   r   rS     s    z-ServiceDescriptor.__init__.<locals>.<genexpr>)r   r   r-   r   r   rY   methods_by_namecontaining_service)r   r7   r8   r   r   r*   r+   r9   r=   r>   method)r   r   r   r-     s    


zServiceDescriptor.__init__c             C   s   | j j|dS )z>Searches for the specified method, and returns its descriptor.N)r   get)r   r7   r   r   r   FindMethodByName  s    z"ServiceDescriptor.FindMethodByNamec                s   t t| j| dS )zCopies this to a descriptor_pb2.ServiceDescriptorProto.

    Args:
      proto: An empty descriptor_pb2.ServiceDescriptorProto.
    N)r   r   rB   )r   rA   )r   r   r   rB     s    zServiceDescriptor.CopyToProto)NNNNN)NNNNN)r   r   r	   r
   r   r   r   r   r   r-   r   rB   r   r   r   )r   r   r     s     
  r   c                   s6   e Zd ZdZer ejZdddZd fdd	Z	  Z
S )	MethodDescriptora  Descriptor for a method in a service.

  name: (str) Name of the method within the service.
  full_name: (str) Full name of method.
  index: (int) 0-indexed index of the method inside the service.
  containing_service: (ServiceDescriptor) The service that contains this
    method.
  input_type: The descriptor of the message that this method accepts.
  output_type: The descriptor of the message that this method returns.
  options: (descriptor_pb2.MethodOptions) Method options message or
    None to use default method options.
  Nc	       	      C   s   t jj  t jj|S )N)r   rD   rE   rF   r   )	r   r7   r8   r   r   
input_typeoutput_typer*   r+   r   r   r   r   6  s    
zMethodDescriptor.__new__c	       	         s<   t t| j||d || _|| _|| _|| _|| _|| _dS )zThe arguments are as described in the description of MethodDescriptor
    attributes above.

    Note that containing_service may be None, and may be set later if necessary.
    ZMethodOptionsN)	r   r   r-   r7   r8   r   r   r   r   )	r   r7   r8   r   r   r   r   r*   r+   )r   r   r   r-   ;  s    

zMethodDescriptor.__init__)NN)NN)r   r   r	   r
   r   r   r   r   r   r-   r   r   r   )r   r   r   $  s
   
r   c                   sB   e Zd ZdZer$ejZd	 fdd	Zd
 fdd	Z	dd Z
  ZS )FileDescriptora<  Descriptor for a file. Mimics the descriptor_pb2.FileDescriptorProto.

  Note that enum_types_by_name, extensions_by_name, and dependencies
  fields are only set by the message_factory module, and not by the
  generated proto code.

  name: name of file, relative to root of source tree.
  package: name of the package
  syntax: string indicating syntax of the file (can be "proto2" or "proto3")
  serialized_pb: (str) Byte string of serialized
    descriptor_pb2.FileDescriptorProto.
  dependencies: List of other FileDescriptors this FileDescriptor depends on.
  public_dependencies: A list of FileDescriptors, subset of the dependencies
    above, which were declared as "public".
  message_types_by_name: Dict of message names and their descriptors.
  enum_types_by_name: Dict of enum names and their descriptors.
  extensions_by_name: Dict of extension names and their descriptors.
  services_by_name: Dict of services names and their descriptors.
  pool: the DescriptorPool this descriptor belongs to.  When not passed to the
    constructor, the global default pool is used.
  Nc
       
         s$   |rt jj|S tt| j| S d S )N)r   rF   AddSerializedFiler   r   r   )
r   r7   packager*   r+   r@   dependenciespublic_dependenciesrO   pool)r   r   r   r   f  s    zFileDescriptor.__new__c
                s   t t| j||d |	dkr0ddlm}
 |
j }	|	| _i | _|| _|| _	|pNd| _
|| _i | _i | _i | _|ppg | _|pzg | _tj dkr| jdk	rtjj| j dS )zConstructor.ZFileOptionsNr   )descriptor_poolrX   r   )r   r   r-   r0   r   ZDefaultr   message_types_by_namer7   r   rO   r@   r[   r]   Zservices_by_namer   r   r   r   r   rF   r   )r   r7   r   r*   r+   r@   r   r   rO   r   r   )r   r   r   r-   s  s&    





zFileDescriptor.__init__c             C   s   |j | j dS )z~Copies this to a descriptor_pb2.FileDescriptorProto.

    Args:
      proto: An empty descriptor_pb2.FileDescriptorProto.
    N)r?   r@   )r   rA   r   r   r   rB     s    zFileDescriptor.CopyToProto)NNNNNNN)NNNNNNN)r   r   r	   r
   r   r   r   r   r   r-   rB   r   r   r   )r   r   r   L  s      
   r   c             C   s   | j | | S )zParses serialized options.

  This helper function is used to parse serialized options in generated
  proto2 files. It must not be used outside proto2.
  )r?   )messagestringr   r   r   r4     s    
r4   c             C   sr   d}g }x>| D ]6}|dkr$|rDd}q|r<|j |j  d}q||7 }qW |rh|d j rh|d j |d< dj|S )z+Converts name to camel-case and returns it.F_Tr    )appendupperisupperlowerjoin)r7   capitalize_nextresultcr   r   r   r     s    
r   c             C   s   | j dr| jS dS dS )zCReturns the value of the field `options`, or None if it is not set.r*   N)HasFieldr*   )Zdescriptor_protor   r   r   _OptionsOrNone  s    
r   c             C   sN   d}g }x:| D ]2}|dkr d}q|r8|j |j  d}q||7 }qW dj|S )z*Converts name to Json name and returns it.Fr   Tr   )r   r   r   )r7   r   r   r   r   r   r   r     s    
r   r   Tc             C   sv  t j dkr|rddlm} |j }|jj j|  tj	t
jdjd}|rrt
jj|jdd|d |_||_n
|d |_tjj| tjj|j}tr|j| j S | jg}|r|jd| i }	xH| jD ]>}
dj||
jg }t|
j|d	d
d t|
jD }||	|< qW i }x@| jD ]6}dj||jg }t|dj|d|d}|||< qW g }x| jD ]}dj||jg }d	}d	}|j r|j }nd	}|j!dr|j"}dj|||j#dd d	 g }||kr|| }n||	kr|	| }t$|j||j%d |j%|j&t$j'|j&|j(d	||d	dd	t)|d|d}|j*| q^W dj|}t+| j|d	d	|t,|j- t,|	j- g t)| d	S )a  Make a protobuf Descriptor given a DescriptorProto protobuf.

  Handles nested descriptors. Note that this is limited to the scope of defining
  a message inside of another message. Composite fields can currently only be
  resolved if the message is defined in the same scope as the field.

  Args:
    desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
    package: Optional package name for the new message Descriptor (string).
    build_file_if_cpp: Update the C++ descriptor pool if api matches.
                       Set to False on recursion, so no duplicates are created.
    syntax: The syntax/semantics that should be used.  Set to "proto3" to get
            proto3 field presence semantics.
  Returns:
    A Descriptor for protobuf messages.
  r   r   )r/   ry   ascii./z.protoNc             S   s    g | ]\}}t |j||jqS r   )r   r7   rP   )rQ   iiZenum_valr   r   r   
<listcomp>  s   z"MakeDescriptor.<locals>.<listcomp>F)r   build_file_if_cpprO   	type_namerj   )r*   r   r   )r*   ).r   r   r0   r/   ZFileDescriptorProtor   addZ	MergeFrombinasciihexlifyosurandomdecodepathr   replacer7   r   r   rF   AddZFindFileByNamer   r   insertr`   r   	enumeraterf   r_   MakeDescriptorr^   r   r   r   rfindri   rP   r   r   r   r   r   rC   listrU   )Z
desc_protor   r   rO   r/   Zfile_descriptor_protoZ
proto_namer   Zfull_message_namerJ   Z
enum_protor8   Z	enum_descrI   Znested_protoZnested_descrH   Zfield_protor   r   Zfull_type_namer^   Z	desc_namer   r   r   r     sz    
 




r   )r   TN)&r
   
__author__r   sixZgoogle.protobuf.internalr   r   r   r   r   Zgoogle.protobuf.pyextr   r1   	Exceptionr   r   r   r   r   r   r   r   with_metaclassr%   r6   rC   ri   r   r   r   r   r   r   r4   r   r   r   r   r   r   r   r   <module>!   sF   	G9 + XB($7(L
 