a
    xd                     @   s   d 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Zddlm	Z	 dZ
dZdZdZd	d
 Zdd Zdd Zdd ZeeedddZdd Zdd Zdd Zedkre  dS )zG
Code generation script for class methods
to be exported as public API
    N)Path)indentZ
_generatedaT  # ***********************************************************
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ******
# *************************************************************
from ._run import GLOBAL_RUN_CONTEXT, _NO_SEND
from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
from ._instrumentation import Instrument

# fmt: off
z
# fmt: on
zlocals()[LOCALS_KEY_KI_PROTECTION_ENABLED] = True
try:
    return{}GLOBAL_RUN_CONTEXT.{}.{}
except AttributeError:
    raise RuntimeError("must be called from async context")
c                 C   s    t | tjst | tjrdS dS )zHCheck if the AST node is either a function
    or an async function
    TF)
isinstanceastFunctionDefAsyncFunctionDef)node r	   O/var/www/html/Ranjet/env/lib/python3.9/site-packages/trio/_tools/gen_exports.pyis_function'   s    r   c                 C   s8   t | sdS | jD ] }t|tjr|jdkr dS qdS )z-Check if the AST node has a _public decoratorFZ_publicT)r   decorator_listr   r   Nameid)r   	decoratorr	   r	   r
   	is_public0   s    
r   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   )treer   r	   r	   r
   get_public_methods:   s    r   c                 C   s   g }| j j D ]}||j q| j jr:|d| j jj  | j jD ]}||jd |j  qB| j jrz|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)"
    *=z**z({})z, )argsappendargvararg
kwonlyargskwargformatjoin)ZfuncdefZ	call_argsr   r	   r	   r
   create_passthrough_argsE   s    r   )source_pathlookup_pathreturnc           	      C   s   t g}tj| }t|D ]}|jjd jdks4J |jjd= g |_t|}t	
|du rh|jdd= n|jdd= tj|dd}tt|t	jrdnd||j| }|t|d }|| q|t d	|S )
zZScan the given .py file for @_public decorators, and generate wrapper
    functions.

    r   selfN   z    )Zindent_withz await  z

)HEADERastorZcode_to_astZ
parse_filer   r   r   r   r   r   get_docstringbodyZ	to_sourceTEMPLATEr   r   r   namer   r   FOOTERr   )	r   r    	generatedsourcemethodnew_argsfunctemplateZsnippetr	   r	   r
   gen_public_wrappers_sourceY   s(    

r2   c              	   C   sl   |   D ]^\}}tj|s" dS t|ddd}| }W d    n1 sN0    Y  ||kr dS qdS )NFrutf-8encodingT)itemsospathexistsopenread)	new_filesnew_path
new_sourceZold_fileZ
old_sourcer	   r	   r
   matches_disk_files   s    &r@   c          
   	   C   s   i }| D ]F\}}t d| t||}tj|\}}tj|t| }|||< q|rzt|spt d t	d qt d nR|
 D ]@\}}t|ddd}	|	| W d    q1 s0    Y  qt d d S )	Nz	Scanning:z2Generated sources are outdated. Please regenerate.r#   z!Generated sources are up to date.wr4   r5   z!Regenerated sources successfully.)printr2   r8   r9   splitr   PREFIXr@   sysexitr7   r;   write)
Zsources_and_lookupsdo_testr=   r   r    r?   dirnamebasenamer>   fr	   r	   r
   process   s     



*rL   c                  C   s   t jdd} | jddddd |  }t }|d  s>J |d	 }|d
 df|d df|d df|d df|d df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
trio/_corez_run.pyrunnerz_instrumentation.pyzrunner.instrumentsz_io_windows.pyzrunner.io_managerz_io_epoll.pyz_io_kqueue.py)rH   )	argparseArgumentParseradd_argument
parse_argsr   cwdr:   rL   test)parserZparsed_argsZsource_rootcoreZto_wrapr	   r	   r
   main   s"    




r[   __main__)__doc__rS   r   r&   r8   pathlibr   rE   textwrapr   rD   r%   r+   r)   r   r   r   r   strr2   r@   rL   r[   __name__r	   r	   r	   r
   <module>   s*   
	
,