<s> import binascii <EOL> import os <EOL> from pyramid . compat import text_ <EOL> from pyramid . decorator import reify <EOL> from pyramid . interfaces import ISession <EOL> from zope . interface import implementer <EOL> from . compat import cPickle <EOL> from . util import ( <EOL> persist , <EOL> refresh , <EOL> to_unicode , <EOL> ) <EOL> class _SessionState ( object ) : <EOL> def __init__ ( self , session_id , managed_dict , created , timeout , new ) : <EOL> self . session_id = session_id <EOL> self . managed_dict = managed_dict <EOL> self . created = created <EOL> self . timeout = timeout <EOL> self . new = new <EOL> @ implementer ( ISession ) <EOL> class RedisSession ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , <EOL> redis , <EOL> session_id , <EOL> new , <EOL> new_session , <EOL> serialize = cPickle . dumps , <EOL> deserialize = cPickle . loads <EOL> ) : <EOL> self . redis = redis <EOL> self . serialize = serialize <EOL> self . deserialize = deserialize <EOL> self . _new_session = new_session <EOL> self . _session_state = self . _make_session_state ( <EOL> session_id = session_id , <EOL> new = new , <EOL> ) <EOL> @ reify <EOL> def _session_state ( self ) : <EOL> return self . _make_session_state ( <EOL> session_id = self . _new_session ( ) , <EOL> new = True , <EOL> ) <EOL> def _make_session_state ( self , session_id , new ) : <EOL> persisted = self . from_redis ( session_id = session_id ) <EOL> return _SessionState ( <EOL> session_id = session_id , <EOL> managed_dict = persisted [ '<STR_LIT>' ] , <EOL> created = persisted [ '<STR_LIT>' ] , <EOL> timeout = persisted [ '<STR_LIT>' ] , <EOL> new = new , <EOL> ) <EOL> @ property <EOL> def session_id ( self ) : <EOL> return self . _session_state . session_id <EOL> @ property <EOL> def managed_dict ( self ) : <EOL> return self . _session_state . managed_dict <EOL> @ property <EOL> def created ( self ) : <EOL> return self . _session_state . created <EOL> @ property <EOL> def timeout ( self ) : <EOL> return self . _session_state . timeout <EOL> @ property <EOL> def new ( self ) : <EOL> return self . _session_state . new <EOL> def to_redis ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . serialize ( { <EOL> '<STR_LIT>' : self . managed_dict , <EOL> '<STR_LIT>' : self . created , <EOL> '<STR_LIT>' : self . timeout , <EOL> } ) <EOL> def from_redis ( self , session_id = None ) : <EOL> """<STR_LIT>""" <EOL> persisted = self . redis . get ( session_id or self . session_id ) <EOL> deserialized = self . deserialize ( persisted ) <EOL> return deserialized <EOL> def invalidate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . redis . delete ( self . session_id ) <EOL> del self . _session_state <EOL> @ persist <EOL> def __delitem__ ( self , key ) : <EOL> del self . managed_dict [ key ] <EOL> @ persist <EOL> def __setitem__ ( self , key , value ) : <EOL> self . managed_dict [ key ] = value <EOL> @ persist <EOL> def setdefault ( self , key , default = None ) : <EOL> return self . managed_dict . setdefault ( key , default ) <EOL> @ persist <EOL> def clear ( self ) : <EOL> return self . managed_dict . clear ( ) <EOL> @ persist <EOL> def pop ( self , key , default = None ) : <EOL> return self . managed_dict . pop ( key , default ) <EOL> @ persist <EOL> def update ( self , other ) : <EOL> return self . managed_dict . update ( other ) <EOL> @ persist <EOL> def popitem ( self ) : <EOL> return self . managed_dict . popitem ( ) <EOL> @ refresh <EOL> def __getitem__ ( self , key ) : <EOL> return self . managed_dict [ key ] <EOL> @ refresh <EOL> def __contains__ ( self , key ) : <EOL> return key in self . managed_dict <EOL> @ refresh <EOL> def keys ( self ) : <EOL> return self . managed_dict . keys ( ) <EOL> @ refresh <EOL> def items ( self ) : <EOL> return self . managed_dict . items ( ) <EOL> @ refresh <EOL> def get ( self , key , default = None ) : <EOL> return self . managed_dict . get ( key , default ) <EOL> @ refresh <EOL> def __iter__ ( self ) : <EOL> return self . managed_dict . __iter__ ( ) <EOL> @ refresh <EOL> def has_key ( self , key ) : <EOL> return key in self . managed_dict <EOL> @ refresh <EOL> def values ( self ) : <EOL> return self . managed_dict . values ( ) <EOL> @ refresh <EOL> def itervalues ( self ) : <EOL> try : <EOL> values = self . managed_dict . itervalues ( ) <EOL> except AttributeError : <EOL> values = self . managed_dict . values ( ) <EOL> return values <EOL> @ refresh <EOL> def iteritems ( self ) : <EOL> try : <EOL> items = self . managed_dict . iteritems ( ) <EOL> except AttributeError : <EOL> items = self . managed_dict . items ( ) <EOL> return items <EOL> @ refresh <EOL> def iterkeys ( self ) : <EOL> try : <EOL> keys = self . managed_dict . iterkeys ( ) <EOL> except AttributeError : <EOL> keys = self . managed_dict . keys ( ) <EOL> return keys <EOL> @ persist <EOL> def changed ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def new_csrf_token ( self ) : <EOL> token = text_ ( binascii . hexlify ( os . urandom ( <NUM_LIT:20> ) ) ) <EOL> self [ '<STR_LIT>' ] = token <EOL> return token <EOL> def get_csrf_token ( self ) : <EOL> token = self . get ( '<STR_LIT>' , None ) <EOL> if token is None : <EOL> token = self . new_csrf_token ( ) <EOL> else : <EOL> token = to_unicode ( token ) <EOL> return token <EOL> def flash ( self , msg , queue = '<STR_LIT>' , allow_duplicate = True ) : <EOL> storage = self . setdefault ( '<STR_LIT>' + queue , [ ] ) <EOL> if allow_duplicate or ( msg not in storage ) : <EOL> storage . append ( msg ) <EOL> self . changed ( ) <EOL> def peek_flash ( self , queue = '<STR_LIT>' ) : <EOL> storage = self . get ( '<STR_LIT>' + queue , [ ] ) <EOL> return storage <EOL> def pop_flash ( self , queue = '<STR_LIT>' ) : <EOL> storage = self . pop ( '<STR_LIT>' + queue , [ ] ) <EOL> return storage <EOL> @ persist <EOL> def adjust_timeout_for_session ( self , timeout_seconds ) : <EOL> """<STR_LIT>""" <EOL> self . _session_state . timeout = timeout_seconds <EOL> @ property <EOL> def _invalidated ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' not in self . __dict__ </s>
