3
š(ƒh$  ã               @   st   d dl Z d dlmZ d dlm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)ÚOrderedDict)Úsixc               @   sZ   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S )Ú
OrderedSetze
    A set which keeps the ordering of the inserted items.
    Currently backs onto OrderedDict.
    Nc             C   s    t |rdd„ |D ƒng ƒ| _d S )Nc             s   s   | ]}|d fV  qd S )N© )Ú.0Úxr   r   ú?/tmp/pip-install-q3hcpn_q/Django/django/utils/datastructures.pyú	<genexpr>   s    z&OrderedSet.__init__.<locals>.<genexpr>)r   Údict)ÚselfÚiterabler   r   r   Ú__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 |ƒ W n tk
r"   Y nX d S )N)r   ÚKeyError)r   r   r   r   r   Údiscard   s    zOrderedSet.discardc             C   s   t | jjƒ ƒS )N)Úiterr
   Úkeys)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)Útyper   )r   r   r   r   Ú__nonzero__%   s    zOrderedSet.__nonzero__c             C   s
   t | jƒS )N)Úlenr
   )r   r   r   r   Ú__len__(   s    zOrderedSet.__len__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__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f f‡ fdd„	Z‡ fdd„Z‡ fdd„Z‡ fdd	„Zd
d„ Zd1dd„Z	dd„ Z
dd„ Zd2dd„Zd3‡ fdd„	Z‡ fdd„Zd4dd„Zd5dd„Zdd„ Zdd „ Z‡ fd!d"„Zd#d$„ ZejrÒeZeZeZn$eZeZeZd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Z‡  Z S )6Ú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 t| ƒj|ƒ d S )N)Úsuperr"   r   )r   Zkey_to_list_mapping)Ú	__class__r   r   r   F   s    zMultiValueDict.__init__c                s   d| j jtt| ƒjƒ f S )Nz<%s: %s>)r$   r   r#   r"   Ú__repr__)r   )r$   r   r   r%   I   s    zMultiValueDict.__repr__c                sX   yt t| ƒj|ƒ}W n  tk
r4   tt|ƒƒ‚Y nX y|d S  tk
rR   g S X dS )z~
        Returns the last data value for this key, or [] if it's an empty list;
        raises KeyError if not found.
        é   Néÿÿÿÿ)r#   r"   Ú__getitem__r   r!   ÚreprÚ
IndexError)r   ÚkeyÚlist_)r$   r   r   r(   M   s    zMultiValueDict.__getitem__c                s   t t| ƒj||gƒ d S )N)r#   r"   Ú__setitem__)r   r+   Úvalue)r$   r   r   r-   [   s    zMultiValueDict.__setitem__c             C   s   | j dd„ | jƒ D ƒƒS )Nc             S   s    g | ]\}}||d d … f‘qS )Nr   )r   ÚkÚvr   r   r   ú
<listcomp>`   s   z+MultiValueDict.__copy__.<locals>.<listcomp>)r$   Úlists)r   r   r   r   Ú__copy__^   s    zMultiValueDict.__copy__Nc             C   sZ   |d kri }| j ƒ }||t| ƒ< x4tj| ƒD ]&\}}tj|tj||ƒtj||ƒƒ q,W |S )N)r$   Úidr
   Úitemsr-   ÚcopyÚdeepcopy)r   ÚmemoÚresultr+   r.   r   r   r   Ú__deepcopy__d   s    zMultiValueDict.__deepcopy__c                s$   ˆ j jƒ }‡ fdd„ˆ D ƒ|d< |S )Nc                s   i | ]}ˆ j |ƒ|“qS r   )Úgetlist)r   r/   )r   r   r   ú
<dictcomp>p   s    z/MultiValueDict.__getstate__.<locals>.<dictcomp>Ú_data)Ú__dict__r6   )r   Úobj_dictr   )r   r   Ú__getstate__n   s    
zMultiValueDict.__getstate__c             C   s>   |j di ƒ}x |jƒ D ]\}}| j||ƒ qW | jj|ƒ d S )Nr=   )Úpopr5   Úsetlistr>   Úupdate)r   r?   Údatar/   r0   r   r   r   Ú__setstate__s   s    zMultiValueDict.__setstate__c             C   s2   y| | }W n t k
r    |S X |g kr.|S |S )z“
        Returns the last data value for the passed key. If key doesn't exist
        or value is an empty list, then default is returned.
        )r   )r   r+   ÚdefaultÚvalr   r   r   Úgety   s    zMultiValueDict.getc                s6   yt t| ƒj|ƒS  tk
r0   |dkr,g S |S X dS )z€
        Returns the list of values for the passed key. If key doesn't exist,
        then a default value is returned.
        N)r#   r"   r(   r   )r   r+   rF   )r$   r   r   r;   †   s    zMultiValueDict.getlistc                s   t t| ƒj||ƒ d S )N)r#   r"   r-   )r   r+   r,   )r$   r   r   rB   ’   s    zMultiValueDict.setlistc             C   s   || kr|| |< | | S )Nr   )r   r+   rF   r   r   r   Ú
setdefault•   s    zMultiValueDict.setdefaultc             C   s*   || kr |d krg }| j ||ƒ | j|ƒS )N)rB   r;   )r   r+   Zdefault_listr   r   r   Úsetlistdefaultœ   s
    zMultiValueDict.setlistdefaultc             C   s   | j |ƒj|ƒ dS )z9Appends an item to the internal list associated with key.N)rJ   Úappend)r   r+   r.   r   r   r   Ú
appendlist¥   s    zMultiValueDict.appendlistc             c   s    x| D ]}|| | fV  qW dS )zv
        Yields (key, value) pairs, where value is the last item in the list
        associated with the key.
        Nr   )r   r+   r   r   r   Ú
_iteritems©   s    
zMultiValueDict._iteritemsc                s   t jtt| ƒƒS )zYields (key, list) pairs.)r   Ú	iteritemsr#   r"   )r   )r$   r   r   Ú
_iterlists±   s    zMultiValueDict._iterlistsc             c   s   x| D ]}| | V  qW dS )z'Yield the last value on every key list.Nr   )r   r+   r   r   r   Ú_itervaluesµ   s    
zMultiValueDict._itervaluesc             C   s   t | jƒ ƒS )N)ÚlistrN   )r   r   r   r   r5   Ã   s    zMultiValueDict.itemsc             C   s   t | jƒ ƒS )N)rQ   Ú	iterlists)r   r   r   r   r2   Æ   s    zMultiValueDict.listsc             C   s   t | jƒ ƒS )N)rQ   Ú
itervalues)r   r   r   r   ÚvaluesÉ   s    zMultiValueDict.valuesc             C   s
   t j | ƒS )z&Returns a shallow copy of this object.)r6   )r   r   r   r   r6   Ì   s    zMultiValueDict.copyc             O   sÎ   t |ƒdkrtdt |ƒ ƒ‚|r¢|d }t|tƒrZxn|jƒ D ]\}}| j|ƒj|ƒ q<W nHy*x$|jƒ D ]\}}| j|ƒj|ƒ qfW W n tk
r    t	dƒ‚Y nX x&t
j|ƒD ]\}}| j|ƒj|ƒ q®W dS )zn
        update() extends rather than replaces existing key lists.
        Also accepts keyword args.
        r&   z+update expected at most 1 arguments, got %dr   zCMultiValueDict.update() takes either a MultiValueDict or dictionaryN)r   Ú	TypeErrorÚ
isinstancer"   r2   rJ   Úextendr5   rK   Ú
ValueErrorr   rN   )r   ÚargsÚkwargsÚ
other_dictr+   Z
value_listr.   r   r   r   rC   Ð   s    
zMultiValueDict.updatec                s   ‡ fdd„ˆ D ƒS )zH
        Returns current object as a dict with singular values.
        c                s   i | ]}ˆ | |“qS r   r   )r   r+   )r   r   r   r<   é   s    z'MultiValueDict.dict.<locals>.<dictcomp>r   )r   r   )r   r   r
   å   s    zMultiValueDict.dict)N)N)N)N)N)!r   r   r   r    r   r%   r(   r-   r3   r:   r@   rE   rH   r;   rB   rI   rJ   rL   rM   rO   rP   r   ÚPY3r5   r2   rT   rN   rR   rS   r6   rC   r
   Ú__classcell__r   r   )r$   r   r"   0   s>   




	r"   c               @   sT   e Zd ZdZ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.
    c             O   s8   d|kr|d }|d= nd}t j| f|ž|Ž}||_|S )NÚwarningz"ImmutableList object is immutable.)ÚtupleÚ__new__r_   )ÚclsrY   rZ   r_   r   r   r   r   ra   ù   s    zImmutableList.__new__c             O   s"   t | jtƒr| j‚n
t| jƒ‚d S )N)rV   r_   Ú	ExceptionÚAttributeError)r   ZwargsrZ   r   r   r   Úcomplain  s    zImmutableList.complainN)r   r   r   r    ra   re   Ú__delitem__Z__delslice__Ú__iadd__Ú__imul__r-   Z__setslice__rK   rW   ÚinsertrA   r   ÚsortÚreverser   r   r   r   r^   ì   s    
r^   c                   s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚDictWrapperaH  
    Wraps 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 t| ƒj|ƒ || _|| _d S )N)r#   rl   r   ÚfuncÚprefix)r   rD   rm   rn   )r$   r   r   r   "  s    zDictWrapper.__init__c                sJ   |j | jƒr$d}|t| jƒd… }nd}tt| ƒj|ƒ}|rF| j|ƒS |S )z×
        Retrieves 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.
        TNF)Ú
startswithrn   r   r#   rl   r(   rm   )r   r+   Zuse_funcr.   )r$   r   r   r(   '  s    
zDictWrapper.__getitem__)r   r   r   r    r   r(   r]   r   r   )r$   r   rl     s   rl   )r6   Úcollectionsr   Zdjango.utilsr   Úobjectr   r   r!   r
   r"   r`   r^   rl   r   r   r   r   Ú<module>   s   % =-