o
    vh                      @   s   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 dd
lmZmZmZ G dd dZG dd deZdd Zdd ZG dd dZdS )    )copy)settings)ImageFieldFile   )hashers)get_default_cachefile_backend)load_strategy)AlreadyRegisteredMissingSource)generator_registryregister)get_by_qname
open_imageprocess_imagec                   @   s8   e Zd ZdZdZ	 dZ	 ejZ	 dd Z	dd Z
eZdS )BaseImageSpecz_
    An object that defines how an new image should be generated from a source
    image.

    Nc                 C   s   | j pt | _ t| j| _d S N)cachefile_backendr   r   cachefile_strategyself r   U/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/imagekit/specs/__init__.py__init__'   s   zBaseImageSpec.__init__c                 C   s   t r   )NotImplementedErrorr   r   r   r   generate+   s   zBaseImageSpec.generate)__name__
__module____qualname____doc__cachefile_storager   r   #IMAGEKIT_DEFAULT_CACHEFILE_STRATEGYr   r   r   r
   r   r   r   r   r      s    r   c                       sv   e Zd ZdZg Z	 dZ	 dZ	 dZ	  fddZe	dd Z
e	dd	 Zejd
d	 Zdd Zdd Zdd Z  ZS )	ImageSpecz
    An object that defines how to generate a new image from a source file using
    PIL-based processors. (See :mod:`imagekit.processors`)

    NTc                    s   || _ t   d S r   )sourcesuperr   r   r"   	__class__r   r   r   X   s   zImageSpec.__init__c                 C   s   | j sd S ttjd}|| S )Nnamer)r"   r   r   IMAGEKIT_SPEC_CACHEFILE_NAMER)r   fnr   r   r   cachefile_name\   s   zImageSpec.cachefile_namec                 C   s@   t | dd }|st | dd }|rt |d |d  }| _| `|S )N_source_field_datainstanceattname)getattrr+   r,   )r   src
field_datar   r   r   r"   c   s   zImageSpec.sourcec                 C   s
   || _ d S r   )r+   )r   valuer   r   r   r"   m   s   
c                 C   sN   t | j}t| jtr%| jj}t| jdd t|dd d|d< |dd  |S )Nr-   name)r-   r.   r,   r+   )r   __dict__
isinstancer"   r   fieldr/   pop)r   stater6   r   r   r   __getstate__q   s   


zImageSpec.__getstate__c                 C   s    t | jj| j| j| j| jgS r   )r   pickler"   r3   
processorsformatoptionsautoconvertr   r   r   r   get_hash   s   zImageSpec.get_hashc                 C   st   | j s	td|  | j j}|r| j   zt| j }t|| j| j| j| j	d}W |r/| j 
  |S |r9| j 
  w w )Nz4The spec '%s' has no source file associated with it.)r;   r<   r>   r=   )r"   r
   closedopenr   r   r;   r<   r>   r=   close)r   r@   img	new_imager   r   r   r      s*   


zImageSpec.generate)r   r   r   r   r;   r<   r=   r>   r   propertyr*   r"   setterr9   r?   r   __classcell__r   r   r%   r   r!   6   s(    

	
	r!   c                    s"   G  fdddt }td|f S )Nc                       s   e Zd Z fddZdS )z*create_spec_class.<locals>.DynamicSpecBasec                    s6   z| j }W n ty   | j}Y nw | }t |ffS r   )r9   AttributeErrorr4   create_spec)r   getstater8   class_attrsr   r   
__reduce__   s   

z5create_spec_class.<locals>.DynamicSpecBase.__reduce__N)r   r   r   rM   r   rK   r   r   DynamicSpecBase   s    rN   DynamicSpec)r!   type)rL   rN   r   rK   r   create_spec_class   s   
rQ   c                 C   sD   t | }||}z|j}W n ty   ||_Y |S w || |S r   )rQ   __new____setstate__rH   r4   )rL   r8   clsr-   setstater   r   r   rI      s   

rI   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
SpecHostzh
    An object that ostensibly has a spec attribute but really delegates to the
    spec registry.

    Nc                 K   sF   dd |  D }|r|rtdt|}|| _|r!| | d S d S )Nc                 S   s   i | ]\}}|d ur||qS r   r   ).0kvr   r   r   
<dictcomp>   s    z%SpecHost.__init__.<locals>.<dictcomp>z^You can provide either an image spec or arguments for the ImageSpec constructor, but not both.)items	TypeErrorrQ   _original_specset_spec_id)r   specspec_idkwargs
spec_attrsr   r   r   r      s   zSpecHost.__init__c                 C   s:   || _ | jrz
t|| j W dS  ty   Y dS w dS )a[  
        Sets the spec id for this object. Useful for when the id isn't
        known when the instance is constructed (e.g. for ImageSpecFields whose
        generated `spec_id`s are only known when they are contributed to a
        class). If the object was initialized with a spec, it will be registered
        under the provided id.

        N)r`   r]   r   	generatorr	   )r   idr   r   r   r^      s   	zSpecHost.set_spec_idc                 C   s(   t | ddstd|  tj| j|dS )a  
        Look up the spec by the spec id. We do this (instead of storing the
        spec as an attribute) so that users can override apps' specs--without
        having to edit model definitions--simply by registering another spec
        with the same id.

        r`   NzObject %s has no spec id.)r"   )r/   	Exceptionr   getr`   r$   r   r   r   get_spec   s   zSpecHost.get_spec)NN)r   r   r   r   r   r^   rg   r   r   r   r   rV      s
    
rV   N)r   django.confr   django.db.models.fields.filesr    r   cachefiles.backendsr   cachefiles.strategiesr   
exceptionsr	   r
   registryr   r   utilsr   r   r   r   r!   rQ   rI   rV   r   r   r   r   <module>   s    (v