
Q,Qc           @   s   d  Z  d Z d d l m Z d d l m Z d d l m Z e j Z	 e j
   d k r e j   d k r d d l m Z e j Z e j Z q d d l m Z e j Z e j Z n" d d	 l m Z e j Z e j Z d
 e f d     YZ d   Z d S(   s  Contains a metaclass and helper functions used to create
protocol message classes from Descriptor objects at runtime.

Recall that a metaclass is the "type" of a class.
(A class is to a metaclass what an instance is to a class.)

In this case, we use the GeneratedProtocolMessageType metaclass
to inject all the useful functionality into the classes
output by the protocol compiler at compile-time.

The upshot of all this is that the real implementation
details for ALL pure-Python protocol buffers are *here in
this file*.
s#   robinson@google.com (Will Robinson)i(   t   api_implementation(   t
   descriptor(   t   messaget   cppi   (   t   cpp_message(   t   python_messaget   GeneratedProtocolMessageTypec           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s:  Metaclass for protocol message classes created at runtime from Descriptors.

  We add implementations for all methods described in the Message class.  We
  also create properties to allow getting/setting all fields in the protocol
  message.  Finally, we create slots to prevent users from accidentally
  "setting" nonexistent fields in the protocol message, which then wouldn't get
  serialized / deserialized properly.

  The protocol compiler currently uses this metaclass to create protocol
  message classes at runtime.  Clients can also manually create their own
  classes at runtime, as in this example:

  mydescriptor = Descriptor(.....)
  class MyProtoClass(Message):
    __metaclass__ = GeneratedProtocolMessageType
    DESCRIPTOR = mydescriptor
  myproto_instance = MyProtoClass()
  myproto.foo_field = 23
  ...
  t
   DESCRIPTORc         C   sZ   | t  j } t | | |  } t t  |   } | j |  | | |  } t | d |  | S(   s  Custom allocation for runtime-generated class types.

    We override __new__ because this is apparently the only place
    where we can meaningfully set __slots__ on the class we're creating(?).
    (The interplay between metaclasses and slots is not very well-documented).

    Args:
      name: Name of the class (ignored, but required by the
        metaclass protocol).
      bases: Base classes of the class we're constructing.
        (Should be message.Message).  We ignore this field, but
        it's required by the metaclass protocol
      dictionary: The class dictionary of the class we're
        constructing.  dictionary[_DESCRIPTOR_KEY] must contain
        a Descriptor object describing this protocol message
        type.

    Returns:
      Newly-allocated class.
    t   _concrete_class(   R   t   _DESCRIPTOR_KEYt   _NewMessaget   supert   __new__t   setattr(   t   clst   namet   basest
   dictionaryR   t
   superclasst	   new_class(    (    s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyR   d   s    c         C   s@   | t  j } t | |   t t  |   } | j | | |  d S(   s  Here we perform the majority of our work on the class.
    We add enum getters, an __init__ method, implementations
    of all Message methods, and properties for all fields
    in the protocol type.

    Args:
      name: Name of the class (ignored, but required by the
        metaclass protocol).
      bases: Base classes of the class we're constructing.
        (Should be message.Message).  We ignore this field, but
        it's required by the metaclass protocol
      dictionary: The class dictionary of the class we're
        constructing.  dictionary[_DESCRIPTOR_KEY] must contain
        a Descriptor object describing this protocol message
        type.
    N(   R   R	   t   _InitMessageR   t   __init__(   R   R   R   R   R   R   (    (    s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyR      s    (   t   __name__t
   __module__t   __doc__R	   R   R   (    (    (    s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyR   I   s   	c            s9   d t  j f   f d     Y} |   } | j |  | S(   s   Generate a new Message instance from this Descriptor and a byte string.

  Args:
    descriptor: Protobuf Descriptor object
    byte_str: Serialized protocol buffer byte string

  Returns:
    Newly created protobuf Message object.
  t   _ResultClassc              s   e  Z e Z   Z RS(    (   R   R   R   t   __metaclass__R   (    (   R   (    s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyR      s   (   R   t   Messaget   ParseFromString(   R   t   byte_strR   t   new_msg(    (   R   s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyt   ParseMessage   s    	N(   R   t
   __author__t   google.protobuf.internalR    t   google.protobufR   t   descriptor_modR   t   FieldDescriptort   _FieldDescriptort   Typet   Versiont   google.protobuf.internal.cppR   t
   NewMessageR
   t   InitMessageR   R   t   typeR   R   (    (    (    s>   /usr/lib/python2.7/site-packages/google/protobuf/reflection.pyt   <module>.   s$   					O