

_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 Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l m Z Gd d	   d	 e  Z d S)
    N)settings)VALID_KEY_CHARSCreateErrorSessionBase)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperation)timezone)
force_textc                   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 d  Z d d   Z d d d  Z d d   Z e d d    Z   S)SessionStorez0
    Implements a file based session store.
    Nc                s;   t  |   j   |  _ t j |  _ t t |   j |  d  S)N)	type_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuperr   __init__)selfsession_key)	__class__ G/tmp/pip-build-ghmbqnp_/Django/django/contrib/sessions/backends/file.pyr      s    zSessionStore.__init__c             C   s}   y |  j  SWnk t k
 rx t t d d   } | sB t j   } n  t j j |  sg t	 d |   n  | |  _  | 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   sb   | d k r |  j    } n  t |  j t t   sE t d   n  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   sa   t  j |  j    j } t j rK t j j |  } | j d t	 j
  } n t 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   modificationr   r   r   _last_modification>   s    	zSessionStore._last_modificationc              C   s>  i  } yt  |  j   d   } | j   } Wd  QX| ry |  j |  } Wns t t f k
 r } zM t | t  r t j d | j	 j
  } | j t |   n  |  j   WYd  d  } ~ Xn X|  j d |  j   d | j d   } | d k  ri  } |  j   |  j   qn  Wn! t t f k
 r9d  |  _ Yn X| S)Nrbzdjango.security.%sr,   ZexpiryZ_session_expiryr   )openr#   readdecodeEOFErrorr   
isinstancelogging	getLoggerr   __name__warningr
   createZget_expiry_ager-   getdeleteIOError_session_key)r   session_datasession_fileZ	file_dataeloggerZ
expiry_ager   r   r   loadJ   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   r8   g   s    	zSessionStore.createFc          %   C   s  |  j  d  k r |  j   S|  j d |  } |  j   } yY t j t j Bt t d d  B} | rp | t j O} n  t j	 | |  } t j
 |  WnI t k
 r } z) | r | j t j k r t  n    WYd  d  } ~ Xn Xt j j |  \ } } y t j d | d | d  \ }	 }
 d } zN z# t j |	 |  j |  j    Wd  t j
 |	  Xt j |
 |  d } Wd  | st j |
  n  XWn t t t f k
 rYn Xd  S)	NZno_loadO_BINARYr   dirprefixZ_out_FT)r   r8   Z_get_sessionr#   r   O_WRONLYO_CREATr   O_EXCLr/   closeOSErrorerrnoEEXISTr   r   splitr   mkstempwriteencodeshutilmoveunlinkr;   r2   )r   rB   r=   Zsession_file_nameflagsfdr?   rF   rG   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rC   q   s:    
 	#
zSessionStore.savec             C   s   t  j j |  j |   S)N)r   r   existsr#   )r   r   r   r   r   rX      s    zSessionStore.existsc             C   s^   | d  k r+ |  j  d  k r d  S|  j  } n  y t j |  j |   Wn t k
 rY Yn Xd  S)N)r   r   rU   r#   rL   )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 } xg t j |  D]V } | j |  s@ q% n  | 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lenr8   rA   )r   r   r   r>   r   sessionr   r   r   clear_expired   s    	zSessionStore.clear_expired)r6   
__module____qualname____doc__r   classmethodr   r#   r-   rA   r8   rC   rX   r:   rY   r_   r   r   )r   r   r      s   
>
r   )r'   rM   r4   r   rS   r   Zdjango.confr   Z%django.contrib.sessions.backends.baser   r   r   Z"django.contrib.sessions.exceptionsr   Zdjango.core.exceptionsr   r   Zdjango.utilsr	   Zdjango.utils.encodingr
   r   r   r   r   r   <module>   s   