U
    ËZj‘  ã                   @   sb   d Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	m
Z
 eZe
eef ZG dd„ dejƒZdS )zCImplementation of the metadata abstraction for gRPC Asyncio Python.é    )ÚOrderedDict)Úabc)ÚAnyÚIteratorÚListÚOptionalÚTupleÚUnionc                   @   sf  e Zd ZdZeeef ddœdd„Zee	dœdd„ƒZ
eedd	œd
d„Zedœdd„Zeedœdd„Zeedd	œdd„Zeddœdd„Zeddœdd„Zeeeef  dœdd„Zejdœdd„Zejdœdd„Zejdœdd„Zd1eeee d œd!d"„Zeee dœd#d$„Zeee dd%œd&d'„Zee dœd(d)„Z!e"e d*œd+d,„Z#e"d d*œd-d.„Z$e%dœd/d0„Z&dS )2ÚMetadataaÍ  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.
    N)ÚargsÚreturnc                 G   s&   t ƒ | _|D ]\}}|  ||¡ qd S ©N)r   Ú	_metadataÚadd)Úselfr   Zmd_keyZmd_value© r   úU/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/grpc/aio/_metadata.pyÚ__init__$   s    zMetadata.__init__)Úraw_metadatac                 C   s   |r| |Ž S | ƒ S r   r   )Úclsr   r   r   r   Ú
from_tuple)   s    zMetadata.from_tuple)ÚkeyÚvaluer   c                 C   s"   | j  |g ¡ | j |  |¡ d S r   )r   Ú
setdefaultÚappend)r   r   r   r   r   r   r   /   s    zMetadata.add)r   c                 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.
        )ÚsumÚmapÚlenr   Úvalues©r   r   r   r   Ú__len__3   s    zMetadata.__len__)r   r   c              
   C   sL   z| j | d W S  ttfk
rF } ztd |¡ƒ|‚W 5 d}~X Y nX dS )zmWhen calling <metadata>[<key>], the first element of all those
        mapped for <key> is returned.
        r   z{0!r}N)r   Ú
ValueErrorÚ
IndexErrorÚKeyErrorÚformat)r   r   Úer   r   r   Ú__getitem__9   s    zMetadata.__getitem__c                 C   s<   || kr|g| j |< n"|  |¡}|f|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__B   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>.r'   N)r(   r#   Úreprr   )r   r   r)   r   r   r   Ú__delitem__L   s    
zMetadata.__delitem__c                 C   s   | j |= dS )zDelete all mappings for <key>.N©r   ©r   r   r   r   r   Ú
delete_allS   s    zMetadata.delete_allc                 c   s,   | j  ¡ D ]\}}|D ]}||fV  qq
d S r   )r   Úitems)r   r   r   r   r   r   r   Ú__iter__W   s    zMetadata.__iter__c                 C   s
   t  | ¡S r   )r   ÚKeysViewr   r   r   r   Úkeys\   s    zMetadata.keysc                 C   s
   t  | ¡S r   )r   Ú
ValuesViewr   r   r   r   r   _   s    zMetadata.valuesc                 C   s
   t  | ¡S r   )r   Ú	ItemsViewr   r   r   r   r0   b   s    zMetadata.items)r   Údefaultr   c                 C   s(   z
| | W S  t k
r"   | Y S X d S r   )r#   )r   r   r6   r   r   r   Úgete   s    
zMetadata.getc                 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>.
        )r   r7   r.   r   r   r   r(   m   s    zMetadata.get_all)r   r   r   c                 C   s   || j |< d S r   r-   )r   r   r   r   r   r   Úset_alls   s    zMetadata.set_allc                 C   s
   || j kS r   r-   r.   r   r   r   Ú__contains__v   s    zMetadata.__contains__)Úotherr   c                 C   s2   t || jƒr| j|jkS t |tƒr.t| ƒ|kS tS r   )Ú
isinstanceÚ	__class__r   ÚtupleÚNotImplemented©r   r:   r   r   r   Ú__eq__y   s
    
zMetadata.__eq__c                 C   s>   t || jƒr tt| ƒt|ƒ Ž S t |tƒr:tt| ƒ| Ž S tS r   )r;   r<   r
   r=   r>   r?   r   r   r   Ú__add__€   s
    
zMetadata.__add__c                 C   s   t | ƒ}d | jj|¡S )Nz
{0}({1!r}))r=   r$   r<   Ú__name__)r   Úviewr   r   r   Ú__repr__‡   s    zMetadata.__repr__)N)'rB   Ú
__module__Ú__qualname__Ú__doc__r   ÚMetadataKeyÚMetadataValuer   Úclassmethodr=   r   r   Úintr    r&   r*   r,   r/   r   r1   r   r2   r3   r4   r   r5   r0   r   r7   r   r(   r8   Úboolr9   r   r@   rA   ÚstrrD   r   r   r   r   r
      s4   	
 ÿ þr
   N)rG   Úcollectionsr   r   Útypingr   r   r   r   r   r	   rM   rH   ÚbytesrI   Ú
Collectionr
   r   r   r   r   Ú<module>   s    