a
    Cg%;                     @   sp   d dl mZ d dlmZ er(ddlmZ G dd deZG dd deZG d	d
 d
eZ	G dd dZ
e
 ZdS )    )defaultdict)TYPE_CHECKING   )OptimizerBasec                   @   s   e Zd ZdZdS )UnrecognisedOperationErrorzL
    Raised when the operation isn't in any of the known image classes.
    N__name__
__module____qualname____doc__ r   r   N/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/willow/registry.pyr      s   r   c                   @   s   e Zd ZdZdS )UnavailableOperationErrorz
    Raised when all the image classes the operation exists in are not available.
    (most likely due to a missing image library.)
    Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )UnroutableOperationErrorzo
    Raised when there is no way to convert the image into an image class that
    supports the operation.
    Nr   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd Zdd Zd)ddZdd	 Zd
d ZddddZdd Z	dd Z
dd Zdd Zd*ddZeed dddZdd Zg e fdd Zd!d" Zd#d$ Zd%d& Zd'd( ZdS )+WillowRegistryc                 C   s.   t  | _i | _tt| _i | _i | _g | _d S N)	set_registered_image_classes_unavailable_image_classesr   dict_registered_operations_registered_converters_registered_converter_costs_registered_optimizersselfr   r   r   __init__#   s    
zWillowRegistry.__init__c                 C   s   || j | |< d S r   r   )r   image_classoperation_namefuncr   r   r   register_operation+   s    z!WillowRegistry.register_operationNc                 C   s(   || j ||f< |d ur$|| j||f< d S r   )r   r   )r   from_image_classto_image_classr    costr   r   r   register_converter.   s    z!WillowRegistry.register_converterc              
   C   s   | j | z|  W n. tyF } z|| j|< W Y d }~n
d }~0 0 t|D ]}t||}t|drz| ||j	| qPt|dr| j
||jd ||jd d qPt|drP|jD ]\}}| j
||||d qqPd S )NZ_willow_operation_willow_converter_tor   r   )r$   _willow_converter_from)r   addcheck	Exceptionr   dirgetattrhasattrr!   r   r%   r&   r'   )r   r   eattrvalZconverter_fromr$   r   r   r   register_image_class4   s&     



z#WillowRegistry.register_image_classc                 C   s   t |dg }t |dg }t |dg }|D ]}| | q(|D ]}| |d |d |d  q<|D ]}| |d |d |d  q`d S )NZwillow_image_classesZwillow_operationsZwillow_convertersr   r      )r,   r1   r!   r%   )r   Zpluginimage_classes
operations
convertersr   Z	operation	converterr   r   r   register_pluginM   s    zWillowRegistry.register_pluginr   )optimizer_classc                 C   s   zddl m} t|dd}W n( tyD   ddl}|jdd}Y n0 |sNdS t|tr|	 dkrhdS |	 dkrzd}n
|
d	}|du rd}n
|j|v }|r| r|| jvr| j| dS )
zRegisters an optimizer class.r   )settingsZWILLOW_OPTIMIZERSFNfalsetrueT,)Zdjango.confr9   r,   ImportErrorosenvironget
isinstancestrlowersplitZlibrary_nameZcheck_libraryr   append)r   r8   r9   Zenabled_optimizersr>   Zadd_optimizerr   r   r   register_optimizer[   s0    


z!WillowRegistry.register_optimizerc                 C   s   | j | | S r   r   )r   r   r   r   r   r   get_operation   s    zWillowRegistry.get_operationc                 C   s"   | j  D ]}||v r
 dS q
dS )NTF)r   values)r   r   Zimage_class_operationsr   r   r   operation_exists   s    zWillowRegistry.operation_existsc                 C   s   | j ||f S r   )r   r   r"   r#   r   r   r   get_converter   s    zWillowRegistry.get_converterc                 C   s   | j ||fdS )Nd   )r   r@   rJ   r   r   r   get_converter_cost   s    z!WillowRegistry.get_converter_costc                    s    j  }r:tt fdd|}|s:td d|r|t j  }|s~tdd dg fdd	|D  |S |S d S )
Nc                    s   |  j v o j |  v S r   r   )r   r   with_operationr   r   <lambda>   s   
z2WillowRegistry.get_image_classes.<locals>.<lambda>z%Could not find image class with the 'z' operation
zThe operation 'zI' is available in the following image classes but they all raised errors:c              	      s*   g | ]"}d j |jt j|ddqS )z#{image_class_name}: {error_message}zUnknown error)Zimage_class_nameerror_message)formatr   rB   r   r@   .0r   r   r   r   
<listcomp>   s   	z4WillowRegistry.get_image_classes.<locals>.<listcomp>)	r   copyr   filterr   r   keysr   join)r   rO   	availabler3   Zavailable_image_classesr   rN   r   get_image_classes   s:    



	z WillowRegistry.get_image_classes)image_formatreturnc                 C   s(   g }| j D ]}||r
|| q
|S r   )r   Z
applies_torE   )r   r]   
optimizersZ	optimizerr   r   r   get_optimizers_for_format   s
    

z(WillowRegistry.get_optimizers_for_formatc                 c   s.   | j  D ]\\}}}||u r
||fV  q
dS )a  
        Yields a tuple for each image class that can be directly converted
        from the specified image classes. The tuple contains the converter
        function and the image class.

        For example:

        >>> list(registry.get_converters_from(Pillow))
        [
            (convert_pillow_to_wand, Wand),
            (save_as_jpeg, JpegFile)
            ...
        ]
        N)r   items)r   r"   Zc_fromZc_tor6   r   r   r   get_converters_from   s    z"WillowRegistry.get_converters_fromc           	   	   C   s   ||kr|gS ||v rg S || j vs.|| jv r2g S g }| |D ]<\}}||vr@| |||||fg ||h}|| q@|S )a  
        Returns all paths between two image classes.

        Each path is a list of tuples representing the steps to take in order to
        convert to the new class. Each tuple contains two items: The converter
        function to call and the class that step converts to.

        The order of the paths returned is undefined.

        For example:

        >>> registry.find_all_paths(JpegFile, OpenCV)
        [
            [
                (load_jpeg_into_pillow, Pillow),
                (convert_pillow_to_opencv, OpenCV)
            ],
            [
                (load_jpeg_into_wand, Wand),
                (convert_wand_to_opencv, OpenCV)
            ]
        ]
        )r   r   rb   find_all_pathsunionextend)	r   startendpathZseen_classespathsr6   
next_classZnewpathsr   r   r   rc      s(    
zWillowRegistry.find_all_pathsc                 C   s.   |}d}|D ]\}}||  ||7 }|}q|S )zE
        Costs up a path and returns the cost as an integer.
        r   )rM   )r   rf   rh   Z
last_classZ
total_costr6   rj   r   r   r   get_path_cost  s    zWillowRegistry.get_path_costc                 C   sF   d}d}|  ||D ](}| ||}|du s4||k r|}|}q||fS )z
        Finds the shortest path between two image classes.

        This is similar to the find_all_paths function, except it only returns
        the path with the lowest cost.
        N)rc   rk   )r   rf   rg   current_pathcurrent_costrh   r$   r   r   r   find_shortest_path)  s    z!WillowRegistry.find_shortest_pathc           	      C   sV   d}d}d}|D ]:}|  ||\}}|du r.q|du s>||k r|}|}|}q|||fS )z
        Finds which of the specified image classes is the closest, based on the
        sum of the costs for the conversions needed to convert the image into it.
        N)rn   )	r   rf   r3   Zcurrent_classrl   rm   r   rh   r$   r   r   r   find_closest_image_class<  s    z'WillowRegistry.find_closest_image_classc                 C   s   z|  ||}|}g }d}W nj ty   | j|dd}| ||\}}}|du rvtd|ddd |D |j|  ||}Y n0 ||||fS )	a  
        Finds an operation that can be used by an image in the specified from_class.

        This function returns four values:
         - The operation function
         - The class which the operation is implemented on
         - A path to convert the image into the correct class for the operation
         - The total cost of all the conversions

        The path (third value) is a list of two-element tuple. Each tuple contains
        a function to call and a reference to the class that step converts to. See
        below for an example.

        How it works:

        If the specified operation_name is implemented for from_class, that is returned
        with an empty conversion path.

        If the specified operation_name is implemented on another class (but not from_class)
        that operation is returned with the conversion path to that new class.

        If it's implemented on multiple image classes, the closest one is chosen (based
        on the sum of the costs of each conversion step).

        If the operation_name is not implemented anywhere, there is no route to
        any image class that implements it or all the image classes that implement
        it are unavailable, a LookupError will be raised.

        Basic example:

            >>> func, cls, path, cost = registry.find_operation(JPEGImageFile, 'resize')
            >>> func
            PillowImage.resize
            >>> cls
            PillowImage
            >>> path
            [
                (PillowImage.open, PillowImage)
            ]
            >>> cost
            100

        To run the found operation on an image,  run each conversion function on that
        image then run the operation function:

            >>> image = Image.open(...)
            >>> func, cls, path, cost = registry.find_operation(type(image), operation_name)
            >>> for converter, new_class in path:
            ...    image = converter(image)
            ...
            >>> func(image, *args, **kwargs)
        r   T)rO   r[   Nz^The operation '{}' is available in the image class '{}' but it can't be converted to from '{}'z, c                 s   s   | ]}|j V  qd S r   )r   rT   r   r   r   	<genexpr>  s   z0WillowRegistry.find_operation.<locals>.<genexpr>)rG   LookupErrorr\   ro   r   rS   rZ   r   )r   Z
from_classr   r    clsrh   r$   r3   r   r   r   find_operationS  s,    5
zWillowRegistry.find_operation)N)NN)r   r	   r
   r   r!   r%   r1   r7   rF   rG   rI   rK   rM   r\   rB   listr`   rb   r   rc   rk   rn   ro   rs   r   r   r   r   r   "   s$   
%
03r   N)collectionsr   typingr   r_   r   rq   r   r   r   r   registryr   r   r   r   <module>   s   		   