
v^'                 @   s   d  d l  Z  d  d l m Z Gd d   d  Z Gd d   d e  Z Gd d   d e  Z Gd	 d
   d
 e  Z	 Gd d   d e  Z
 d d   Z Gd d   d e  Z d S)    N)Mappingc               @   sy   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 S)
OrderedSetz?
    A set which keeps the ordering of the inserted items.
    Nc             C   s   t  j | p f   |  _  d  S)N)dictfromkeys)selfiterable r   =/tmp/pip-build-8lau8j11/django/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)N)r   )r   r   r   r   r	   remove   s    zOrderedSet.removec             C   s*   y |  j  |  Wn t k
 r% Yn Xd  S)N)r   KeyError)r   r   r   r   r	   discard   s    zOrderedSet.discardc             C   s   t  |  j  S)N)iterr   )r   r   r   r	   __iter__   s    zOrderedSet.__iter__c             C   s   | |  j  k S)N)r   )r   r   r   r   r	   __contains__   s    zOrderedSet.__contains__c             C   s   t  |  j  S)N)boolr   )r   r   r   r	   __bool__   s    zOrderedSet.__bool__c             C   s   t  |  j  S)N)lenr   )r   r   r   r	   __len__"   s    zOrderedSet.__len__)__name__
__module____qualname____doc__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                   sQ  e  Z d  Z d Z f    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 d d d  Z d d   f d d  Z d d d  Z   f d d   Z d d d  Z d d d  Z d  d!   Z d" d#   Z   f d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z   S).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.
    c                s   t    j |  d  S)N)superr
   )r   Zkey_to_list_mapping)	__class__r   r	   r
   @   s    zMultiValueDict.__init__c                s   d |  j  j t   j   f S)Nz<%s: %s>)r   r   r   __repr__)r   )r   r   r	   r   C   s    zMultiValueDict.__repr__c                s`   y t    j |  } Wn t k
 r6 t |   Yn Xy | d SWn t k
 r[ g  SYn Xd 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"   F   s    zMultiValueDict.__getitem__c                s   t    j | | g  d  S)N)r   __setitem__)r   r$   value)r   r   r	   r&   T   s    zMultiValueDict.__setitem__c             C   s    |  j  d d   |  j   D  S)Nc             S   s,   g  |  ]" \ } } | | d  d   f  q S)Nr   ).0kvr   r   r	   
<listcomp>Y   s   	z+MultiValueDict.__copy__.<locals>.<listcomp>)r   lists)r   r   r   r	   __copy__W   s    	zMultiValueDict.__copy__c             C   sk   |  j    } | | t |   <xH t j |   D]7 \ } } t j | t j | |  t j | |   q, W| S)N)r   idr   itemsr&   copydeepcopy)r   memoresultr$   r'   r   r   r	   __deepcopy__]   s    zMultiValueDict.__deepcopy__c                s&     j  d   f d d     D i  S)N_datac                s"   i  |  ] }   j  |  |  q Sr   )_getlist)r(   r)   )r   r   r	   
<dictcomp>f   s   	 z/MultiValueDict.__getstate__.<locals>.<dictcomp>)__dict__)r   r   )r   r	   __getstate__e   s    zMultiValueDict.__getstate__c             C   sS   | j  d i   } x* | j   D] \ } } |  j | |  q W|  j j |  d  S)Nr5   )popr/   setlistr8   update)r   Zobj_dictdatar)   r*   r   r   r	   __setstate__h   s    zMultiValueDict.__setstate__Nc             C   s;   y |  | } Wn t  k
 r& | SYn X| g  k r7 | 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   r$   defaultvalr   r   r	   getn   s    	zMultiValueDict.getFc                sk   y t    j |  } Wn& t k
 r> | d k r6 g  S| SYn) X| rc | d k	 r] t |  n d } | Sd 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   list)r   r$   r?   
force_listvalues)r   r   r	   r6   {   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.
        rC   T)r6   )r   r$   r?   r   r   r	   getlist   s    zMultiValueDict.getlistc                s   t    j | |  d  S)N)r   r&   )r   r$   r%   )r   r   r	   r;      s    zMultiValueDict.setlistc             C   s   | |  k r | |  | <|  | S)Nr   )r   r$   r?   r   r   r	   
setdefault   s    
zMultiValueDict.setdefaultc             C   s;   | |  k r. | d  k r g  } |  j  | |  |  j |  S)N)r;   r6   )r   r$   Zdefault_listr   r   r	   setlistdefault   s
    zMultiValueDict.setlistdefaultc             C   s   |  j  |  j |  d S)z8Append an item to the internal list associated with key.N)rG   append)r   r$   r'   r   r   r	   
appendlist   s    zMultiValueDict.appendlistc             c   s$   x |  D] } | |  | f Vq Wd S)zu
        Yield (key, value) pairs, where value is the last item in the list
        associated with the key.
        Nr   )r   r$   r   r   r	   r/      s    zMultiValueDict.itemsc                s   t  t   j    S)zYield (key, list) pairs.)r   r   r/   )r   )r   r   r	   r,      s    zMultiValueDict.listsc             c   s   x |  D] } |  | Vq Wd S)z'Yield the last value on every key list.Nr   )r   r$   r   r   r	   rD      s    zMultiValueDict.valuesc             C   s   t  j  |   S)z%Return a shallow copy of this object.)r0   )r   r   r   r	   r0      s    zMultiValueDict.copyc             O   s  t  |  d k r( t d t  |    | r | d } t | t  r} x | j   D]" \ } } |  j |  j |  qT WnX y7 x0 | j   D]" \ } } |  j |  j |  q WWn t k
 r t	 d   Yn Xx0 | j   D]" \ } } |  j |  j |  q Wd S)z.Extend rather than replace existing key lists.r    z*update expected at most 1 argument, got %dr   zCMultiValueDict.update() takes either a MultiValueDict or dictionaryN)
r   	TypeError
isinstancer   r,   rG   extendr/   rH   
ValueError)r   argskwargs
other_dictr$   Z
value_listr'   r   r   r	   r<      s    
zMultiValueDict.updatec                s     f d d     D S)z5Return current object as a dict with singular values.c                s   i  |  ] }   | |  q Sr   r   )r(   r$   )r   r   r	   r7      s   	 z'MultiValueDict.dict.<locals>.<dictcomp>r   )r   r   )r   r	   r      s    zMultiValueDict.dict)r   r   r   r   r
   r   r"   r&   r-   r4   r9   r>   rA   r6   rE   r;   rF   rG   rI   r/   r,   rD   r0   r<   r   r   r   )r   r	   r   *   s,   	r   c               @   s   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.
    warningz"ImmutableList object is immutable.c            O   s"   t  j |  | |  } | | _ | S)N)tuple__new__rR   )clsrR   rN   rO   r   r   r   r	   rT      s    	zImmutableList.__new__c             O   s1   t  |  j t  r |  j  n t |  j   d  S)N)rK   rR   	ExceptionAttributeError)r   ZwargsrO   r   r   r	   complain   s    zImmutableList.complainN)r   r   r   r   rT   rX   __delitem__Z__delslice____iadd____imul__r&   Z__setslice__rH   rL   insertr:   r   sortreverser   r   r   r	   rQ      s    rQ   c                   s:   e  Z d  Z d Z   f d d   Z   f d d   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    j |  | |  _ | |  _ d  S)N)r   r
   funcprefix)r   r=   r`   ra   )r   r   r	   r
     s    	zDictWrapper.__init__c                sZ   | j  |  j  } | r1 | t |  j  d  } t   j |  } | rV |  j |  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)
startswithra   r   r   r"   r`   )r   r$   Zuse_funcr'   )r   r   r	   r"     s    zDictWrapper.__getitem__)r   r   r   r   r
   r"   r   r   )r   r	   r_      s   r_   c             c   s   x| t  |   D]n \ } } t |  d k rI t d j | t |     t | d t  sp t d | d   t |  Vq Wd  S)N   zDdictionary update sequence element #{} has length {}; 2 is required.r   z0Element key %r invalid, only strings are allowed)	enumerater   rM   formatrK   strrS   )r=   ielemr   r   r	   !_destruct_iterable_mapping_values  s    ri   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   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   sH   t  | t  s( d d   t |  D } d d   | j   D |  _ d  S)Nc             S   s   i  |  ] \ } } | |  q Sr   r   )r(   r)   r*   r   r   r	   r7   <  s   	 z3CaseInsensitiveMapping.__init__.<locals>.<dictcomp>c             S   s+   i  |  ]! \ } } | | f | j     q Sr   )lower)r(   r)   r*   r   r   r	   r7   =  s   	 )rK   r   ri   r/   _store)r   r=   r   r   r	   r
   :  s    zCaseInsensitiveMapping.__init__c             C   s   |  j  | j   d S)Nr    )rl   rk   )r   r$   r   r   r	   r"   ?  s    z"CaseInsensitiveMapping.__getitem__c             C   s   t  |  j  S)N)r   rl   )r   r   r   r	   r   B  s    zCaseInsensitiveMapping.__len__c             C   s?   t  | t  o> d d   |  j   D d d   | j   D k S)Nc             S   s%   i  |  ] \ } } | | j     q Sr   )rk   )r(   r)   r*   r   r   r	   r7   F  s   	z1CaseInsensitiveMapping.__eq__.<locals>.<dictcomp>c             S   s%   i  |  ] \ } } | | j     q Sr   )rk   )r(   r)   r*   r   r   r	   r7   H  s   	)rK   r   r/   )r   otherr   r   r	   __eq__E  s    	zCaseInsensitiveMapping.__eq__c             C   s   d d   |  j  j   D S)Nc             s   s   |  ] \ } } | Vq d  S)Nr   )r(   Zoriginal_keyr'   r   r   r	   	<genexpr>M  s    z2CaseInsensitiveMapping.__iter__.<locals>.<genexpr>)rl   rD   )r   r   r   r	   r   L  s    zCaseInsensitiveMapping.__iter__c             C   s    t  d d   |  j j   D  S)Nc             S   s   i  |  ] \ } } | |  q Sr   r   )r(   r$   r'   r   r   r	   r7   P  s   	 z3CaseInsensitiveMapping.__repr__.<locals>.<dictcomp>)reprrl   rD   )r   r   r   r	   r   O  s    zCaseInsensitiveMapping.__repr__c             C   s   |  S)Nr   )r   r   r   r	   r0   R  s    zCaseInsensitiveMapping.copyN)r   r   r   r   r
   r"   r   rn   r   r   r0   r   r   r   r	   rj   (  s   rj   )r0   collections.abcr   r   r   r   r   r   rS   rQ   r_   ri   rj   r   r   r   r	   <module>   s   !(