3
d                 @   sb   d Z ddlZddlmZ ddlmZ G dd deZG dd deZd	d
 ZdddZ	dd Z
dS )zReformat interface docstrings.    N)_)NumpyDocstringc               @   s   e Zd ZdZdd ZdS )NipypeDocstringzFPatch the NumpyDocstring from napoleon to get special section headers.c             C   sN   t dt dt dd}|j|j |}| j }| jjrB| j|S | j||S )NZ
Parameters)args	arguments
parameters)r   getlowerZ_consume_fieldsZ_configZnapoleon_use_paramZ_format_docutils_paramsZ_format_fields)selfsectionlabelslabelfields r   C/tmp/pip-build-7vycvbft/nipype/nipype/sphinxext/apidoc/docstring.py_parse_parameters_section   s    
z)NipypeDocstring._parse_parameters_sectionN)__name__
__module____qualname____doc__r   r   r   r   r   r   	   s   r   c                   s0   e Zd ZdZejdejZd fdd	Z  Z	S )InterfaceDocstringa|  
    Convert docstrings of Nipype Interfaces to reStructuredText.

    Parameters
    ----------
    docstring : :obj:`str` or :obj:`list` of :obj:`str`
        The docstring to parse, given either as a string or split into
        individual lines.
    config: :obj:`sphinx.ext.napoleon.Config` or :obj:`sphinx.config.Config`
        The configuration settings to use. If not given, defaults to the
        config object on `app`; or if `app` is not given defaults to the
        a new :class:`nipype.sphinxext.apidoc.Config` object.

    Other Parameters
    ----------------
    app : :class:`sphinx.application.Sphinx`, optional
        Application object representing the Sphinx process.
    what : :obj:`str`, optional
        A string specifying the type of the object to which the docstring
        belongs. Valid values: "module", "class", "exception", "function",
        "method", "attribute".
    name : :obj:`str`, optional
        The fully qualified name of the object.
    obj : module, class, exception, function, method, or attribute
        The object to which the docstring belongs.
    options : :class:`sphinx.ext.autodoc.Options`, optional
        The options given to the directive: an object with attributes
        inherited_members, undoc_members, show_inheritance and noindex that
        are True if the flag option of same name was given to the auto
        directive.

    zO^\s*(:(?P<role>\w+):`(?P<name>[a-zA-Z0-9_.-]+)`| (?P<name2>[a-zA-Z0-9_.-]+))\s*N c       	         sf   t  j||||||| t|dd}|rH|j rHd|j  dg| j | _|d k	rb|  jt|7  _d S )NZ_cmdr   zWrapped executable: ``%s``.)super__init__getattrstripZ_parsed_lines_parse_interface)	r
   Z	docstringconfigZappwhatnameobjoptionscmd)	__class__r   r   r   D   s    

zInterfaceDocstring.__init__)NNr   r   NN)
r   r   r   r   recompileXZ	_name_rgxr   __classcell__r   r   )r#   r   r      s
    r   c                sV  g }| j r| j  }t|jddj }|rn|ddg7 }|dt|d  g7 }x |D ]\}}|t|||7 }qRW dd |D  t fd	d
|jddj D }|r|ddg7 }|dt|d  g7 }x |D ]\}}|t|||7 }qW | jrRt| j jddj }|rR|ddg7 }|dt|d  g7 }x"|D ]\}}|t|||7 }q4W |S )z'Print description for input parameters.T)	mandatoryr   zMandatory Inputs-   c             S   s   h | ]}|d  qS )r   r   ).0itemr   r   r   	<setcomp>a   s    z#_parse_interface.<locals>.<setcomp>c                s    g | ]\}}| kr||fqS r   r   )r+   r   val)mandatory_keysr   r   
<listcomp>d   s   z$_parse_interface.<locals>.<listcomp>N)Z	transientzOptional InputsZOutputsr1   r1   )Z
input_specsortedZtraitsitemslen_parse_specZoutput_spec)r    parsedinputsZmandatory_itemsr   specZoptional_itemsoutputsr   )r/   r   r   U   s4    
r      c                s    fdd| D S )Nc                s   g | ]}d   | qS ) r   )r+   line)nr   r   r0   |   s    z_indent.<locals>.<listcomp>r   )linesr=   r   )r=   r   _indentz   s    r?   c             C   sf  g }|j rXdj|j d j |j dd g}|jd rL|jd rLd| }||j 7 }|j}|r|j r|j}|dkr|dj|j d	g7 }n|d
j|j |dg7 }|j	}|r|ddjdd |D  g7 }|j
}|r|ddjdd |D  g7 }|jr:|j d }	t|	ttfr(|	 r(d}	|dt|	 g7 }dj||j| |ddg}
|
t|d7 }
|
S )z6Parse a HasTraits object into a Numpy-style docstring.r   r   r*   N.
z%s.z/Maps to a command-line argument: :code:`{arg}`.)argzAMaps to a command-line argument: :code:`{arg}` (position: {pos}).)rB   posz'Mutually **exclusive** with inputs: %s.z, c             S   s   g | ]}d | qS )z``%s``r   )r+   xr   r   r   r0      s    z_parse_spec.<locals>.<listcomp>z**Requires** inputs: %s.c             S   s   g | ]}d | qS )z``%s``r   )r+   rD   r   r   r   r0      s    z""z"(Nipype **default** value: ``%s``)z{name} : {type})r   typer:   )descjoin
capitalizeendswith
splitlinesargstrr   positionformatxorrequiresZ
usedefaultdefault_value
isinstancebytesstrZ	full_infor?   )r7   r   r8   Z
desc_linesrF   rK   rC   rN   rO   defaultZout_rstr   r   r   r5      sB    "r5   )r:   )r   r$   Zsphinx.localer   Zsphinx.ext.napoleon.docstringr   r   r   r   r?   r5   r   r   r   r   <module>   s   9%
