a
    ©lcB  ã                   @   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   úV/var/www/html/django/DPS/env/lib/python3.9/site-packages/traitlets/config/sphinxdoc.pyÚsetup*   s    
r
   c                 C   s4   | d u s| t u 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   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   sf  g }| j }t| jdd ¡ ƒD ]:\}}|jj }|d |j }|d| dg7 }|j ¡ pZd}| t	t
|ƒƒd ¡ d|v r | t	d	d
 dd„ |jD ƒ¡ ƒ¡ n| t	d| ƒ¡ t|jƒr(z| ¡ }	W n tyà   d}	Y n0 |	dur(t|	ƒdkr
|	dd… d }	|	 dd¡}	| t	d|	 ƒ¡ || rPt|| ƒ}
| 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>X   ó    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_docE   s4    &


r;   c           
      C   s    t tƒ}| j ¡ D ]\}}||  |¡ q| j ¡ D ]f\}\}}t|ƒdkr4t|ƒd }|| }t|ƒdkr4t|ƒd }	||	 du r4||d |	   |¡ q4|S )zBProduce a mapping of trait names to lists of command line aliases.r   r   Tr   )r   r   r   r*   r   Úflagsr   )
r   ÚresÚaliasr8   ÚflagÚcfgr7   r6   Zcls_cfgZ	traitnamer   r   r	   Úreverse_aliasesu   s    rA   Nc                 C   s¢   t |ƒ}t| dƒ|}| |d ¡ | dt|ƒ d ¡ | d¡ |durX| |d ¡ | ¡ D ]}| t||ƒ¡ | d¡ q`W d  ƒ n1 s”0    Y  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

)rA   ÚopenÚwriter   Z_classes_inc_parentsr;   )ÚpathÚtitler   Úpreambler4   ÚfÚcr   r   r	   Ú	write_doc‰   s    
rK   )N)Ú__doc__Úcollectionsr   Útextwrapr   Z	traitletsr   Ztraitlets.utils.textr   r
   r   r   r;   rA   rK   r   r   r   r	   Ú<module>   s   "0