o
    ȷeB                     @   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 )zRegisters the Sphinx extension.

    You shouldn't need to call this directly; configure Sphinx to use this
    module instead.
    configtraitzConfig option)objnameT)parallel_read_safeparallel_write_safe)add_object_type)appmetadata r   Q/var/www/ideatree/venv/lib/python3.10/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rt| S dS )NFT)r   
isinstancestrlisttupledictsetbool)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)aliasesfmtedadashesr   r   r   format_aliases=   s
   
r#   c              	   C   s\  g }| j }t| jdd D ]\}}|jj }|d |j }|d| dg7 }|j p,d}|t	t
|d  d|v rO|t	d	d
dd |jD   n	|t	d|  t|jrz| }	W n tyn   d}	Y nw |	durt|	dkr|	dd d }	|	dd}	|t	d|	  || rt|| }
|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   s    z'class_config_rst_doc.<locals>.<genexpr>z:trait type: N@   =   z...z\nz\\nz:default: ``%s``z:CLI option: )__name__sortedclass_traitsitems	__class__namehelprstripr   r   r   r   valuesr   default_valuedefault_value_repr	Exceptionr   replacer#   )clstrait_aliaseslines	classname_traitttypefullnamer4   dvrfmt_aliasesr   r   r   class_config_rst_docE   s6   &

rE   c           
      C   s   t t}| j D ]\}}|| | q	| j D ]3\}\}}t|dkrMt|d }|| }t|dkrMt|d }	||	 du rM||d |	  | q|S )zBProduce a mapping of trait names to lists of command line aliases.r   r   Tr%   )r   r   r   r1   r   flagsr   )
r   resaliasr@   flagcfgr?   r>   cls_cfg	traitnamer   r   r   reverse_aliasesu   s   rM   Nc                 C   s   t |}t| d?}||d  |dt| d  |d |dur,||d  | D ]}|t|| |d q0W d   dS 1 sKw   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

)rM   openwriter   _classes_inc_parentsrE   )pathtitler   preambler<   fcr   r   r   	write_doc   s   
"rX   )N)__doc__collectionsr   textwrapr   	traitletsr   traitlets.utils.textr   r   r   r#   rE   rM   rX   r   r   r   r   <module>   s    "0