3 hf+,@sddlmZyddlmZmZWn$ek rDddlmZmZYnXyddlmZWn"ek rxGdddZYnXddlm Z ddl m Z dd l m Z mZmZd d gZeZGd d d eZGd d d eZdS))absolute_import)MappingMutableMapping)RLockc@seZdZddZddZdS)rcCsdS)N)selfrr"/usr/lib/python3.6/_collections.py __enter__ szRLock.__enter__cCsdS)Nr)rexc_type exc_value tracebackrrr__exit__ szRLock.__exit__N)__name__ __module__ __qualname__r r rrrrr sr) OrderedDict) InvalidHeader)iterkeys itervaluesPY3RecentlyUsedContainerHTTPHeaderDictc@sVeZdZdZeZdddZddZdd Zd d Z d d Z ddZ ddZ ddZ dS)ra Provides a thread-safe dict-like container which maintains up to ``maxsize`` keys while throwing away the least-recently-used keys beyond ``maxsize``. :param maxsize: Maximum number of recent elements to retain. :param dispose_func: Every time an item is evicted from the container, ``dispose_func(value)`` is called. Callback which will get called NcCs"||_||_|j|_t|_dS)N)_maxsize dispose_func ContainerCls _containerrlock)rmaxsizerrrr__init__,s zRecentlyUsedContainer.__init__c Cs,|j|jj|}||j|<|SQRXdS)N)rrpop)rkeyitemrrr __getitem__3s  z!RecentlyUsedContainer.__getitem__c Cslt}|j@|jj|t}||j|<t|j|jkrF|jjdd\}}WdQRX|jrh|tk rh|j|dS)NF)Zlast)_Nullrrgetlenrpopitemr)rr"valueZ evicted_valueZ_keyrrr __setitem__:s z!RecentlyUsedContainer.__setitem__c Cs2|j|jj|}WdQRX|jr.|j|dS)N)rrr!r)rr"r)rrr __delitem__Isz!RecentlyUsedContainer.__delitem__c Cs|j t|jSQRXdS)N)rr'r)rrrr__len__PszRecentlyUsedContainer.__len__cCs tddS)Nz7Iteration over this class is unlikely to be threadsafe.)NotImplementedError)rrrr__iter__TszRecentlyUsedContainer.__iter__c CsL|jtt|j}|jjWdQRX|jrHx|D]}|j|q6WdS)N)rlistrrclearr)rvaluesr)rrrr0Ws  zRecentlyUsedContainer.clearc Cs |jtt|jSQRXdS)N)rr/rr)rrrrkeysaszRecentlyUsedContainer.keys)rN)rrr__doc__rrr r$r*r+r,r.r0r2rrrrrs   cseZdZdZd/fdd ZddZddZd d Zd d Zd dZ ddZ e sZe j Z e jZeZddZddZefddZddZddZddZefddZdd ZeZeZeZeZd!d"Zd#d$Zd%d&Zd'd(Z d)d*Z!d+d,Z"e#d-d.Z$Z%S)0rap :param headers: An iterable of field-value pairs. Must not contain multiple field names when compared case-insensitively. :param kwargs: Additional field-value pairs to pass in to ``dict.update``. A ``dict`` like container for storing HTTP Headers. Field names are stored and compared case-insensitively in compliance with RFC 7230. Iteration provides the first case-sensitive key seen for each case-insensitive pair. Using ``__setitem__`` syntax overwrites fields that compare equal case-insensitively in order to maintain ``dict``'s api. For fields that compare equal, instead create a new ``HTTPHeaderDict`` and use ``.add`` in a loop. If multiple fields that are equal case-insensitively are passed to the constructor or ``.update``, the behavior is undefined and some will be lost. >>> headers = HTTPHeaderDict() >>> headers.add('Set-Cookie', 'foo=bar') >>> headers.add('set-cookie', 'baz=quxx') >>> headers['content-length'] = '7' >>> headers['SET-cookie'] 'foo=bar, baz=quxx' >>> headers['Content-Length'] '7' Nc sPtt|jt|_|dk r>t|tr4|j|n |j||rL|j|dS)N)superrr rr isinstance _copy_fromextend)rheaderskwargs) __class__rrr s   zHTTPHeaderDict.__init__cCs ||g|j|j<|j|jS)N)rlower)rr"valrrrr*szHTTPHeaderDict.__setitem__cCs |j|j}dj|ddS)Nz, r)rr;join)rr"r<rrrr$szHTTPHeaderDict.__getitem__cCs|j|j=dS)N)rr;)rr"rrrr+szHTTPHeaderDict.__delitem__cCs|j|jkS)N)r;r)rr"rrr __contains__szHTTPHeaderDict.__contains__cCsbt|t rt|d rdSt|t|s6t||}tdd|jDtdd|jDkS)Nr2Fcss|]\}}|j|fVqdS)N)r;).0kvrrr sz(HTTPHeaderDict.__eq__..css|]\}}|j|fVqdS)N)r;)r?r@rArrrrBs)r5rhasattrtypedict itermerged)rotherrrr__eq__s  zHTTPHeaderDict.__eq__cCs |j| S)N)rH)rrGrrr__ne__szHTTPHeaderDict.__ne__cCs t|jS)N)r'r)rrrrr,szHTTPHeaderDict.__len__ccs"x|jjD]}|dVq WdS)Nr)rr1)rvalsrrrr.szHTTPHeaderDict.__iter__c Cs<y ||}Wn tk r,||jkr(|SX||=|SdS)zD.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised. N)KeyError_HTTPHeaderDict__marker)rr"defaultr)rrrr!s  zHTTPHeaderDict.popc Cs$y ||=Wntk rYnXdS)N)rK)rr"rrrdiscards zHTTPHeaderDict.discardcCs4|j}||g}|jj||}||k r0|j|dS)zAdds a (name, value) pair, doesn't overwrite the value if it already exists. >>> headers = HTTPHeaderDict(foo='bar') >>> headers.add('Foo', 'baz') >>> headers['foo'] 'bar, baz' N)r;r setdefaultappend)rr"r<Z key_lowerZnew_valsrJrrradds zHTTPHeaderDict.addcOst|dkrtdjt|t|dkr2|dnf}t|trdx|jD]\}}|j||qJWnvt|trxj|D]}|j|||qtWnLt|drx@|j D]}|j|||qWnx|D]\}}|j||qWx |j D]\}}|j||qWdS)zGeneric import function for any type of header-like object. Adapted version of MutableMapping.update in order to insert items with self.add instead of self.__setitem__ rz9extend() takes at most 1 positional arguments ({0} given)rr2N) r' TypeErrorformatr5r iteritemsrQrrCr2items)rargsr9rGr"r<r)rrrr7s"      zHTTPHeaderDict.extendc CsFy|j|j}Wn"tk r4||jkr0gS|SX|ddSdS)zmReturns a list of all the values for the named field. Returns an empty list if the key doesn't exist.rN)rr;rKrL)rr"rMrJrrrgetlists zHTTPHeaderDict.getlistcCs.dddddddg}x|D]}|j|qW|S)z Remove content-specific header fields before changing the request method to GET or HEAD according to RFC 9110, Section 15.4. zContent-EncodingzContent-LanguagezContent-Locationz Content-TypezContent-LengthZDigestz Last-Modified)rN)rZcontent_specific_headersheaderrrr_prepare_for_method_changes z)HTTPHeaderDict._prepare_for_method_changecCsdt|jt|jfS)Nz%s(%s))rDrrErF)rrrr__repr__!szHTTPHeaderDict.__repr__cCsBx<|D]4}|j|}t|tr&t|}|g||j|j<qWdS)N)rWr5r/rr;)rrGr"r<rrrr6$s    zHTTPHeaderDict._copy_fromcCst|}|j||S)N)rDr6)rZclonerrrcopy,s  zHTTPHeaderDict.copyccsDx>|D]6}|j|j}x"|ddD]}|d|fVq&WqWdS)z8Iterate over all header lines, including duplicate ones.rNr)rr;)rr"rJr<rrrrT1s zHTTPHeaderDict.iteritemsccs<x6|D].}|j|j}|ddj|ddfVqWdS)z:Iterate over all headers, merging duplicate ones together.rz, rN)rr;r=)rr"r<rrrrF8s zHTTPHeaderDict.itermergedcCs t|jS)N)r/rT)rrrrrU>szHTTPHeaderDict.itemscCsd}g}xr|jD]h}|j|rV|s0td|n&|d\}}||d|jf|d<q|jdd\}}|j||jfqW||S) z4Read headers from a Python 2 httplib message object.  z/Header continuation with no previous header: %sr:)r\r]r_)r8 startswithrstripsplitrP)clsmessageZobs_fold_continued_leadersr8liner"r)rrr from_httplibAs    zHTTPHeaderDict.from_httplib)N)&rrrr3r r*r$r+r>rHrIrrrrobjectrLr,r.r!rNrQr7rWrYZ getheadersZgetallmatchingheadersZigetZget_allrZr6r[rTrFrU classmethodrf __classcell__rr)r:rrfs>    N)Z __future__rcollections.abcrr ImportError collectionsZ threadingrr exceptionsrZ packages.sixrrr__all__rgr%rrrrrrs   J