3
d                 @   s.   d Z ddlZddlmZ dd ZdddZdS )	zy
Handles custom functions used in Function interface. Future imports
are avoided to keep namespace as clear as possible.
    N)dedentc             C   s   t tj| S )z%Returns the source code of a function)r   inspect	getsource)function r   8/tmp/pip-build-7vycvbft/nipype/nipype/utils/functions.pyr   
   s    r   c       	      C   s   i }g }y:|dk	r6x|D ]}t || qW t|j }t | | W n> tk
r } z"dj| }|d7 }t||W Y dd}~X nX tt|t|dg  }t|dkstd||d  }|S )aA  Return a function object from a function source

    Parameters
    ----------
    function_source : unicode string
        unicode string defining a function
    imports : list of strings
        list of import statements in string form that allow the function
        to be executed in an otherwise empty namespace
    NzError executing function
{}
zFunctions in connection strings have to be standalone. They cannot be declared either interactively or inside another function or inline in the connect string. Any imports should be done inside the function.__builtins__   z"Function or inputs are ill-definedr   )	execlistkeys	ExceptionformatRuntimeErrorsetlenAssertionError)	Zfunction_sourceZimportsnsZimport_keysZ	statementemsgZns_funcsfuncr   r   r   create_function_from_source   s"    

r   )N)__doc__r   textwrapr   r   r   r   r   r   r   <module>   s   