a
    Sic:  ã                   @   sz   d Z dZddlmZ ddlmZ ddlmZ e ¡ dkrFddlm	Z
 nddlmZ
 e
jZG d	d
„ d
eƒZeƒ Z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)é    )Úapi_implementation)Údescriptor_pool)ÚmessageÚcpp)Úcpp_message)Úpython_messagec                   @   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  ¡ | _i | _dS )zInitializes a new factory.N)r   ÚDescriptorPoolÚpoolÚ_classes)Úselfr
   © r   ú[/var/www/html/django/DPS/env/lib/python3.9/site-packages/google/protobuf/message_factory.pyÚ__init__;   s    zMessageFactory.__init__c                 C   s,   || j vr"|  |¡}|| j |< |S | j | 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   ÚCreatePrototype)r   Ú
descriptorÚresult_classr   r   r   ÚGetPrototypeB   s
    


zMessageFactory.GetPrototypec                 C   sŠ   |j }t|tjf|ddœƒ}| |_|| j|< |jD ]}|jr2|  |j¡ q2|j	j
D ]2}|j| jvrn|  |j¡ | j|j }| |¡ qR|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
    GetPrototype() instead. This method is meant to be overridden in subblasses
    to perform additional operations on the newly constructed class.

    Args:
      descriptor: The descriptor to build from.

    Returns:
      A class describing the passed in descriptor.
    N)Ú
DESCRIPTORÚ
__module__)ÚnameÚ _GENERATED_PROTOCOL_MESSAGE_TYPEr   ÚMessageÚ_FACTORYr   ÚfieldsÚmessage_typer   r   Ú
extensionsÚcontaining_typeÚRegisterExtension)r   r   Zdescriptor_namer   ÚfieldÚ	extensionÚextended_classr   r   r   r   W   s&    ýý

zMessageFactory.CreatePrototypec                 C   s|   i }|D ]n}| j  |¡}|j ¡ D ]}|  |¡||j< q"|j ¡ D ]2}|j| jvr^|  |j¡ | j|j }| 	|¡ qBq|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
   ÚFindFileByNameÚmessage_types_by_nameÚvaluesr   Ú	full_nameÚextensions_by_namer   r   r   )r   ÚfilesÚresultÚ	file_nameZ	file_descÚdescr    r!   r   r   r   ÚGetMessages{   s    zMessageFactory.GetMessages)N)Ú__name__r   Ú__qualname__Ú__doc__r   r   r   r+   r   r   r   r   r   8   s
   
$r   c                    sF   dd„ | D ƒ‰‡ ‡fdd„‰ ˆr2ˆ ˆ  ¡ 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.

  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>°   ó    zGetMessages.<locals>.<dictcomp>c                    s2   | j D ]}|ˆv rˆ ˆ |¡ƒ qtj | ¡ d S )N)Ú
dependencyÚpopr   r
   ÚAdd)r2   r5   ©Ú_AddFileZfile_by_namer   r   r9   ±   s    
zGetMessages.<locals>._AddFileé   c                 S   s   g | ]
}|j ‘qS r   r/   r0   r   r   r   Ú
<listcomp>¹   r4   zGetMessages.<locals>.<listcomp>)Úpopitemr   r+   )Zfile_protosr   r8   r   r+   £   s
    r+   N)r.   Ú
__author__Úgoogle.protobuf.internalr   Úgoogle.protobufr   r   ÚTypeÚgoogle.protobuf.pyextr   Zmessage_implr   ÚGeneratedProtocolMessageTyper   Úobjectr   r   r+   r   r   r   r   Ú<module>   s   	h