<s> """<STR_LIT>""" <EOL> from Cookie import SimpleCookie , Morsel <EOL> import copy <EOL> class CookiePreHandlerMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> cookies = CookieHandler ( ) <EOL> for k , v in request . COOKIES . iteritems ( ) : <EOL> cookies [ k ] = str ( v ) <EOL> request . COOKIES = cookies <EOL> request . _orig_cookies = copy . deepcopy ( request . COOKIES ) <EOL> class CookiePostHandlerMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_response ( self , request , response ) : <EOL> if hasattr ( request , '<STR_LIT>' ) and request . COOKIES != request . _orig_cookies : <EOL> for k , v in request . COOKIES . iteritems ( ) : <EOL> if request . _orig_cookies . get ( k ) != v : <EOL> dict . __setitem__ ( response . cookies , k , v ) <EOL> return response <EOL> class StringMorsel ( Morsel ) : <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> def __eq__ ( self , a ) : <EOL> if isinstance ( a , str ) : <EOL> return str ( self ) == a <EOL> elif isinstance ( a , Morsel ) : <EOL> return a . output ( ) == self . output ( ) <EOL> return False <EOL> def __ne__ ( self , a ) : <EOL> if isinstance ( a , str ) : <EOL> return str ( self ) != a <EOL> elif isinstance ( a , Morsel ) : <EOL> return a . output ( ) != self . output ( ) <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> class CookieHandler ( SimpleCookie ) : <EOL> def __set ( self , key , real_value , coded_value ) : <EOL> """<STR_LIT>""" <EOL> M = self . get ( key , StringMorsel ( ) ) <EOL> M . set ( key , real_value , coded_value ) <EOL> dict . __setitem__ ( self , key , M ) <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> rval , cval = self . value_encode ( value ) <EOL> self . __set ( key , rval , cval ) <EOL> def set ( self , key , value = '<STR_LIT>' , max_age = None , expires = None , path = '<STR_LIT:/>' , domain = None , secure = None ) : <EOL> self [ key ] = value <EOL> for var in ( '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> val = locals ( ) [ var ] <EOL> if val is not None : <EOL> self [ key ] [ var . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) ] = val <EOL> def delete ( self , key , path = '<STR_LIT:/>' , domain = None ) : <EOL> self [ key ] = '<STR_LIT>' <EOL> if path is not None : <EOL> self [ key ] [ '<STR_LIT:path>' ] = path <EOL> if domain is not None : <EOL> self [ key ] [ '<STR_LIT>' ] = domain <EOL> self [ key ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self [ key ] [ '<STR_LIT>' ] = <NUM_LIT:0> </s>
