U
    S³dŠ#  ã                   @   s–   d Z dZddlZddlmZ ddlmZ ddlmZ e ¡ dkrNddlm	Z
 ndd	lmZ
 e
jZd
d„ Zdd„ Zdd„ ZG dd„ deƒZddd„ZdS )ao  Provides a factory class for generating dynamic messages.

The easiest way to use this class is if you have access to the FileDescriptor
protos containing the messages you want to create you can just do the following:

message_classes = message_factory.GetMessages(iterable_of_file_descriptors)
my_proto_instance = message_classes['some.proto.package.MessageName']()
z"matthewtoia@google.com (Matt Toia)é    N)Úapi_implementation)Údescriptor_pool)ÚmessageÚpython)Úpython_message)Úcpp_messagec                 C   s   t | ddƒ}|r|S t| ƒS )a7  Obtains 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.
  Z_concrete_classN)ÚgetattrÚ_InternalCreateMessageClass)Ú
descriptorZconcrete_class© r   úC/tmp/pip-unpacked-wheel-1ori_g8k/google/protobuf/message_factory.pyÚGetMessageClass:   s    r   c                 C   sn   i }| D ]`}|  |¡}|j ¡ D ]}t|ƒ||j< q |j ¡ D ](}t|jƒ}| |¡ |jr>t|jƒ q>q|S )ak  Gets all the messages from specified files.

  This will find and resolve dependencies, failing if the descriptor
  pool cannot satisfy them.

  Args:
    files: The file names to extract messages from.
    pool: The descriptor pool to find the files including the dependent
      files.

  Returns:
    A dictionary mapping proto names to the message classes.
  )	ZFindFileByNameZmessage_types_by_nameÚvaluesr   Z	full_nameZextensions_by_nameÚcontaining_typeÚRegisterExtensionÚmessage_type)ÚfilesÚpoolÚresultÚ	file_nameZ	file_descÚdescÚ	extensionÚextended_classr   r   r   ÚGetMessageClassesForFilesL   s    


r   c                 C   sn   | j }t|tjf| ddœƒ}| jD ]}|jr"t|jƒ q"|jjD ](}t|j	ƒ}| 
|¡ |jr@t|jƒ q@|S )z·Builds a proto2 message class based on the passed in descriptor.

  Args:
    descriptor: The descriptor to build from.

  Returns:
    A class describing the passed in descriptor.
  N)Ú
DESCRIPTORÚ
__module__)ÚnameÚ _GENERATED_PROTOCOL_MESSAGE_TYPEr   ÚMessageÚfieldsr   r   r   Ú
extensionsr   r   )r
   Zdescriptor_nameZresult_classÚfieldr   r   r   r   r   r	   t   s"    	ýý


r	   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚMessageFactoryz@Factory for creating Proto2 messages from descriptors in a pool.Nc                 C   s   |p
t  ¡ | _dS )zInitializes a new factory.N)r   ÚDescriptorPoolr   )Úselfr   r   r   r   Ú__init__–   s    zMessageFactory.__init__c                 C   s   t |ƒS )aE  Obtains 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   r   r   ÚGetPrototypeš   s    zMessageFactory.GetPrototypec                 C   s   t |ƒS )a,  Builds a proto2 message class based on the passed in descriptor.

    Don't call this function directly, it always creates a new class. Call
    GetMessageClass() instead.

    Args:
      descriptor: The descriptor to build from.

    Returns:
      A class describing the passed in descriptor.
    )r	   r&   r   r   r   ÚCreatePrototype¬   s    zMessageFactory.CreatePrototypec                 C   s   t || jƒS )a£  Gets all the messages from a specified file.

    This will find and resolve dependencies, failing if the descriptor
    pool cannot satisfy them.

    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.
    )r   r   )r$   r   r   r   r   ÚGetMessages¾   s    zMessageFactory.GetMessages)N)Ú__name__r   Ú__qualname__Ú__doc__r%   r'   r(   r)   r   r   r   r   r"   “   s
   
r"   c                    sT   |p
t  ¡ ‰dd„ | D ƒ‰‡ ‡‡fdd„‰ ˆr@ˆ ˆ ¡ d ƒ q*tdd„ | D ƒˆƒS )a”  Builds a dictionary of all the messages available in a set of files.

  Args:
    file_protos: Iterable of FileDescriptorProto to build messages out of.
    pool: The descriptor pool to add the file protos.

  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.
  c                 S   s   i | ]}|j |“qS r   ©r   ©Ú.0Ú
file_protor   r   r   Ú
<dictcomp>ã   s      zGetMessages.<locals>.<dictcomp>c                    s0   | j D ]}|ˆkrˆ ˆ |¡ƒ qˆ | ¡ d S )N)Ú
dependencyÚpopZAdd)r0   r2   ©Ú_AddFileZdes_poolZfile_by_namer   r   r5   ä   s    
zGetMessages.<locals>._AddFileé   c                 S   s   g | ]
}|j ‘qS r   r-   r.   r   r   r   Ú
<listcomp>í   s     zGetMessages.<locals>.<listcomp>)r   r#   Úpopitemr   )Zfile_protosr   r   r4   r   r)   Ô   s     ÿr)   )N)r,   Ú
__author__ÚwarningsZgoogle.protobuf.internalr   Zgoogle.protobufr   r   ÚTyper   Zmessage_implZgoogle.protobuf.pyextr   ZGeneratedProtocolMessageTyper   r   r   r	   Úobjectr"   r)   r   r   r   r   Ú<module>   s   	(A