
v^<                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z m	 Z	 m
 Z
 m Z d  d l m Z d  d l m Z m Z d  d l m Z Gd d   d e
  Z d S)	    N)settings)VALID_KEY_CHARSCreateErrorSessionBaseUpdateError)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperation)timezonec                   s   e  Z d  Z d Z d   f d d  Z e d d    Z d d d  Z d	 d
   Z d d   Z	 d d   Z
 d d   Z d d d  Z d d   Z d d d  Z d d   Z e d d    Z   S)SessionStorez/
    Implement a file based session store.
    Nc                s/   |  j    |  _ t j |  _ t   j |  d  S)N)_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuper__init__)selfsession_key)	__class__ G/tmp/pip-build-8lau8j11/django/django/contrib/sessions/backends/file.pyr      s    zSessionStore.__init__c             C   sq   y |  j  SWn_ t k
 rl t t d d   p6 t j   } t j j |  s[ t	 d |   | |  _  | SYn Xd  S)NZSESSION_FILE_PATHzThe session storage path %r doesn't exist. Please set your SESSION_FILE_PATH setting to an existing directory in which Django can store session data.)
Z_storage_pathAttributeErrorgetattrr   tempfile
gettempdirospathisdirr   )clsr   r   r   r   r      s    
	zSessionStore._get_storage_pathc             C   sV   | d k r |  j    } t |  j t  s9 t d   t j j |  j |  j	 |  S)z@
        Get the file associated with this session key.
        Nz!Invalid characters in session key)
Z_get_or_create_session_keysetissubsetr   r   r   r   joinr   r   )r   r   r   r   r   _key_to_file)   s    	zSessionStore._key_to_filec             C   sV   t  j |  j    j } t j rF t j j |  } | j d t	 j
  St j j |  S)zY
        Return the modification time of the file storing the session's content.
        tzinfo)r   statr"   st_mtimer   ZUSE_TZdatetimeutcfromtimestampreplacer
   utcfromtimestamp)r   Zmodificationr   r   r   _last_modification9   s
    	zSessionStore._last_modificationc             C   s/   | j  d  p. |  j   t j d |  j    S)zS
        Return the expiry time of the file storing the session's content.
        Z_session_expiryseconds)getr+   r&   	timedeltaZget_session_cookie_age)r   session_datar   r   r   _expiry_dateC   s    zSessionStore._expiry_datec             C   s-  i  } y t  |  j   d d  } | j   } Wd  QRX| ry |  j |  } Wnp t t f k
 r } zJ t | t  r t j d | j	 j
  } | j t |   |  j   WYd  d  } ~ Xn X|  j d |  j |   } | d k ri  } |  j   |  j   Wn! t t f k
 r(d  |  _ Yn X| S)Nencodingasciizdjango.security.%sZexpiryr   )openr"   readdecodeEOFErrorr	   
isinstancelogging	getLoggerr   __name__warningstrcreateZget_expiry_ager0   deleteOSError_session_key)r   r/   session_fileZ	file_dataeloggerZ
expiry_ager   r   r   loadK   s(    
zSessionStore.loadc             C   sP   xI |  j    |  _ y |  j d d  Wn t k
 r= w Yn Xd |  _ d  SWd  S)Nmust_createT)Z_get_new_session_keyr@   saver   modified)r   r   r   r   r=   e   s    	zSessionStore.createFc             C   s  |  j  d  k r |  j   S|  j d |  } |  j   } yV t j t t d d  B} | rm | t j t j BO} t j	 | |  } t j
 |  Wn; t k
 r | s t  Yn t k
 r | r t  Yn Xt j j |  \ } } y t j d | d | d  \ } }	 d }
 zN z# t j | |  j |  j    Wd  t j
 |  Xt j |	 |  d }
 Wd  |
 srt j |	  XWn t t f k
 rYn Xd  S)	NZno_loadO_BINARYr   dirprefixZ_out_FT)r   r=   Z_get_sessionr"   r   O_WRONLYr   O_EXCLO_CREATr3   closeFileNotFoundErrorr   FileExistsErrorr   r   splitr   mkstempwriteencodeshutilmoveunlinkr6   r?   )r   rE   r/   Zsession_file_nameflagsfdrI   rJ   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rF   o   s<    

"#
zSessionStore.savec             C   s   t  j j |  j |   S)N)r   r   existsr"   )r   r   r   r   r   rZ      s    zSessionStore.existsc             C   s[   | d  k r( |  j  d  k r d  S|  j  } y t j |  j |   Wn t k
 rV Yn Xd  S)N)r   r   rW   r"   r?   )r   r   r   r   r   r>      s    	zSessionStore.deletec             C   s   d  S)Nr   )r   r   r   r   clean   s    zSessionStore.cleanc             C   s   |  j    } t j } xd t j |  D]S } | j |  s= q% | t |  d   } |  |  } d d   | _ | j   q% Wd  S)Nc               S   s   d  S)Nr   r   r   r   r   <lambda>   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   listdir
startswithlenr=   rD   )r   r   r   rA   r   sessionr   r   r   clear_expired   s    	zSessionStore.clear_expired)r:   
__module____qualname____doc__r   classmethodr   r"   r+   r0   rD   r=   rF   rZ   r>   r[   ra   r   r   )r   r   r      s   

=
r   )r&   r8   r   rU   r   Zdjango.confr   Z%django.contrib.sessions.backends.baser   r   r   r   Z"django.contrib.sessions.exceptionsr   Zdjango.core.exceptionsr   r	   Zdjango.utilsr
   r   r   r   r   r   <module>   s   "