o 3a&@s~ddlZddlmZGdddZGdddeZGdddeZGd d d eZ Gd d d eZ d dZ GdddeZ dS)N)Mappingc@sReZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ dS) OrderedSetz? A set which keeps the ordering of the inserted items. NcCst|pd|_dS)N)dictfromkeys)selfiterablerr=/usr/lib/python3/dist-packages/django/utils/datastructures.py__init__ zOrderedSet.__init__cCsd|j|<dSNrritemrrr add szOrderedSet.addcCs |j|=dSr r rrrr removes zOrderedSet.removecCs&z||WdStyYdSwr )rKeyErrorrrrr discards  zOrderedSet.discardcC t|jSr )iterrrrrr __iter__ zOrderedSet.__iter__cCs ||jvSr r rrrr __contains__rzOrderedSet.__contains__cCrr )boolrrrrr __bool__rzOrderedSet.__bool__cCrr )lenrrrrr __len__"rzOrderedSet.__len__r ) __name__ __module__ __qualname____doc__r rrrrrrrrrrr rs  rc@s eZdZdS)MultiValueDictKeyErrorN)rrr rrrr r"&sr"cseZdZdZd/fdd ZfddZfddZfd d Zd d Zd dZ ddZ ddZ d0ddZ d1fdd Z d0ddZfddZd0ddZd0dd Zd!d"Zd#d$Zfd%d&Zd'd(Zd)d*Zd+d,Zd-d.ZZS)2MultiValueDicta 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. rcst|dSr )superr )rkey_to_list_mapping __class__rr r @szMultiValueDict.__init__csd|jjtfS)Nz<%s: %s>)r'rr$__repr__rr&rr r(CszMultiValueDict.__repr__csJzt|}Wn tyt|wz|dWSty$gYSw)z| Return the last data value for this key, or [] if it's an empty list; raise KeyError if not found. )r$ __getitem__rr" IndexErrorrkeylist_r&rr r*Fs   zMultiValueDict.__getitem__cst||gdSr r$ __setitem__rr-valuer&rr r0Tr zMultiValueDict.__setitem__cCs|dd|DS)NcSs g|] \}}||ddfqSr r.0kvrrr Xsz+MultiValueDict.__copy__..)r'listsrrrr __copy__Ws zMultiValueDict.__copy__c CsJ|}||t|<t|D]\}}t|t||t||q|Sr )r'idritemsr0copydeepcopy)rmemoresultr-r2rrr __deepcopy__]s  zMultiValueDict.__deepcopy__cs ijdfddDiS)N_datacsi|]}||qSr_getlist)r4r5rrr fsz/MultiValueDict.__getstate__..)__dict__rrrr __getstate__es zMultiValueDict.__getstate__cCs:|di}|D] \}}|||q |j|dS)NrA)popr;setlistrEupdate)robj_dictdatar5r6rrr __setstate__hs zMultiValueDict.__setstate__NcCs4z||}Wn ty|YSw|gkr|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)rr-defaultvalrrr getns  zMultiValueDict.getFcsTzt|}Wnty|durgYS|YSw|r(|dur&t|nd}|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*rlist)rr-rM force_listvaluesr&rr rC{s zMultiValueDict._getlistcCs|j||ddS)zn Return the list of values for the key. If key doesn't exist, return a default value. T)rQrBrr-rMrrr getlistszMultiValueDict.getlistcst||dSr r/r,r&rr rHzMultiValueDict.setlistcCs||vr|||<||Sr rrSrrr setdefaultszMultiValueDict.setdefaultcCs*||vr|dur g}|||||Sr )rHrC)rr- default_listrrr setlistdefaults   zMultiValueDict.setlistdefaultcCs|||dS)z8Append an item to the internal list associated with key.N)rXappendr1rrr appendlistszMultiValueDict.appendlistccs|D] }|||fVqdS)zu Yield (key, value) pairs, where value is the last item in the list associated with the key. Nrrr-rrr r;szMultiValueDict.itemscsttS)zYield (key, list) pairs.)rr$r;rr&rr r8szMultiValueDict.listsccs|D]}||VqdS)z'Yield the last value on every key list.Nrr[rrr rRs zMultiValueDict.valuescCs t|S)z%Return a shallow copy of this object.)r<rrrr r<s zMultiValueDict.copycOst|dkrtdt||rC|d}t|tr+|D] \}}|||qnt|tr4|}|D] \}}|| |q6|D] \}}|| |qGdS)z.Extend rather than replace existing key lists.z*update expected at most 1 argument, got %drN) r TypeError isinstancer#r8rXextendrr;rY)rargskwargsargr- value_listr2rrr rIs    zMultiValueDict.updatecsfddDS)z5Return current object as a dict with singular values.csi|]}||qSrr)r4r-rrr rDz'MultiValueDict.dict..rrrrr rszMultiValueDict.dict)rr )NF)rrr r!r r(r*r0r9r@rFrLrOrCrTrHrVrXrZr;r8rRr<rIr __classcell__rrr&r r#*s.          r#c@sZeZdZdZddddZddZeZeZeZeZ eZ eZ eZ eZ eZeZeZeZeZdS) 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.)warningcOs"tj|g|Ri|}||_|Sr )tuple__new__rg)clsrgr`rarrrr riszImmutableList.__new__cOs t|jr )AttributeErrorrg)rr`rarrr complainrzImmutableList.complainN)rrr r!rirl __delitem__ __delslice____iadd____imul__r0 __setslice__rYr_insertrGrsortreverserrrr rfs" rfcs,eZdZdZfddZfddZZS) 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. cst|||_||_dSr )r$r funcprefix)rrKrvrwr&rr r s  zDictWrapper.__init__cs@||j}|r|t|jd}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) startswithrwrr$r*rv)rr-use_funcr2r&rr r* s   zDictWrapper.__getitem__)rrr r!r r*rerrr&r rus ruccs`t|D](\}}t|dkrtd|t|t|dts(td|dt|VqdS)NzDdictionary update sequence element #{} has length {}; 2 is required.rz0Element key %r invalid, only strings are allowed) enumerater ValueErrorformatr^strrh)rKielemrrr !_destruct_iterable_mapping_valuess   rc@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)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'} cCs4t|tsddt|D}dd|D|_dS)NcSi|]\}}||qSrrr3rrr rD8rdz3CaseInsensitiveMapping.__init__..cSsi|] \}}|||fqSrlowerr3rrr rD9s)r^rrr;_store)rrKrrr r 6s zCaseInsensitiveMapping.__init__cCs|j|dS)Nr\)rrr[rrr r*;rUz"CaseInsensitiveMapping.__getitem__cCrr )rrrrrr r>rzCaseInsensitiveMapping.__len__cCs.t|todd|Ddd|DkS)NcSi|] \}}||qSrrr3rrr rDBz1CaseInsensitiveMapping.__eq__..cSrrrr3rrr rDDr)r^rr;)rotherrrr __eq__AszCaseInsensitiveMapping.__eq__cCsdd|jDS)Ncss|]\}}|VqdSr r)r4 original_keyr2rrr Isz2CaseInsensitiveMapping.__iter__..)rrRrrrr rHr zCaseInsensitiveMapping.__iter__cCstdd|jDS)NcSrrr)r4r-r2rrr rDLrdz3CaseInsensitiveMapping.__repr__..)reprrrRrrrr r(KszCaseInsensitiveMapping.__repr__cCs|Sr rrrrr r<NszCaseInsensitiveMapping.copyN) rrr r!r r*rrrr(r<rrrr r$s r) r<collections.abcrrrr"rr#rhrfrurrrrrr s !-%