U
    mf.(                     @   sv   d dl Z d dlmZ G dd dZG dd deZG dd deZG d	d
 d
eZ	G dd deZ
G dd deZdS )    N)Mappingc                   @   sb   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S )
OrderedSetz?
    A set which keeps the ordering of the inserted items.
    Nc                 C   s   t |p
d| _ d S )N )dictfromkeys)selfiterabler   r   ?/tmp/pip-unpacked-wheel-siwebuq3/django/utils/datastructures.py__init__
   s    zOrderedSet.__init__c                 C   s   d | j |< d S Nr   r   itemr   r   r	   add   s    zOrderedSet.addc                 C   s   | j |= d S r   r   r   r   r   r	   remove   s    zOrderedSet.removec                 C   s(   z|  | W n tk
r"   Y nX d S r   )r   KeyErrorr   r   r   r	   discard   s    zOrderedSet.discardc                 C   s
   t | jS r   )iterr   r   r   r   r	   __iter__   s    zOrderedSet.__iter__c                 C   s
   t | jS r   )reversedr   r   r   r   r	   __reversed__   s    zOrderedSet.__reversed__c                 C   s
   || j kS r   r   r   r   r   r	   __contains__   s    zOrderedSet.__contains__c                 C   s
   t | jS r   )boolr   r   r   r   r	   __bool__"   s    zOrderedSet.__bool__c                 C   s
   t | jS r   )lenr   r   r   r   r	   __len__%   s    zOrderedSet.__len__c                 C   s,   | j rtt| j nd}| jj d| dS )N ())r   reprlist	__class____qualname__r   datar   r   r	   __repr__(   s    zOrderedSet.__repr__)N)__name__
__module__r#   __doc__r
   r   r   r   r   r   r   r   r   r&   r   r   r   r	   r      s   
r   c                   @   s   e Zd ZdS )MultiValueDictKeyErrorN)r'   r(   r#   r   r   r   r	   r*   -   s   r*   c                       s   e Zd ZdZd/ fdd	Z fddZ fddZ fd	d
Zdd Zdd Z	dd Z
dd Zd0ddZd1 fdd	Zd2ddZ fddZd3ddZd4dd Zd!d" Zd#d$ Z fd%d&Zd'd( Zd)d* Zd+d, Zd-d. Z  ZS )5MultiValueDicta  
    A subclass of dictionary customized to handle multiple values for the
    same key.

    >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
    >>> d['name']
    'Simon'
    >>> d.getlist('name')
    ['Adrian', 'Simon']
    >>> d.getlist('doesnotexist')
    []
    >>> d.getlist('doesnotexist', ['Adrian', 'Simon'])
    ['Adrian', 'Simon']
    >>> d.get('lastname', 'nonexistent')
    'nonexistent'
    >>> d.setlist('lastname', ['Holovaty', 'Willison'])

    This class exists to solve the irritating problem raised by cgi.parse_qs,
    which returns a list for every key, even though most web forms submit
    single name-value pairs.
    r   c                    s   t  | d S r   )superr
   )r   Zkey_to_list_mappingr"   r   r	   r
   H   s    zMultiValueDict.__init__c                    s   d| j jt  f S )Nz<%s: %s>)r"   r'   r,   r&   r   r-   r   r	   r&   K   s    zMultiValueDict.__repr__c                    sV   zt  |}W n tk
r,   t|Y nX z
|d W S  tk
rP   g  Y S X dS )z|
        Return the last data value for this key, or [] if it's an empty list;
        raise KeyError if not found.
        N)r,   __getitem__r   r*   
IndexErrorr   keylist_r-   r   r	   r/   N   s    
zMultiValueDict.__getitem__c                    s   t  ||g d S r   r,   __setitem__r   r2   valuer-   r   r	   r5   \   s    zMultiValueDict.__setitem__c                 C   s   |  dd |  D S )Nc                 S   s    g | ]\}}||d d  fqS r   r   .0kvr   r   r	   
<listcomp>`   s     z+MultiValueDict.__copy__.<locals>.<listcomp>)r"   listsr   r   r   r	   __copy___   s    zMultiValueDict.__copy__c              	   C   sJ   |   }||t| < t| D ]&\}}t|t||t|| q|S r   )r"   idr   itemsr5   copydeepcopy)r   memoresultr2   r7   r   r   r	   __deepcopy__b   s     
 
zMultiValueDict.__deepcopy__c                    s    j d fdd D iS )N_datac                    s   i | ]}|  |qS r   _getlist)r9   r:   r   r   r	   
<dictcomp>l   s      z/MultiValueDict.__getstate__.<locals>.<dictcomp>)__dict__r   r   r   r	   __getstate__k   s    zMultiValueDict.__getstate__c                 C   s:   | di }| D ]\}}| || q| j| d S )NrF   )popr@   setlistrJ   update)r   Zobj_dictr%   r:   r;   r   r   r	   __setstate__n   s    zMultiValueDict.__setstate__Nc                 C   s6   z| | }W n t k
r$   | Y S X |g kr2|S |S )z
        Return the last data value for the passed key. If key doesn't exist
        or value is an empty list, return `default`.
        )r   )r   r2   defaultvalr   r   r	   gett   s    
zMultiValueDict.getFc                    sZ   zt  |}W n( tk
r8   |dkr0g  Y S | Y S X |rR|dk	rNt|nd}|S dS )z
        Return a list of values for the key.

        Used internally to manipulate values list. If force_list is True,
        return a new copy of values.
        N)r,   r/   r   r!   )r   r2   rP   
force_listvaluesr-   r   r	   rH      s    
zMultiValueDict._getlistc                 C   s   | j ||ddS )zn
        Return the list of values for the key. If key doesn't exist, return a
        default value.
        T)rS   rG   r   r2   rP   r   r   r	   getlist   s    zMultiValueDict.getlistc                    s   t  || d S r   r4   r1   r-   r   r	   rM      s    zMultiValueDict.setlistc                 C   s   || kr|| |< | | S r   r   rU   r   r   r	   
setdefault   s    zMultiValueDict.setdefaultc                 C   s*   || kr |d krg }|  || | |S r   )rM   rH   )r   r2   Zdefault_listr   r   r	   setlistdefault   s
    zMultiValueDict.setlistdefaultc                 C   s   |  || dS )z8Append an item to the internal list associated with key.N)rX   appendr6   r   r   r	   
appendlist   s    zMultiValueDict.appendlistc                 c   s   | D ]}|| | fV  qdS )zu
        Yield (key, value) pairs, where value is the last item in the list
        associated with the key.
        Nr   r   r2   r   r   r	   r@      s    zMultiValueDict.itemsc                    s   t t  S )zYield (key, list) pairs.)r   r,   r@   r   r-   r   r	   r=      s    zMultiValueDict.listsc                 c   s   | D ]}| | V  qdS )z'Yield the last value on every key list.Nr   r[   r   r   r	   rT      s    zMultiValueDict.valuesc                 C   s
   t  | S )z%Return a shallow copy of this object.)rA   r   r   r   r	   rA      s    zMultiValueDict.copyc                 O   s   t |dkrtdt | |r|d }t|trV| D ]\}}| || q:n0t|trh| }|D ]\}}| |	| ql| D ]\}}| |	| qdS )z.Extend rather than replace existing key lists.   z*update expected at most 1 argument, got %dr   N)
r   	TypeError
isinstancer+   r=   rX   extendr   r@   rY   )r   argskwargsargr2   Z
value_listr7   r   r   r	   rN      s    

zMultiValueDict.updatec                    s    fdd D S )z5Return current object as a dict with singular values.c                    s   i | ]}| | qS r   r   )r9   r2   r   r   r	   rI      s      z'MultiValueDict.dict.<locals>.<dictcomp>r   r   r   r   r	   r      s    zMultiValueDict.dict)r   )N)NF)N)N)N)r'   r(   r#   r)   r
   r&   r/   r5   r>   rE   rK   rO   rR   rH   rV   rM   rW   rX   rZ   r@   r=   rT   rA   rN   r   __classcell__r   r   r-   r	   r+   1   s,   	



	r+   c                   @   sZ   e Zd ZdZddddZdd ZeZeZeZeZ	eZ
eZeZeZeZeZeZeZeZdS )	ImmutableLista0  
    A tuple-like object that raises useful errors when it is asked to mutate.

    Example::

        >>> a = ImmutableList(range(5), warning="You cannot mutate this.")
        >>> a[3] = '4'
        Traceback (most recent call last):
            ...
        AttributeError: You cannot mutate this.
    z"ImmutableList object is immutable.)warningc                O   s   t j| f||}||_|S r   )tuple__new__re   )clsre   r`   ra   r   r   r   r	   rg      s    zImmutableList.__new__c                 O   s   t | jd S r   )AttributeErrorre   )r   r`   ra   r   r   r	   complain   s    zImmutableList.complainN)r'   r(   r#   r)   rg   rj   __delitem__Z__delslice____iadd____imul__r5   Z__setslice__rY   r_   insertrL   r   sortreverser   r   r   r	   rd      s    rd   c                       s,   e Zd ZdZ fddZ fddZ  ZS )DictWrapperaG  
    Wrap accesses to a dictionary so that certain values (those starting with
    the specified prefix) are passed through a function before being returned.
    The prefix is removed before looking up the real value.

    Used by the SQL construction code to ensure that values are correctly
    quoted before being used.
    c                    s   t  | || _|| _d S r   )r,   r
   funcprefix)r   r%   rr   rs   r-   r   r	   r
     s    zDictWrapper.__init__c                    s@   | | j}|r"|t| jd }t |}|r<| |S |S )z
        Retrieve the real value after stripping the prefix string (if
        present). If the prefix is present, pass the value through self.func
        before returning, otherwise return the raw value.
        N)
startswithrs   r   r,   r/   rr   )r   r2   Zuse_funcr7   r-   r   r	   r/     s    
zDictWrapper.__getitem__)r'   r(   r#   r)   r
   r/   rc   r   r   r-   r	   rq     s   	rq   c                   @   sT   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
edd ZdS )CaseInsensitiveMappinga  
    Mapping allowing case-insensitive key lookups. Original case of keys is
    preserved for iteration and string representation.

    Example::

        >>> ci_map = CaseInsensitiveMapping({'name': 'Jane'})
        >>> ci_map['Name']
        Jane
        >>> ci_map['NAME']
        Jane
        >>> ci_map['name']
        Jane
        >>> ci_map  # original case preserved
        {'name': 'Jane'}
    c                 C   s   dd |  |D | _d S )Nc                 S   s   i | ]\}}|  ||fqS r   lowerr8   r   r   r	   rI   2  s      z3CaseInsensitiveMapping.__init__.<locals>.<dictcomp>)_unpack_items_storer$   r   r   r	   r
   1  s    zCaseInsensitiveMapping.__init__c                 C   s   | j |  d S )Nr\   )ry   rw   r[   r   r   r	   r/   4  s    z"CaseInsensitiveMapping.__getitem__c                 C   s
   t | jS r   )r   ry   r   r   r   r	   r   7  s    zCaseInsensitiveMapping.__len__c                 C   s.   t |to,dd |  D dd | D kS )Nc                 S   s   i | ]\}}|  |qS r   rv   r8   r   r   r	   rI   ;  s     z1CaseInsensitiveMapping.__eq__.<locals>.<dictcomp>c                 S   s   i | ]\}}|  |qS r   rv   r8   r   r   r	   rI   =  s      )r^   r   r@   )r   otherr   r   r	   __eq__:  s
    zCaseInsensitiveMapping.__eq__c                 C   s   dd | j  D S )Nc                 s   s   | ]\}}|V  qd S r   r   )r9   Zoriginal_keyr7   r   r   r	   	<genexpr>@  s     z2CaseInsensitiveMapping.__iter__.<locals>.<genexpr>)ry   rT   r   r   r   r	   r   ?  s    zCaseInsensitiveMapping.__iter__c                 C   s   t dd | j D S )Nc                 S   s   i | ]\}}||qS r   r   )r9   r2   r7   r   r   r	   rI   C  s      z3CaseInsensitiveMapping.__repr__.<locals>.<dictcomp>)r    ry   rT   r   r   r   r	   r&   B  s    zCaseInsensitiveMapping.__repr__c                 C   s   | S r   r   r   r   r   r	   rA   E  s    zCaseInsensitiveMapping.copyc                 c   sz   t | ttfr |  E d H  d S t| D ]L\}}t|dkrPtd|t|t |d tsntd|d  |V  q(d S )N   zDdictionary update sequence element #{} has length {}; 2 is required.r   z0Element key %r invalid, only strings are allowed)	r^   r   r   r@   	enumerater   
ValueErrorformatstr)r%   ielemr   r   r	   rx   H  s      
z$CaseInsensitiveMapping._unpack_itemsN)r'   r(   r#   r)   r
   r/   r   r{   r   r&   rA   staticmethodrx   r   r   r   r	   ru     s   ru   )rA   collections.abcr   r   r   r*   r   r+   rf   rd   rq   ru   r   r   r   r	   <module>   s   ( ,%