B
    èéNcB  ã               @   sj   d Z ddlmZ ddlmZ ddlmZ ddlmZ dd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zddd„ZdS )a‡  Machinery for documenting traitlets config options with Sphinx.

This includes:

- A Sphinx extension defining directives and roles for config options.
- A function to generate an rst file given an Application instance.

To make this documentation, first set this module as an extension in Sphinx's
conf.py::

    extensions = [
        # ...
        'traitlets.config.sphinxdoc',
    ]

Autogenerate the config documentation by running code like this before
Sphinx builds::

    from traitlets.config.sphinxdoc import write_doc
    from myapp import MyApplication

    writedoc('config/options.rst',    # File to write
             'MyApp config options',  # Title
             MyApplication()
            )

The generated rST syntax looks like this::

    .. configtrait:: Application.log_datefmt

        Description goes here.

    Cross reference like this: :configtrait:`Application.log_datefmt`.
é    )Údefaultdict)Údedent)Ú	Undefined)Úindentc             C   s   | j dddd dddœ}|S )z„Registers the Sphinx extension.

    You shouldn't need to call this directly; configure Sphinx to use this
    module instead.
    ZconfigtraitzConfig option)ZobjnameT)Zparallel_read_safeZparallel_write_safe)Zadd_object_type)ÚappZmetadata© r   ú9lib/python3.7/site-packages/traitlets/config/sphinxdoc.pyÚsetup*   s    
r	   c             C   s4   | d ks| t krdS t| tttttfƒr0t| ƒS dS )NFT)r   Ú
isinstanceÚstrÚlistÚtupleÚdictÚsetÚbool)Zdvr   r   r   Úinteresting_default_value5   s
    r   c             C   sF   g }x6| D ].}t |ƒdkrdnd}| d|› |› d¡ q
W d |¡S )Né   ú-z--z``z, )ÚlenÚappendÚjoin)ÚaliasesZfmtedÚaZdashesr   r   r   Úformat_aliases=   s
    
r   c          	   C   sn  g }| j }xVt| jdd ¡ ƒD ]<\}}|jj }|d |j }|d| dg7 }|j ¡ p^d}| t	t
|ƒƒd ¡ d|kr¤| t	d	d
 dd„ |jD ƒ¡ ƒ¡ n| t	d| ƒ¡ t|jƒr.y| ¡ }	W n tk
ræ   d}	Y nX |	dk	r.t|	ƒdkr|	dd… d }	|	 dd¡}	| t	d|	 ƒ¡ || rVt|| ƒ}
| t	d|
 ƒ¡ | d¡ q"W d |¡S )zoGenerate rST documentation for this class' config options.

    Excludes traits defined on parent classes.
    T)ZconfigÚ.z.. configtrait:: Ú zNo descriptionÚ
ÚEnumz
:options: z, c             s   s   | ]}d | V  qdS )z``%r``Nr   )Ú.0Úxr   r   r   ú	<genexpr>X   s    z'class_config_rst_doc.<locals>.<genexpr>z:trait type: Né@   é=   z...z\nz\\nz:default: ``%s``z:CLI option: )Ú__name__ÚsortedZclass_traitsÚitemsÚ	__class__ÚnameÚhelpÚrstripr   r   r   r   Úvaluesr   Zdefault_valueZdefault_value_reprÚ	Exceptionr   Úreplacer   )ÚclsÚtrait_aliasesÚlinesÚ	classnameÚ_ÚtraitZttypeÚfullnamer(   ZdvrZfmt_aliasesr   r   r   Úclass_config_rst_docE   s4    "&


r4   c       
      C   s¨   t tƒ}x$| j ¡ D ]\}}||  |¡ qW xt| j ¡ D ]f\}\}}t|ƒdkr:t|ƒd }|| }t|ƒdkr:t|ƒd }	||	 dkr:||d |	   |¡ q:W |S )zBProduce a mapping of trait names to lists of command line aliases.r   r   Tr   )r   r   r   r%   r   Úflagsr   )
r   ZresÚaliasr2   ÚflagZcfgr1   r0   Zcls_cfgZ	traitnamer   r   r   Úreverse_aliasesu   s    r8   Nc          	   C   s’   t |ƒ}t| dƒv}| |d ¡ | dt|ƒ d ¡ | d¡ |dk	rX| |d ¡ x*| ¡ D ]}| t||ƒ¡ | d¡ qbW W dQ R X dS )a  Write a rst file documenting config options for a traitlets application.

    Parameters
    ----------
    path : str
        The file to be written
    title : str
        The human-readable title of the document
    app : traitlets.config.Application
        An instance of the application class to be documented
    preamble : str
        Extra text to add just after the title (optional)
    Úwr   ú=Nz

)r8   ÚopenÚwriter   Z_classes_inc_parentsr4   )ÚpathÚtitler   Zpreambler.   ÚfÚcr   r   r   Ú	write_doc‰   s    
rA   )N)Ú__doc__Úcollectionsr   Útextwrapr   Z	traitletsr   Ztraitlets.utils.textr   r	   r   r   r4   r8   rA   r   r   r   r   Ú<module>"   s   0