o
    ˷e                     @   s^   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	 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TupleUnionc                   @   s:  e Zd ZdZdeeef ddfddZede	fdd	Z
d
ededdfddZdefddZd
edefddZd
ededdfddZd
eddfddZd
eddfddZdeeeef  fddZd
edee fddZd
edee ddfddZd
edefdd Zd!edefd"d#Zd!edd fd$d%Zdefd&d'ZdS )(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.
    argsreturnNc                 G   s&   t  | _|D ]
\}}| || qd S N)r   	_metadataadd)selfr
   md_keymd_value r   I/var/www/ideatree/venv/lib/python3.10/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valuec                 C   s"   | j |g  | j | | d S r   )r   
setdefaultappend)r   r   r   r   r   r   r   /   s   zMetadata.addc                 C   s   t tt| j S )zReturn 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__c              
   C   s>   z| j | d W S  ttfy } ztd||d}~ww )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>   || 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__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   | j |g S )zFor compatibility with other Metadata abstraction objects (like in Java),
        this would return all items under the desired <key>.
        )r   getr.   r   r   r   r(   \   s   zMetadata.get_allr   c                 C   s   || j |< d S r   r-   )r   r   r   r   r   r   set_allb   s   zMetadata.set_allc                 C   s
   || j v S r   r-   r.   r   r   r   __contains__e   s   
zMetadata.__contains__otherc                 C   s2   t || jr| j|jkS t |trt| |kS tS r   )
isinstance	__class__r   tupleNotImplementedr   r5   r   r   r   __eq__h   s
   
zMetadata.__eq__c                 C   s>   t || jrtt| t|  S t |trtt| |  S tS r   )r6   r7   r	   r8   r9   r:   r   r   r   __add__o   s
   
zMetadata.__add__c                 C   s   t | }d| jj|S )Nz
{0}({1!r}))r8   r$   r7   __name__)r   viewr   r   r   __repr__v   s   zMetadata.__repr__)r=   
__module____qualname____doc__r   MetadataKeyMetadataValuer   classmethodr8   r   r   intr    r&   r*   r,   r/   r   r1   r   r(   r3   boolr4   r   r;   r<   strr?   r   r   r   r   r	      s$    	
r	   N)rB   collectionsr   r   typingr   r   r   r   r   rH   rC   bytesrD   Mappingr	   r   r   r   r   <module>   s   