<s> from collections import Mapping , MutableMapping <EOL> try : <EOL> from threading import RLock <EOL> except ImportError : <EOL> class RLock : <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> pass <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> from . packages . ordered_dict import OrderedDict <EOL> from . packages . six import iterkeys , itervalues , PY3 <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _Null = object ( ) <EOL> class RecentlyUsedContainer ( MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> ContainerCls = OrderedDict <EOL> def __init__ ( self , maxsize = <NUM_LIT:10> , dispose_func = None ) : <EOL> self . _maxsize = maxsize <EOL> self . dispose_func = dispose_func <EOL> self . _container = self . ContainerCls ( ) <EOL> self . lock = RLock ( ) <EOL> def __getitem__ ( self , key ) : <EOL> with self . lock : <EOL> item = self . _container . pop ( key ) <EOL> self . _container [ key ] = item <EOL> return item <EOL> def __setitem__ ( self , key , value ) : <EOL> evicted_value = _Null <EOL> with self . lock : <EOL> evicted_value = self . _container . get ( key , _Null ) <EOL> self . _container [ key ] = value <EOL> if len ( self . _container ) > self . _maxsize : <EOL> _key , evicted_value = self . _container . popitem ( last = False ) <EOL> if self . dispose_func and evicted_value is not _Null : <EOL> self . dispose_func ( evicted_value ) <EOL> def __delitem__ ( self , key ) : <EOL> with self . lock : <EOL> value = self . _container . pop ( key ) <EOL> if self . dispose_func : <EOL> self . dispose_func ( value ) <EOL> def __len__ ( self ) : <EOL> with self . lock : <EOL> return len ( self . _container ) <EOL> def __iter__ ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def clear ( self ) : <EOL> with self . lock : <EOL> values = list ( itervalues ( self . _container ) ) <EOL> self . _container . clear ( ) <EOL> if self . dispose_func : <EOL> for value in values : <EOL> self . dispose_func ( value ) <EOL> def keys ( self ) : <EOL> with self . lock : <EOL> return list ( iterkeys ( self . _container ) ) <EOL> _dict_setitem = dict . __setitem__ <EOL> _dict_getitem = dict . __getitem__ <EOL> _dict_delitem = dict . __delitem__ <EOL> _dict_contains = dict . __contains__ <EOL> _dict_setdefault = dict . setdefault <EOL> class HTTPHeaderDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , headers = None , ** kwargs ) : <EOL> dict . __init__ ( self ) <EOL> if headers is not None : <EOL> if isinstance ( headers , HTTPHeaderDict ) : <EOL> self . _copy_from ( headers ) <EOL> else : <EOL> self . extend ( headers ) <EOL> if kwargs : <EOL> self . extend ( kwargs ) <EOL> def __setitem__ ( self , key , val ) : <EOL> return _dict_setitem ( self , key . lower ( ) , ( key , val ) ) <EOL> def __getitem__ ( self , key ) : <EOL> val = _dict_getitem ( self , key . lower ( ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( val [ <NUM_LIT:1> : ] ) <EOL> def __delitem__ ( self , key ) : <EOL> return _dict_delitem ( self , key . lower ( ) ) <EOL> def __contains__ ( self , key ) : <EOL> return _dict_contains ( self , key . lower ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Mapping ) and not hasattr ( other , '<STR_LIT>' ) : <EOL> return False <EOL> if not isinstance ( other , type ( self ) ) : <EOL> other = type ( self ) ( other ) <EOL> return dict ( ( k1 , self [ k1 ] ) for k1 in self ) == dict ( ( k2 , other [ k2 ] ) for k2 in other ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> values = MutableMapping . values <EOL> get = MutableMapping . get <EOL> update = MutableMapping . update <EOL> if not PY3 : <EOL> iterkeys = MutableMapping . iterkeys <EOL> itervalues = MutableMapping . itervalues <EOL> __marker = object ( ) <EOL> def pop ( self , key , default = __marker ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> value = self [ key ] <EOL> except KeyError : <EOL> if default is self . __marker : <EOL> raise <EOL> return default <EOL> else : <EOL> del self [ key ] <EOL> return value <EOL> def discard ( self , key ) : <EOL> try : <EOL> del self [ key ] <EOL> except KeyError : <EOL> pass <EOL> def add ( self , key , val ) : <EOL> """<STR_LIT>""" <EOL> key_lower = key . lower ( ) <EOL> new_vals = key , val <EOL> vals = _dict_setdefault ( self , key_lower , new_vals ) <EOL> if new_vals is not vals : <EOL> if isinstance ( vals , list ) : <EOL> vals . append ( val ) <EOL> else : <EOL> _dict_setitem ( self , key_lower , [ vals [ <NUM_LIT:0> ] , vals [ <NUM_LIT:1> ] , val ] ) <EOL> def extend ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( len ( args ) ) ) <EOL> other = args [ <NUM_LIT:0> ] if len ( args ) >= <NUM_LIT:1> else ( ) <EOL> if isinstance ( other , HTTPHeaderDict ) : <EOL> for key , val in other . iteritems ( ) : <EOL> self . add ( key , val ) <EOL> elif isinstance ( other , Mapping ) : <EOL> for key in other : <EOL> self . add ( key , other [ key ] ) <EOL> elif hasattr ( other , "<STR_LIT>" ) : <EOL> for key in other . keys ( ) : <EOL> self . add ( key , other [ key ] ) <EOL> else : <EOL> for key , value in other : <EOL> self . add ( key , value ) <EOL> for key , value in kwargs . items ( ) : <EOL> self . add ( key , value ) <EOL> def getlist ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vals = _dict_getitem ( self , key . lower ( ) ) <EOL> except KeyError : <EOL> return [ ] <EOL> else : <EOL> if isinstance ( vals , tuple ) : <EOL> return [ vals [ <NUM_LIT:1> ] ] <EOL> else : <EOL> return vals [ <NUM_LIT:1> : ] <EOL> getheaders = getlist <EOL> getallmatchingheaders = getlist <EOL> iget = getlist <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , dict ( self . itermerged ( ) ) ) <EOL> def _copy_from ( self , other ) : <EOL> for key in other : <EOL> val = _dict_getitem ( other , key ) <EOL> if isinstance ( val , list ) : <EOL> val = list ( val ) <EOL> _dict_setitem ( self , key , val ) <EOL> def copy ( self ) : <EOL> clone = type ( self ) ( ) <EOL> clone . _copy_from ( self ) <EOL> return clone <EOL> def iteritems ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self : <EOL> vals = _dict_getitem ( self , key ) <EOL> for val in vals [ <NUM_LIT:1> : ] : <EOL> yield vals [ <NUM_LIT:0> ] , val <EOL> def itermerged ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self : <EOL> val = _dict_getitem ( self , key ) <EOL> yield val [ <NUM_LIT:0> ] , '<STR_LIT:U+002CU+0020>' . join ( val [ <NUM_LIT:1> : ] ) <EOL> def items ( self ) : <EOL> return list ( self . iteritems ( ) ) <EOL> @ classmethod <EOL> def from_httplib ( cls , message ) : <EOL> """<STR_LIT>""" <EOL> headers = [ ] <EOL> for line in message . headers : <EOL> if line . startswith ( ( '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ) ) : <EOL> key , value = headers [ - <NUM_LIT:1> ] <EOL> headers [ - <NUM_LIT:1> ] = ( key , value + '<STR_LIT:\r\n>' + line . rstrip ( ) ) <EOL> continue <EOL> key , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> headers . append ( ( key , value . strip ( ) ) ) <EOL> return cls ( headers ) </s>
