
    jKj8                         d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZmZmZ d	d
lmZmZ  ee de j0                        Z ee d      xr	  ee d      Ze G d de             Z G d de      Z G d de      Zy)    N)defaultdict)IntEnumunique)BytesIO)
ThreadPool)LockRLock)MappingProxyType)AnyCallableOptionalUnion   )AbstractContextAbstractOperation	fdatasyncpreadpwritec                        e Zd ZdZdZdZdZdZy)OpCoder   r         N)__name__
__module____qualname__READWRITEFSYNCFDSYNCNOOP     `/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/caio/python_aio.pyr   r      s    DEEFDr#   r   c                   6   e Zd ZdZdZddedefdZedefd       ZddZ	e
rd	 Zd
 Znd Zd ZddZddZddZddZddZdefdZdefdZd Zd Z eej0                  eej2                  eej4                  eej6                  eej8                  ei      Zy)Contextz+
    python aio context implementation
       max_requests	pool_sizec                     || j                   k  sJ || _        t        |      | _        d| _        d| _        t               | _        t        s$t               | _
        t        t              | _        y y )Nr   F)MAX_POOL_SIZE_Context__max_requestsr   pool_in_progress_closedr   _closed_lockNATIVE_PREAD_PWRITEr	   _locks_cleanerr   _locks)selfr(   r)   s      r$   __init__zContext.__init__!   sa    4-----*y)	 F""''D%e,DK #r#   returnc                     | j                   S N)r,   r4   s    r$   r(   zContext.max_requests.   s    """r#   c                      j                   j                     } fd} fd} j                   j                  kD  rt	        d       xj                  dz  c_         j
                  j                  | f||       y )Nc                 n    xj                   dz  c_         | _        d_        j                  d        y )Nr   r   )r.   	exceptionwrittencallback)exc	operationr4   s    r$   on_errorz"Context._execute.<locals>.on_error5   s2    ""%I !It$r#   c                 `    xj                   dz  c_         | _        j                  |        y )Nr   )r.   r=   r>   )resultr@   r4   s    r$   
on_successz$Context._execute.<locals>.on_success;   s*    " &Iv&r#   z/Maximum simultaneous requests have been reachedr   )argsr>   error_callback)_OP_MAPopcoder.   r,   RuntimeErrorr-   apply_async)r4   r@   handlerrA   rD   s   ``   r$   _executezContext._execute2   s    ,,y//0	%	'
 t222A  	Q		4+# 	 	
r#   c                 0    t        j                  |||      S r8   )osr   r4   fdsizeoffsets       r$   __preadzContext.__preadN   s    88Bf--r#   c                 0    t        j                  |||      S r8   )rN   r   r4   rP   bytesrR   s       r$   __pwritezContext.__pwriteQ   s    99R//r#   c                    | j                   |   5  t        j                  |dt        j                         t        j                  ||t        j                         t        j                  ||      cd d d        S # 1 sw Y   y xY wNr   )r3   rN   lseekSEEK_SETreadrO   s       r$   rS   zContext.__preadT   sM    RQ,VR[[1wwr4( !   A A::Bc                    | j                   |   5  t        j                  |dt        j                         t        j                  ||t        j                         t        j                  ||      cd d d        S # 1 sw Y   y xY wrY   )r3   rN   rZ   r[   writerU   s       r$   rW   zContext.__pwriteZ   sM    RQ,VR[[1xxE* !r]   c                     |j                   j                  | j                  |j                  |j                  |j
                              S r8   )bufferr_   _Context__preadfilenonbytesrR   r4   r@   s     r$   _handle_readzContext._handle_read`   sB    %%LL      
 	
r#   c                     | j                  |j                  |j                  j                         |j                        S r8   )_Context__pwriterc   ra   getvaluerR   re   s     r$   _handle_writezContext._handle_writei   s5    }}i..7799;K;K
 	
r#   c                 @    t        j                  |j                        S r8   )rN   fsyncrc   re   s     r$   _handle_fsynczContext._handle_fsyncn   s    xx	(())r#   c                 ,    t        |j                        S r8   )fdsyncrc   re   s     r$   _handle_fdsynczContext._handle_fdsyncq   s    i&&''r#   c                      y r8   r"   re   s     r$   _handle_noopzContext._handle_noopt   s    r#   c                     g }|D ]/  }t        |t              st        d|      |j                  |       1 d}|D ]  }| j	                  |       |dz  } |S )NzInvalid Operation %rr   r   )
isinstance	Operation
ValueErrorappendrL   )r4   aio_operations
operationsr@   counts        r$   submitzContext.submitw   sd    
'Ii3 !7CCi(	 ( #IMM)$QJE $ r#   c                      y)z
        Cancels multiple Operations. Returns

         Operation.cancel(aio_op1, aio_op2, aio_opN, ...) -> int

        (Always returns zero, this method exists for compatibility reasons)
        r   r"   )r4   rx   s     r$   cancelzContext.cancel   s     r#   c                     | j                   ry | j                  5  | j                  j                          d| _         d d d        y # 1 sw Y   y xY wNT)r/   r0   r-   closer9   s    r$   r   zContext.close   s6    <<IIOODL s   "AAc                 Z    | j                   j                         r| j                          y y r8   )r-   r   r9   s    r$   __del__zContext.__del__   s    99??JJL r#   N)       )r@   ru   )r   r   r   __doc__r+   intr5   propertyr(   rL   r1   rb   rh   rf   rj   rm   rp   rr   r{   r}   r   r   r
   r   r   r   r   r    r!   rG   r"   r#   r$   r&   r&      s     M-S -# - #c # #
6 	.	0	)	+


*(     \mm~\  Gr#   r&   c                   b   e Zd ZdZ	 	 ddedee   dee   dedee   dee   fd	Ze		 ddededed
d fd       Z
e		 ddededed
d fd       Ze	dded
d fd       Ze	dded
d fd       Zd
eeef   fdZed
efd       Zed
efd       Zed
ee   fd       Zed
efd       Zdeegef   d
efdZy)ru   z-
    python aio operation implementation
    NrP   rd   rR   rH   payloadpriorityc                    d | _         t               | _        |t        j                  k(  r|rt        |      | _        || _        || _        |xs d| _        || _        |xs d| _	        |xs d| _
        d | _        d| _        y rY   )r>   r   ra   r   r   rH   _Operation__fileno_Operation__offset_Operation__opcode_Operation__nbytes_Operation__priorityr<   r=   )r4   rP   rd   rR   rH   r   r   s          r$   r5   zOperation.__init__   sr     iV\\!g!'*DK!!"-ar#   r6   c                 8     | |||t         j                  |      S )zC
        Creates a new instance of Operation on read mode.
        rH   r   )r   r   )clsrd   rP   rR   r   s        r$   r\   zOperation.read   s     2vvfkkHMMr#   payload_bytesc                 L     | |t        |      ||t        j                  |      S )zG
        Creates a new instance of AIOOperation on write mode.
        )r   rH   r   )lenr   r   )r   r   rP   rR   r   s        r$   r_   zOperation.write   s,     !<<
 	
r#   c                 8     | |ddt         j                  |      S )zG
        Creates a new instance of AIOOperation on fsync mode.
        Nr   )r   r   r   rP   r   s      r$   rl   zOperation.fsync   s     2tT&,,JJr#   c                 8     | |ddt         j                  |      S )zH
        Creates a new instance of AIOOperation on fdsync mode.
        Nr   )r   r    r   s      r$   ro   zOperation.fdsync   s     2tT&--(KKr#   c                     | j                   r| j                   | j                  t        j                  k(  r| j                  S | j
                  y| j
                  j                         S )zP
        Method returns a bytes value of AIOOperation's result or None.
        N)r<   rH   r   r   r=   ra   ri   r9   s    r$   	get_valuezOperation.get_value   sO     >>.. ;;&,,&<<;;{{##%%r#   c                     | j                   S r8   )r   r9   s    r$   rc   zOperation.fileno       }}r#   c                     | j                   S r8   )r   r9   s    r$   rR   zOperation.offset   r   r#   c                 6    | j                   j                         S r8   )ra   	getbufferr9   s    r$   r   zOperation.payload  s    {{$$&&r#   c                     | j                   S r8   )r   r9   s    r$   rd   zOperation.nbytes  r   r#   r>   c                     || _         yr   )r>   )r4   r>   s     r$   set_callbackzOperation.set_callback  s     r#   )NN)r   )r   r   r   r   r   r   r   rV   r5   classmethodr\   r_   rl   ro   r   r   r   rc   rR   
memoryviewr   rd   r   r   boolr   r"   r#   r$   ru   ru      s    $("&  	
  % 3-0 9:NN!N+.N	N N BC
!
'*
47
	
 
 Ks K; K K L LK L L&5, &       '*- ' '   XseSj%9 d r#   ru   )rN   collectionsr   enumr   r   ior   multiprocessing.poolr   	threadingr   r	   typesr
   typingr   r   r   r   abstractr   r   getattrrl   ro   hasattrr1   r   r&   ru   r"   r#   r$   <module>r      s    	 #    + ! " 1 1 8 
["((	+b'*Dwr8/D  W  Io IZf! fr#   