o
    Df                     @   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 schemezThe 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 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|i | || _| |}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__ S/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/backends/filesystem.pyr   $   s   
zFilesystemBackend.__init__r   c                    s(   |si n|}t  |i |d| jiS )Nr   )r   
__reduce__r   )r   r   r   r   r   r   r    8   s   zFilesystemBackend.__reduce__c                 C   s@   |st t|dr|dd  S |dr|dd  S t t)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   sF   z|  |d | |dksJ | | W d S  ty"   ttw )Ns
   test value)setgetdeleteOSErrorr   E_PATH_INVALIDr   keyr   r   r   r   E   s   z$FilesystemBackend._do_directory_testc                 C   s   | j | j|fS N)r   joinr   r*   r   r   r   	_filenameM   s   zFilesystemBackend._filenamec                 C   sX   z!|  | |d}| W  d    W S 1 sw   Y  W d S  ty+   Y d S w )Nrb)r   r.   readFileNotFoundError)r   r+   infiler   r   r   r&   P   s   (zFilesystemBackend.getc                 C   sF   |  | |d}|t| W d    d S 1 sw   Y  d S )Nwb)r   r.   writer   )r   r+   valueoutfiler   r   r   r%   W   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   r.   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 ])}| j	| j
| jfD ]}||rKtj| j|}t|j|k rI| |  nq-q#dS )zDelete expired meta-data.Ni  r
   )tzinfo)expiresr   apptimezonenowtotal_secondsr   listdirr   task_keyprefixgroup_keyprefixchord_keyprefixr   r-   statst_mtimer   )r   epochnow_ts	cutoff_tsfilenameprefixr   r   r   r   cleanupb   s"   


zFilesystemBackend.cleanup)r   N)__name__
__module____qualname____doc__r   r   r   r   default_encodingr   r    r   r   r.   r&   r%   r8   r'   rJ   __classcell__r   r   r   r   r      s    
	r   )rN   localer   r   kombu.utils.encodingr   celeryr   celery.backends.baser   celery.exceptionsr   getpreferredencodingrO   r#   r$   r)   r   r   r   r   r   <module>   s    
