a
    -=ic;{                    @   sH  d dl 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	 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 Zdd ZG dd dZG dd deeZG dd dZG dd deZdd ZG dd deZG dd deZG dd deeZG dd  d eZG d!d" d"Z G d#d$ d$eZ!d%d& Z"d'd( Z#G d)d* d*Z$G d+d, d,Z%G d-d. d.e%e$Z&G d/d0 d0eeZ'G d1d2 d2eZ(G d3d4 d4eeZ)G d5d6 d6eeZ*G d7d8 d8ee!Z+G d9d: d:eZ,e-d;Z.d<d= Z/G d>d? d?e,Z0e-d@Z1dAdB Z2G dCdD dDe,Z3G dEdF dFe,Z4dGdH Z5G dIdJ dJeeZ6G dKdL dLee6Z7G dMdN dNe6Z8dOdP Z9G dQdR dReeZ:G dSdT dTeeZ;G dUdV dVeZ<G dWdX dXeZ=G dYdZ dZZ>G d[d\ d\Z?d]d^ Z@G d_d` d`ZAG dadb dbeeZBdldcddZCdmdedfZDG dgdh dheeZEG didj djZFddklmGZG dS )n    N)
Collection)
MutableSetdeepcopy)BytesIO)repeat)fspath   )
exceptions)_missingc                 C   s   t t| jdd S )Nz objects are immutable	TypeErrortype__name__self r   h/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/werkzeug/datastructures.pyis_immutable   s    r   c                 c   sx   t | tr| jddE dH  nVt | trj|  D ]6\}}t |ttfr\|D ]}||fV  qJq0||fV  q0n
| E dH  dS )zyIterates over the items of a mapping yielding keys and values
    without dropping any from more complex structures.
    TmultiN)
isinstance	MultiDictitemsdicttuplelist)mappingkeyvaluevr   r   r   iter_multi_items   s    

r!   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdddZdd Zd ddZdS )!ImmutableListMixinzOMakes a :class:`list` immutable.

    .. versionadded:: 0.5

    :private:
    Nc                 C   s&   | j d ur| j S tt|  }| _ |S N)_hash_cachehashr   r   rvr   r   r   __hash__0   s    
zImmutableListMixin.__hash__c                 C   s   t | t| ffS r#   )r   r   r   protocolr   r   r   __reduce_ex__6   s    z ImmutableListMixin.__reduce_ex__c                 C   s   t |  d S r#   r   r   r   r   r   r   __delitem__9   s    zImmutableListMixin.__delitem__c                 C   s   t |  d S r#   r,   r   otherr   r   r   __iadd__<   s    zImmutableListMixin.__iadd__c                 C   s   t |  d S r#   r,   r/   r   r   r   __imul__?   s    zImmutableListMixin.__imul__c                 C   s   t |  d S r#   r,   r   r   r   r   r   r   __setitem__B   s    zImmutableListMixin.__setitem__c                 C   s   t |  d S r#   r,   r   itemr   r   r   appendE   s    zImmutableListMixin.appendc                 C   s   t |  d S r#   r,   r5   r   r   r   removeH   s    zImmutableListMixin.removec                 C   s   t |  d S r#   r,   )r   iterabler   r   r   extendK   s    zImmutableListMixin.extendc                 C   s   t |  d S r#   r,   r   posr   r   r   r   insertN   s    zImmutableListMixin.insertc                 C   s   t |  d S r#   r,   )r   indexr   r   r   popQ   s    zImmutableListMixin.popc                 C   s   t |  d S r#   r,   r   r   r   r   reverseT   s    zImmutableListMixin.reverseFc                 C   s   t |  d S r#   r,   )r   r   rA   r   r   r   sortW   s    zImmutableListMixin.sort)r>   )NF)r   
__module____qualname____doc__r$   r(   r+   r.   r1   r2   r4   r7   r8   r:   r=   r@   rA   rB   r   r   r   r   r"   &   s   
r"   c                   @   s   e Zd ZdZdd ZdS )ImmutableListzJAn immutable :class:`list`.

    .. versionadded:: 0.5

    :private:
    c                 C   s   t | j dt|  dS N())r   r   r   __repr__r   r   r   r   rJ   c   s    zImmutableList.__repr__N)r   rC   rD   rE   rJ   r   r   r   r   rF   [   s   rF   c                       s~   e Zd ZdZdZed fdd	Zdd Zdd Zd	d
 Z	dddZ
dd ZdddZdd Zdd Zdd Zdd Z  ZS )ImmutableDictMixinzOMakes a :class:`dict` immutable.

    .. versionadded:: 0.5

    :private:
    Nc                    s$   t  | }|t|t| |S r#   )super__new____init__zipr   )clskeysr   instance	__class__r   r   fromkeysq   s    zImmutableDictMixin.fromkeysc                 C   s   t | t| ffS r#   )r   r   r)   r   r   r   r+   w   s    z ImmutableDictMixin.__reduce_ex__c                 C   s   |   S r#   r   r   r   r   r   _iter_hashitemsz   s    z"ImmutableDictMixin._iter_hashitemsc                 C   s*   | j d ur| j S tt|   }| _ |S r#   )r$   r%   	frozensetrW   r&   r   r   r   r(   }   s    
zImmutableDictMixin.__hash__c                 C   s   t |  d S r#   r,   r   r   defaultr   r   r   
setdefault   s    zImmutableDictMixin.setdefaultc                 O   s   t |  d S r#   r,   r   argskwargsr   r   r   update   s    zImmutableDictMixin.updatec                 C   s   t |  d S r#   r,   rY   r   r   r   r@      s    zImmutableDictMixin.popc                 C   s   t |  d S r#   r,   r   r   r   r   popitem   s    zImmutableDictMixin.popitemc                 C   s   t |  d S r#   r,   r3   r   r   r   r4      s    zImmutableDictMixin.__setitem__c                 C   s   t |  d S r#   r,   r-   r   r   r   r.      s    zImmutableDictMixin.__delitem__c                 C   s   t |  d S r#   r,   r   r   r   r   clear   s    zImmutableDictMixin.clear)N)N)N)r   rC   rD   rE   r$   classmethodrU   r+   rW   r(   r[   r_   r@   r`   r4   r.   ra   __classcell__r   r   rS   r   rK   g   s   

rK   c                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dS )ImmutableMultiDictMixinzTMakes a :class:`MultiDict` immutable.

    .. versionadded:: 0.5

    :private:
    c                 C   s   t | t| jddffS NTr   r   r   r   r)   r   r   r   r+      s    z%ImmutableMultiDictMixin.__reduce_ex__c                 C   s   | j ddS re   rV   r   r   r   r   rW      s    z'ImmutableMultiDictMixin._iter_hashitemsc                 C   s   t |  d S r#   r,   r3   r   r   r   add   s    zImmutableMultiDictMixin.addc                 C   s   t |  d S r#   r,   r   r   r   r   popitemlist   s    z#ImmutableMultiDictMixin.popitemlistc                 C   s   t |  d S r#   r,   r-   r   r   r   poplist   s    zImmutableMultiDictMixin.poplistc                 C   s   t |  d S r#   r,   r   r   new_listr   r   r   setlist   s    zImmutableMultiDictMixin.setlistNc                 C   s   t |  d S r#   r,   r   r   Zdefault_listr   r   r   setlistdefault   s    z&ImmutableMultiDictMixin.setlistdefault)N)r   rC   rD   rE   r+   rW   rg   rh   ri   rl   rn   r   r   r   r   rd      s   rd   c                    s    fdd} |_ |S )Nc                    s2   t tt|  |i |}| jd ur.| |  |S r#   )getattrrL   UpdateDictMixin	on_update)r   r]   kwr'   namer   r   oncall   s    

z_calls_update.<locals>.oncall)r   )rt   ru   r   rs   r   _calls_update   s    rv   c                       s^   e Zd ZdZdZd fdd	Zef fdd	ZedZ	edZ
ed	Zed
ZedZ  ZS )rp   zbMakes dicts call `self.on_update` on modifications.

    .. versionadded:: 0.5

    :private:
    Nc                    s2   || v}t  ||}|r.| jd ur.| |  |S r#   )rL   r[   rq   r   r   rZ   modifiedr'   rS   r   r   r[      s
    
zUpdateDictMixin.setdefaultc                    sH   || v }|t u rt |}nt ||}|rD| jd urD| |  |S r#   )r   rL   r@   rq   rw   rS   r   r   r@      s    
zUpdateDictMixin.popr4   r.   ra   r`   r_   )N)r   rC   rD   rE   rq   r[   r   r@   rv   r4   r.   ra   r`   r_   rc   r   r   rS   r   rp      s   
rp   c                   @   s   e Zd ZdZdddZdS )TypeConversionDictzWorks like a regular dict but the :meth:`get` method can perform
    type conversions.  :class:`MultiDict` and :class:`CombinedMultiDict`
    are subclasses of this class and provide the same feature.

    .. versionadded:: 0.5
    Nc                 C   sT   z| | }W n t y"   | Y S 0 |durPz||}W n tyN   |}Y n0 |S )a  Return the default value if the requested data doesn't exist.
        If `type` is provided and is a callable it should convert the value,
        return it or raise a :exc:`ValueError` if that is not possible.  In
        this case the function will return the default as if the value was not
        found:

        >>> d = TypeConversionDict(foo='42', bar='blub')
        >>> d.get('foo', type=int)
        42
        >>> d.get('bar', -1, type=int)
        -1

        :param key: The key to be looked up.
        :param default: The default value to be returned if the key can't
                        be looked up.  If not further specified `None` is
                        returned.
        :param type: A callable that is used to cast the value in the
                     :class:`MultiDict`.  If a :exc:`ValueError` is raised
                     by this callable the default value is returned.
        N)KeyError
ValueError)r   r   rZ   r   r'   r   r   r   get   s    

zTypeConversionDict.get)NN)r   rC   rD   rE   r|   r   r   r   r   ry      s   ry   c                   @   s    e Zd ZdZdd Zdd ZdS )ImmutableTypeConversionDictzpWorks like a :class:`TypeConversionDict` but does not support
    modifications.

    .. versionadded:: 0.5
    c                 C   s   t | S zReturn a shallow mutable copy of this object.  Keep in mind that
        the standard library's :func:`copy` function is a no-op for this class
        like for any other python immutable type (eg: :class:`tuple`).
        )ry   r   r   r   r   copy  s    z ImmutableTypeConversionDict.copyc                 C   s   | S r#   r   r   r   r   r   __copy__  s    z$ImmutableTypeConversionDict.__copy__Nr   rC   rD   rE   r   r   r   r   r   r   r}     s   r}   c                   @   s   e Zd ZdZd9ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
d:ddZdd Zd;ddZd<ddZd=ddZdd Zdd Zd d! Zd"d# Zd>d$d%Zd?d'd(Zd)d* Zefd+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 ZdS )@r   a  A :class:`MultiDict` is a dictionary subclass customized to deal with
    multiple values for the same key which is for example used by the parsing
    functions in the wrappers.  This is necessary because some HTML form
    elements pass multiple values for the same key.

    :class:`MultiDict` implements all standard dictionary methods.
    Internally, it saves all values for a key as a list, but the standard dict
    access methods will only return the first value for a key. If you want to
    gain access to the other values, too, you have to use the `list` methods as
    explained below.

    Basic Usage:

    >>> d = MultiDict([('a', 'b'), ('a', 'c')])
    >>> d
    MultiDict([('a', 'b'), ('a', 'c')])
    >>> d['a']
    'b'
    >>> d.getlist('a')
    ['b', 'c']
    >>> 'a' in d
    True

    It behaves like a normal dict thus all dict functions will only return the
    first value when multiple values for one key are found.

    From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
    subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
    render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
    exceptions.

    A :class:`MultiDict` can be constructed from an iterable of
    ``(key, value)`` tuples, a dict, a :class:`MultiDict` or from Werkzeug 0.2
    onwards some keyword parameters.

    :param mapping: the initial value for the :class:`MultiDict`.  Either a
                    regular dict, an iterable of ``(key, value)`` tuples
                    or `None`.
    Nc                 C   s   t |tr&t| dd | D  nt |tri }| D ]<\}}t |ttfrjt|dkr`q<t|}n|g}|||< q<t| | n4i }|pdD ]\}}|	|g 
| qt| | d S )Nc                 s   s"   | ]\}}||d d  fV  qd S r#   r   ).0klr   r   r   	<genexpr>L      z%MultiDict.__init__.<locals>.<genexpr>r   r   )r   r   r   rN   listsr   r   r   lenr[   r7   )r   r   tmpr   r   r   r   r   rN   J  s     



zMultiDict.__init__c                 C   s   t |  S r#   )r   r   r   r   r   r   __getstate__^  s    zMultiDict.__getstate__c                 C   s   t |  t | | d S r#   )r   ra   r_   r   r   r   r   r   __setstate__a  s    
zMultiDict.__setstate__c                 C   s
   t | S r#   )r   __iter__r   r   r   r   r   e  s    zMultiDict.__iter__c                 C   s6   || v r(t | |}t|dkr(|d S t|dS )zReturn the first data value for this key;
        raises KeyError if not found.

        :param key: The key to be looked up.
        :raise KeyError: if the key does not exist.
        r   N)r   __getitem__r   r
   BadRequestKeyError)r   r   lstr   r   r   r   k  s
    zMultiDict.__getitem__c                 C   s   t | ||g dS )zLike :meth:`add` but removes an existing key first.

        :param key: the key for the value.
        :param value: the value to set.
        N)r   r4   r3   r   r   r   r4   y  s    zMultiDict.__setitem__c                 C   s   t | |g | dS )zAdds a new value for the key.

        .. versionadded:: 0.6

        :param key: the key for the value.
        :param value: the value to add.
        N)r   r[   r7   r3   r   r   r   rg     s    zMultiDict.addc              	   C   sp   zt | |}W n ty&   g  Y S 0 |du r8t|S g }|D ]*}z||| W q@ tyh   Y q@0 q@|S )a]  Return the list of items for a given key. If that key is not in the
        `MultiDict`, the return value will be an empty list.  Just like `get`,
        `getlist` accepts a `type` parameter.  All items will be converted
        with the callable defined there.

        :param key: The key to be looked up.
        :param type: A callable that is used to cast the value in the
                     :class:`MultiDict`.  If a :exc:`ValueError` is raised
                     by this callable the value will be removed from the list.
        :return: a :class:`list` of all the values for the key.
        N)r   r   rz   r   r7   r{   r   r   r   r'   resultr6   r   r   r   getlist  s    
zMultiDict.getlistc                 C   s   t | |t| dS )a  Remove the old values for a key and add new ones.  Note that the list
        you pass the values in will be shallow-copied before it is inserted in
        the dictionary.

        >>> d = MultiDict()
        >>> d.setlist('foo', ['1', '2'])
        >>> d['foo']
        '1'
        >>> d.getlist('foo')
        ['1', '2']

        :param key: The key for which the values are set.
        :param new_list: An iterable with the new values for the key.  Old values
                         are removed first.
        N)r   r4   r   rj   r   r   r   rl     s    zMultiDict.setlistc                 C   s   || vr|| |< n| | }|S )aG  Returns the value for the key if it is in the dict, otherwise it
        returns `default` and sets that value for `key`.

        :param key: The key to be looked up.
        :param default: The default value to be returned if the key is not
                        in the dict.  If not further specified it's `None`.
        r   rY   r   r   r   r[     s    
zMultiDict.setdefaultc                 C   s4   || vr$t |pd}t| || nt| |}|S )a  Like `setdefault` but sets multiple values.  The list returned
        is not a copy, but the list that is actually used internally.  This
        means that you can put new values into the dict by appending items
        to the list:

        >>> d = MultiDict({"foo": 1})
        >>> d.setlistdefault("foo").extend([2, 3])
        >>> d.getlist("foo")
        [1, 2, 3]

        :param key: The key to be looked up.
        :param default_list: An iterable of default values.  It is either copied
                             (in case it was a list) or converted into a list
                             before returned.
        :return: a :class:`list`
        r   )r   r   r4   r   rm   r   r   r   rn     s
    zMultiDict.setlistdefaultFc                 c   s@   t | D ]0\}}|r,|D ]}||fV  qq
||d fV  q
dS )a  Return an iterator of ``(key, value)`` pairs.

        :param multi: If set to `True` the iterator returned will have a pair
                      for each value of each key.  Otherwise it will only
                      contain pairs for the first value of each key.
        r   N)r   r   )r   r   r   valuesr   r   r   r   r     s
    zMultiDict.itemsc                 c   s&   t | D ]\}}|t|fV  q
dS )zuReturn a iterator of ``(key, values)`` pairs, where values is the list
        of all values associated with the key.N)r   r   r   r   r   r   r   r   r   r     s    zMultiDict.listsc                 c   s   t | D ]}|d V  q
dS )zAReturns an iterator of the first value on every key's value list.r   Nr   r   r   r   r   r   r   r     s    zMultiDict.valuesc                 C   s
   t | S )a   Return an iterator of all values associated with a key.  Zipping
        :meth:`keys` and this is the same as calling :meth:`lists`:

        >>> d = MultiDict({"foo": [1, 2, 3]})
        >>> zip(d.keys(), d.listvalues()) == d.lists()
        True
        r   r   r   r   r   
listvalues  s    zMultiDict.listvaluesc                 C   s
   |  | S )z%Return a shallow copy of this object.rS   r   r   r   r   r      s    zMultiDict.copyc                 C   s   |  t| jdd|S )z"Return a deep copy of this object.F)flat)rT   r   to_dictr   memor   r   r   r     s    zMultiDict.deepcopyTc                 C   s   |rt |  S t |  S )a  Return the contents as regular dict.  If `flat` is `True` the
        returned dict will only have the first item present, if `flat` is
        `False` all values will be returned as lists.

        :param flat: If set to `False` the dict returned will have lists
                     with all the values in it.  Otherwise it will only
                     contain the first value for each key.
        :return: a :class:`dict`
        r   r   r   r   r   r   r   r   r     s    
zMultiDict.to_dictc                 C   s$   t |D ]\}}t| || qdS )a  update() extends rather than replaces existing key lists:

        >>> a = MultiDict({'x': 1})
        >>> b = MultiDict({'x': 2, 'y': 3})
        >>> a.update(b)
        >>> a
        MultiDict([('y', 3), ('x', 1), ('x', 2)])

        If the value list for a key in ``other_dict`` is empty, no new values
        will be added to the dict and the key will not be created:

        >>> x = {'empty_list': []}
        >>> y = MultiDict()
        >>> y.update(x)
        >>> y
        MultiDict([])
        N)r!   r   rg   r   r   r   r   r   r   r   r_     s    zMultiDict.updatec                 C   s`   z,t | |}t|dkr$t||d W S  tyZ   |turJ| Y S t|dY n0 dS )a  Pop the first item for a list on the dict.  Afterwards the
        key is removed from the dict, so additional values are discarded:

        >>> d = MultiDict({"foo": [1, 2, 3]})
        >>> d.pop("foo")
        1
        >>> "foo" in d
        False

        :param key: the key to pop.
        :param default: if provided the value to return if the key was
                        not in the dictionary.
        r   N)r   r@   r   r
   r   rz   r   )r   r   rZ   r   r   r   r   r@   +  s    

zMultiDict.popc              
   C   sz   z>t | }t|d dkr*t|d |d |d d fW S  tyt } zt|jd dW Y d}~n
d}~0 0 dS )zPop an item from the dict.r	   r   N)r   r`   r   r
   r   rz   r]   )r   r6   er   r   r   r`   F  s    
zMultiDict.popitemc                 C   s   t | |g S )zPop the list for a key from the dict.  If the key is not in the dict
        an empty list is returned.

        .. versionchanged:: 0.5
           If the key does no longer exist a list is returned instead of
           raising an error.
        )r   r@   r-   r   r   r   ri   R  s    zMultiDict.poplistc              
   C   sH   zt | W S  tyB } zt|jd dW Y d}~n
d}~0 0 dS )z*Pop a ``(key, list)`` tuple from the dict.r   N)r   r`   rz   r
   r   r]   )r   r   r   r   r   rh   \  s    zMultiDict.popitemlistc                 C   s   |   S r#   r   r   r   r   r   r   c  s    zMultiDict.__copy__c                 C   s   | j |dS )N)r   r   r   r   r   r   __deepcopy__f  s    zMultiDict.__deepcopy__c                 C   s"   t | j dt| jdddS )NrH   Tr   rI   )r   r   r   r   r   r   r   r   rJ   i  s    zMultiDict.__repr__)N)N)N)N)F)N)T)r   rC   rD   rE   rN   r   r   r   r   r4   rg   r   rl   r[   rn   r   r   r   r   r   r   r   r_   r   r@   r`   ri   rh   r   r   rJ   r   r   r   r   r   !  s6   (









r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )_omd_bucketa  Wraps values in the :class:`OrderedMultiDict`.  This makes it
    possible to keep an order over multiple different keys.  It requires
    a lot of extra memory and slows down access a lot, but makes it
    possible to access elements in O(1) and iterate in O(n).
    )prevr   r   nextc                 C   sF   |j | _|| _|| _d | _|jd u r*| |_|j d ur<| |j _| |_ d S r#   )_last_bucketr   r   r   r   _first_bucket)r   omdr   r   r   r   r   rN   v  s    

z_omd_bucket.__init__c                 C   sH   | j r| j| j _| jr | j | j_ |j| u r2| j|_|j| u rD| j |_d S r#   )r   r   r   r   )r   r   r   r   r   unlink  s    



z_omd_bucket.unlinkN)r   rC   rD   rE   	__slots__rN   r   r   r   r   r   r   m  s   r   c                   @   s   e Zd ZdZd2ddZdd ZdZdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zd3ddZdd Zdd Zd d! Zd4d"d#Zd$d% Zd5d&d'Zd(d) Zd*d+ Zefd,d-Zd.d/ Zd0d1 ZdS )6OrderedMultiDictaN  Works like a regular :class:`MultiDict` but preserves the
    order of the fields.  To convert the ordered multi dict into a
    list you can use the :meth:`items` method and pass it ``multi=True``.

    In general an :class:`OrderedMultiDict` is an order of magnitude
    slower than a :class:`MultiDict`.

    .. admonition:: note

       Due to a limitation in Python you cannot convert an ordered
       multi dict into a regular dict by using ``dict(multidict)``.
       Instead you have to use the :meth:`to_dict` method, otherwise
       the internal bucket objects are exposed.
    Nc                 C   s.   t |  d  | _| _|d ur*t| | d S r#   )r   rN   r   r   r   r_   )r   r   r   r   r   rN     s    
zOrderedMultiDict.__init__c           
      C   s   t |tstS t |trt| jdd}t|jdd}z6|D ],\}}t|\}}||ksb||kr> W dS q>W n ty   Y dS 0 zt| W n ty   Y dS 0 dS t| t|krdS | 	 D ]\}}	|
||	kr dS qdS )NTr   F)r   r   NotImplementedr   iterr   r   StopIterationr   r   r   )
r   r0   Ziter1Ziter2k1v1k2v2r   r   r   r   r   __eq__  s.    

zOrderedMultiDict.__eq__c                 C   s   t | t| jddffS re   rf   r)   r   r   r   r+     s    zOrderedMultiDict.__reduce_ex__c                 C   s   t | jddS re   )r   r   r   r   r   r   r     s    zOrderedMultiDict.__getstate__c                 C   s(   t |  |D ]\}}| || qd S r#   )r   ra   rg   )r   r   r   r   r   r   r   r     s    
zOrderedMultiDict.__setstate__c                 C   s(   || v rt | |d jS t|d S Nr   )r   r   r   r
   r   r-   r   r   r   r     s    zOrderedMultiDict.__getitem__c                 C   s   |  | | || d S r#   ri   rg   r3   r   r   r   r4     s    
zOrderedMultiDict.__setitem__c                 C   s   |  | d S r#   r@   r-   r   r   r   r.     s    zOrderedMultiDict.__delitem__c                 C   s   dd |   D S )Nc                 s   s   | ]\}}|V  qd S r#   r   r   r   r   r   r   r   r     r   z(OrderedMultiDict.keys.<locals>.<genexpr>rV   r   r   r   r   rQ     s    zOrderedMultiDict.keysc                 C   s   t |  S r#   r   rQ   r   r   r   r   r     s    zOrderedMultiDict.__iter__c                 C   s   dd |   D S )Nc                 s   s   | ]\}}|V  qd S r#   r   r   r   r   r   r     r   z*OrderedMultiDict.values.<locals>.<genexpr>rV   r   r   r   r   r     s    zOrderedMultiDict.valuesFc                 c   sh   | j }|r*|d urd|j|jfV  |j}q
n:t }|d urd|j|vr\||j |j|jfV  |j}q0d S r#   )r   r   r   r   setrg   )r   r   ptrreturned_keysr   r   r   r     s    

zOrderedMultiDict.itemsc                 c   sJ   t  }| j}|d urF|j|vr>|j| |jfV  ||j |j}qd S r#   )r   r   r   r   rg   r   )r   r   r   r   r   r   r     s    
zOrderedMultiDict.listsc                 c   s   |   D ]\}}|V  qd S r#   r   )r   _keyr   r   r   r   r     s    zOrderedMultiDict.listvaluesc                 C   s    t | |g t| || d S r#   )r   r[   r7   r   r3   r   r   r   rg     s    zOrderedMultiDict.addc              	   C   sx   zt | |}W n ty&   g  Y S 0 |d u r>dd |D S g }|D ],}z|||j W qF typ   Y qF0 qF|S )Nc                 S   s   g | ]
}|j qS r   r   r   xr   r   r   
<listcomp>  r   z,OrderedMultiDict.getlist.<locals>.<listcomp>)r   r   rz   r7   r   r{   r   r   r   r   r     s    
zOrderedMultiDict.getlistc                 C   s$   |  | |D ]}| || qd S r#   r   )r   r   rk   r   r   r   r   rl     s    
zOrderedMultiDict.setlistc                 C   s   t dd S )Nz5setlistdefault is unsupported for ordered multi dicts)r   rm   r   r   r   rn     s    zOrderedMultiDict.setlistdefaultc                 C   s$   t |D ]\}}t| || qd S r#   )r!   r   rg   r   r   r   r   r_     s    zOrderedMultiDict.updatec                 C   s0   t | |d}|D ]}||  qdd |D S )Nr   c                 S   s   g | ]
}|j qS r   r   r   r   r   r   r     r   z,OrderedMultiDict.poplist.<locals>.<listcomp>)r   r@   r   )r   r   bucketsbucketr   r   r   ri     s    zOrderedMultiDict.poplistc                 C   s^   zt | |}W n. ty>   |tur.| Y S t|d Y n0 |D ]}||  qD|d jS r   )r   r@   rz   r   r
   r   r   r   )r   r   rZ   r   r   r   r   r   r@     s    zOrderedMultiDict.popc              
   C   sl   zt | \}}W n6 tyH } zt|jd d W Y d }~n
d }~0 0 |D ]}||  qN||d jfS r   )r   r`   rz   r
   r   r]   r   r   r   r   r   r   r   r   r   r   r`   +  s    (zOrderedMultiDict.popitemc              
   C   sp   zt | \}}W n6 tyH } zt|jd d W Y d }~n
d }~0 0 |D ]}||  qN|dd |D fS )Nr   c                 S   s   g | ]
}|j qS r   r   r   r   r   r   r   ?  r   z0OrderedMultiDict.popitemlist.<locals>.<listcomp>)r   r`   rz   r
   r   r]   r   r   r   r   r   rh   6  s    (zOrderedMultiDict.popitemlist)N)F)N)N)r   rC   rD   rE   rN   r   r(   r+   r   r   r   r4   r.   rQ   r   r   r   r   r   rg   r   rl   rn   r_   ri   r   r@   r`   rh   r   r   r   r   r     s2   

	

r   c                 C   s   t | dd | D S )Nc                 S   s   i | ]\}}| d d|qS )_-)replacer   r   r    r   r   r   
<dictcomp>D  r   z'_options_header_vkw.<locals>.<dictcomp>)httpZdump_options_headerr   )r   rr   r   r   r   _options_header_vkwB  s    r   c                 C   s*   t | tr| d} t | ts&t| } | S )Nzlatin-1)r   bytesdecodestrr   r   r   r   _unicodify_header_valueH  s
    


r   c                   @   s(  e Zd ZdZdEddZdFddZdd	 ZdZdGd
dZdHddZ	dd Z
dIddZdJddZdd Zdd ZdKddZdd ZdefddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dS )LHeadersaC  An object that stores some headers. It has a dict-like interface,
    but is ordered, can store the same key multiple times, and iterating
    yields ``(key, value)`` pairs instead of only keys.

    This data structure is useful if you want a nicer way to handle WSGI
    headers which are stored as tuples in a list.

    From Werkzeug 0.3 onwards, the :exc:`KeyError` raised by this class is
    also a subclass of the :class:`~exceptions.BadRequest` HTTP exception
    and will render a page for a ``400 BAD REQUEST`` if caught in a
    catch-all for HTTP exceptions.

    Headers is mostly compatible with the Python :class:`wsgiref.headers.Headers`
    class, with the exception of `__getitem__`.  :mod:`wsgiref` will return
    `None` for ``headers['missing']``, whereas :class:`Headers` will raise
    a :class:`KeyError`.

    To create a new ``Headers`` object, pass it a list, dict, or
    other ``Headers`` object with default values. These values are
    validated the same way values added later are.

    :param defaults: The list of default values for the :class:`Headers`.

    .. versionchanged:: 2.1.0
        Default values are validated the same as values added later.

    .. versionchanged:: 0.9
       This data structure now stores unicode values similar to how the
       multi dicts do it.  The main difference is that bytes can be set as
       well which will automatically be latin1 decoded.

    .. versionchanged:: 0.9
       The :meth:`linked` function was removed without replacement as it
       was an API that does not support the changes to the encoding model.
    Nc                 C   s   g | _ |d ur| | d S r#   )_listr:   )r   defaultsr   r   r   rN   u  s    zHeaders.__init__Fc                 C   s   |s2t |tr| j| S t |tr2| | j| S t |tsFt|| }| jD ]\}}| |krT|  S qT|r|t	 t|d S r#   )
r   intr   slicerT   r   r
   r   lowerrz   )r   r   	_get_modeikeyr   r    r   r   r   r   z  s    





zHeaders.__getitem__c                 C   s4   dd }|j | j u o2tt||jtt|| jkS )Nc                 S   s   | d   f| dd   S Nr   r	   r   )r6   r   r   r   lowered  s    zHeaders.__eq__.<locals>.lowered)rT   r   mapr   )r   r0   r   r   r   r   r     s    
zHeaders.__eq__c                 C   sj   z| j |dd}W n ty(   | Y S 0 |r8|d}|du rD|S z
||W S  tyd   | Y S 0 dS )a  Return the default value if the requested data doesn't exist.
        If `type` is provided and is a callable it should convert the value,
        return it or raise a :exc:`ValueError` if that is not possible.  In
        this case the function will return the default as if the value was not
        found:

        >>> d = Headers([('Content-Length', '42')])
        >>> d.get('Content-Length', type=int)
        42

        .. versionadded:: 0.9
           Added support for `as_bytes`.

        :param key: The key to be looked up.
        :param default: The default value to be returned if the key can't
                        be looked up.  If not further specified `None` is
                        returned.
        :param type: A callable that is used to cast the value in the
                     :class:`Headers`.  If a :exc:`ValueError` is raised
                     by this callable the default value is returned.
        :param as_bytes: return bytes instead of strings.
        Tr   latin1N)r   rz   encoder{   )r   r   rZ   r   as_bytesr'   r   r   r   r|     s    


zHeaders.getc              	   C   sn   |  }g }| D ]X\}}|  |kr|r2|d}|dur^z||}W n ty\   Y qY n0 || q|S )a  Return the list of items for a given key. If that key is not in the
        :class:`Headers`, the return value will be an empty list.  Just like
        :meth:`get`, :meth:`getlist` accepts a `type` parameter.  All items will
        be converted with the callable defined there.

        .. versionadded:: 0.9
           Added support for `as_bytes`.

        :param key: The key to be looked up.
        :param type: A callable that is used to cast the value in the
                     :class:`Headers`.  If a :exc:`ValueError` is raised
                     by this callable the value will be removed from the list.
        :return: a :class:`list` of all the values for the key.
        :param as_bytes: return bytes instead of strings.
        r   N)r   r   r{   r7   )r   r   r   r   r   r   r   r    r   r   r   r     s    

zHeaders.getlistc                 C   s
   |  |S )zReturn a list of all the values for the named field.

        This method is compatible with the :mod:`wsgiref`
        :meth:`~wsgiref.headers.Headers.get_all` method.
        )r   r   rt   r   r   r   get_all  s    zHeaders.get_allc                 c   s(   | D ]\}}|r|  }||fV  qd S r#   r   )r   r   r   r   r   r   r   r     s    zHeaders.itemsc                 c   s   |  |D ]\}}|V  q
d S r#   rV   )r   r   r   r   r   r   r   rQ     s    zHeaders.keysc                 c   s   |   D ]\}}|V  qd S r#   rV   )r   r   r   r   r   r   r     s    zHeaders.valuesc                 O   sf   t |dkrtdt | |rDt|d D ]\}}| || q.t|D ]\}}| || qLdS )a  Extend headers in this object with items from another object
        containing header items as well as keyword arguments.

        To replace existing keys instead of extending, use
        :meth:`update` instead.

        If provided, the first argument can be another :class:`Headers`
        object, a :class:`MultiDict`, :class:`dict`, or iterable of
        pairs.

        .. versionchanged:: 1.0
            Support :class:`MultiDict`. Allow passing ``kwargs``.
        r	   )update expected at most 1 arguments, got r   N)r   r   r!   rg   )r   r]   r^   r   r   r   r   r   r:     s    zHeaders.extendTc                 C   sf   |rt |ttfr| j|= d S | }g }| jD ]"\}}| |kr0|||f q0|| jd d < d S r#   )r   r   r   r   r   r7   )r   r   _index_operationnewr   r    r   r   r   r.     s    zHeaders.__delitem__c                 C   s   | j |ddS )zBRemove a key.

        :param key: The key to be removed.
        F)r   )r.   r-   r   r   r   r8     s    zHeaders.removec                 C   sh   |du r| j  S t|tr(| j |S z| | }| | W n$ tyb   |tur\| Y S  Y n0 |S )aQ  Removes and returns a key or index.

        :param key: The key to be popped.  If this is an integer the item at
                    that position is removed, if it's a string the value for
                    that key is.  If the key is omitted or `None` the last
                    item is removed.
        :return: an item.
        N)r   r@   r   r   r8   rz   r   )r   r   rZ   r'   r   r   r   r@     s    	

zHeaders.popc                 C   s   |   S )z7Removes a key or index and returns a (key, value) item.r   r   r   r   r   r`   /  s    zHeaders.popitemc                 C   s,   z| j |dd W n ty&   Y dS 0 dS )zCheck if a key is present.Tr   F)r   rz   r-   r   r   r   __contains__3  s
    zHeaders.__contains__c                 C   s
   t | jS )zYield ``(key, value)`` tuples.)r   r   r   r   r   r   r   ;  s    zHeaders.__iter__c                 C   s
   t | jS r#   )r   r   r   r   r   r   __len__?  s    zHeaders.__len__c                 K   s<   |rt ||}t|}t|}| | | j||f dS )a  Add a new header tuple to the list.

        Keyword arguments can specify additional parameters for the header
        value, with underscores converted to dashes::

        >>> d = Headers()
        >>> d.add('Content-Type', 'text/plain')
        >>> d.add('Content-Disposition', 'attachment', filename='foo.png')

        The keyword argument dumping uses :func:`dump_options_header`
        behind the scenes.

        .. versionadded:: 0.4.1
            keyword arguments were added for :mod:`wsgiref` compatibility.
        N)r   r   _validate_valuer   r7   r   r   _valuerr   r   r   r   rg   B  s    

zHeaders.addc                 C   s.   t |tstdd|v s"d|v r*tdd S )NzValue should be a string.
zGDetected newline in header value.  This is a potential security problem)r   r   r   r{   r   r   r   r   r   Y  s    
zHeaders._validate_valuec                 K   s   | j ||fi | dS )zAdd a new header tuple to the list.

        An alias for :meth:`add` for compatibility with the :mod:`wsgiref`
        :meth:`~wsgiref.headers.Headers.add_header` method.
        N)rg   r   r   r   Z_kwr   r   r   
add_headerb  s    zHeaders.add_headerc                 C   s   | j dd= dS )zClears all headers.N)r   r   r   r   r   ra   j  s    zHeaders.clearc                    s   |rt ||}t|}t|}| | | jsB| j||f dS t| j}|  t|D ]*\}\}}|  kr\||f| j|<  qq\| j||f dS  fdd|D | j|d d< dS )a0  Remove all header tuples for `key` and add a new one.  The newly
        added key either appears at the end of the list if there was no
        entry or replaces the first one.

        Keyword arguments can specify additional parameters for the header
        value, with underscores converted to dashes.  See :meth:`add` for
        more information.

        .. versionchanged:: 0.6.1
           :meth:`set` now accepts the same arguments as :meth:`add`.

        :param key: The key to be inserted.
        :param value: The value to be inserted.
        Nc                    s    g | ]}|d     kr|qS )r   r   )r   tr   r   r   r     r   zHeaders.set.<locals>.<listcomp>r	   )r   r   r   r   r7   r   r   	enumerate)r   r   r   rr   listiteridxold_keyZ
_old_valuer   r   r   r   n  s"    


zHeaders.setc                 C   sB   |r4t |}| |t| |D ]}| || q n
| | dS )zRemove any existing values for a header and add new ones.

        :param key: The header key to set.
        :param values: An iterable of values to set for the key.

        .. versionadded:: 1.0
        N)r   r   r   rg   r8   )r   r   r   Zvalues_iterr   r   r   r   rl     s    zHeaders.setlistc                 C   s    || v r| | S |  || |S )a*  Return the first value for the key if it is in the headers,
        otherwise set the header to the value given by ``default`` and
        return that.

        :param key: The header key to get.
        :param default: The value to set for the key if it is not in the
            headers.
        r   rY   r   r   r   r[     s    	zHeaders.setdefaultc                 C   s   || vr|  || | |S )a  Return the list of values for the key if it is in the
        headers, otherwise set the header to the list of values given
        by ``default`` and return that.

        Unlike :meth:`MultiDict.setlistdefault`, modifying the returned
        list will not affect the headers.

        :param key: The header key to get.
        :param default: An iterable of values to set for the key if it
            is not in the headers.

        .. versionadded:: 1.0
        )rl   r   rY   r   r   r   rn     s    zHeaders.setlistdefaultc                 C   sz   t |ttfrjt |tr|g}dd |D }|D ]\}}| | q0t |tr^|d | j|< qv|| j|< n| || dS )z=Like :meth:`set` but also supports index/slice based setting.c                 S   s    g | ]\}}t |t |fqS r   )r   r   r   r   r   r     s   z'Headers.__setitem__.<locals>.<listcomp>r   N)r   r   r   r   r   r   )r   r   r   r   r    r   r   r   r4     s    

zHeaders.__setitem__c                 O   s   t |dkrtdt | |r|d }t|ttfrZ| D ]}| ||| q@n`t|tr|	 D ]0\}}t|t
tfr| || ql| || qln|D ]\}}| || q|	 D ]0\}}t|t
tfr| || q| || qdS )a  Replace headers in this object with items from another
        headers object and keyword arguments.

        To extend existing keys instead of replacing, use :meth:`extend`
        instead.

        If provided, the first argument can be another :class:`Headers`
        object, a :class:`MultiDict`, :class:`dict`, or iterable of
        pairs.

        .. versionadded:: 1.0
        r	   r   r   N)r   r   r   r   r   rQ   rl   r   r   r   r   r   r   )r   r]   r^   r   r   r   r   r   r   r_     s$    
zHeaders.updatec                 C   s   t | S )zRConvert the headers into a list suitable for WSGI.

        :return: list
        )r   r   r   r   r   to_wsgi_list  s    zHeaders.to_wsgi_listc                 C   s   |  | jS r#   )rT   r   r   r   r   r   r     s    zHeaders.copyc                 C   s   |   S r#   r   r   r   r   r   r     s    zHeaders.__copy__c                 C   s>   g }|   D ]\}}|| d|  q|d d|S )z9Returns formatted headers suitable for HTTP transmission.: z
)r   r7   join)r   strsr   r   r   r   r   __str__	  s
    
zHeaders.__str__c                 C   s   t | j dt| dS rG   )r   r   r   r   r   r   r   rJ     s    zHeaders.__repr__)N)F)NNF)NF)F)F)T)&r   rC   rD   rE   rN   r   r   r(   r|   r   r   r   rQ   r   r:   r.   r8   r   r@   r`   r   r   r   rg   r   r   ra   r   rl   r[   rn   r4   r_   r   r   r   r   rJ   r   r   r   r   r   P  sD   $


$



	#&r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdefddZdd Zdd Zdd ZdS )ImmutableHeadersMixinzMakes a :class:`Headers` immutable.  We do not mark them as
    hashable though since the only usecase for this datastructure
    in Werkzeug is a view on a mutable structure.

    .. versionadded:: 0.5

    :private:
    c                 K   s   t |  d S r#   r,   )r   r   r^   r   r   r   r.     s    z!ImmutableHeadersMixin.__delitem__c                 C   s   t |  d S r#   r,   r3   r   r   r   r4   "  s    z!ImmutableHeadersMixin.__setitem__c                 K   s   t |  d S r#   r,   r   r   r   r   r   %  s    zImmutableHeadersMixin.setc                 C   s   t |  d S r#   r,   r   r   r   r   rl   (  s    zImmutableHeadersMixin.setlistc                 K   s   t |  d S r#   r,   r   r   r   r   rg   +  s    zImmutableHeadersMixin.addc                 K   s   t |  d S r#   r,   r   r   r   r   r   .  s    z ImmutableHeadersMixin.add_headerc                 C   s   t |  d S r#   r,   r-   r   r   r   r8   1  s    zImmutableHeadersMixin.removec                 O   s   t |  d S r#   r,   r\   r   r   r   r:   4  s    zImmutableHeadersMixin.extendc                 O   s   t |  d S r#   r,   r\   r   r   r   r_   7  s    zImmutableHeadersMixin.updatec                 C   s   t |  d S r#   r,   r;   r   r   r   r=   :  s    zImmutableHeadersMixin.insertNc                 C   s   t |  d S r#   r,   rY   r   r   r   r@   =  s    zImmutableHeadersMixin.popc                 C   s   t |  d S r#   r,   r   r   r   r   r`   @  s    zImmutableHeadersMixin.popitemc                 C   s   t |  d S r#   r,   rY   r   r   r   r[   C  s    z ImmutableHeadersMixin.setdefaultc                 C   s   t |  d S r#   r,   rY   r   r   r   rn   F  s    z$ImmutableHeadersMixin.setlistdefault)r   rC   rD   rE   r.   r4   r   rl   rg   r   r8   r:   r_   r=   r   r@   r`   r[   rn   r   r   r   r   r     s   	r   c                   @   sF   e Zd ZdZdd Zdd ZdZddd	Zd
d Zdd Z	dd Z
dS )EnvironHeadersa  Read only version of the headers from a WSGI environment.  This
    provides the same interface as `Headers` and is constructed from
    a WSGI environment.

    From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
    subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
    render a page for a ``400 BAD REQUEST`` if caught in a catch-all for
    HTTP exceptions.
    c                 C   s
   || _ d S r#   environ)r   r   r   r   r   rN   U  s    zEnvironHeaders.__init__c                 C   s   | j |j u S r#   r   r/   r   r   r   r   X  s    zEnvironHeaders.__eq__NFc                 C   sL   t |tst|| dd}|dv r8t| j| S t| jd|  S )Nr   r   ZCONTENT_TYPEZCONTENT_LENGTHHTTP_)r   r   rz   upperr   r   r   )r   r   r   r   r   r   r   ]  s    
zEnvironHeaders.__getitem__c                 C   s   t tt| S r#   )r   r   r   r   r   r   r   r   g  s    zEnvironHeaders.__len__c                 c   st   | j  D ]d\}}|drH|dvrH|dd  dd t|fV  q
|dv r
|r
|dd t|fV  q
d S )Nr   )ZHTTP_CONTENT_TYPEZHTTP_CONTENT_LENGTH   r   r   r   )r   r   
startswithr   titler   r3   r   r   r   r   l  s    zEnvironHeaders.__iter__c                 C   s   t dt| jdd S )Ncannot create z copiesr   r   r   r   r   r   y  s    zEnvironHeaders.copy)F)r   rC   rD   rE   rN   r   r(   r   r   r   r   r   r   r   r   r   J  s   


r   c                   @   s   e Zd ZdZdd Zd)ddZed*ddZd	d
 Zd+ddZ	d,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d Zd.d!d"Zd#d$ Zd%d& Zd'd( ZdS )/CombinedMultiDicta>  A read only :class:`MultiDict` that you can pass multiple :class:`MultiDict`
    instances as sequence and it will combine the return values of all wrapped
    dicts:

    >>> from werkzeug.datastructures import CombinedMultiDict, MultiDict
    >>> post = MultiDict([('foo', 'bar')])
    >>> get = MultiDict([('blub', 'blah')])
    >>> combined = CombinedMultiDict([get, post])
    >>> combined['foo']
    'bar'
    >>> combined['blub']
    'blah'

    This works for all read operations and will raise a `TypeError` for
    methods that usually change data which isn't possible.

    From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
    subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
    render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
    exceptions.
    c                 C   s   t | | jffS r#   )r   dictsr)   r   r   r   r+     s    zCombinedMultiDict.__reduce_ex__Nc                 C   s   t |p
g | _d S r#   )r   r  )r   r  r   r   r   rN     s    zCombinedMultiDict.__init__c                 C   s   t d| jdd S )Nr  z instances by fromkeys)r   r   )rP   rQ   r   r   r   r   rU     s    zCombinedMultiDict.fromkeysc                 C   s.   | j D ]}||v r||   S qt|d S r#   )r  r
   r   r   r   dr   r   r   r     s    
zCombinedMultiDict.__getitem__c              	   C   sV   | j D ]J}||v r|d urDz||| W   S  tyB   Y qY n0 ||   S q|S r#   )r  r{   )r   r   rZ   r   r  r   r   r   r|     s    

zCombinedMultiDict.getc                 C   s&   g }| j D ]}|||| q
|S r#   )r  r:   r   )r   r   r   r'   r  r   r   r   r     s    
zCombinedMultiDict.getlistc                 C   s   t  }|j| j  |S )zThis function exists so __len__ can be implemented more efficiently,
        saving one list creation from an iterator.
        )r   r_   r  r&   r   r   r   
_keys_impl  s    zCombinedMultiDict._keys_implc                 C   s   |   S r#   )r	  r   r   r   r   rQ     s    zCombinedMultiDict.keysc                 C   s   t |  S r#   r   r   r   r   r   r     s    zCombinedMultiDict.__iter__Fc                 c   sV   t  }| jD ]D}||D ]4\}}|r2||fV  q||vr|| ||fV  qqd S r#   )r   r  r   rg   )r   r   foundr  r   r   r   r   r   r     s    

zCombinedMultiDict.itemsc                 c   s   |   D ]\}}|V  qd S r#   rV   )r   r   r   r   r   r   r     s    zCombinedMultiDict.valuesc                 C   s@   i }| j D ](}| D ]\}}||g | qq
t| S r#   )r  r   r[   r:   r   r   )r   r'   r  r   r   r   r   r   r     s
    
zCombinedMultiDict.listsc                 C   s   dd |   D S )Nc                 s   s   | ]}|d  V  qdS )r	   Nr   r   r   r   r   r     r   z/CombinedMultiDict.listvalues.<locals>.<genexpr>r   r   r   r   r   r     s    zCombinedMultiDict.listvaluesc                 C   s   t | S )a0  Return a shallow mutable copy of this object.

        This returns a :class:`MultiDict` representing the data at the
        time of copying. The copy will no longer reflect changes to the
        wrapped dicts.

        .. versionchanged:: 0.15
            Return a mutable :class:`MultiDict`.
        r   r   r   r   r   r     s    
zCombinedMultiDict.copyTc                 C   s   |rt |  S t |  S )a  Return the contents as regular dict.  If `flat` is `True` the
        returned dict will only have the first item present, if `flat` is
        `False` all values will be returned as lists.

        :param flat: If set to `False` the dict returned will have lists
                     with all the values in it.  Otherwise it will only
                     contain the first item for each key.
        :return: a :class:`dict`
        r   r   r   r   r   r     s    
zCombinedMultiDict.to_dictc                 C   s   t |  S r#   )r   r	  r   r   r   r   r     s    zCombinedMultiDict.__len__c                 C   s   | j D ]}||v r dS qdS NTF)r  r  r   r   r   r     s    
zCombinedMultiDict.__contains__c                 C   s   t | j d| jdS rG   )r   r   r  r   r   r   r   rJ     s    zCombinedMultiDict.__repr__)N)N)NN)N)F)T)r   rC   rD   rE   r+   rN   rb   rU   r   r|   r   r	  rQ   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r  }  s(   





r  c                   @   s   e Zd ZdZdddZdS )FileMultiDictzA special :class:`MultiDict` that has convenience methods to add
    files to it.  This is used for :class:`EnvironBuilder` and generally
    useful for unittesting.

    .. versionadded:: 0.5
    Nc                 C   sl   t |tr|}nLt |tr0|du r&|}t|d}|rN|du rNt|d pLd}t||||}| || dS )aP  Adds a new file to the dict.  `file` can be a file name or
        a :class:`file`-like or a :class:`FileStorage` object.

        :param name: the name of the field.
        :param file: a filename or :class:`file`-like object
        :param filename: an optional filename
        :param content_type: an optional content type
        Nrbr   zapplication/octet-stream)r   FileStorager   open	mimetypes
guess_typerg   )r   rt   filefilenamecontent_typer   r   r   r   add_file  s    	


zFileMultiDict.add_file)NN)r   rC   rD   rE   r  r   r   r   r   r    s   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ImmutableDictz;An immutable :class:`dict`.

    .. versionadded:: 0.5
    c                 C   s   t | j dt|  dS rG   r   r   r   rJ   r   r   r   r   rJ   *  s    zImmutableDict.__repr__c                 C   s   t | S r~   )r   r   r   r   r   r   -  s    zImmutableDict.copyc                 C   s   | S r#   r   r   r   r   r   r   4  s    zImmutableDict.__copy__N)r   rC   rD   rE   rJ   r   r   r   r   r   r   r  $  s   r  c                   @   s    e Zd ZdZdd Zdd ZdS )ImmutableMultiDictz@An immutable :class:`MultiDict`.

    .. versionadded:: 0.5
    c                 C   s   t | S r~   r  r   r   r   r   r   >  s    zImmutableMultiDict.copyc                 C   s   | S r#   r   r   r   r   r   r   E  s    zImmutableMultiDict.__copy__Nr   r   r   r   r   r  8  s   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ImmutableOrderedMultiDictzGAn immutable :class:`OrderedMultiDict`.

    .. versionadded:: 0.6
    c                 C   s   t | jddS re   )r   r   r   r   r   r   rW   O  s    z)ImmutableOrderedMultiDict._iter_hashitemsc                 C   s   t | S r~   )r   r   r   r   r   r   R  s    zImmutableOrderedMultiDict.copyc                 C   s   | S r#   r   r   r   r   r   r   Y  s    z"ImmutableOrderedMultiDict.__copy__N)r   rC   rD   rE   rW   r   r   r   r   r   r   r  I  s   r  c                   @   s   e Zd ZdZd"ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd#ddZed d! ZdS )$Accepta  An :class:`Accept` object is just a list subclass for lists of
    ``(value, quality)`` tuples.  It is automatically sorted by specificity
    and quality.

    All :class:`Accept` objects work similar to a list but provide extra
    functionality for working with the data.  Containment checks are
    normalized to the rules of that header:

    >>> a = CharsetAccept([('ISO-8859-1', 1), ('utf-8', 0.7)])
    >>> a.best
    'ISO-8859-1'
    >>> 'iso-8859-1' in a
    True
    >>> 'UTF8' in a
    True
    >>> 'utf7' in a
    False

    To get the quality for an item you can use normal item lookup:

    >>> print a['utf-8']
    0.7
    >>> a['utf7']
    0

    .. versionchanged:: 0.5
       :class:`Accept` objects are forced immutable now.

    .. versionchanged:: 1.0.0
       :class:`Accept` internal values are no longer ordered
       alphabetically for equal quality tags. Instead the initial
       order is preserved.

    r   c                    sf   |d u rt   d _nHt|tr:|j _t  | n(d _t| fdddd}t  | d S )NFTc                    s     | d | d fS r   )_specificityr   r   r   r   <lambda>  r   z!Accept.__init__.<locals>.<lambda>)r   rA   )r   rN   providedr   r  sortedr   r   r   r   rN     s    

zAccept.__init__c                 C   s
   |dkfS )z3Returns a tuple describing the value's specificity.*r   r   r   r   r   r    s    zAccept._specificityc                 C   s   |dkp|  |  kS )z-Check if a value matches a given accept item.r!  r   r   r   r6   r   r   r   _value_matches  s    zAccept._value_matchesc                 C   s    t |tr| |S t| |S )zBesides index lookup (getting item n) you can also pass it a string
        to get the quality for the item.  If the item is not in the list, the
        returned quality is ``0``.
        )r   r   qualityr   r   r-   r   r   r   r     s    

zAccept.__getitem__c                 C   s&   | D ]\}}|  ||r|  S qdS )zReturns the quality of the key.

        .. versionadded:: 0.6
           In previous versions you had to use the item-lookup syntax
           (eg: ``obj[key]`` instead of ``obj.quality(key)``)
        r   r#  )r   r   r6   r$  r   r   r   r$    s    
zAccept.qualityc                 C   s$   | D ]\}}|  ||r dS qdS r  r%  )r   r   r6   _qualityr   r   r   r     s    zAccept.__contains__c                 C   s*   d dd | D }t| j d| dS )N, c                 s   s$   | ]\}}d |d| dV  qdS )rH   r'  rI   Nr   )r   r   yr   r   r   r     r   z"Accept.__repr__.<locals>.<genexpr>z([z]))r   r   r   )r   Z	pairs_strr   r   r   rJ     s    zAccept.__repr__c                 C   sH   t |tr<t| D ] \}\}}| ||r|  S qt|t| |S )zGet the position of an entry or raise :exc:`ValueError`.

        :param key: The key to be looked up.

        .. versionchanged:: 0.5
           This used to raise :exc:`IndexError`, which was inconsistent
           with the list API.
        )r   r   r   r#  r{   r   r?   )r   r   r   r6   r&  r   r   r   r?     s    	

zAccept.indexc                 C   s&   z|  |W S  ty    Y dS 0 dS )zaGet the position of an entry or return -1.

        :param key: The key to be looked up.
        r>   N)r?   r{   r-   r   r   r   find  s    zAccept.findc                 c   s   | D ]}|d V  qdS )zIterate over all values.r   Nr   r5   r   r   r   r     s    zAccept.valuesc                 C   s<   g }| D ](\}}|dkr&| d| }| | qd|S )2Convert the header set into an HTTP header string.r	   z;q=,)r7   r   )r   r   r   r$  r   r   r   	to_header  s    zAccept.to_headerc                 C   s   |   S r#   r,  r   r   r   r   r     s    zAccept.__str__c                 C   s*   | D ] \}}|  ||r||f  S qd S r#   r%  )r   matchclient_itemr$  r   r   r   _best_single_match  s    zAccept._best_single_matchNc                 C   sj   |}d}d}|D ]T}|  |}|s$q|\}}	| |}
|	dks|	|k rHq|	|ksX|
|kr|}|	}|
}q|S )aV  Returns the best match from a list of possible matches based
        on the specificity and quality of the client. If two items have the
        same quality and specificity, the one is returned that comes first.

        :param matches: a list of matches to check for
        :param default: the value that is returned if none match
        r>   )r>   r   )r0  r  )r   matchesrZ   r   Zbest_qualityZbest_specificityZserver_itemr.  r/  r$  Zspecificityr   r   r   
best_match  s     

zAccept.best_matchc                 C   s   | r| d d S dS )zThe best match as value.r   Nr   r   r   r   r   best  s    zAccept.best)r   )N)r   rC   rD   rE   rN   r  r#  r   r$  r   rJ   r?   r)  r   r,  r   r0  r2  propertyr3  r   r   r   r   r  ]  s"   #
	
	
r  z/|(?:\s*;\s*)c                 C   s   t |  S r#   )_mime_split_resplitr   r   r   r   r   _normalize_mime  s    r7  c                   @   sD   e Zd ZdZdd Zdd Zedd Zedd	 Zed
d Z	dS )
MIMEAcceptzRLike :class:`Accept` but with special methods and behavior for
    mimetypes.
    c                 C   s   t dd t|D S )Nc                 s   s   | ]}|d kV  qdS )r!  Nr   r   r   r   r   r     r   z*MIMEAccept._specificity.<locals>.<genexpr>)r   r5  r6  r   r   r   r   r    s    zMIMEAccept._specificityc                 C   s   d|vrdS d|vr"t d|t|}|d d \}}t|dd  }|dkrh|dkrht d|t|}|d d \}}	t|dd  }
|dkr|	dkrdS |dkr|	dkp|dkr|dkp||ko|	dkp|dkp|	|ko|
|kS )N/Fzinvalid mimetype    r!  )r{   r7  r   )r   r   r6   Znormalized_value
value_typeZvalue_subtypeZvalue_paramsnormalized_itemZ	item_typeZitem_subtypeZitem_paramsr   r   r   r#    s.    zMIMEAccept._value_matchesc                 C   s   d| v pd| v p| j S )z!True if this object accepts HTML.z	text/htmlapplication/xhtml+xml)accept_xhtmlr   r   r   r   accept_html@  s    zMIMEAccept.accept_htmlc                 C   s   d| v pd| v S )z"True if this object accepts XHTML.r=  zapplication/xmlr   r   r   r   r   r>  G  s    zMIMEAccept.accept_xhtmlc                 C   s   d| v S )z!True if this object accepts JSON.zapplication/jsonr   r   r   r   r   accept_jsonL  s    zMIMEAccept.accept_jsonN)
r   rC   rD   rE   r  r#  r4  r?  r>  r@  r   r   r   r   r8    s   (

r8  z[_-]c                 C   s   t |  S )z$Process a language tag for matching.)_locale_delim_rer6  r   r   r   r   r   _normalize_langU  s    rB  c                       s*   e Zd ZdZdd Zd fdd	Z  ZS )LanguageAcceptz>Like :class:`Accept` but with normalization for language tags.c                 C   s   |dkpt |t |kS )Nr!  )rB  r"  r   r   r   r#  ]  s    zLanguageAccept._value_matchesNc                    s|   t  |  dur S tdd | D }||  dur@ S dd |D }t  |  durxt fdd|D S |S )a  Given a list of supported values, finds the best match from
        the list of accepted values.

        Language tags are normalized for the purpose of matching, but
        are returned unchanged.

        If no exact match is found, this will fall back to matching
        the first subtag (primary language only), first with the
        accepted values then with the match values. This partial is not
        applied to any other language subtags.

        The default is returned if no exact or fallback match is found.

        :param matches: A list of supported languages to find a match.
        :param default: The value that is returned if none match.
        Nc                 S   s(   g | ] }t |d  dd  |d fqS )r   r	   rA  r6  r   r6   r   r   r   r   |  r   z-LanguageAccept.best_match.<locals>.<listcomp>c                 S   s   g | ]}t |d d qS )r	   r   rD  rE  r   r   r   r     r   c                 3   s   | ]}|  r|V  qd S r#   )r  rE  r   r   r   r     r   z,LanguageAccept.best_match.<locals>.<genexpr>)rL   r2  r  r   )r   r1  rZ   fallbackZfallback_matchesrS   rF  r   r2  `  s    
zLanguageAccept.best_match)N)r   rC   rD   rE   r#  r2  rc   r   r   rS   r   rC  Z  s   rC  c                   @   s   e Zd ZdZdd ZdS )CharsetAcceptz9Like :class:`Accept` but with normalization for charsets.c                 C   s    dd }|dkp||||kS )Nc                 S   s.   zt | jW S  ty(   |   Y S 0 d S r#   )codecslookuprt   LookupErrorr   rs   r   r   r   
_normalize  s    z0CharsetAccept._value_matches.<locals>._normalizer!  r   )r   r   r6   rL  r   r   r   r#    s    zCharsetAccept._value_matchesN)r   rC   rD   rE   r#  r   r   r   r   rH    s   rH  c                    s2   t  fddfddfdddS )zReturn a new property object for a cache header. Useful if you
    want to add support for a cache extension in a subclass.

    .. versionchanged:: 2.0
        Renamed from ``cache_property``.
    c                    s   |   S r#   )_get_cache_valuer  emptyr   r   r   r   r    r   z(cache_control_property.<locals>.<lambda>c                    s   |   |S r#   )_set_cache_valuer   r    )r   r   r   r   r    r   c                    s
   |   S r#   )_del_cache_valuer  r   r   r   r    r   accessor for r4  )r   rO  r   r   rN  r   cache_control_property  s    
rV  c                   @   s   e Zd ZdZedddZeddeZeddeZ	edddZ
dd
dZdd Zdd Zdd Zdd Zdd Zdd ZeeZdS )_CacheControla  Subclass of a dict that stores values for a Cache-Control header.  It
    has accessors for all the cache-control directives specified in RFC 2616.
    The class does not differentiate between request and response directives.

    Because the cache-control directives in the HTTP header use dashes the
    python descriptors use underscores for that.

    To get a header of the :class:`CacheControl` object again you can convert
    the object into a string or call the :meth:`to_header` method.  If you plan
    to subclass it and add your own items have a look at the sourcecode for
    that class.

    .. versionchanged:: 2.1.0
        Setting int properties such as ``max_age`` will convert the
        value to an int.

    .. versionchanged:: 0.4

       Setting `no_cache` or `private` to boolean `True` will set the implicit
       none-value which is ``*``:

       >>> cc = ResponseCacheControl()
       >>> cc.no_cache = True
       >>> cc
       <ResponseCacheControl 'no-cache'>
       >>> cc.no_cache
       '*'
       >>> cc.no_cache = None
       >>> cc
       <ResponseCacheControl ''>

       In versions before 0.5 the behavior documented here affected the now
       no longer existing `CacheControl` class.
    zno-cacher!  Nzno-storezmax-ager>   zno-transformr   c                 C   s$   t | |pd || _|d u| _d S Nr   r   rN   rq   r  r   r   rq   r   r   r   rN     s    z_CacheControl.__init__c                 C   s\   |t u r|| v S || v rX| | }|du r,|S |durTz||}W n tyR   Y n0 |S dS +Used internally by the accessor properties.N)boolr{   )r   r   rO  r   r   r   r   r   rM    s    z_CacheControl._get_cache_valuec                 C   sn   |t u r$|rd| |< qj| |d nF|du r:| |d n0|du rLd| |< n|durb||| |< n|| |< dS )r\  NT)r]  r@   )r   r   r   r   r   r   r   rP    s    

z_CacheControl._set_cache_valuec                 C   s   || v r| |= dS r[  r   r-   r   r   r   rR    s    z_CacheControl._del_cache_valuec                 C   s
   t | S z6Convert the stored values into a cache control header.)r   dump_headerr   r   r   r   r,    s    z_CacheControl.to_headerc                 C   s   |   S r#   r-  r   r   r   r   r     s    z_CacheControl.__str__c                 C   s4   d dd t|  D }dt| j d| dS )N c                 s   s    | ]\}}| d |V  qdS =Nr   r   r   r   r   r   	  r   z)_CacheControl.__repr__.<locals>.<genexpr><>r   r   r   r   r   r   Zkv_strr   r   r   rJ     s    z_CacheControl.__repr__)r   N)r   rC   rD   rE   rV  no_cacher]  no_storer   Zmax_ageZno_transformrN   rM  rP  rR  r,  r   rJ   staticmethodZcache_propertyr   r   r   r   rW    s   #
rW  c                   @   s4   e Zd ZdZeddeZeddeZeddeZ	dS )RequestCacheControla  A cache control for requests.  This is immutable and gives access
    to all the request-relevant cache control headers.

    To get a header of the :class:`RequestCacheControl` object again you can
    convert the object into a string or call the :meth:`to_header` method.  If
    you plan to subclass it and add your own items have a look at the sourcecode
    for that class.

    .. versionchanged:: 2.1.0
        Setting int properties such as ``max_age`` will convert the
        value to an int.

    .. versionadded:: 0.5
       In previous versions a `CacheControl` class existed that was used
       both for request and response.
    z	max-staler!  z	min-freshzonly-if-cachedN)
r   rC   rD   rE   rV  r   Z	max_stale	min_freshr]  Zonly_if_cachedr   r   r   r   rj    s   rj  c                   @   sX   e Zd ZdZeddeZedddZeddeZeddeZ	edde
Zed	deZdS )
ResponseCacheControla  A cache control for responses.  Unlike :class:`RequestCacheControl`
    this is mutable and gives access to response-relevant cache control
    headers.

    To get a header of the :class:`ResponseCacheControl` object again you can
    convert the object into a string or call the :meth:`to_header` method.  If
    you plan to subclass it and add your own items have a look at the sourcecode
    for that class.

    .. versionchanged:: 2.1.1
        ``s_maxage`` converts the value to an int.

    .. versionchanged:: 2.1.0
        Setting int properties such as ``max_age`` will convert the
        value to an int.

    .. versionadded:: 0.5
       In previous versions a `CacheControl` class existed that was used
       both for request and response.
    publicNprivater!  zmust-revalidatezproxy-revalidatezs-maxage	immutable)r   rC   rD   rE   rV  r]  rm  rn  Zmust_revalidateZproxy_revalidater   Zs_maxagero  r   r   r   r   rl  &  s   rl  c                    s,   t  fdd fdd fddd S )zReturn a new property object for a content security policy header.
    Useful if you want to add support for a csp extension in a
    subclass.
    c                    s
   |   S r#   )
_get_valuer  rS  r   r   r  J  r   zcsp_property.<locals>.<lambda>c                    s   |   |S r#   )
_set_valuerQ  rS  r   r   r  K  r   c                    s
   |   S r#   )
_del_valuer  rS  r   r   r  L  r   rT  rU  rS  r   rS  r   csp_propertyD  s    


rs  c                   @   s  e Zd ZdZedZedZedZedZedZ	edZ
edZed	Zed
ZedZedZedZedZedZedZedZedZedZedZedZedZedZedZedZedZd+ddZdd  Zd!d" Z d#d$ Z!d%d& Z"d'd( Z#d)d* Z$dS ),ContentSecurityPolicyaV  Subclass of a dict that stores values for a Content Security Policy
    header. It has accessors for all the level 3 policies.

    Because the csp directives in the HTTP header use dashes the
    python descriptors use underscores for that.

    To get a header of the :class:`ContentSecuirtyPolicy` object again
    you can convert the object into a string or call the
    :meth:`to_header` method.  If you plan to subclass it and add your
    own items have a look at the sourcecode for that class.

    .. versionadded:: 1.0.0
       Support for Content Security Policy headers was added.

    zbase-uriz	child-srczconnect-srczdefault-srczfont-srczform-actionzframe-ancestorsz	frame-srczimg-srczmanifest-srcz	media-srcznavigate-toz
object-srczprefetch-srczplugin-typesz	report-toz
report-urisandboxz
script-srczscript-src-attrzscript-src-elemz	style-srczstyle-src-attrzstyle-src-elemz
worker-srcr   Nc                 C   s$   t | |pd || _|d u| _d S rX  rY  rZ  r   r   r   rN   |  s    zContentSecurityPolicy.__init__c                 C   s
   |  |S )r\  r|   r-   r   r   r   rp    s    z ContentSecurityPolicy._get_valuec                 C   s"   |du r|  |d n|| |< dS r[  r   r3   r   r   r   rq    s    z ContentSecurityPolicy._set_valuec                 C   s   || v r| |= dS r[  r   r-   r   r   r   rr    s    z ContentSecurityPolicy._del_valuec                 C   s
   t | S r^  )r   Zdump_csp_headerr   r   r   r   r,    s    zContentSecurityPolicy.to_headerc                 C   s   |   S r#   r-  r   r   r   r   r     s    zContentSecurityPolicy.__str__c                 C   s4   d dd t|  D }dt| j d| dS )Nr`  c                 s   s    | ]\}}| d |V  qdS ra  r   r   r   r   r   r     r   z1ContentSecurityPolicy.__repr__.<locals>.<genexpr>rc  rd  re  rf  r   r   r   rJ     s    zContentSecurityPolicy.__repr__)r   N)%r   rC   rD   rE   rs  Zbase_uriZ	child_srcZconnect_srcZdefault_srcZfont_srcZform_actionZframe_ancestorsZ	frame_srcZimg_srcZmanifest_srcZ	media_srcZnavigate_toZ
object_srcZprefetch_srcZplugin_typesZ	report_toZ
report_uriru  Z
script_srcZscript_src_attrZscript_src_elemZ	style_srcZstyle_src_attrZstyle_src_elemZ
worker_srcrN   rp  rq  rr  r,  r   rJ   r   r   r   r   rt  Q  sB   
rt  c                   @   s"   e Zd ZdZdddZdd ZdS )CallbackDictzwA dict that calls a function passed every time something is changed.
    The function is passed the dict instance.
    Nc                 C   s   t | |pd || _d S rX  r   rN   rq   )r   initialrq   r   r   r   rN     s    zCallbackDict.__init__c                 C   s   dt | j dt|  dS Nrc  r`  rd  r  r   r   r   r   rJ     s    zCallbackDict.__repr__)NN)r   rC   rD   rE   rN   rJ   r   r   r   r   rw    s   
rw  c                   @   s   e Zd ZdZd*ddZdd Zdd Z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d Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdS ),	HeaderSeta  Similar to the :class:`ETags` class this implements a set-like structure.
    Unlike :class:`ETags` this is case insensitive and used for vary, allow, and
    content-language headers.

    If not constructed using the :func:`parse_set_header` function the
    instantiation works like this:

    >>> hs = HeaderSet(['foo', 'bar', 'baz'])
    >>> hs
    HeaderSet(['foo', 'bar', 'baz'])
    Nc                 C   s*   t |pd| _dd | jD | _|| _d S )Nr   c                 S   s   h | ]}|  qS r   r   r   r   r   r   	<setcomp>  r   z%HeaderSet.__init__.<locals>.<setcomp>)r   _headers_setrq   )r   headersrq   r   r   r   rN     s    zHeaderSet.__init__c                 C   s   |  |f dS )zAdd a new header to the set.N)r_   r   headerr   r   r   rg     s    zHeaderSet.addc                 C   sj   |  }|| jvrt|| j| t| jD ] \}}|  |kr0| j|=  qRq0| jdurf| |  dS )aC  Remove a header from the set.  This raises an :exc:`KeyError` if the
        header is not in the set.

        .. versionchanged:: 0.5
            In older versions a :exc:`IndexError` was raised instead of a
            :exc:`KeyError` if the object was missing.

        :param header: the header to be removed.
        N)r   r~  rz   r8   r   r}  rq   )r   r  r   r   r   r   r   r8     s    


zHeaderSet.removec                 C   sX   d}|D ]2}|  }|| jvr| j| | j| d}q|rT| jdurT| |  dS )zAdd all the headers from the iterable to the set.

        :param iterable: updates the set with the items from the iterable.
        FTN)r   r~  r}  r7   rg   rq   )r   r9   Zinserted_anyr  r   r   r   r   r_     s    
zHeaderSet.updatec                 C   s&   z|  | W n ty    Y n0 dS )zdLike :meth:`remove` but ignores errors.

        :param header: the header to be discarded.
        N)r8   rz   r  r   r   r   discard  s    zHeaderSet.discardc                 C   s4   |  }t| jD ]\}}|  |kr|  S qdS )zReturn the index of the header in the set or return -1 if not found.

        :param header: the header to be looked up.
        r>   )r   r   r}  )r   r  r   r6   r   r   r   r)    s
    
zHeaderSet.findc                 C   s   |  |}|dk rt||S )zReturn the index of the header in the set or raise an
        :exc:`IndexError`.

        :param header: the header to be looked up.
        r   )r)  
IndexError)r   r  r'   r   r   r   r?     s    
zHeaderSet.indexc                 C   s.   | j   | jdd= | jdur*| |  dS )zClear the set.N)r~  ra   r}  rq   r   r   r   r   ra   	  s    

zHeaderSet.clearFc                 C   s   |rt | jS t | jS )a  Return the set as real python set type.  When calling this, all
        the items are converted to lowercase and the ordering is lost.

        :param preserve_casing: if set to `True` the items in the set returned
                                will have the original case like in the
                                :class:`HeaderSet`, otherwise they will
                                be lowercase.
        )r   r}  r~  )r   Zpreserve_casingr   r   r   as_set	  s    	
zHeaderSet.as_setc                 C   s   d ttj| jS )r*  r'  )r   r   r   quote_header_valuer}  r   r   r   r   r,  	  s    zHeaderSet.to_headerc                 C   s
   | j | S r#   )r}  )r   r   r   r   r   r   	  s    zHeaderSet.__getitem__c                 C   s4   | j |}| j|  | jd ur0| |  d S r#   )r}  r@   r~  r8   r   rq   )r   r   r'   r   r   r   r.   	  s    
zHeaderSet.__delitem__c                 C   sL   | j | }| j|  || j |< | j|  | jd urH| |  d S r#   )r}  r~  r8   r   rg   rq   )r   r   r   oldr   r   r   r4   %	  s    


zHeaderSet.__setitem__c                 C   s   |  | jv S r#   )r   r~  r  r   r   r   r   -	  s    zHeaderSet.__contains__c                 C   s
   t | jS r#   )r   r~  r   r   r   r   r   0	  s    zHeaderSet.__len__c                 C   s
   t | jS r#   )r   r}  r   r   r   r   r   3	  s    zHeaderSet.__iter__c                 C   s
   t | jS r#   )r]  r~  r   r   r   r   __bool__6	  s    zHeaderSet.__bool__c                 C   s   |   S r#   r-  r   r   r   r   r   9	  s    zHeaderSet.__str__c                 C   s   t | j d| jdS rG   )r   r   r}  r   r   r   r   rJ   <	  s    zHeaderSet.__repr__)NN)F)r   rC   rD   rE   rN   rg   r8   r_   r  r)  r?   ra   r  r,  r   r.   r4   r   r   r   r  r   rJ   r   r   r   r   r{    s(   


r{  c                   @   s   e Zd ZdZd"ddZd#ddZdd	 Z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d Zdd Zd d! ZdS )%ETagszYA set that can be used to check if one etag is present in a collection
    of etags.
    NFc                 C   s4   |s|rt || _nt  | _t |p$d| _|| _d S rX  )rX   _strong_weakstar_tag)r   Zstrong_etagsZ
weak_etagsr  r   r   r   rN   E	  s
    zETags.__init__c                 C   s   t | j}|r|| j |S )zoConvert the `ETags` object into a python set.  Per default all the
        weak etags are not part of this set.)r   r  r_   r  )r   include_weakr'   r   r   r   r  N	  s    
zETags.as_setc                 C   s
   || j v S )zCheck if an etag is weak.)r  r   etagr   r   r   is_weakV	  s    zETags.is_weakc                 C   s
   || j v S )zCheck if an etag is strong.)r  r  r   r   r   	is_strongZ	  s    zETags.is_strongc                 C   s   |  |p| |S )zCCheck if an etag is part of the set including weak and strong tags.)r  containsr  r   r   r   contains_weak^	  s    zETags.contains_weakc                 C   s   | j r
dS | |S )zxCheck if an etag is part of the set ignoring weak tags.
        It is also possible to use the ``in`` operator.
        T)r  r  r  r   r   r   r  b	  s    zETags.containsc                 C   s&   t |\}}|r| |S | |S )zWhen passed a quoted tag it will check if this tag is part of the
        set.  If the tag is weak it is checked against weak and strong tags,
        otherwise strong only.)r   Zunquote_etagr  r  )r   r  Zweakr   r   r   contains_rawj	  s    
zETags.contains_rawc                 C   s0   | j r
dS ddd | jD dd | jD  S )z0Convert the etags set into a HTTP header string.r!  r'  c                 S   s   g | ]}d | d qS )"r   r   r   r   r   r   x	  r   z#ETags.to_header.<locals>.<listcomp>c                 S   s   g | ]}d | dqS )zW/"r  r   r   r   r   r   r   x	  r   )r  r   r  r  r   r   r   r   r,  s	  s
    zETags.to_headerc                 C   sH   ||g d dkrtd|d u r,t|}|r>|| jv r>dS || jv S )Nr	   z-either tag or data required, but at least oneT)countr   r   Zgenerate_etagr  r  )r   r  datar  r   r   r   __call__{	  s    

zETags.__call__c                 C   s   t | jp| jp| jS r#   )r]  r  r  r  r   r   r   r   r  	  s    zETags.__bool__c                 C   s   |   S r#   r-  r   r   r   r   r   	  s    zETags.__str__c                 C   s
   t | jS r#   )r   r  r   r   r   r   r   	  s    zETags.__len__c                 C   s
   t | jS r#   )r   r  r   r   r   r   r   	  s    zETags.__iter__c                 C   s
   |  |S r#   )r  r  r   r   r   r   	  s    zETags.__contains__c                 C   s   dt | j dt| dS rz  r   r   r   r   r   r   r   rJ   	  s    zETags.__repr__)NNF)F)NNF)r   rC   rD   rE   rN   r  r  r  r  r  r  r,  r  r  r   r   r   r   rJ   r   r   r   r   r  @	  s    
	
	

r  c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )IfRangezVery simple object that represents the `If-Range` header in parsed
    form.  It will either have neither a etag or date or one of either but
    never both.

    .. versionadded:: 0.7
    Nc                 C   s   || _ || _d S r#   )r  date)r   r  r  r   r   r   rN   	  s    zIfRange.__init__c                 C   s0   | j durt| j S | jdur,t| jS dS )-Converts the object back into an HTTP header.N )r  r   Z	http_dater  Z
quote_etagr   r   r   r   r,  	  s
    

zIfRange.to_headerc                 C   s   |   S r#   r-  r   r   r   r   r   	  s    zIfRange.__str__c                 C   s   dt | j dt| dS rz  r  r   r   r   r   rJ   	  s    zIfRange.__repr__)NN)r   rC   rD   rE   rN   r,  r   rJ   r   r   r   r   r  	  s
   
r  c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )RangeaX  Represents a ``Range`` header. All methods only support only
    bytes as the unit. Stores a list of ranges if given, but the methods
    only work if only one range is provided.

    :raise ValueError: If the ranges provided are invalid.

    .. versionchanged:: 0.15
        The ranges passed in are validated.

    .. versionadded:: 0.7
    c                 C   sP   || _ || _|D ]:\}}|d u s8|d ur|dk s8||krt||f dqd S )Nr   z is not a valid range.)unitsrangesr{   )r   r  r  startendr   r   r   rN   	  s
     zRange.__init__c                 C   sn   | j dks |du s t| jdkr$dS | jd \}}|du rN|}|dk rN||7 }t|||rj|t||fS dS )zIf the range is for bytes, the length is not None and there is
        exactly one range and it is satisfiable it returns a ``(start, stop)``
        tuple, otherwise `None`.
        r   Nr	   r   )r  r   r  r   is_byte_range_validmin)r   lengthr  r  r   r   r   range_for_length	  s     zRange.range_for_lengthc                 C   s.   |  |}|dur*t| j|d |d |S dS )zCreates a :class:`~werkzeug.datastructures.ContentRange` object
        from the current range and given content length.
        Nr   r	   )r  ContentRanger  )r   r  rngr   r   r   make_content_range	  s    
zRange.make_content_rangec                 C   sl   g }| j D ]J\}}|du r<||dkr0| dnt| q
|| d|d   q
| j dd| S )r  Nr   r   r	   rb  r+  )r  r7   r   r  r   )r   r  beginr  r   r   r   r,  	  s    "zRange.to_headerc                 C   s>   |  |}|dur:| j d|d  d|d d  d| S dS )z\Converts the object into `Content-Range` HTTP header,
        based on given length
        Nr`  r   r   r	   r9  )r  r  )r   r  ranger   r   r   to_content_range_header	  s    
(zRange.to_content_range_headerc                 C   s   |   S r#   r-  r   r   r   r   r   	  s    zRange.__str__c                 C   s   dt | j dt| dS rz  r  r   r   r   r   rJ   	  s    zRange.__repr__N)r   rC   rD   rE   rN   r  r  r,  r  r   rJ   r   r   r   r   r  	  s   	
	r  c                    s"    fdd} fdd}t ||S )Nc                    s
   t |  S r#   )ro   r   rs   r   r   fget
  s    z _callback_property.<locals>.fgetc                    s$   t |  | | jd ur | |  d S r#   )setattrrq   r   rs   r   r   fset
  s    
z _callback_property.<locals>.fsetrU  )rt   r  r  r   rs   r   _callback_property
  s    r  c                   @   sl   e Zd ZdZdddZedZedZedZedZ	dd
dZ
dd Zdd Zdd Zdd Zdd ZdS )r  zDRepresents the content range header.

    .. versionadded:: 0.7
    Nc                 C   s0   t |||sJ d|| _| |||| d S )NBad range provided)r   r  rq   r   )r   r  r  stopr  rq   r   r   r   rN   
  s    zContentRange.__init___units_start_stop_lengthr   c                 C   sF   t |||sJ d|| _|| _|| _|| _| jdurB| |  dS )z#Simple method to update the ranges.r  N)r   r  r  r  r  r  rq   )r   r  r  r  r  r   r   r   r   "
  s    
zContentRange.setc                 C   s   | j dddd dS )zcSets the units to `None` which indicates that the header should
        no longer be used.
        Nr  r   r   r   r   r   unset,
  s    zContentRange.unsetc                 C   sb   | j d u rdS | jd u rd}n| j}| jd u r>| j  d| S | j  d| j d| jd  d| S )Nr  r!  z */r`  r   r	   r9  )r  r  r  r  )r   r  r   r   r   r,  2
  s    


zContentRange.to_headerc                 C   s
   | j d uS r#   r  r   r   r   r   r  =
  s    zContentRange.__bool__c                 C   s   |   S r#   r-  r   r   r   r   r   @
  s    zContentRange.__str__c                 C   s   dt | j dt| dS rz  r  r   r   r   r   rJ   C
  s    zContentRange.__repr__)NN)Nr   )r   rC   rD   rE   rN   r  r  r  r  r  r   r  r,  r  r   rJ   r   r   r   r   r  
  s   


r  c                   @   s   e Zd ZdZdddZedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zdd ZdS )Authorizationa=  Represents an ``Authorization`` header sent by the client.

    This is returned by
    :func:`~werkzeug.http.parse_authorization_header`. It can be useful
    to create the object manually to pass to the test
    :class:`~werkzeug.test.Client`.

    .. versionchanged:: 0.5
        This object became immutable.
    Nc                 C   s   t | |pi  || _d S r#   )r   rN   r   )r   	auth_typer  r   r   r   rN   S
  s    zAuthorization.__init__c                 C   s
   |  dS )zdThe username transmitted.  This is set for both basic and digest
        auth all the time.
        usernamerv  r   r   r   r   r  W
  s    zAuthorization.usernamec                 C   s
   |  dS )zsWhen the authentication type is basic this is the password
        transmitted by the client, else `None`.
        passwordrv  r   r   r   r   r  ^
  s    zAuthorization.passwordc                 C   s
   |  dS )z8This is the server realm sent back for HTTP digest auth.realmrv  r   r   r   r   r  e
  s    zAuthorization.realmc                 C   s
   |  dS )zThe nonce the server sent for digest auth, sent back by the client.
        A nonce should be unique for every 401 response for HTTP digest auth.
        noncerv  r   r   r   r   r  j
  s    zAuthorization.noncec                 C   s
   |  dS )zThe URI from Request-URI of the Request-Line; duplicated because
        proxies are allowed to change the Request-Line in transit.  HTTP
        digest auth only.
        urirv  r   r   r   r   r  q
  s    zAuthorization.uric                 C   s
   |  dS )zzThe nonce count value transmitted by clients if a qop-header is
        also transmitted.  HTTP digest auth only.
        ncrv  r   r   r   r   r  y
  s    zAuthorization.ncc                 C   s
   |  dS )zIf the server sent a qop-header in the ``WWW-Authenticate``
        header, the client has to provide this value for HTTP digest auth.
        See the RFC for more details.
        cnoncerv  r   r   r   r   r  
  s    zAuthorization.cnoncec                 C   s
   |  dS )zA string of 32 hex digits computed as defined in RFC 2617, which
        proves that the user knows a password.  Digest auth only.
        responserv  r   r   r   r   r  
  s    zAuthorization.responsec                 C   s
   |  dS )zThe opaque header from the server returned unchanged by the client.
        It is recommended that this string be base64 or hexadecimal data.
        Digest auth only.
        opaquerv  r   r   r   r   r  
  s    zAuthorization.opaquec                 C   s
   |  dS )zIndicates what "quality of protection" the client has applied to
        the message for HTTP digest auth. Note that this is a single token,
        not a quoted list of alternatives as in WWW-Authenticate.
        qoprv  r   r   r   r   r  
  s    zAuthorization.qopc                 C   sf   | j dkr6t| j d| j  d}d| S | j dkrPdt|  S t	d| j dd	S )
zConvert to a string value for an ``Authorization`` header.

        .. versionadded:: 2.0
            Added to support passing authorization to the test client.
        basic:utf8zBasic digestzDigest zUnsupported type .N)
r   base64	b64encoder  r  r   r   r   r_  r{   r   r   r   r   r,  
  s    


zAuthorization.to_header)N)r   rC   rD   rE   rN   r4  r  r  r  r  r  r  r  r  r  r  r,  r   r   r   r   r  G
  s.   










r  c                    s"    fdd}t  fdd||dS )al  A static helper function for Authentication subclasses to add
    extra authentication system properties onto a class::

        class FooAuthenticate(WWWAuthenticate):
            special_realm = auth_property('special_realm')

    For more information have a look at the sourcecode to see how the
    regular properties (:attr:`realm` etc.) are implemented.
    c                    s&   |d u r|   d  nt||  < d S r#   )r@   r   r   rs   r   r   rq  
  s    z!auth_property.<locals>._set_valuec                    s
   |   S r#   rv  r  rs   r   r   r  
  r   zauth_property.<locals>.<lambda>docrU  )rt   r  rq  r   rs   r   auth_property
  s    r  c                    s    fdd}t ||dS )Nc                    s     fdd}t  |S )Nc                    s(   | s v r = n| r$|    < d S r#   r-  )Z
header_set)rt   r   r   r   rq   
  s    z._set_property.<locals>.fget.<locals>.on_update)r   Zparse_set_headerr|   )r   rq   rs   r   r   r  
  s    z_set_property.<locals>.fgetr  rU  )rt   r  r  r   rs   r   _set_property
  s    	r  c                   @   s   e Zd ZdZeg dZd%ddZd&ddZd'ddZdd Z	dd Z
dd ZedddZedddZedddZedddZedddZedddZed d!dZed"d# Zejd$d# ZeeZdS )(WWWAuthenticatez5Provides simple access to `WWW-Authenticate` headers.)domainr  r  r  r  Nc                 C   s&   t | |pd |r|| d< || _d S )Nr   __auth_type__rx  )r   r  r   rq   r   r   r   rN   
  s    zWWWAuthenticate.__init__authentication requiredc                 C   s0   t |  t | d|d | jr,| |  dS )z*Clear the auth info and enable basic auth.r  )r  r  N)r   ra   r_   rq   )r   r  r   r   r   	set_basic
  s    
zWWWAuthenticate.set_basicauthFc                 C   sj   d||t |d}|r d|d< |dur0||d< |dur@||d< t|  t| | | jrf| |  dS )z+Clear the auth info and enable digest auth.r  )r  r  r  r  TRUEstaleNr  	algorithm)r   r_  r   ra   r_   rq   )r   r  r  r  r  r  r  r  r   r   r   
set_digest
  s    
zWWWAuthenticate.set_digestc                    sT   t  }|ddpd} fdd| D }ddd |D }|  d	| S )
z9Convert the stored values into a WWW-Authenticate header.r  Nr  c                 3   s*   | ]"\}}|t j|| jvd fV  qdS ))Zallow_tokenN)r   r  _require_quotingr   r   r   r   r   
  s   z,WWWAuthenticate.to_header.<locals>.<genexpr>r'  c                 S   s   g | ]\}}| d | qS )rb  r   r   r   r   r   r     r   z-WWWAuthenticate.to_header.<locals>.<listcomp>r`  )r   r@   r   r   r  )r   r  r  Zkv_itemsZ	kv_stringr   r   r   r,  
  s    
zWWWAuthenticate.to_headerc                 C   s   |   S r#   r-  r   r   r   r   r     s    zWWWAuthenticate.__str__c                 C   s   dt | j d|  dS rz  )r   r   r,  r   r   r   r   rJ     s    zWWWAuthenticate.__repr__r  zZThe type of the auth mechanism. HTTP currently specifies
        ``Basic`` and ``Digest``.r  r  a  A string to be displayed to users so they know which
        username and password to use. This string should contain at
        least the name of the host performing the authentication and
        might additionally indicate the collection of users who might
        have access.r  zA list of URIs that define the protection space. If a URI
        is an absolute path, it is relative to the canonical root URL of
        the server being accessed.r  z
        A server-specified data string which should be uniquely generated
        each time a 401 response is made. It is recommended that this
        string be base64 or hexadecimal data.r  a  A string of data, specified by the server, which should
        be returned by the client unchanged in the Authorization header
        of subsequent requests with URIs in the same protection space.
        It is recommended that this string be base64 or hexadecimal
        data.r  a   A string indicating a pair of algorithms used to produce
        the digest and a checksum. If this is not present it is assumed
        to be "MD5". If the algorithm is not understood, the challenge
        should be ignored (and a different one used, if there is more
        than one).r  zIA set of quality-of-privacy directives such as auth and
        auth-int.c                 C   s"   |  d}|dur| dkS dS )z}A flag, indicating that the previous request from the client
        was rejected because the nonce value was stale.
        r  Ntrue)r|   r   )r   valr   r   r   r  ;  s    
zWWWAuthenticate.stalec                 C   s*   |d u r|  dd  n|rdnd| d< d S )Nr  r  FALSEr   r   r   r   r   r  D  s    )NNN)r  )r  NNF)r   rC   rD   rE   rX   r  rN   r  r  r,  r   rJ   r  r   r  r  r  r  r  r  r  r4  r  setterri  r   r   r   r   r  
  sT   

 


r  c                   @   s   e Zd ZdZdddZdd Zedd Zed	d
 Zedd Z	edd Z
dddZdd Zdd Zdd Zdd Zdd ZdS )r  a@  The :class:`FileStorage` class is a thin wrapper over incoming files.
    It is used by the request object to represent uploaded files.  All the
    attributes of the wrapper stream are proxied by the file storage so
    it's possible to do ``storage.read()`` instead of the long form
    ``storage.stream.read()``.
    Nc                 C   s   || _ |pt | _|d u rZt|dd }|d ur8t|}|rd|d dkrd|d dkrdd }n
t|}|| _|d u rxt }|| _|d ur||d< |d urt	||d< d S )Nrt   r   rc  r>   rd  zContent-TypezContent-Length)
rt   r   streamro   osfsdecoder  r   r  r   )r   r  r  rt   r  content_lengthr  r   r   r   rN   V  s"    	

zFileStorage.__init__c                 C   s   t | dst| j| _d S )N_parsed_content_type)hasattrr   Zparse_options_headerr  r  r   r   r   r   _parse_content_typez  s    
zFileStorage._parse_content_typec                 C   s   | j dS )z;The content-type sent in the header.  Usually not availablezcontent-type)r  r|   r   r   r   r   r  ~  s    zFileStorage.content_typec                 C   s0   zt | jdpdW S  ty*   Y dS 0 dS )z=The content-length sent in the header.  Usually not availablezcontent-lengthr   N)r   r  r|   r{   r   r   r   r   r    s    zFileStorage.content_lengthc                 C   s   |    | jd  S )a  Like :attr:`content_type`, but without parameters (eg, without
        charset, type etc.) and always lowercase.  For example if the content
        type is ``text/HTML; charset=utf-8`` the mimetype would be
        ``'text/html'``.

        .. versionadded:: 0.7
        r   )r  r  r   r   r   r   r   mimetype  s    	zFileStorage.mimetypec                 C   s   |    | jd S )zThe mimetype parameters as dict.  For example if the content
        type is ``text/html; charset=utf-8`` the params would be
        ``{'charset': 'utf-8'}``.

        .. versionadded:: 0.7
        r	   )r  r  r   r   r   r   mimetype_params  s    zFileStorage.mimetype_params @  c                 C   sl   ddl m} d}t|dr"t|}t|tr:t|d}d}z|| j|| W |rh|  n|rf|  0 dS )an  Save the file to a destination path or file object.  If the
        destination is a file object you have to close it yourself after the
        call.  The buffer size is the number of bytes held in memory during
        the copy process.  It defaults to 16KB.

        For secure file saving also have a look at :func:`secure_filename`.

        :param dst: a filename, :class:`os.PathLike`, or open file
            object to write to.
        :param buffer_size: Passed as the ``length`` parameter of
            :func:`shutil.copyfileobj`.

        .. versionchanged:: 1.0
            Supports :mod:`pathlib`.
        r   )copyfileobjF
__fspath__wbTN)	shutilr  r  r   r   r   r  r  close)r   dstbuffer_sizer  Z	close_dstr   r   r   save  s    



zFileStorage.savec                 C   s&   z| j   W n ty    Y n0 dS )z&Close the underlying file if possible.N)r  r  	Exceptionr   r   r   r   r    s    zFileStorage.closec                 C   s
   t | jS r#   )r]  r  r   r   r   r   r    s    zFileStorage.__bool__c                 C   sF   zt | j|W S  ty@   t| jdr:t | jj| Y S  Y n0 d S )N_file)ro   r  AttributeErrorr  r  r   r   r   r   __getattr__  s    zFileStorage.__getattr__c                 C   s
   t | jS r#   )r   r  r   r   r   r   r     s    zFileStorage.__iter__c                 C   s"   dt | j d| jd| jdS )Nrc  r   z (z)>)r   r   r  r  r   r   r   r   rJ     s    zFileStorage.__repr__)NNNNNN)r  )r   rC   rD   rE   rN   r  r4  r  r  r  r  r  r  r  r  r   rJ   r   r   r   r   r  N  s.   	      
$





!r  )r   )N)N)Hr  rI  r  r  recollections.abcr   r   r   r   ior   	itertoolsr   r   r  r
   	_internalr   r   r!   r"   r   rF   rK   rd   rv   r   rp   ry   r}   r   r   r   r   r   r   r   r   r  r  r  r  r  r  compiler5  r7  r8  rA  rB  rC  rH  rV  rW  rj  rl  rs  rt  rw  r{  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r   <module>   s   52")  N  6   H53 ! -
B
6cL XK:j

| 