o
    @a
                     @   s\   d Z ddlZddlZddlmZmZmZ ddlmZ ddl	Z	eG dd deZ
dd ZdS )	zW
Pythran integration into IPython.

* provides the %%pythran magic function to ipython
    N)Magicsmagics_class
cell_magic)magic_argumentsc                
       s   e Zd ZdZ fddZdd Ze ejddg dejd	dg dejd
dg dejddg dejddg dedd Z	  Z
S )PythranMagicszE Class to make it possible to use pythran as a magic IPython command.c                    s   t t| | i | _dS )z Init the pythran magic stuff. N)superr   __init__Z_reloads)selfshell	__class__ //usr/lib/python3/dist-packages/pythran/magic.pyr      s   
zPythranMagics.__init__c                 C   s    | j dd |j D  dS )z Import only globals modules. c                 S   s    i | ]\}}| d s||qS )__)
startswith).0kvr   r   r   
<dictcomp>"   s    z-PythranMagics._import_all.<locals>.<dictcomp>N)r
   push__dict__items)r	   moduler   r   r   _import_all    s    zPythranMagics._import_allz-Dappend)actiondefaultz-Oz-mz-Wz-fc                    s   t | j|}i }|jr|j|d< dD ] t| }dD ]}||g  fdd|D  qqt }|	|
d |	|
d d|  }tj||fi |}	tj||	}
tjj||
|	d}tj|}| | d	S )
z
        Compile and import everything from a Pythran code cell.

        %%pythran
        #pythran export foo(int)
        def foo(x):
            return x + x
        Zdefine_macrosZOmWf)Zextra_compile_argsZextra_link_argsc                 3   s    | ]	}d   |V  qdS )z-{}{}N)format)r   xr   r   r   	<genexpr><   s    z(PythranMagics.pythran.<locals>.<genexpr>zutf-8Zpythranized_)nameloaderoriginN)r   Zparse_argstringpythranDgetattr
setdefaultextendhashlibZmd5updateencodeZ	hexdigestZcompile_pythrancode	importlib	machineryExtensionFileLoader
ModuleSpec
_bootstrap_loadr   )r	   lineZcellargskwargsZargs_vtargetmZmodule_nameZmodule_pathr"   specr   r   r   r   r$   %   s,   



zPythranMagics.pythran)__name__
__module____qualname____doc__r   r   r   argumentr   r$   __classcell__r   r   r   r   r      s    r   c                 C   s   |  t dS )zLoad the extension in IPython.N)Zregister_magicsr   )Zipythonr   r   r   load_ipython_extensionK   s   r>   )r;   r)   r,   ZIPython.core.magicr   r   r   ZIPython.corer   r$   r   r>   r   r   r   r   <module>   s    4