<s> import errno <EOL> import os <EOL> import tempfile <EOL> from django . conf import settings <EOL> from django . contrib . sessions . backends . base import SessionBase , CreateError <EOL> from django . core . exceptions import SuspiciousOperation , ImproperlyConfigured <EOL> class SessionStore ( SessionBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session_key = None ) : <EOL> self . storage_path = getattr ( settings , "<STR_LIT>" , None ) <EOL> if not self . storage_path : <EOL> self . storage_path = tempfile . gettempdir ( ) <EOL> if not os . path . isdir ( self . storage_path ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . storage_path ) <EOL> self . file_prefix = settings . SESSION_COOKIE_NAME <EOL> super ( SessionStore , self ) . __init__ ( session_key ) <EOL> VALID_KEY_CHARS = set ( "<STR_LIT>" ) <EOL> def _key_to_file ( self , session_key = None ) : <EOL> """<STR_LIT>""" <EOL> if session_key is None : <EOL> session_key = self . session_key <EOL> if not set ( session_key ) . issubset ( self . VALID_KEY_CHARS ) : <EOL> raise SuspiciousOperation ( <EOL> "<STR_LIT>" ) <EOL> return os . path . join ( self . storage_path , self . file_prefix + session_key ) <EOL> def load ( self ) : <EOL> session_data = { } <EOL> try : <EOL> session_file = open ( self . _key_to_file ( ) , "<STR_LIT:rb>" ) <EOL> try : <EOL> file_data = session_file . read ( ) <EOL> if file_data : <EOL> try : <EOL> session_data = self . decode ( file_data ) <EOL> except ( EOFError , SuspiciousOperation ) : <EOL> self . create ( ) <EOL> finally : <EOL> session_file . close ( ) <EOL> except IOError : <EOL> pass <EOL> return session_data <EOL> def create ( self ) : <EOL> while True : <EOL> self . _session_key = self . _get_new_session_key ( ) <EOL> try : <EOL> self . save ( must_create = True ) <EOL> except CreateError : <EOL> continue <EOL> self . modified = True <EOL> self . _session_cache = { } <EOL> return <EOL> def save ( self , must_create = False ) : <EOL> session_data = self . _get_session ( no_load = must_create ) <EOL> session_file_name = self . _key_to_file ( ) <EOL> try : <EOL> flags = os . O_WRONLY | os . O_CREAT | getattr ( os , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if must_create : <EOL> flags |= os . O_EXCL <EOL> fd = os . open ( session_file_name , flags ) <EOL> os . close ( fd ) <EOL> except OSError , e : <EOL> if must_create and e . errno == errno . EEXIST : <EOL> raise CreateError <EOL> raise <EOL> dir , prefix = os . path . split ( session_file_name ) <EOL> try : <EOL> output_file_fd , output_file_name = tempfile . mkstemp ( dir = dir , <EOL> prefix = prefix + '<STR_LIT>' ) <EOL> renamed = False <EOL> try : <EOL> try : <EOL> os . write ( output_file_fd , self . encode ( session_data ) ) <EOL> finally : <EOL> os . close ( output_file_fd ) <EOL> os . rename ( output_file_name , session_file_name ) <EOL> renamed = True <EOL> finally : <EOL> if not renamed : <EOL> os . unlink ( output_file_name ) <EOL> except ( OSError , IOError , EOFError ) : <EOL> pass <EOL> def exists ( self , session_key ) : <EOL> if os . path . exists ( self . _key_to_file ( session_key ) ) : <EOL> return True <EOL> return False <EOL> def delete ( self , session_key = None ) : <EOL> if session_key is None : <EOL> if self . _session_key is None : <EOL> return <EOL> session_key = self . _session_key <EOL> try : <EOL> os . unlink ( self . _key_to_file ( session_key ) ) <EOL> except OSError : <EOL> pass <EOL> def clean ( self ) : <EOL> pass </s>
