o
    [h                      @   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 G dd de
ZdS )    N)settings)VALID_KEY_CHARSCreateErrorSessionBaseUpdateError)InvalidSessionKey)ImproperlyConfiguredSuspiciousOperationc                       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 Zdd Zd'ddZd'ddZdd Zdd Zd&ddZd&d d!Zed"d# Zed$d% Z  ZS )(SessionStorez/
    Implement a file based session store.
    Nc                    s"   |   | _tj| _t | d S N)_get_storage_pathstorage_pathr   SESSION_COOKIE_NAMEfile_prefixsuper__init__selfsession_key	__class__ /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/contrib/sessions/backends/file.pyr      s   
zSessionStore.__init__c                 C   sR   z| j W S  ty(   ttdd pt }tj|s!t	d| || _ | Y S w )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   s<   |du r|   }t|tstdt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   _key_to_file/   s
   zSessionStore._key_to_filec                 C   s2   t |  j}tjrtjjnd}tjj	||dS )zY
        Return the modification time of the file storing the session's content.
        N)tz)
r   statr$   st_mtimer   ZUSE_TZdatetimetimezoneutcfromtimestamp)r   Zmodificationr%   r   r   r   _last_modification>   s   zSessionStore._last_modificationc                 C   s"   | dp|  tj|  d 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_dateF   s
   
zSessionStore._expiry_datec              
   C   s
  i }zst |  dd}| }W d    n1 sw   Y  |rpz| |}W n- ttfyU } zt|trGtd|j	j
 }|t| |   W Y d }~nd }~ww | j| |d}|dkrsi }|   |   W |S W |S W |S  ttfy   d | _Y |S w )Nascii)encodingzdjango.security.%s)Zexpiryr   )openr$   readdecodeEOFErrorr	   
isinstancelogging	getLoggerr   __name__warningstrcreateZget_expiry_ager1   deleteOSError_session_key)r   r0   session_file	file_dataeloggerZ
expiry_ager   r   r   loadO   s>   


	
zSessionStore.loadc                    
   |   S r   )rF   r   r   r   r   aloadk      zSessionStore.aloadc                 C   s:   	 |   | _z| jdd W n	 ty   Y q w d| _d S )NTmust_create)Z_get_new_session_keyrA   saver   modifiedrH   r   r   r   r>   n   s   
zSessionStore.createc                    rG   r   )r>   rH   r   r   r   acreatex   rJ   zSessionStore.acreateFc              	   C   sP  | j d u r	|  S | j|d}|  }z tjttddB }|r'|tjtjB O }t	||}t
| W n ty@   |s>tY n tyL   |rJtY nw tj|\}}zFtj||d d\}}	d}
z/zt|| |  W t
| nt
| w t|	| d}
W |
st|	 W d S W d S |
st|	 w w  ttfy   Y d S w )N)Zno_loadO_BINARYr   Z_out_)dirprefixFT)r   r>   Z_get_sessionr$   r   O_WRONLYr   O_EXCLO_CREATr4   closeFileNotFoundErrorr   FileExistsErrorr   r   splitr   mkstempwriteencodeshutilmoveunlinkr7   r@   )r   rL   r0   Zsession_file_nameflagsfdrQ   rR   Zoutput_file_fdZoutput_file_nameZrenamedr   r   r   rM   {   sN   


zSessionStore.savec                       | j |dS )NrK   )rM   )r   rL   r   r   r   asave      zSessionStore.asavec                 C   s   t j| |S r   )r   r   existsr$   r   r   r   r   re      s   zSessionStore.existsc                    s   |  |S r   )re   r   r   r   r   aexists   s   
zSessionStore.aexistsc                 C   sH   |d u r| j d u rd S | j }zt| | W d S  ty#   Y d S w r   )r   r   r_   r$   r@   r   r   r   r   r?      s   
zSessionStore.deletec                    rb   )N)r   )r?   r   r   r   r   adelete   rd   zSessionStore.adeletec                 C   sR   |   }tj}t|D ]}||sq||}| |}dd |_|  qd S )Nc                   S   s   d S r   r   r   r   r   r   <lambda>   s    z,SessionStore.clear_expired.<locals>.<lambda>)	r   r   r   r   listdir
startswithremoveprefixr>   rF   )r    r   r   rB   r   sessionr   r   r   clear_expired   s   



zSessionStore.clear_expiredc                    s   |    d S r   )rm   )r    r   r   r   aclear_expired   s   zSessionStore.aclear_expiredr   )F)r;   
__module____qualname____doc__r   classmethodr   r$   r,   r1   rF   rI   r>   rO   rM   rc   re   rf   r?   rg   rm   rn   __classcell__r   r   r   r   r
      s,    

	


?



r
   )r(   r9   r   r]   r   Zdjango.confr   Z%django.contrib.sessions.backends.baser   r   r   r   Z"django.contrib.sessions.exceptionsr   Zdjango.core.exceptionsr   r	   r
   r   r   r   r   <module>   s    