a
    њh                     @  s   d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
 g dZG dd deZG dd deeef ZG d	d
 d
e	Ze
eeeef eeeef  ef ZdS )    )annotations)IterableIteratorMappingMutableMapping)AnyProtocolUnion)HeadersHeadersLikeMultipleValuesErrorc                      s&   e Zd ZdZdd fddZ  ZS )r   zP
    Exception raised when :class:`Headers` has multiple values for a key.

    strreturnc                   s&   t | jdkrt| jd S t  S N   r   )lenargsreprsuper__str__self	__class__ T/wd/webapps/venvs/v2025_4um/lib/python3.9/site-packages/websockets/datastructures.pyr      s    zMultipleValuesError.__str__)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s   r   c                      s  e Zd ZdZddgZdddddd	Zdd
ddZdd
ddZd d
ddZdd
ddZ	dddddZ
dd
ddZdd
ddZddddd Zdddd!d"d#Zdddd$d%Zd&dd'd(d)Zdd
d*d+Zdddd fd,d-Zdd.dd/d0Zd1d
d2d3Z  ZS )4r
   a  
    Efficient data structure for manipulating HTTP headers.

    A :class:`list` of ``(name, values)`` is inefficient for lookups.

    A :class:`dict` doesn't suffice because header names are case-insensitive
    and multiple occurrences of headers with the same name are possible.

    :class:`Headers` stores HTTP headers in a hybrid data structure to provide
    efficient insertions and lookups while preserving the original data.

    In order to account for multiple values with minimal hassle,
    :class:`Headers` follows this logic:

    - When getting a header with ``headers[name]``:
        - if there's no value, :exc:`KeyError` is raised;
        - if there's exactly one value, it's returned;
        - if there's more than one value, :exc:`MultipleValuesError` is raised.

    - When setting a header with ``headers[name] = value``, the value is
      appended to the list of values for that header.

    - When deleting a header with ``del headers[name]``, all values for that
      header are removed (this is slow).

    Other methods for manipulating headers are consistent with this logic.

    As long as no header occurs multiple times, :class:`Headers` behaves like
    :class:`dict`, except keys are lower-cased to provide case-insensitivity.

    Two methods support manipulating multiple values explicitly:

    - :meth:`get_all` returns a list of all values for a header;
    - :meth:`raw_items` returns an iterator of ``(name, values)`` pairs.

    _dict_listr   r   None)r   kwargsr   c                 O  s    i | _ g | _| j|i | d S N)r"   r#   updater   r   r%   r   r   r   __init__D   s    zHeaders.__init__r   c                 C  s   d dd | jD d S )N c                 s  s"   | ]\}}| d | dV  qdS )z: 
Nr   ).0keyvaluer   r   r   	<genexpr>J       z"Headers.__str__.<locals>.<genexpr>r+   )joinr#   r   r   r   r   r   I   s    zHeaders.__str__c                 C  s   | j j d| jdS )N())r   r   r#   r   r   r   r   __repr__L   s    zHeaders.__repr__c                 C  s$   |   }| j |_| j |_|S r&   )r   r"   copyr#   )r   r5   r   r   r   r5   O   s    zHeaders.copybytesc                 C  s   t |  S r&   )r   encoder   r   r   r   	serializeU   s    zHeaders.serializeobjectboolr-   r   c                 C  s   t |to| | jv S r&   )
isinstancer   lowerr"   r   r-   r   r   r   __contains__[   s    zHeaders.__contains__zIterator[str]c                 C  s
   t | jS r&   )iterr"   r   r   r   r   __iter__^   s    zHeaders.__iter__intc                 C  s
   t | jS r&   )r   r"   r   r   r   r   __len__a   s    zHeaders.__len__c                 C  s.   | j |  }t|dkr"|d S t|d S r   )r"   r=   r   r   r   r-   r.   r   r   r   __getitem__f   s    zHeaders.__getitem__)r-   r.   r   c                 C  s,   | j | g | | j||f d S r&   )r"   
setdefaultr=   appendr#   rD   r   r   r   __setitem__m   s    zHeaders.__setitem__c                   s.   |   | j   fdd| jD | _d S )Nc                   s$   g | ]\}}|   kr||fqS r   )r=   )r,   kv	key_lowerr   r   
<listcomp>u   r0   z'Headers.__delitem__.<locals>.<listcomp>)r=   r"   __delitem__r#   r>   r   rK   r   rN   q   s    zHeaders.__delitem__r   )otherr   c                 C  s   t |tstS | j|jkS r&   )r<   r
   NotImplementedr"   )r   rO   r   r   r   __eq__w   s    
zHeaders.__eq__c                 C  s   i | _ g | _dS )z&
        Remove all headers.

        N)r"   r#   r   r   r   r   clear|   s    zHeaders.clearc                   s(   t dd |D }t j|i | dS )zT
        Update from a :class:`Headers` instance and/or keyword arguments.

        c                 s  s$   | ]}t |tr| n|V  qd S r&   )r<   r
   	raw_items)r,   argr   r   r   r/      s   z!Headers.update.<locals>.<genexpr>N)tupler   r'   r(   r   r   r   r'      s    zHeaders.updatez	list[str]c                 C  s   | j | g S )z|
        Return the (possibly empty) list of all values for a header.

        Args:
            key: Header name.

        )r"   getr=   r>   r   r   r   get_all   s    zHeaders.get_allzIterator[tuple[str, str]]c                 C  s
   t | jS )zO
        Return an iterator of all values as ``(name, value)`` pairs.

        )r@   r#   r   r   r   r   rS      s    zHeaders.raw_items)r   r   r   r    	__slots__r)   r   r4   r5   r8   r?   rA   rC   rE   rH   rN   rQ   rR   r'   rW   rS   r!   r   r   r   r   r
      s$   %
r
   c                   @  s.   e Zd ZdZddddZddddd	Zd
S )SupportsKeysAndGetItemz_
    Dict-like types with ``keys() -> str`` and ``__getitem__(key: str) -> str`` methods.

    zIterable[str]r   c                 C  s   d S r&   r   r   r   r   r   keys   r0   zSupportsKeysAndGetItem.keysr   r;   c                 C  s   d S r&   r   r>   r   r   r   rE      r0   z"SupportsKeysAndGetItem.__getitem__N)r   r   r   r    rZ   rE   r   r   r   r   rY      s   rY   N)
__future__r   collections.abcr   r   r   r   typingr   r   r	   __all__LookupErrorr   r   r
   rY   rU   r   r   r   r   r   <module>   s    	
