o
    vh                     @   s   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
 d dlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZmZmZmZ G dd deeZG dd deZdS )    N)copy)settings)File)	ImageFile)	smart_str)SimpleLazyObject   )
BaseIKFile)generator_registry)content_requiredexistence_required)generateget_by_qname
get_loggerget_singletonget_storagec                       s|   e Zd ZdZd fdd	Zdd Zdd Ze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  ZS )ImageCacheFilea;  
    A file that represents the result of a generator. Creating an instance of
    this class is not enough to trigger the generation of the file. In fact,
    one of the main points of this class is to allow the creation of the file
    to be deferred until the time that the cache file strategy requires it.

    Nc                    s   || _ |sz|j}W n ty   ttjd}||}Y nw || _t|r(| p2|p2t|ddp2t	 }|p@t|ddp@t
tjd| _|pOt|ddpOt
tjd| _t j|d dS )	a  
        :param generator: The object responsible for generating a new image.
        :param name: The filename
        :param storage: A Django storage object, or a callable which returns a
            storage object that will be used to save the file.
        :param cachefile_backend: The object responsible for managing the
            state of the file.
        :param cachefile_strategy: The object responsible for handling events
            for this file.

        namercachefile_storageNcachefile_backendzcache file backendcachefile_strategyzcache file strategy)storage)	generatorcachefile_nameAttributeErrorr   r   IMAGEKIT_CACHEFILE_NAMERnamecallablegetattrr   r   "IMAGEKIT_DEFAULT_CACHEFILE_BACKENDr   #IMAGEKIT_DEFAULT_CACHEFILE_STRATEGYr   super__init__)selfr   r   r   r   r   fn	__class__ Z/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/imagekit/cachefiles/__init__.pyr"      s:   



zImageCacheFile.__init__c                 C   s8   t | dd d u rtj| | d | j| jd| _d S d S )N_filesenderfilerb)r   r   sendr   openr   r)   r#   r'   r'   r(   _require_file@   s   zImageCacheFile._require_filec                 C   s4   t | dd d u rtj| | d t | j|}|| jS )Nr)   r*   )r   r   r.   r   r   )r#   attrr$   r'   r'   r(   _storage_attrM   s   
zImageCacheFile._storage_attrc                 C   
   |  dS )Npathr3   r0   r'   r'   r(   r5   S      
zImageCacheFile.pathc                 C   r4   )Nurlr6   r0   r'   r'   r(   r8   W   r7   zImageCacheFile.urlFc                 C   s*   |s
t | dddu r| j| | dS dS )z
        Generate the file. If ``force`` is ``True``, the file will be generated
        whether the file already exists or not.

        r)   N)r   r   r   )r#   forcer'   r'   r(   r   [   s   zImageCacheFile.generatec                 C   s   t | j}| j| j|}t|dr| r|d t|| _	t
j|t
j| jkr>t d| j| j|| jf  d S d S )Nseekabler   aN  The storage backend %s did not save the file with the requested name ("%s") and instead used "%s". This may be because a file already existed with the requested name. If so, you may have meant to call generate() instead of generate(force=True), or there may be a race condition in the file backend %s. The saved file will not be used.)r   r   r   saver   hasattrr:   seekr   r,   osr5   normpathr   warningr   )r#   contentactual_namer'   r'   r(   	_generated   s   


zImageCacheFile._generatec                 C   s^   | j sdS tj| | d z| j| }W n ty$   t| jdd}Y nw |r-| j| S dS )NFr*   is_asyncT)	r   r   r.   r   should_verify_existencer   r   r   exists)r#   checkr'   r'   r(   __bool__   s   zImageCacheFile.__bool__c                 C   s6   t | j}|dd  t }|d |kr|d |S )Nr)   r   )r   __dict__popr   )r#   statesettings_storager'   r'   r(   __getstate__   s   

zImageCacheFile.__getstate__c                 C   s"   d|vr	t  |d< | j| d S )Nr   )r   rI   update)r#   rK   r'   r'   r(   __setstate__   s   
zImageCacheFile.__setstate__c                 C   s$   t d| jj| jr| f S df S Nz<%s: %s>None)r   r&   __name__r   r0   r'   r'   r(   __repr__   s
   zImageCacheFile.__repr__)NNNN)F)rR   
__module____qualname____doc__r"   r1   r3   propertyr5   r8   r   rC   rH   rM   rO   rS   __classcell__r'   r'   r%   r(   r      s    &


	$r   c                       s$   e Zd Z fddZdd Z  ZS )LazyImageCacheFilec                    s     fdd}t  | d S )Nc                     s    t jg R i } t| S )N)r
   getr   )r   argsgenerator_idkwargsr'   r(   setup   s   z*LazyImageCacheFile.__init__.<locals>.setup)r!   r"   )r#   r]   r\   r^   r_   r%   r[   r(   r"      s   zLazyImageCacheFile.__init__c                 C   s   d| j jt| p	df S rP   )r&   rR   strr0   r'   r'   r(   rS      s   zLazyImageCacheFile.__repr__)rR   rT   rU   r"   rS   rX   r'   r'   r%   r(   rY      s    rY   )os.pathr>   r   django.confr   django.core.filesr   django.core.files.imagesr   django.utils.encodingr   django.utils.functionalr   filesr	   registryr
   signalsr   r   utilsr   r   r   r   r   r   rY   r'   r'   r'   r(   <module>   s     