o
    okj                     @   sF  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 Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zeje ddZejddZejdddZejddd d! ejd"ejd#d$d%d&d' ejd(d)d ejd*d+dZejd,d-d ejd.ejd#d$d%d/d' ejd0ejd#d$d%d1d' ejd(d)d d4d2d3ZdS )5a  Implementation of the ``cffi-gen-src`` command-line tool.

This module is private; the command line is the only supported
interface. Two subcommands:

``exec-python``
    Execute a Python script that constructs a :class:`cffi.FFI`
    (the same kind of script that the CFFI docs' "Main mode of usage"
    describes) and emit the generated C source.

``read-sources``
    Create the :class:`cffi.FFI` from a separate ``cdef`` file and C
    source prelude, then emit the generated C source.
    N   )FFIc                 C   s   t | |dd}t||| d S )Nexec)sourcefilenamemode)compiler   )pysrcr   globscompiled r   P/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/cffi/_cffi_gen_src.py	_execfile3   s   r   c              
   C   s   t j|}d|d}tjdd }tjdt j| z7t| || ||vr/td|f || }t|t	s?t
|r?| }t|t	sKtd|f |W |tjdd< S |tjdd< w )a
  Execute ``pysrc`` and return the :class:`FFI` object it defines.

    The script is executed with ``__name__`` set to ``"cffi.gen_src"``,
    so a trailing ``if __name__ == "__main__": ffibuilder.compile()``
    block in the script is skipped.

    ``ffivar`` is the name bound by the script to the :class:`FFI`
    object, or to a callable that returns one.

    Raises :class:`NameError` if the name is not bound by the script,
    or :class:`TypeError` if the name does not resolve to an
    :class:`FFI` instance.
    zcffi.gen_src)__name____file__Nr   zGExpected to find the FFI object with the name %r, but it was not found.zKFound an object with the name %r but it was not an instance of cffi.api.FFI)ospathabspathsysinsertdirnamer   	NameError
isinstancer   callable	TypeError)r	   r   ffivarr
   old_pathffir   r   r   find_ffi_in_python_script8   s.   

 r   c                 C   s    t  }|| || | |S )zACreate an :class:`FFI` from ``cdef`` text and a C source prelude.)r   cdef
set_source)
modulenamer   csrc
ffibuilderr   r   r   make_ffi_from_sources_   s   
r$   c                 C   s   t  }| | | S )z=Return the C source that :meth:`FFI.emit_c_code` would write.)ioStringIOemit_c_codegetvalue)r   outputr   r   r   generate_c_sourceg   s   
r*   c                 C   sV   | dkrt j| d S t| ddd}|| W d    d S 1 s$w   Y  d S )N-wutf-8encoding)r   stdoutwriteopen)r)   	generatedfr   r   r   write_c_sourcen   s   "r5   c                 C   s6   | |u rdS z
t j| j|jW S  ty   Y dS w )NTF)r   r   samefilenameOSError)file1file2r   r   r   same_input_filev   s   r;   c                 C   sL   | t | |j|}W d    n1 sw   Y  t|}t| | d S N)r   readr7   r*   r5   )r)   pyfileffi_varr   r3   r   r   r   exec_python   s
   r@   c              	   C   sz   |" | |  }|  }W d    n1 sw   Y  W d    n1 s'w   Y  t|||}t|}t| | d S r<   )r=   r$   r*   r5   )r)   module_name
cdef_input
csrc_inputr"   r   r   r3   r   r   r   read_sources   s   
 rD   c                  C   s.   t jrtjt jd nd} | drdS dS )Nr    zcffi-gen-srczpython -m cffi.gen_src)r   argvr   r   basename
startswith)argv0r   r   r   _prog   s   
rJ   z-Generate CFFI C source for extension modules.)progdescriptionr   )destexec-pythonz2Execute a Python script that defines an FFI object)helpz	--ffi-varr#   zFName of the FFI object in the Python script; defaults to 'ffibuilder'.)defaultrO   r>   rr-   r.   zPath to the Python script)typerO   r)   zOutput path for the C sourceread-sourcesz>Read cdef and C source prelude files that define an FFI objectrA   z5Full name of the generated module, including packagesr   zFile containing C definitionsr"   z File containing C source preludec                 C   s   t j| d} | jdkrt| j| j| jd n#| jdkr4t| j| j	r't 
d t| j| j| j| j	d nt 
d t d d S )	NargsrN   )r)   r>   r?   rS   z1cdef and csrc are the same file and should not be)r)   rA   rB   rC   z5a subcommand is required: exec-python or read-sourcesr   )parser
parse_argsr   r@   r)   r>   r?   r;   r   r"   errorrD   rA   exitrT   r   r   r   run   s   



rZ   r<   )__doc__argparser%   r   r   apir   r   r   r$   r*   r5   r;   r@   rD   rJ   ArgumentParserrV   add_subparsers
subparsers
add_parserexec_python_parseradd_argumentFileTyperead_sources_parserrZ   r   r   r   r   <module>   sz   '			