<s> """<STR_LIT>""" <EOL> import re <EOL> import uuid <EOL> from google . appengine . api import memcache <EOL> from google . appengine . ext import db <EOL> from tipfy . sessions import BaseSession <EOL> from tipfy . appengine . db import ( PickleProperty , get_protobuf_from_entity , <EOL> get_entity_from_protobuf ) <EOL> _UUID_RE = re . compile ( r'<STR_LIT>' ) <EOL> class SessionModel ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> kind_name = '<STR_LIT>' <EOL> created = db . DateTimeProperty ( auto_now_add = True ) <EOL> updated = db . DateTimeProperty ( auto_now = True ) <EOL> data = PickleProperty ( ) <EOL> @ property <EOL> def sid ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key ( ) . name ( ) <EOL> @ classmethod <EOL> def kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . kind_name <EOL> @ classmethod <EOL> def get_by_sid ( cls , sid ) : <EOL> """<STR_LIT>""" <EOL> session = cls . get_cache ( sid ) <EOL> if not session : <EOL> session = SessionModel . get_by_key_name ( sid ) <EOL> if session : <EOL> session . set_cache ( ) <EOL> return session <EOL> @ classmethod <EOL> def create ( cls , sid , data = None ) : <EOL> """<STR_LIT>""" <EOL> return cls ( key_name = sid , data = data or { } ) <EOL> @ classmethod <EOL> def get_cache ( cls , sid ) : <EOL> data = memcache . get ( sid ) <EOL> if data : <EOL> return get_entity_from_protobuf ( data ) <EOL> def set_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> memcache . set ( self . sid , get_protobuf_from_entity ( self ) ) <EOL> def delete_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> memcache . delete ( self . sid ) <EOL> def put ( self ) : <EOL> """<STR_LIT>""" <EOL> self . set_cache ( ) <EOL> db . put ( self ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . delete_cache ( ) <EOL> db . delete ( self ) <EOL> class AppEngineBaseSession ( BaseSession ) : <EOL> __slots__ = BaseSession . __slots__ + ( '<STR_LIT>' , ) <EOL> def __init__ ( self , data = None , sid = None , new = False ) : <EOL> BaseSession . __init__ ( self , data , new ) <EOL> if new : <EOL> self . sid = self . __class__ . _get_new_sid ( ) <EOL> elif sid is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> self . sid = sid <EOL> @ classmethod <EOL> def _get_new_sid ( cls ) : <EOL> return cls . __module__ + '<STR_LIT:.>' + cls . __name__ + '<STR_LIT:.>' + uuid . uuid4 ( ) . hex <EOL> @ classmethod <EOL> def get_session ( cls , store , name = None , ** kwargs ) : <EOL> if name : <EOL> cookie = store . get_secure_cookie ( name ) <EOL> if cookie is not None : <EOL> sid = cookie . get ( '<STR_LIT>' ) <EOL> if sid and _is_valid_key ( sid ) : <EOL> return cls . _get_by_sid ( sid , ** kwargs ) <EOL> return cls ( new = True ) <EOL> class DatastoreSession ( AppEngineBaseSession ) : <EOL> """<STR_LIT>""" <EOL> model_class = SessionModel <EOL> @ classmethod <EOL> def _get_by_sid ( cls , sid , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> entity = cls . model_class . get_by_sid ( sid ) <EOL> if entity is not None : <EOL> return cls ( entity . data , sid ) <EOL> return cls ( new = True ) <EOL> def save_session ( self , response , store , name , ** kwargs ) : <EOL> if not self . modified : <EOL> return <EOL> self . model_class . create ( self . sid , dict ( self ) ) . put ( ) <EOL> store . set_secure_cookie ( response , name , { '<STR_LIT>' : self . sid } , ** kwargs ) <EOL> class MemcacheSession ( AppEngineBaseSession ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def _get_by_sid ( cls , sid , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = memcache . get ( sid ) <EOL> if data is not None : <EOL> return cls ( data , sid ) <EOL> return cls ( new = True ) <EOL> def save_session ( self , response , store , name , ** kwargs ) : <EOL> if not self . modified : <EOL> return <EOL> memcache . set ( self . sid , dict ( self ) ) <EOL> store . set_secure_cookie ( response , name , { '<STR_LIT>' : self . sid } , ** kwargs ) <EOL> def _is_valid_key ( key ) : <EOL> """<STR_LIT>""" <EOL> return _UUID_RE . match ( key . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) is not None </s>
