3
d                 @   s   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZmZ ejdZG d	d
 d
eeZG dd de
ZdS )zb
# changing to temporary directories
    >>> tmp = getfixture('tmpdir')
    >>> old = tmp.chdir()
   )logging   )traitsDynamicTraitedSpec	Undefined	isdefinedBaseInterfaceInputSpec)IOBase
add_traits)ensure_list)	getsourcecreate_function_from_sourceznipype.interfacec               @   s   e Zd ZejdddZdS )FunctionInputSpecTzcode for function)	mandatorydescN)__name__
__module____qualname__r   Strfunction_str r   r   D/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/utility/wrappers.pyr      s   r   c                   sJ   e Zd ZdZeZeZd fdd	Zdd Z	dd	 Z
d
d Zdd Z  ZS )Functiona0  Runs arbitrary function as an interface

    Examples
    --------

    >>> func = 'def func(arg1, arg2=5): return arg1 + arg2'
    >>> fi = Function(input_names=['arg1', 'arg2'], output_names=['out'])
    >>> fi.inputs.function_str = func
    >>> res = fi.run(arg1=1)
    >>> res.outputs.out
    6

    Noutc                s  t t| jf | |rt|dr^yt|| j_W n tk
rL   tdY qX |dkr|j	}n4t
|ttfr|| j_|dkrt||j	}ntd|dkr|jd|j }| jj| jd t|| _t|| _t| jdd | jD  || _i | _x| jD ]}d| j|< qW dS )a  

        Parameters
        ----------

        input_names: single str or list or None
            names corresponding to function inputs
            if ``None``, derive input names from function argument names
        output_names: single str or list
            names corresponding to function outputs (default: 'out').
            if list of length > 1, has to match the number of outputs
        function : callable
            callable python object. must be able to execute in an
            isolated namespace (possibly in concert with the ``imports``
            parameter)
        imports : list of strings
            list of import statements that allow the function to execute
            in an otherwise empty namespace
        __call__z]Interface Function does not accept function objects defined interactively in a python sessionNzUnknown type of functionr   c             S   s   g | ]}|qS r   r   ).0namer   r   r   
<listcomp>c   s    z%Function.__init__.<locals>.<listcomp>)superr   __init__hasattrr   inputsr   IOError	Exception__code__
isinstancestrbytesr   co_varnamesco_argcountZon_trait_change_set_function_stringr   _input_names_output_namesr
   imports_out)selfinput_namesZoutput_namesfunctionr-   r!   fninfor   )	__class__r   r   r   .   s2    


zFunction.__init__c       	      C   s   |dkrt |dr"t|}|j}n t|ttfrB|}t|| jj}| jj	f ddid| |i |j
d |j }t|t| j }t| jt| | jj| d S )Nr   r   trait_change_notifyFz%s)r    r   r$   r%   r&   r'   r   r-   r!   	trait_setr(   r)   setr+   r
   listextend)	r/   objr   oldnewZfunction_sourcer2   r0   Z	new_namesr   r   r   r*   i   s    

zFunction._set_function_stringc             C   sB   i }x$| j D ]}|j|tj t||< qW |jf ddi| |S )Nr4   F)r,   Z	add_traitr   ZAnyr   r5   )r/   baseZundefined_traitskeyr   r   r   _add_output_traitsz   s    zFunction._add_output_traitsc             C   s   t | jj| j}i }x*| jD ] }t| j|}t|r|||< qW |f |}t| jdkrj|| j	| jd < nLt
|trt|t| jkrtdn&x$t| jD ]\}}|| | j	|< qW |S )N       z&Mismatch in number of expected outputs)r   r!   r   r-   r+   getattrr   lenr,   r.   r%   tupleRuntimeError	enumerate)r/   ZruntimeZfunction_handleargsr   valuer   idxr   r   r   _run_interface   s    

zFunction._run_interfacec             C   s.   | j  j }x| jD ]}| j| ||< qW |S )N)Z_outputsgetr,   r.   )r/   outputsr=   r   r   r   _list_outputs   s    zFunction._list_outputs)Nr   NN)r   r   r   __doc__r   Z
input_specr   Zoutput_specr   r*   r>   rI   rL   __classcell__r   r   )r3   r   r      s      6r   N)rM    r   r<   r   r   r   r   r   ior	   r
   Zutils.filemanipr   Zutils.functionsr   r   	getLoggerZifloggerr   r   r   r   r   r   <module>   s   
