U
    ‹d²  ã                   @   sz   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ e d¡Zd	Zd
ZdZG dd„ de	ƒZdS )z!File-system result store backend.é    N)Údatetime)Úensure_bytes)Úuuid)ÚKeyValueStoreBackend)ÚImproperlyConfiguredFz8You need to configure a path for the file-system backendzHA path for the file-system backend should conform to the file URI schemez‰The configured path for the file-system backend does not
work correctly, please make sure that it exists and has
the correct permissions.c                       s‚   e Zd ZdZdeejejef‡ fdd„	Z	di f‡ fdd„	Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚFilesystemBackendas  File-system result backend.

    Arguments:
        url (str):  URL to the directory we should use
        open (Callable): open function to use when opening files
        unlink (Callable): unlink function to use when deleting files
        sep (str): directory separator (to join the directory with the key)
        encoding (str): encoding used on the file-system
    Nc           	         s|   t ƒ j||Ž || _|  |¡}tjdkr>| d¡r>|dd … }| |¡| _| |¡| _	|| _
|| _|  dtƒ  |¡ ¡ d S )NÚntú/é   s   .fs-backend-)ÚsuperÚ__init__ÚurlÚ
_find_pathÚosÚnameÚ
startswithÚencodeÚpathÚsepÚopenÚunlinkÚ_do_directory_testr   )	Úselfr   r   r   r   ÚencodingÚargsÚkwargsr   ©Ú	__class__© ú>/tmp/pip-unpacked-wheel-9cz4377o/celery/backends/filesystem.pyr   $   s    
zFilesystemBackend.__init__r   c                    s    |  t| jd¡ tƒ  ||¡S )N)r   )ÚupdateÚdictr   r   Ú
__reduce__)r   r   r   r   r   r   r"   8   s    
ÿzFilesystemBackend.__reduce__c                 C   sD   |st tƒ‚| d¡r"|dd … S | d¡r8|dd … S t tƒ‚d S )Nzfile://localhost/é   zfile://é   )r   ÚE_NO_PATH_SETr   ÚE_PATH_NON_CONFORMING_SCHEME)r   r   r   r   r   r   =   s    

zFilesystemBackend._find_pathc                 C   sN   z,|   |d¡ |  |¡dks t‚|  |¡ W n tk
rH   ttƒ‚Y nX d S )Ns
   test value)ÚsetÚgetÚAssertionErrorÚdeleteÚOSErrorr   ÚE_PATH_INVALID©r   Úkeyr   r   r   r   F   s    z$FilesystemBackend._do_directory_testc                 C   s   | j  | j|f¡S ©N)r   Újoinr   r-   r   r   r   Ú	_filenameN   s    zFilesystemBackend._filenamec              
   C   sN   z4|   |  |¡d¡}| ¡ W  5 Q R £ W S Q R X W n tk
rH   Y nX d S )NÚrb)r   r1   ÚreadÚFileNotFoundError)r   r.   Úinfiler   r   r   r(   Q   s
     zFilesystemBackend.getc              	   C   s0   |   |  |¡d¡}| t|ƒ¡ W 5 Q R X d S )NÚwb)r   r1   Úwriter   )r   r.   ÚvalueÚoutfiler   r   r   r'   X   s    zFilesystemBackend.setc                 c   s   |D ]}|   |¡V  qd S r/   )r(   )r   Úkeysr.   r   r   r   Úmget\   s    zFilesystemBackend.mgetc                 C   s   |   |  |¡¡ d S r/   )r   r1   r-   r   r   r   r*   `   s    zFilesystemBackend.deletec                 C   sž   | j s
dS tddd| jjd}| j ¡ |  ¡ }|| j  }t | j¡D ]R}| j	| j
| jfD ]<}| |¡rZtj | j|¡}t |¡j|k r’|  |¡  qFqZqFdS )zDelete expired meta-data.Ni²  r
   )Útzinfo)Úexpiresr   ZappÚtimezoneÚnowÚtotal_secondsr   Úlistdirr   Ztask_keyprefixZgroup_keyprefixZchord_keyprefixr   r0   ÚstatÚst_mtimer   )r   ÚepochZnow_tsZ	cutoff_tsÚfilenameÚprefixr   r   r   r   Úcleanupc   s    
ÿ

zFilesystemBackend.cleanup)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Údefault_encodingr   r"   r   r   r1   r(   r'   r;   r*   rG   Ú__classcell__r   r   r   r   r      s   
ÿ	r   )rK   Úlocaler   r   Zkombu.utils.encodingr   Zceleryr   Zcelery.backends.baser   Zcelery.exceptionsr   ÚgetpreferredencodingrL   r%   r&   r,   r   r   r   r   r   Ú<module>   s   
ÿ