
    Dh %                       d Z ddlmZ ddlZddlZddlZddlmZ ej                  r&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 G d dej(                  j*                        Z G d dej,                        Zej0                  ddd	 	 	 	 	 	 	 dd       Zej0                  dd	 	 	 	 	 	 	 dd       Zej0                  	 dddd	 	 	 	 	 	 	 dd       Z	 dddd	 	 	 	 	 	 	 ddZy)z3ThreadPooled implementation.

Asyncio is supported
    )annotationsN   )_base_threaded)AbstractEventLoop)Task)	Awaitable)Callable)	ParamSpecSpec)ThreadPooledthreadpooledc                  8    e Zd ZdZdZedd       Zedd       Zy)ThreadPoolExecutorzhProvide readers for protected attributes.

    Simply extend concurrent.futures.ThreadPoolExecutor.
     c                    | j                   S )z)MaxWorkers.

        :rtype: int
        )_max_workersselfs    x/var/www/fastuser/data/www/generator.snapmosaic.io/flask_app/venv/lib/python3.12/site-packages/threaded/_threadpooled.pymax_workerszThreadPoolExecutor.max_workers3   s            c                    | j                   S )z7Executor shutdown state.

        :rtype: bool
        )	_shutdownr   s    r   is_shutdownzThreadPoolExecutor.is_shutdown;   s     ~~r   N)returnintr   bool)__name__
__module____qualname____doc__	__slots__propertyr   r   r   r   r   r   r   +   s4    
 I! !  r   r   c                       e Zd ZU dZdZdZded<   eddd       Zedd       Z	e
dd       Z	 ddd	d
	 	 	 	 	 	 	 d fdZe
	 	 dd       Ze
dd       ZddZ	 	 	 	 ddZ	 	 	 	 	 	 d fdZddZ xZS )r   z$Post function to ThreadPoolExecutor.)__loop_getter__loop_getter_need_contextNzThreadPoolExecutor | None_ThreadPooled__executorc                    t        | j                  t              r4| j                  j                  |k(  ry| j                  j	                          t        |      | _        y)zPool executor create and configure.

        :param max_workers: Maximum workers
        :type max_workers: typing.Optional[int]
        N)r   )
isinstancer(   r   r   shutdown)clsr   s     r   	configurezThreadPooled.configureK   sD     cnn&89~~))[8NN##%+Dr   c                R    | j                   | j                   j                          yy)zShutdown executor.N)r(   r+   )r,   s    r   r+   zThreadPooled.shutdownY   s#     >>%NN##% &r   c                    t        | j                  t              r| j                  j                  r| j	                          | j                  S )z?Executor instance.

        :rtype: ThreadPoolExecutor
        )r*   r(   r   r   r-   r   s    r   executorzThreadPooled.executor_   s3     $//+=>$//B]B]NNr   Floop_getterloop_getter_need_contextc               B    t         |   |       || _        || _        y)a(  Wrap function in future and return.

        :param func: function to wrap
        :type func: typing.Optional[Callable]
        :param loop_getter: Method to get event loop, if wrap in asyncio task
        :type loop_getter: typing.Union[
                               None,
                               Callable[..., AbstractEventLoop],
                               AbstractEventLoop
                           ]
        :param loop_getter_need_context: Loop getter requires function context
        :type loop_getter_need_context: bool
        )funcN)super__init___ThreadPooled__loop_getter'_ThreadPooled__loop_getter_need_context)r   r5   r2   r3   	__class__s       r   r7   zThreadPooled.__init__i   s%    ( 	d#\g0H'r   c                    | j                   S )znLoop getter.

        :rtype: typing.Union[None, Callable[..., AbstractEventLoop], AbstractEventLoop]
        )r8   r   s    r   r2   zThreadPooled.loop_getter   s     !!!r   c                    | j                   S )zBLoop getter need execution context.

        :rtype: bool
        )r9   r   s    r   r3   z%ThreadPooled.loop_getter_need_context   s     ...r   c                    t        | j                        r.| j                  r | j                  |i |S | j                         S | j                  S )zGet event loop in decorator class.

        :return: event loop if available or getter available
        :rtype: Optional[AbstractEventLoop]
        )callabler2   r3   )r   argskwargss      r   	_get_loopzThreadPooled._get_loop   sN     D$$%,,'t''888##%%r   c                v      j                  |      t        j                        	 	 	 	 	 	 d fd       }|S )zHere should be constructed and returned real decorator.

        :param func: Wrapped function
        :type func: Callable
        :return: wrapped coroutine or function
        :rtype: Callable[..., Union[Awaitable, concurrent.futures.Future]]
        c                      j                   | i |}|  j                  j                  g| i |S |j                  j                  t	        j
                  g| i |      S )zMain function wrapper.

            :return: coroutine or function
            :rtype: Union[Awaitable, concurrent.futures.Future]
            )rA   r0   submitrun_in_executor	functoolspartial)r?   r@   looppreparedr   s      r   wrapperz3ThreadPooled._get_function_wrapper.<locals>.wrapper   sn     .<T^^T-LV-LD|+t}}++HFtFvFF''y7H7H7cTX7c\b7cddr   )r?   z	Spec.argsr@   zSpec.kwargsr   z=concurrent.futures.Future[typing.Any] | Awaitable[typing.Any])_await_if_requiredrF   wraps)r   r5   rJ   rI   s   `  @r   _get_function_wrapperz"ThreadPooled._get_function_wrapper   sV     **40 
	"	e	e(3	eJ	e 
#	e r   c                "    t        |   |i |S )zCallable instance.

        :return: Future, Awaitable or it's getter (depends of decoration way and asyncio.Loop provided)
        :rtype: Union[concurrent.futures.Future[Any], Awaitable[Any] Callable[..., ...]]
        )r6   __call__)r   r?   r@   r:   s      r   rO   zThreadPooled.__call__   s     w000r   c                    d| j                   j                   d| j                  d| j                  d| j                  dt        |       ddS )zLFor debug purposes.

        :return: repr info
        :rtype: str
        <(z, loop_getter=z, loop_getter_need_context=z	, ) at 0xX>)r:   r   _funcr2   r3   idr   s    r   __repr__zThreadPooled.__repr__   s`     ''(zzn ++. /((,(E(E'H Ihq\	$	
r   N)r,   type[ThreadPooled]r   z
int | Noner   None)r,   rY   r   rZ   )r   r   )r5   8Callable[..., Awaitable[typing.Any] | typing.Any] | Noner2   z=None | (Callable[..., AbstractEventLoop] | AbstractEventLoop)r3   r   r   rZ   )r   z;Callable[..., AbstractEventLoop] | AbstractEventLoop | Noner   )r?   
typing.Anyr@   r\   r   zAbstractEventLoop | None)r5   z2Callable[Spec, Awaitable[typing.Any] | typing.Any]r   zMCallable[Spec, concurrent.futures.Future[typing.Any] | Awaitable[typing.Any]])r?   z>Callable[..., Awaitable[typing.Any] | typing.Any] | typing.Anyr@   r\   r   zconcurrent.futures.Future[typing.Any] | Awaitable[typing.Any] | Callable[..., concurrent.futures.Future[typing.Any] | Awaitable[typing.Any]])r   str)r   r    r!   r"   r#   r(   __annotations__classmethodr-   r+   r$   r0   r7   r2   r3   rA   rM   rO   rW   __classcell__)r:   s   @r   r   r   D   s   .?I,0J)0E E & &
   JNI VZ).IFI S	I
 #'I 
I0 "	D" " / /
 F	V>1M1 1
	W1 
r   r   Fr1   c                    y)z,Overload: function callable, no loop getter.Nr   r5   r2   r3   s      r   r   r          r   )r3   c                    y)z3Overload: function callable, loop getter available.Nr   rb   s      r   r   r      rc   r   c                    y)zOverload: No function.Nr   rb   s      r   r   r      rc   r   c               J    | t        | ||      S  t        d||      |       S )a  Post function to ThreadPoolExecutor.

    :param func: function to wrap
    :type func: typing.Optional[Callable[..., typing.Union[Awaitable, typing.Any]]]
    :param loop_getter: Method to get event loop, if wrap in asyncio task
    :type loop_getter: typing.Union[
                           None,
                           Callable[..., AbstractEventLoop],
                           AbstractEventLoop
                       ]
    :param loop_getter_need_context: Loop getter requires function context
    :type loop_getter_need_context: bool
    :return: ThreadPooled instance, if called as function or argumented decorator, else callable wrapper
    :rtype: typing.Union[ThreadPooled, Callable[..., typing.Union[concurrent.futures.Future, Awaitable]]]
    Nrb   )r   rb   s      r   r   r      sC    * |#%=
 	

<!9 	 r   )r5   1Callable[..., Awaitable[typing.Any] | typing.Any]r2   rZ   r3   r   r   z4Callable[..., concurrent.futures.Future[typing.Any]])r5   rg   r2   z4Callable[..., AbstractEventLoop] | AbstractEventLoopr3   r   r   zCallable[..., Task[typing.Any]]rX   )r5   rZ   r2   ;None | Callable[..., AbstractEventLoop] | AbstractEventLoopr3   r   r   r   )r5   r[   r2   rh   r3   r   r   z[ThreadPooled | Callable[..., concurrent.futures.Future[typing.Any] | Awaitable[typing.Any]])r"   
__future__r   concurrent.futures
concurrentrF   typing r   TYPE_CHECKINGasyncior   r   collections.abcr   r	   typing_extensionsr
   r   __all__futuresr   	APIPooledr   overloadr   r   r   r   <module>rv      s  
 #    	))(+VD
*++>> 2V
>++ V
r  %*	7
;7 7 #	7
 :7 7 
 &+	>
;> F> #	>
 %> > ! PT%*	!
! M! #	!
 ! ! FJ PT%*	
B M #	
 ar   