<s> """<STR_LIT>""" <EOL> from tornado import gen <EOL> import logging <EOL> import os <EOL> from os import path <EOL> import tempfile <EOL> import time <EOL> import uuid <EOL> from tinman import config <EOL> from tinman import exceptions <EOL> from tinman import mapping <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> class Session ( mapping . Mapping ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> ip_address = None <EOL> last_request_at = None <EOL> last_request_uri = None <EOL> def __init__ ( self , session_id = None , duration = <NUM_LIT> , settings = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Session , self ) . __init__ ( ) <EOL> self . _duration = duration <EOL> self . _settings = settings or dict ( ) <EOL> self . id = session_id or str ( uuid . uuid4 ( ) ) <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class FileSession ( Session ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_SUBDIR = '<STR_LIT>' <EOL> def __init__ ( self , session_id = None , duration = None , settings = None ) : <EOL> """<STR_LIT>""" <EOL> super ( FileSession , self ) . __init__ ( session_id , duration , settings ) <EOL> self . _storage_dir = self . _setup_storage_dir ( ) <EOL> if settings . get ( '<STR_LIT>' , True ) : <EOL> self . _cleanup ( ) <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clear ( ) <EOL> if os . path . isfile ( self . _filename ) : <EOL> os . unlink ( self . _filename ) <EOL> else : <EOL> LOGGER . debug ( '<STR_LIT>' , self . _filename ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( self . _filename , '<STR_LIT:wb>' ) as session_file : <EOL> session_file . write ( self . dumps ( ) ) <EOL> except IOError as error : <EOL> LOGGER . error ( '<STR_LIT>' , error ) <EOL> raise error <EOL> def _cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> for filename in os . listdir ( self . _storage_dir ) : <EOL> file_path = path . join ( self . _storage_dir , filename ) <EOL> file_stat = os . stat ( file_path ) <EOL> evaluate = max ( file_stat . st_ctime , file_stat . st_mtime ) <EOL> if evaluate + self . _duration < time . time ( ) : <EOL> LOGGER . debug ( '<STR_LIT>' , file_path ) <EOL> os . unlink ( file_path ) <EOL> @ property <EOL> def _default_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return path . join ( tempfile . gettempdir ( ) , self . DEFAULT_SUBDIR ) <EOL> @ property <EOL> def _filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return path . join ( self . _storage_dir , self . id ) <EOL> @ staticmethod <EOL> def _make_path ( dir_path ) : <EOL> """<STR_LIT>""" <EOL> os . makedirs ( dir_path , <NUM_LIT> ) <EOL> def _setup_storage_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> dir_path = self . _settings . get ( config . DIRECTORY ) <EOL> if dir_path is None : <EOL> dir_path = self . _default_path <EOL> if not os . path . exists ( dir_path ) : <EOL> self . _make_path ( dir_path ) <EOL> else : <EOL> dir_path = path . abspath ( dir_path ) <EOL> if not os . path . exists ( dir_path ) or not os . path . isdir ( dir_path ) : <EOL> raise exceptions . ConfigurationException ( self . __class__ . __name__ , <EOL> config . DIRECTORY ) <EOL> return dir_path . rstrip ( '<STR_LIT:/>' ) <EOL> class RedisSession ( Session ) : <EOL> """<STR_LIT>""" <EOL> _redis_client = None <EOL> REDIS_DB = <NUM_LIT:2> <EOL> REDIS_HOST = '<STR_LIT:localhost>' <EOL> REDIS_PORT = <NUM_LIT> <EOL> def __init__ ( self , session_id , duration = None , settings = None ) : <EOL> """<STR_LIT>""" <EOL> if not RedisSession . _redis_client : <EOL> RedisSession . _redis_connect ( settings ) <EOL> super ( RedisSession , self ) . __init__ ( session_id , duration , settings ) <EOL> @ property <EOL> def _key ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> @ classmethod <EOL> def _redis_connect ( cls , settings ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in globals ( ) : <EOL> import tornadoredis <EOL> kwargs = { '<STR_LIT:host>' : settings . get ( '<STR_LIT:host>' , cls . REDIS_HOST ) , <EOL> '<STR_LIT:port>' : settings . get ( '<STR_LIT:port>' , cls . REDIS_PORT ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , cls . REDIS_DB ) } <EOL> LOGGER . info ( '<STR_LIT>' , <EOL> kwargs ) <EOL> cls . _redis_client = tornadoredis . Client ( ** kwargs ) <EOL> cls . _redis_client . connect ( ) <EOL> @ gen . coroutine <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> result = yield gen . Task ( RedisSession . _redis_client . delete , self . _key ) <EOL> LOGGER . debug ( '<STR_LIT>' , self . id , result ) <EOL> self . clear ( ) <EOL> raise gen . Return ( result ) <EOL> @ gen . coroutine <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . debug ( '<STR_LIT>' , self . id ) <EOL> result = yield gen . Task ( RedisSession . _redis_client . get , self . _key ) <EOL> if result : <EOL> self . loads ( result ) <EOL> raise gen . Return ( True ) <EOL> else : <EOL> raise gen . Return ( False ) <EOL> @ gen . coroutine <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> result = yield gen . Task ( RedisSession . _redis_client . set , <EOL> self . _key , self . dumps ( ) ) <EOL> LOGGER . debug ( '<STR_LIT>' , self . id , result ) <EOL> raise gen . Return ( result ) </s>
