U
    g.                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlZerddlmZmZ ddlmZ ddlZd	Zd
ZdZejG dd dZdddddZdddddZdddddZdddddZddddd d!Zddddd"d#Zddddd$d%Z ddd&d'd(Z!d)d*d+d,d-Z"d.d*d/d0d1d2Z#d/d3d4d5Z$d6Z%d7Z&d8Z'd9Z(d:Z)e*d;kre$  dS )<zG
Code generation script for class methods
to be exported as public API
    )annotationsN)Path)indent)TYPE_CHECKING)IterableIterator)	TypeGuardZ
_generatedaE  # ***********************************************************
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from __future__ import annotations

import sys

from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._run import GLOBAL_RUN_CONTEXT
zsys._getframe().f_locals[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
    return{}GLOBAL_RUN_CONTEXT.{}.{}
except AttributeError:
    raise RuntimeError("must be called from async context") from None
c                   @  sJ   e Zd ZU ded< ded< ejdddZded< ejdddZded	< d
S )Filer   pathstrmodname T)defaultZkw_onlyplatformimportsN)__name__
__module____qualname____annotations__attrsfieldr   r    r   r   ;/tmp/pip-unpacked-wheel-ks04xdmi/trio/_tools/gen_exports.pyr	   1   s   
r	   zast.ASTz1TypeGuard[ast.FunctionDef | ast.AsyncFunctionDef])nodereturnc                 C  s   t | tjtjfS )zHCheck if the AST node is either a function
    or an async function
    )
isinstanceastZFunctionDefAsyncFunctionDef)r   r   r   r   is_function9   s    r   c                 C  s4   t | r0| jD ] }t|tjr|jdkr dS qdS )z-Check if the AST node has a _public decoratorZ_publicTF)r   decorator_listr   r   Nameid)r   	decoratorr   r   r   	is_public@   s
    
r#   z0Iterator[ast.FunctionDef | ast.AsyncFunctionDef])treer   c                 c  s"   t | D ]}t|r
|V  q
dS )zReturn a list of methods marked as public.
    The function walks the given tree and extracts
    all objects that are functions which are marked
    public.
    N)r   walkr#   )r$   r   r   r   r   get_public_methodsI   s    r&   z&ast.FunctionDef | ast.AsyncFunctionDefr   )funcdefr   c                 C  s~   dd | j j D }| j jr.|d| j jj  | j jD ]}||jd |j  q6| j jrn|d| j jj  dd|S )a  Given a function definition, create a string that represents taking all
    the arguments from the function, and passing them through to another
    invocation of the same function.

    Example input: ast.parse("def f(a, *, b): ...")
    Example output: "(a, b=b)"
    c                 S  s   g | ]
}|j qS r   )arg).0r(   r   r   r   
<listcomp>^   s     z+create_passthrough_args.<locals>.<listcomp>*=z**z({})z, )argsvarargappendr(   
kwonlyargskwargformatjoin)r'   Z	call_argsr(   r   r   r   create_passthrough_argsV   s    r4   ztuple[bool, str])filesourcer   c                 C  sN   ddl }tjtjddd| jdg|ddd	}|jdkrDd
d|j fS d|jfS )a	  Run black on the specified file.

    Returns:
      Tuple of success and result string.
      ex.:
        (False, "Failed to run black!
error: cannot format ...")
        (True, "<formatted source>")

    Raises:
      ImportError: If black is not installed.
    r   N-mblack--stdin-filename-Tutf8inputcapture_outputencodingFzFailed to run black!
)	r8   
subprocessrunsys
executabler
   
returncodestderrstdout)r5   r6   r8   resultr   r   r   	run_blackh   s    
rH   c              
   C  sT   ddl }tjtjdddddd| jd	g	|d
dd}|jdkrJdd|j fS d
|jfS )a  Run ruff on the specified file.

    Returns:
      Tuple of success and result string.
      ex.:
        (False, "Failed to run ruff!
error: Failed to parse ...")
        (True, "<formatted source>")

    Raises:
      ImportError: If ruff is not installed.
    r   Nr7   ruffcheckz--fixz--unsafe-fixesr9   r:   Tr;   r<   FzFailed to run ruff!
)	rI   r@   rA   rB   rC   r
   rD   rE   rF   )r5   r6   rI   rG   r   r   r   run_ruff   s&    
rK   c                 C  sp   t | |\}}|s$t| td t| |\}}|sHt| td t | |\}}|slt| td |S )zFormat the specified file using black and ruff.

    Returns:
      Formatted source code.

    Raises:
      ImportError: If either is not installed.
      SystemExit: If either failed.
       )rH   printrB   exitrK   )r5   r6   successresponser   r   r   run_linters   s    


rQ   )r5   r   c                 C  s  t g}| jr|| j | jrZd| jkr2|d d| jkrF|d |d| j d d|g}tj| j}g }t	|D ]}|j
j
d jd	kst|j
j
d= ||j |jD ]"}t|tjr|jd
krd} qqd}g |_t|}t|dkr
|jdd= n|jdd= tj|dd}	|r6|	dd}	tt|tjrLdnd| j|j| }
|	t|
d }|| q|  |dd| d|S )zZScan the given .py file for @_public decorators, and generate wrapper
    functions.

    r   z!from typing import TYPE_CHECKING
z
import syszimport sys
z,
assert not TYPE_CHECKING or sys.platform=="z"
r   r   selfcontextmanagerTFNrL   z    )Zindent_withz
->Iteratorz->ContextManagerz await  z
__all__ = z

)HEADERr   r/   r   r3   astorZcode_to_astZ
parse_filer
   r&   r-   r(   AssertionErrornamer   r   r   r    r!   r4   Zget_docstringbodyZ	to_sourcereplaceTEMPLATEr2   r   r   r   sortinsert)r5   header	generatedr6   Zmethod_namesmethoddecZis_cmnew_argsfunctemplateZsnippetr   r   r   gen_public_wrappers_source   sR    





re   zdict[str, str]bool)	new_filesr   c              
   C  sV   |   D ]H\}}tj|s" dS t|dd}| }W 5 Q R X ||kr dS qdS )NFutf-8)r?   T)itemsosr
   existsopenread)rg   new_path
new_sourceZold_fileZ
old_sourcer   r   r   matches_disk_files  s    rp   zIterable[File]None)filesdo_testr   c          
   
   C  s   i }| D ]N}t d|j t|}t||}tj|j\}}tj|t| }|||< qt|}|r|s|t d t	
d qt d nN| D ].\}}t|dddd}	|	| W 5 Q R X qt d	 |st	
d d S )
Nz	Scanning:z2Generated sources are outdated. Please regenerate.rL   z!Generated sources are up to date.wrh   
)r?   newlinez!Regenerated sources successfully.)rM   r
   re   rQ   rj   splitr3   PREFIXrp   rB   rN   ri   rl   write)
rr   rs   rg   r5   ro   dirnamebasenamern   Zmatches_diskfr   r   r   process   s(    


r}   )r   c               
   C  s   t jdd} | jddddd |  }t }|d  s>t|d	 }t|d
 dt	dt|d dt
dt|d ddtdt|d ddtdt|d ddtdg}t||jd d S )Nz,Generate python code for public api wrappers)descriptionz--testz-t
store_truez test if code is still up to date)actionhelpLICENSEzsrc/trio/_corez_run.pyrunner)r   z_instrumentation.pyzrunner.instrumentsz_io_windows.pyzrunner.io_managerwin32)r   r   z_io_epoll.pylinuxz_io_kqueue.pydarwin)rs   )argparseArgumentParseradd_argument
parse_argsr   cwdrk   rW   r	   IMPORTS_RUNIMPORTS_INSTRUMENTIMPORTS_WINDOWSIMPORTS_EPOLLIMPORTS_KQUEUEr}   test)parserZparsed_argsZsource_rootcoreZto_wrapr   r   r   main<  sN    r   aN  from collections.abc import Awaitable, Callable
from typing import Any, TYPE_CHECKING

from outcome import Outcome
import contextvars

from ._run import _NO_SEND, RunStatistics, Task
from ._entry_queue import TrioToken
from .._abc import Clock

if TYPE_CHECKING:
    from typing_extensions import Unpack
    from ._run import PosArgT
z)from ._instrumentation import Instrument
zZfrom typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .._file_io import _HasFileNo
zfrom typing import Callable, ContextManager, TYPE_CHECKING

if TYPE_CHECKING:
    import select

    from .. import _core
    from ._traps import Abort, RaiseCancelT
    from .._file_io import _HasFileNo
zfrom typing import TYPE_CHECKING, ContextManager

if TYPE_CHECKING:
    from .._file_io import _HasFileNo
    from ._windows_cffi import Handle, CData
    from typing_extensions import Buffer

    from ._unbounded_queue import UnboundedQueue
__main__)+__doc__
__future__r   r   r   rj   r@   rB   pathlibr   textwrapr   typingr   r   collections.abcr   r   Ztyping_extensionsr   rV   rx   rU   r[   Zdefiner	   r   r#   r&   r4   rH   rK   rQ   re   rp   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   sJ   	&K.
