o
    /i0                  	   @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ e	 Z
ejejejejejejejejejf	Zdd Zdd	 Zd
d Zd"ddZe
fddZdd Zdd Zdd Zdd Zd#ddZdd Zdd Z d d! Z!dS )$zHelpers for :mod:`protobuf`.    N)field_mask_pb2)message)wrappers_pb2c                 C   sR   |  }t t| ddr| |}n|}||s'td|  d|jj d|S )a~  Converts an ``Any`` protobuf to the specified message type.

    Args:
        pb_type (type): the type of the message that any_pb stores an instance
            of.
        any_pb (google.protobuf.any_pb2.Any): the object to be converted.

    Returns:
        pb_type: An instance of the pb_type message.

    Raises:
        TypeError: if the message could not be converted.
    pbNzCould not convert `z9` with underlying type `google.protobuf.any_pb2.Any` to ``)callablegetattrr   Unpack	TypeErrorTypeName
DESCRIPTOR	full_name)pb_typeany_pbmsgmsg_pb r   T/var/www/passon-env/lib/python3.10/site-packages/google/api_core/protobuf_helpers.pyfrom_any_pb)   s   
r   c                  K   sH   | sdS dd |   D }t|dkr"tdjdt|  ddS )zRaise ValueError if more than one keyword argument is not ``None``.

    Args:
        kwargs (dict): The keyword arguments sent to the function.

    Raises:
        ValueError: If more than one entry in ``kwargs`` is not ``None``.
    Nc                 S   s   g | ]}|d ur|qS Nr   ).0valr   r   r   
<listcomp>V   s    zcheck_oneof.<locals>.<listcomp>   z#Only one of {fields} should be set.z, )fields)valueslen
ValueErrorformatjoinsortedkeys)kwargs	not_nonesr   r   r   check_oneofI   s   
r$   c                 C   sB   t  }t| D ]}t| |}t|rt|tjr|||< q|S )a  Discovers all protobuf Message classes in a given import module.

    Args:
        module (module): A Python module; :func:`dir` will be run against this
            module to find Message subclasses.

    Returns:
        dict[str, google.protobuf.message.Message]: A dictionary with the
            Message class names as keys, and the Message subclasses themselves
            as values.
    )	collectionsOrderedDictdirr   inspectisclass
issubclassr   Message)moduleanswername	candidater   r   r   get_messages_   s   
r0   .c                 C   s(   |  |d}t|dkr|S |d dfS )ab  Resolve a potentially nested key.

    If the key contains the ``separator`` (e.g. ``.``) then the key will be
    split on the first instance of the subkey::

       >>> _resolve_subkeys('a.b.c')
       ('a', 'b.c')
       >>> _resolve_subkeys('d|e|f', separator='|')
       ('d', 'e|f')

    If not, the subkey will be :data:`None`::

        >>> _resolve_subkeys('foo')
        ('foo', None)

    Args:
        key (str): A string that may or may not contain the separator.
        separator (str): The namespace separator. Defaults to `.`.

    Returns:
        Tuple[str, str]: The key and subkey(s).
    r   r   N)splitr   )key	separatorpartsr   r   r   _resolve_subkeyss   s   r6   c                 C   s   t |\}}t| tjrt| ||}nt| tjjr!| ||}n	t	d
t| |tu r2t||durA||urAt|||dS |S )a  Retrieve a key's value from a protobuf Message or dictionary.

    Args:
        mdg_or_dict (Union[~google.protobuf.message.Message, Mapping]): the
            object.
        key (str): The key to retrieve from the object.
        default (Any): If the key is not present on the object, and a default
            is set, returns that default instead. A type-appropriate falsy
            default is generally recommended, as protobuf messages almost
            always have default values for unset values and it is not always
            possible to tell the difference between a falsy value and an
            unset one. If no default is set then :class:`KeyError` will be
            raised if the key is not present in the object.

    Returns:
        Any: The return value from the underlying Message or dict.

    Raises:
        KeyError: If the key is not found. Note that, for unset values,
            messages and dictionaries may not have consistent behavior.
        TypeError: If ``msg_or_dict`` is not a Message or Mapping.
    z4get() expected a dict or protobuf message, got {!r}.Ndefault)r6   
isinstancer   r+   r   r%   abcMappinggetr
   r   type	_SENTINELKeyError)msg_or_dictr3   r8   subkeyr-   r   r   r   r<      s   r<   c                 C   s   t |tjjtfr=t| |rt| |  t| |s|D ]}t |tjjr1t| |jdi | qt| |	|g qdS t |tjjrX|
 D ]\}}tt| ||| qHdS t |tjrht| || dS t| || dS )z!Set helper for protobuf Messages.Nr   )r9   r%   r:   MutableSequencetupler   popr;   addextenditemssetr   r+   CopyFromsetattr)r   r3   valueitemitem_key
item_valuer   r   r   _set_field_on_message   s    

rO   c                 C   s   t | tjjtjfstdt| t	|\}}|dur5t | tjjr*| 
|i  tt| ||| dS t | tjjrB|| |< dS t| || dS )aC  Set a key's value on a protobuf Message or dictionary.

    Args:
        msg_or_dict (Union[~google.protobuf.message.Message, Mapping]): the
            object.
        key (str): The key to set.
        value (Any): The value to set.

    Raises:
        TypeError: If ``msg_or_dict`` is not a Message or dictionary.
    z4set() expected a dict or protobuf message, got {!r}.N)r9   r%   r:   MutableMappingr   r+   r
   r   r=   r6   
setdefaultrH   r<   rO   )r@   r3   rK   basekeyrA   r   r   r   rH      s   rH   c                 C   s"   t | |ddst| || dS dS )a~  Set the key on a protobuf Message or dictionary to a given value if the
    current value is falsy.

    Because protobuf Messages do not distinguish between unset values and
    falsy ones particularly well (by design), this method treats any falsy
    value (e.g. 0, empty list) as a target to be overwritten, on both Messages
    and dictionaries.

    Args:
        msg_or_dict (Union[~google.protobuf.message.Message, Mapping]): the
            object.
        key (str): The key on the object in question.
        value (Any): The value to set.

    Raises:
        TypeError: If ``msg_or_dict`` is not a Message or dictionary.
    Nr7   )r<   rH   )r@   r3   rK   r   r   r   rQ     s   rQ   c                 C   s   | du r|du rt  S | du r|durt|} |   |du r.| dur.t| }|  t| t|sAtdt| t|t jt	| |dS )a  Create a field mask by comparing two messages.

    Args:
        original (~google.protobuf.message.Message): the original message.
            If set to None, this field will be interpreted as an empty
            message.
        modified (~google.protobuf.message.Message): the modified message.
            If set to None, this field will be interpreted as an empty
            message.

    Returns:
        google.protobuf.field_mask_pb2.FieldMask: field mask that contains
        the list of field names that have different values between the two
        messages. If the messages are equivalent, then the field mask is empty.

    Raises:
        ValueError: If the ``original`` or ``modified`` are not the same type.
    Nz`expected that both original and modified should be of the same type, received "{!r}" and "{!r}".)paths)
r   	FieldMaskcopydeepcopyClearr9   r=   r   r   _field_mask_helper)originalmodifiedr   r   r   
field_mask  s    

r[    c                 C   s   g }| j jD ]H}t||}t| |}t||}t|st|rE||krDt|s+t|r1|| q| s;|| q|t	||| q||krN|| q|S r   )
r   fields_by_name	_get_pathr   _is_message_is_wrapperappend
ListFieldsrF   rX   )rY   rZ   currentr-   r.   
field_pathoriginal_valmodified_valr   r   r   rX   E  s&   




rX   c                 C   s   | d}| s	|S d| |f S )N_z%s.%s)rstrip)rc   r.   r   r   r   r^   a  s   
r^   c                 C   s   t | tjS r   )r9   r   r+   rK   r   r   r   r_   n     r_   c                 C   s   t | tv S r   )r=   _WRAPPER_TYPESri   r   r   r   r`   r  rj   r`   )r1   )r\   )"__doc__r%   collections.abcrU   r(   google.protobufr   r   r   objectr>   	BoolValue
BytesValueDoubleValue
FloatValue
Int32Value
Int64ValueStringValueUInt32ValueUInt64Valuerk   r   r$   r0   r6   r<   rO   rH   rQ   r[   rX   r^   r_   r`   r   r   r   r   <module>   s@    
3%
)