<s> import copy <EOL> import warnings <EOL> from django . utils import six <EOL> class MergeDict ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * dicts ) : <EOL> self . dicts = dicts <EOL> def __getitem__ ( self , key ) : <EOL> for dict_ in self . dicts : <EOL> try : <EOL> return dict_ [ key ] <EOL> except KeyError : <EOL> pass <EOL> raise KeyError <EOL> def __copy__ ( self ) : <EOL> return self . __class__ ( * self . dicts ) <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return default <EOL> def getlist ( self , key ) : <EOL> for dict_ in self . dicts : <EOL> if key in dict_ : <EOL> return dict_ . getlist ( key ) <EOL> return [ ] <EOL> def _iteritems ( self ) : <EOL> seen = set ( ) <EOL> for dict_ in self . dicts : <EOL> for item in six . iteritems ( dict_ ) : <EOL> k = item [ <NUM_LIT:0> ] <EOL> if k in seen : <EOL> continue <EOL> seen . add ( k ) <EOL> yield item <EOL> def _iterkeys ( self ) : <EOL> for k , v in self . _iteritems ( ) : <EOL> yield k <EOL> def _itervalues ( self ) : <EOL> for k , v in self . _iteritems ( ) : <EOL> yield v <EOL> if six . PY3 : <EOL> items = _iteritems <EOL> keys = _iterkeys <EOL> values = _itervalues <EOL> else : <EOL> iteritems = _iteritems <EOL> iterkeys = _iterkeys <EOL> itervalues = _itervalues <EOL> def items ( self ) : <EOL> return list ( self . iteritems ( ) ) <EOL> def keys ( self ) : <EOL> return list ( self . iterkeys ( ) ) <EOL> def values ( self ) : <EOL> return list ( self . itervalues ( ) ) <EOL> def has_key ( self , key ) : <EOL> for dict_ in self . dicts : <EOL> if key in dict_ : <EOL> return True <EOL> return False <EOL> __contains__ = has_key <EOL> __iter__ = _iterkeys <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __copy__ ( ) <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return str ( dict ( self . items ( ) ) ) <EOL> def __repr__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> dictreprs = '<STR_LIT:U+002CU+0020>' . join ( repr ( d ) for d in self . dicts ) <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , dictreprs ) <EOL> class SortedDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> instance = super ( SortedDict , cls ) . __new__ ( cls , * args , ** kwargs ) <EOL> instance . keyOrder = [ ] <EOL> return instance <EOL> def __init__ ( self , data = None ) : <EOL> if data is None or isinstance ( data , dict ) : <EOL> data = data or [ ] <EOL> super ( SortedDict , self ) . __init__ ( data ) <EOL> self . keyOrder = list ( data ) if data else [ ] <EOL> else : <EOL> super ( SortedDict , self ) . __init__ ( ) <EOL> super_set = super ( SortedDict , self ) . __setitem__ <EOL> for key , value in data : <EOL> if key not in self : <EOL> self . keyOrder . append ( key ) <EOL> super_set ( key , value ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> return self . __class__ ( [ ( key , copy . deepcopy ( value , memo ) ) <EOL> for key , value in self . items ( ) ] ) <EOL> def __copy__ ( self ) : <EOL> return self . copy ( ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if key not in self : <EOL> self . keyOrder . append ( key ) <EOL> super ( SortedDict , self ) . __setitem__ ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> super ( SortedDict , self ) . __delitem__ ( key ) <EOL> self . keyOrder . remove ( key ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . keyOrder ) <EOL> def __reversed__ ( self ) : <EOL> return reversed ( self . keyOrder ) <EOL> def pop ( self , k , * args ) : <EOL> result = super ( SortedDict , self ) . pop ( k , * args ) <EOL> try : <EOL> self . keyOrder . remove ( k ) <EOL> except ValueError : <EOL> pass <EOL> return result <EOL> def popitem ( self ) : <EOL> result = super ( SortedDict , self ) . popitem ( ) <EOL> self . keyOrder . remove ( result [ <NUM_LIT:0> ] ) <EOL> return result <EOL> def _iteritems ( self ) : <EOL> for key in self . keyOrder : <EOL> yield key , self [ key ] <EOL> def _iterkeys ( self ) : <EOL> for key in self . keyOrder : <EOL> yield key <EOL> def _itervalues ( self ) : <EOL> for key in self . keyOrder : <EOL> yield self [ key ] <EOL> if six . PY3 : <EOL> items = _iteritems <EOL> keys = _iterkeys <EOL> values = _itervalues <EOL> else : <EOL> iteritems = _iteritems <EOL> iterkeys = _iterkeys <EOL> itervalues = _itervalues <EOL> def items ( self ) : <EOL> return [ ( k , self [ k ] ) for k in self . keyOrder ] <EOL> def keys ( self ) : <EOL> return self . keyOrder [ : ] <EOL> def values ( self ) : <EOL> return [ self [ k ] for k in self . keyOrder ] <EOL> def update ( self , dict_ ) : <EOL> for k , v in six . iteritems ( dict_ ) : <EOL> self [ k ] = v <EOL> def setdefault ( self , key , default ) : <EOL> if key not in self : <EOL> self . keyOrder . append ( key ) <EOL> return super ( SortedDict , self ) . setdefault ( key , default ) <EOL> def value_for_index ( self , index ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> "<STR_LIT>" , PendingDeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> <EOL> ) <EOL> return self [ self . keyOrder [ index ] ] <EOL> def insert ( self , index , key , value ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> "<STR_LIT>" , PendingDeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> <EOL> ) <EOL> if key in self . keyOrder : <EOL> n = self . keyOrder . index ( key ) <EOL> del self . keyOrder [ n ] <EOL> if n < index : <EOL> index -= <NUM_LIT:1> <EOL> self . keyOrder . insert ( index , key ) <EOL> super ( SortedDict , self ) . __setitem__ ( key , value ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in six . iteritems ( self ) ] ) <EOL> def clear ( self ) : <EOL> super ( SortedDict , self ) . clear ( ) <EOL> self . keyOrder = [ ] <EOL> class MultiValueDictKeyError ( KeyError ) : <EOL> pass <EOL> class MultiValueDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key_to_list_mapping = ( ) ) : <EOL> super ( MultiValueDict , self ) . __init__ ( key_to_list_mapping ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> super ( MultiValueDict , self ) . __repr__ ( ) ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> list_ = super ( MultiValueDict , self ) . __getitem__ ( key ) <EOL> except KeyError : <EOL> raise MultiValueDictKeyError ( "<STR_LIT>" % ( key , self ) ) <EOL> try : <EOL> return list_ [ - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> return [ ] <EOL> def __setitem__ ( self , key , value ) : <EOL> super ( MultiValueDict , self ) . __setitem__ ( key , [ value ] ) <EOL> def __copy__ ( self ) : <EOL> return self . __class__ ( [ <EOL> ( k , v [ : ] ) <EOL> for k , v in self . lists ( ) <EOL> ] ) <EOL> def __deepcopy__ ( self , memo = None ) : <EOL> if memo is None : <EOL> memo = { } <EOL> result = self . __class__ ( ) <EOL> memo [ id ( self ) ] = result <EOL> for key , value in dict . items ( self ) : <EOL> dict . __setitem__ ( result , copy . deepcopy ( key , memo ) , <EOL> copy . deepcopy ( value , memo ) ) <EOL> return result <EOL> def __getstate__ ( self ) : <EOL> obj_dict = self . __dict__ . copy ( ) <EOL> obj_dict [ '<STR_LIT>' ] = dict ( [ ( k , self . getlist ( k ) ) for k in self ] ) <EOL> return obj_dict <EOL> def __setstate__ ( self , obj_dict ) : <EOL> data = obj_dict . pop ( '<STR_LIT>' , { } ) <EOL> for k , v in data . items ( ) : <EOL> self . setlist ( k , v ) <EOL> self . __dict__ . update ( obj_dict ) <EOL> def get ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> val = self [ key ] <EOL> except KeyError : <EOL> return default <EOL> if val == [ ] : <EOL> return default <EOL> return val <EOL> def getlist ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return super ( MultiValueDict , self ) . __getitem__ ( key ) <EOL> except KeyError : <EOL> if default is None : <EOL> return [ ] <EOL> return default <EOL> def setlist ( self , key , list_ ) : <EOL> super ( MultiValueDict , self ) . __setitem__ ( key , list_ ) <EOL> def setdefault ( self , key , default = None ) : <EOL> if key not in self : <EOL> self [ key ] = default <EOL> return self [ key ] <EOL> def setlistdefault ( self , key , default_list = None ) : <EOL> if key not in self : <EOL> if default_list is None : <EOL> default_list = [ ] <EOL> self . setlist ( key , default_list ) <EOL> return self . getlist ( key ) <EOL> def appendlist ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . setlistdefault ( key ) . append ( value ) <EOL> def _iteritems ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self : <EOL> yield key , self [ key ] <EOL> def _iterlists ( self ) : <EOL> """<STR_LIT>""" <EOL> return six . iteritems ( super ( MultiValueDict , self ) ) <EOL> def _itervalues ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self : <EOL> yield self [ key ] <EOL> if six . PY3 : <EOL> items = _iteritems <EOL> lists = _iterlists <EOL> values = _itervalues <EOL> else : <EOL> iteritems = _iteritems <EOL> iterlists = _iterlists <EOL> itervalues = _itervalues <EOL> def items ( self ) : <EOL> return list ( self . iteritems ( ) ) <EOL> def lists ( self ) : <EOL> return list ( self . iterlists ( ) ) <EOL> def values ( self ) : <EOL> return list ( self . itervalues ( ) ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return copy . copy ( self ) <EOL> def update ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise TypeError ( "<STR_LIT>" % len ( args ) ) <EOL> if args : <EOL> other_dict = args [ <NUM_LIT:0> ] <EOL> if isinstance ( other_dict , MultiValueDict ) : <EOL> for key , value_list in other_dict . lists ( ) : <EOL> self . setlistdefault ( key ) . extend ( value_list ) <EOL> else : <EOL> try : <EOL> for key , value in other_dict . items ( ) : <EOL> self . setlistdefault ( key ) . append ( value ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> for key , value in six . iteritems ( kwargs ) : <EOL> self . setlistdefault ( key ) . append ( value ) <EOL> def dict ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( key , self [ key ] ) for key in self ) <EOL> class ImmutableList ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> warning = kwargs [ '<STR_LIT>' ] <EOL> del kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> warning = '<STR_LIT>' <EOL> self = tuple . __new__ ( cls , * args , ** kwargs ) <EOL> self . warning = warning <EOL> return self <EOL> def complain ( self , * wargs , ** kwargs ) : <EOL> if isinstance ( self . warning , Exception ) : <EOL> raise self . warning <EOL> else : <EOL> raise AttributeError ( self . warning ) <EOL> __delitem__ = complain <EOL> __delslice__ = complain <EOL> __iadd__ = complain <EOL> __imul__ = complain <EOL> __setitem__ = complain <EOL> __setslice__ = complain <EOL> append = complain <EOL> extend = complain <EOL> insert = complain <EOL> pop = complain <EOL> remove = complain <EOL> sort = complain <EOL> reverse = complain <EOL> class DictWrapper ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , func , prefix ) : <EOL> super ( DictWrapper , self ) . __init__ ( data ) <EOL> self . func = func <EOL> self . prefix = prefix <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key . startswith ( self . prefix ) : <EOL> use_func = True <EOL> key = key [ len ( self . prefix ) : ] <EOL> else : <EOL> use_func = False <EOL> value = super ( DictWrapper , self ) . __getitem__ ( key ) <EOL> if use_func : <EOL> return self . func ( value ) <EOL> return value </s>
