U
    S³dŽ  ã                   @   sX   d Z ddlZddlmZ ddlmZ ddlmZ G dd„ dƒZee ¡ dZ	d	d
„ ZdS )aS  A database of Python protocol buffer generated symbols.

SymbolDatabase is the MessageFactory for messages generated at compile time,
and makes it easy to create new instances of a registered type, given only the
type's protocol buffer symbol name.

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')()
é    N)Úapi_implementation)Údescriptor_pool)Úmessage_factoryc                   @   sn   e Zd ZdZi Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd
„ ZdS )ÚSymbolDatabasez'A database of Python generated symbols.Nc                 C   s   |p
t  ¡ | _dS )z!Initializes a new SymbolDatabase.N)r   ZDescriptorPoolÚpool)Úselfr   © r   úC/tmp/pip-unpacked-wheel-1ori_g8k/google/protobuf/symbol_database.pyÚ__init__I   s    zSymbolDatabase.__init__c                 C   s   t  d¡ t |¡S )Nz–SymbolDatabase.GetPrototype() is deprecated. Please use message_factory.GetMessageClass() instead. SymbolDatabase.GetPrototype() will be removed soon.)ÚwarningsÚwarnr   ZGetMessageClass©r   Z
descriptorr   r   r	   ÚGetPrototypeM   s    
zSymbolDatabase.GetPrototypec                 C   s   t  d¡ t |¡S )Nz–Directly call CreatePrototype() is wrong. Please use message_factory.GetMessageClass() instead. SymbolDatabase.CreatePrototype() will be removed soon.)r   r   r   Z_InternalCreateMessageClassr   r   r   r	   ÚCreatePrototypeS   s    
zSymbolDatabase.CreatePrototypec                 C   s   t  d¡ t || j¡S )NzžSymbolDatabase.GetMessages() is deprecated. Please use message_factory.GetMessageClassedForFiles() instead. SymbolDatabase.GetMessages() will be removed soon.)r   r   r   ZGetMessageClassedForFilesr   )r   Úfilesr   r   r	   ÚGetMessagesY   s    
zSymbolDatabase.GetMessagesc                 C   s   |j }|| j|< |  |¡ |S )a@  Registers the given message type in the local database.

    Calls to GetSymbol() and GetMessages() will return messages registered here.

    Args:
      message: A :class:`google.protobuf.message.Message` subclass (or
        instance); its descriptor will be registered.

    Returns:
      The provided message.
    )Z
DESCRIPTORÚ_classesÚRegisterMessageDescriptor)r   ÚmessageÚdescr   r   r	   ÚRegisterMessage_   s    

zSymbolDatabase.RegisterMessagec                 C   s   t  ¡ dkr| j |¡ dS )z“Registers the given message descriptor in the local database.

    Args:
      message_descriptor (Descriptor): the message descriptor to add.
    ÚpythonN)r   ÚTyper   Z_AddDescriptor)r   Zmessage_descriptorr   r   r	   r   q   s    z(SymbolDatabase.RegisterMessageDescriptorc                 C   s   t  ¡ dkr| j |¡ |S )zÐRegisters the given enum descriptor in the local database.

    Args:
      enum_descriptor (EnumDescriptor): The enum descriptor to register.

    Returns:
      EnumDescriptor: The provided descriptor.
    r   )r   r   r   Z_AddEnumDescriptor)r   Zenum_descriptorr   r   r	   ÚRegisterEnumDescriptor{   s    	z%SymbolDatabase.RegisterEnumDescriptorc                 C   s   t  ¡ dkr| j |¡ dS )z§Registers the given service descriptor in the local database.

    Args:
      service_descriptor (ServiceDescriptor): the service descriptor to
        register.
    r   N)r   r   r   Z_AddServiceDescriptor)r   Zservice_descriptorr   r   r	   ÚRegisterServiceDescriptor‰   s    z(SymbolDatabase.RegisterServiceDescriptorc                 C   s   t  ¡ dkr| j |¡ dS )z“Registers the given file descriptor in the local database.

    Args:
      file_descriptor (FileDescriptor): The file descriptor to register.
    r   N)r   r   r   Z_InternalAddFileDescriptor)r   Zfile_descriptorr   r   r	   ÚRegisterFileDescriptor”   s    z%SymbolDatabase.RegisterFileDescriptorc                 C   s   | j | j |¡ S )aw  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 (str): a protocol buffer symbol.

    Returns:
      A Python class corresponding to the symbol.

    Raises:
      KeyError: if the symbol could not be found.
    )r   r   ZFindMessageTypeByName)r   Úsymbolr   r   r	   Ú	GetSymbolž   s    zSymbolDatabase.GetSymbolc                    sr   ‡ fdd„‰ i }|D ]X}| j  |¡}|j ¡ D ]<}ˆ |ƒD ].}z| j| ||j< W q: tk
rf   Y q:X q:q.q|S )a  Gets all registered messages from a specified file.

    Only messages already created and registered will be returned; (this is the
    case for imported _pb2 modules)
    But unlike MessageFactory, this version also returns already defined nested
    messages, but does not register any message extensions.

    Args:
      files (list[str]): The file names to extract messages from.

    Returns:
      A dictionary mapping proto names to the message classes.

    Raises:
      KeyError: if a file could not be found.
    c                 3   s*   | V  | j D ]}ˆ |ƒD ]
}|V  qqdS )zCWalk a message Descriptor and recursively yields all message names.N)Znested_types)r   Úmsg_descZnested_desc©Ú_GetAllMessagesr   r	   r    Ã   s    
z3SymbolDatabase.GetMessages.<locals>._GetAllMessages)r   ZFindFileByNameZmessage_types_by_nameÚvaluesr   Z	full_nameÚKeyError)r   r   ÚresultÚ	file_nameZ	file_descr   r   r   r   r	   r   °   s    )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   C   s   


r   )r   c                   C   s   t S )z#Returns the default SymbolDatabase.)Ú_DEFAULTr   r   r   r	   ÚDefaultÚ   s    r*   )
r(   r   Zgoogle.protobuf.internalr   Zgoogle.protobufr   r   r   r*   r)   r   r   r   r	   Ú<module>   s    