U
    üÇ-e  ã                   @   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)ÚappÚmetadata© r   ú[/var/www/html/Darija-Ai-Train/env/lib/python3.8/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_value6   s
    r   c                 C   sB   g }| D ].}t |ƒdkrdnd}| d|› |› d¡ qd |¡S )Né   ú-z--z``ú, )ÚlenÚappendÚjoin)ÚaliasesZfmtedÚaÚdashesr   r   r	   Úformat_aliases>   s
    r   c              	   C   sh  g }| j }t| jdd ¡ ƒD ]<\}}|jj }|d |j }|d| dg7 }|j ¡ pZd}| t	t
|ƒƒd ¡ d|kr | t	d	d
 dd„ |jD ƒ¡ ƒ¡ n| t	d| ƒ¡ t|jƒr*z| ¡ }	W n tk
râ   d}	Y nX |	dk	r*t|	ƒdkr|	dd… d }	|	 dd¡}	| t	d|	 ƒ¡ || rRt|| ƒ}
| t	d|
 ƒ¡ | d¡ qd |¡S )zoGenerate rST documentation for this class' config options.

    Excludes traits defined on parent classes.
    T)ÚconfigÚ.z.. configtrait:: Ú zNo descriptionÚ
ÚEnumz
:options: r   c                 s   s   | ]}d | V  qdS )z``%r``Nr   )Ú.0Úxr   r   r	   Ú	<genexpr>Y   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   Údefault_valueZdefault_value_reprÚ	Exceptionr   Úreplacer   )ÚclsÚtrait_aliasesÚlinesÚ	classnameÚ_ÚtraitÚttypeÚfullnamer,   ZdvrZfmt_aliasesr   r   r	   Úclass_config_rst_docF   s4    &


r:   c           
      C   s    t tƒ}| j ¡ D ]\}}||  |¡ q| j ¡ D ]f\}\}}t|ƒdkr4tt|ƒƒ}|| }t|ƒdkr4tt|ƒƒ}	||	 dkr4||d |	   |¡ q4|S )zBProduce a mapping of trait names to lists of command line aliases.r   Tr   )	r   r   r   r)   r   Úflagsr   ÚnextÚiter)
r   ÚresÚaliasr7   ÚflagÚcfgr6   r5   Zcls_cfgZ	traitnamer   r   r	   Úreverse_aliasesv   s    rB   Nc              	   C   sŽ   t |ƒ}t| dƒr}| |d ¡ | dt|ƒ d ¡ | d¡ |dk	rX| |d ¡ | ¡ D ]}| t||ƒ¡ | d¡ q`W 5 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

)rB   ÚopenÚwriter   Z_classes_inc_parentsr:   )ÚpathÚtitler   Úpreambler3   ÚfÚcr   r   r	   Ú	write_docŠ   s    
rL   )N)Ú__doc__Úcollectionsr   Útextwrapr   Z	traitletsr   Ztraitlets.utils.textr   r
   r   r   r:   rB   rL   r   r   r   r	   Ú<module>   s   #0