
    g5                     ~    d Z ddlZddlmZ ddlmZ ddlmZ  G d d      Z e ej                               Z	d	 Zy)
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                   X    e Zd ZdZi ZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)SymbolDatabasez'A database of Python generated symbols.Nc                 >    |xs t        j                         | _        y)z!Initializes a new SymbolDatabase.N)r   DescriptorPoolpool)selfr
   s     T/var/www/openai/venv/lib/python3.12/site-packages/google/protobuf/symbol_database.py__init__zSymbolDatabase.__init__2   s    8668DI    c                 V    t        j                  d       t        j                  |      S )NzSymbolDatabase.GetPrototype() is deprecated. Please use message_factory.GetMessageClass() instead. SymbolDatabase.GetPrototype() will be removed soon.)warningswarnr   GetMessageClassr   
descriptors     r   GetPrototypezSymbolDatabase.GetPrototype6   s(    MM H I **:66r   c                 V    t        j                  d       t        j                  |      S )NzDirectly call CreatePrototype() is wrong. Please use message_factory.GetMessageClass() instead. SymbolDatabase.CreatePrototype() will be removed soon.)r   r   r   _InternalCreateMessageClassr   s     r   CreatePrototypezSymbolDatabase.CreatePrototype<   s(    MM K L 66zBBr   c                 l    t        j                  d       t        j                  || j                        S )NzSymbolDatabase.GetMessages() is deprecated. Please use message_factory.GetMessageClassedForFiles() instead. SymbolDatabase.GetMessages() will be removed soon.)r   r   r   GetMessageClassedForFilesr
   )r   filess     r   GetMessageszSymbolDatabase.GetMessagesB   s.    MM G H 44UDIIFFr   c                 ^    |j                   }|| 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.
    )
DESCRIPTOR_classesRegisterMessageDescriptor)r   messagedescs      r   RegisterMessagezSymbolDatabase.RegisterMessageH   s0     D!DMM$""4(Nr   c                 j    t        j                         dk(  r| j                  j                  |       yy)zRegisters the given message descriptor in the local database.

    Args:
      message_descriptor (Descriptor): the message descriptor to add.
    pythonN)r   Typer
   _AddDescriptor)r   message_descriptors     r   r    z(SymbolDatabase.RegisterMessageDescriptorZ   s-      H,
ii12 -r   c                 j    t        j                         dk(  r| j                  j                  |       |S )zRegisters 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
   _AddEnumDescriptor)r   enum_descriptors     r   RegisterEnumDescriptorz%SymbolDatabase.RegisterEnumDescriptord   s-      H,
ii""?3r   c                 j    t        j                         dk(  r| j                  j                  |       yy)zRegisters the given service descriptor in the local database.

    Args:
      service_descriptor (ServiceDescriptor): the service descriptor to
        register.
    r%   N)r   r&   r
   _AddServiceDescriptor)r   service_descriptors     r   RegisterServiceDescriptorz(SymbolDatabase.RegisterServiceDescriptorr   s-      H,
ii%%&89 -r   c                 j    t        j                         dk(  r| j                  j                  |       yy)zRegisters the given file descriptor in the local database.

    Args:
      file_descriptor (FileDescriptor): The file descriptor to register.
    r%   N)r   r&   r
   _InternalAddFileDescriptor)r   file_descriptors     r   RegisterFileDescriptorz%SymbolDatabase.RegisterFileDescriptor}   s,      H,
ii**?; -r   c                 R    | j                   | 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
   FindMessageTypeByName)r   symbols     r   	GetSymbolzSymbolDatabase.GetSymbol   s"      ==88@AAr   c                 
   fdi }|D ]f  }| j                   j                  |      }|j                  j                         D ],  } |      D ]  }	 | j                  |   ||j
                  <   ! . h |S # t        $ r Y 3w xY w)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   Z   K   |  | j                   D ]  } |      D ]  }|   yw)zCWalk a message Descriptor and recursively yields all message names.N)nested_types)r"   msg_descnested_desc_GetAllMessagess      r   r>   z3SymbolDatabase.GetMessages.<locals>._GetAllMessages   s1     j''(*84K
 5 (s   (+)r
   FindFileByNamemessage_types_by_namevaluesr   	full_nameKeyError)r   r   result	file_name	file_descr<   r"   r>   s          @r   r   zSymbolDatabase.GetMessages   s    & F	))**95i55<<>(#H-D%)]]4%8F4>>" . ?  M  s   A66	B	B	)N)__name__
__module____qualname____doc__r   r   r   r   r   r#   r    r,   r0   r4   r8    r   r   r   r   ,   sF    / (97CG$3	:<B$$r   r   )r
   c                      t         S )z#Returns the default SymbolDatabase.)_DEFAULTrK   r   r   DefaultrN      s    	/r   )
rJ   r   google.protobuf.internalr   google.protobufr   r   r   rN   rM   rK   r   r   <module>rQ      sB   :  7 + +Q Qh 66689r   