ó
ÉÈ÷Xc           @   sU   d  Z  d d l m Z d d l m Z d e f d „  ƒ  YZ e j ƒ  Z d „  Z	 d S(   sÆ  A database of Python protocol buffer generated symbols.

SymbolDatabase makes it easy to create new instances of a registered type, given
only the type's protocol buffer symbol name. Once all symbols are registered,
they can be accessed using either the MessageFactory interface which
SymbolDatabase exposes, or the DescriptorPool interface of the underlying
pool.

Example usage:

  db = symbol_database.SymbolDatabase()

  # Register symbols of interest, from one or multiple files.
  db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR)
  db.RegisterMessage(my_proto_pb2.MyMessage)
  db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR)

  # The database can be used as a MessageFactory, to generate types based on
  # their name:
  types = db.GetMessages(['my_proto.proto'])
  my_message_instance = types['MyMessage']()

  # The database's underlying descriptor pool can be queried, so it's not
  # necessary to know a type's filename to be able to generate it:
  filename = db.pool.FindFileContainingSymbol('MyMessage')
  my_message_instance = db.GetMessages([filename])['MyMessage']()

  # This functionality is also provided directly via a convenience method:
  my_message_instance = db.GetSymbol('MyMessage')()
iÿÿÿÿ(   t
   descriptor(   t   descriptor_poolt   SymbolDatabasec           B   s€   e  Z d  Z e j r0 d „  Z e d „  ƒ Z n e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z RS(   sÛ   A database of Python generated symbols.

  SymbolDatabase also models message_factory.MessageFactory.

  The symbol database can be used to keep a global registry of all protocol
  buffer types used within a program.
  c         C   s   t  d ƒ ‚ d  S(   Ns-   Instances of SymbolDatabase cannot be created(   t	   TypeError(   t   cls(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   __new__O   s    c         C   s4   t  j |  ƒ } i  | _ i  | _ t j j | _ | S(   N(   t   objectR   t   _symbolst   _symbols_by_filet   _descriptort   _messaget   default_poolt   pool(   R   t   self(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   _CreateDefaultDatabaseR   s
    		c         C   s   |  ƒ  S(   N(    (   R   (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyR   a   s    c         C   s%   i  |  _  i  |  _ t j ƒ  |  _ d S(   s   Constructor.N(   R   R   R   t   DescriptorPoolR   (   R   (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   __init__e   s    		c         C   sr   | j  } | |  j | j <| j j |  j k rD i  |  j | j j <n  | |  j | j j | j <|  j j | ƒ | S(   s¥   Registers the given message type in the local database.

    Args:
      message: a message.Message, to be registered.

    Returns:
      The provided message.
    (   t
   DESCRIPTORR   t	   full_namet   filet   nameR   R   t   AddDescriptor(   R   t   messaget   desc(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   RegisterMessagel   s    
	c         C   s   |  j  j | ƒ | S(   s«   Registers the given enum descriptor in the local database.

    Args:
      enum_descriptor: a descriptor.EnumDescriptor.

    Returns:
      The provided descriptor.
    (   R   t   AddEnumDescriptor(   R   t   enum_descriptor(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   RegisterEnumDescriptor~   s    	c         C   s   |  j  j | ƒ d S(   s«   Registers the given file descriptor in the local database.

    Args:
      file_descriptor: a descriptor.FileDescriptor.

    Returns:
      The provided descriptor.
    N(   R   t   AddFileDescriptor(   R   t   file_descriptor(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   RegisterFileDescriptorŠ   s    	c         C   s   |  j  | S(   sx  Tries to find a symbol in the local database.

    Currently, this method only returns message.Message instances, however, if
    may be extended in future to support other symbol types.

    Args:
      symbol: A str, a protocol buffer symbol.

    Returns:
      A Python class corresponding to the symbol.

    Raises:
      KeyError: if the symbol could not be found.
    (   R   (   R   t   symbol(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt	   GetSymbol•   s    c         C   s   |  j  | j ƒ S(   sD  Builds a proto2 message class based on the passed in descriptor.

    Passing a descriptor with a fully qualified name matching a previous
    invocation will cause the same class to be returned.

    Args:
      descriptor: The descriptor to build from.

    Returns:
      A class describing the passed in descriptor.
    (   R    R   (   R   R    (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   GetPrototype§   s    c         C   s/   i  } x" | D] } | j  |  j | ƒ q W| S(   sæ  Gets all the messages from a specified file.

    This will find and resolve dependencies, failing if they are not registered
    in the symbol database.


    Args:
      files: The file names to extract messages from.

    Returns:
      A dictionary mapping proto names to the message classes. This will include
      any dependent messages as well as any messages defined in the same file as
      a specified message.

    Raises:
      KeyError: if a file could not be found.
    (   t   updateR   (   R   t   filest   resultt   f(    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   GetMessages¶   s    (   t   __name__t
   __module__t   __doc__R	   t   _USE_C_DESCRIPTORSR   t   classmethodR   R   R   R   R   R    R!   R&   (    (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyR   C   s   								c           C   s   t  S(   s#   Returns the default SymbolDatabase.(   t   _DEFAULT(    (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   DefaultÑ   s    N(
   R)   t   google.protobufR    R	   R   R   R   R   R,   R-   (    (    (    sa   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/symbol_database.pyt   <module><   s
   ‹