o
    =j„  ã                   @  sØ   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZmZmZmZmZ ddlmZ eZeeef Zeeef Zedee f ZG dd„ deƒZdS )zCImplementation of the metadata abstraction for gRPC Asyncio Python.é    )Úannotations)ÚOrderedDict)Ú
Collection)Ú	ItemsView)ÚIterable)ÚIterator)ÚKeysView)ÚSequence)Ú
ValuesView)ÚAnyÚListÚOptionalÚTupleÚUnion)ÚSelfÚMetadatac                   @  sä   e Zd ZdZdFdd„ZedGd
d„ƒZedHdd„ƒZdIdd„ZdJdd„Z	dKdd„Z
dIdd„ZdLdd„ZdLdd „ZdMd"d#„ZdNd%d&„ZdOd(d)„ZdPd+d,„Z	-dQdRd0d1„ZdSd3d4„ZdTd6d7„ZdUd9d:„ZdVd=d>„ZdWdAdB„ZdXdDdE„Zd-S )Yr   aÍ  Metadata abstraction for the asynchronous calls and interceptors.

    The metadata is a mapping from str -> List[str]

    Traits
        * Multiple entries are allowed for the same key
        * The order of the values by key is preserved
        * Getting by an element by key, retrieves the first mapped value
        * Supports an immutable view of the data
        * Allows partial mutation on the data without recreating the new object from scratch.
    ÚargsÚMetadatumTypeÚreturnÚNonec                 G  s&   t ƒ | _|D ]
\}}|  ||¡ qd S ©N)r   Ú	_metadataÚadd)Úselfr   Zmd_keyZmd_value© r   ú\C:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\grpc/aio/_metadata.pyÚ__init__0   s   ÿzMetadata.__init__Úraw_metadataÚtuplec                 C  s   |r| |Ž S | ƒ S r   r   ©Úclsr   r   r   r   Ú
from_tuple5   s   zMetadata.from_tupleú*Union[None, Self, Iterable[MetadatumType]]r   c                 C  s.   |d u rt ƒ S t|| ƒr|S |r| |Ž S | ƒ S r   )r   Ú
isinstancer   r   r   r   Ú_create=   s   
zMetadata._createÚkeyÚMetadataKeyÚvalueÚMetadataValuec                 C  s"   | j  |g ¡ | j |  |¡ d S r   )r   Ú
setdefaultÚappend)r   r%   r'   r   r   r   r   L   s   zMetadata.addÚintc                 C  s   t tt| j ¡ ƒƒS )z€Return the total number of elements that there are in the metadata,
        including multiple values for the same key.
        N)ÚsumÚmapÚlenr   Úvalues©r   r   r   r   Ú__len__P   s   zMetadata.__len__c              
   C  s>   z| j | d W S  ttfy } z|›}t|ƒ|‚d}~ww )zmWhen calling <metadata>[<key>], the first element of all those
        mapped for <key> is returned.
        r   N)r   Ú
ValueErrorÚ
IndexErrorÚKeyError)r   r%   ÚeÚ	error_msgr   r   r   Ú__getitem__V   s   
€þzMetadata.__getitem__c                 C  s>   || vr|g| j |< dS |  |¡}|g|dd… ¢| j |< dS )z_Calling metadata[<key>] = <value>
        Maps <value> to the first instance of <key>.
        é   N)r   Úget_all)r   r%   r'   Úcurrent_valuesr   r   r   Ú__setitem__`   s   
zMetadata.__setitem__c                 C  s0   |   |¡}|stt|ƒƒ‚|dd… | j|< dS )z<``del metadata[<key>]`` deletes the first mapping for <key>.r8   N)r9   r4   Úreprr   )r   r%   r:   r   r   r   Ú__delitem__j   s   
zMetadata.__delitem__c                 C  s   | j |= dS )zDelete all mappings for <key>.N©r   ©r   r%   r   r   r   Ú
delete_allq   s   zMetadata.delete_allú+Iterator[Tuple[MetadataKey, MetadataValue]]c                 c  s.   � | j  ¡ D ]\}}|D ]}||fV  qqd S r   )r   Úitems)r   r%   r/   r'   r   r   r   Ú__iter__u   s   €ÿÿzMetadata.__iter__r   c                 C  ó
   t | jƒS r   )r   r   r0   r   r   r   Úkeysz   ó   
zMetadata.keysr
   c                 C  rD   r   )r
   r   r0   r   r   r   r/   }   rF   zMetadata.valuesr   c                 C  rD   r   )r   r   r0   r   r   r   rB   €   rF   zMetadata.itemsNÚdefaultúOptional[MetadataValue]c                 C  s"   z| | W S  t y   | Y S w r   )r4   )r   r%   rG   r   r   r   Úgetƒ   s
   
ÿzMetadata.getúList[MetadataValue]c                 C  s   | j  |g ¡S )z�For compatibility with other Metadata abstraction objects (like in Java),
        this would return all items under the desired <key>.
        N)r   rI   r?   r   r   r   r9   ‹   s   zMetadata.get_allr/   c                 C  s   || j |< d S r   r>   )r   r%   r/   r   r   r   Úset_all‘   s   zMetadata.set_allÚboolc                 C  s
   || j v S r   r>   r?   r   r   r   Ú__contains__”   rF   zMetadata.__contains__ÚotherÚobjectc                 C  s2   t || jƒr| j|jkS t |tƒrt| ƒ|kS tS r   )r#   Ú	__class__r   r   ÚNotImplemented©r   rN   r   r   r   Ú__eq__—   s
   
zMetadata.__eq__r   ú
'Metadata'c                 C  s>   t || jƒrtt| ƒt|ƒ Ž S t |tƒrtt| ƒ| Ž S tS r   )r#   rP   r   r   rQ   rR   r   r   r   Ú__add__ž   s
   
zMetadata.__add__Ústrc                 C  s   t | ƒ}d | jj|¡S )Nz
{0}({1!r}))r   ÚformatrP   Ú__name__)r   Úviewr   r   r   Ú__repr__¥   s   zMetadata.__repr__)r   r   r   r   )r   r   )r   r"   r   r   )r%   r&   r'   r(   r   r   )r   r+   )r%   r&   r   r(   )r%   r&   r   r   )r   rA   )r   r   )r   r
   )r   r   r   )r%   r&   rG   rH   r   rH   )r%   r&   r   rJ   )r%   r&   r/   rJ   r   r   )r%   r&   r   rL   )rN   rO   r   rL   )rN   r   r   rT   )r   rV   )rX   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr!   r$   r   r1   r7   r;   r=   r@   rC   rE   r/   rB   rI   r9   rK   rM   rS   rU   rZ   r   r   r   r   r   #   s2    












ÿ




N)r]   Ú
__future__r   Úcollectionsr   Úcollections.abcr   r   r   r   r   r	   r
   Útypingr   r   r   r   r   Ztyping_extensionsr   rV   r&   Úbytesr(   r   ZMetadataTyper   r   r   r   r   Ú<module>   s"   