
    _dB                     ^    d Z ddlmZ ddlmZ ddlmZ ddlmZ d Z	d Z
d Zd	 Zd
 ZddZy)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                 8    | j                  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metadatas     </usr/lib/python3/dist-packages/traitlets/config/sphinxdoc.pysetupr   *   s(     }oN&*4HHO    c                 z    | | t         u ryt        | t        t        t        t
        t        f      rt        |       S y)NFT)r   
isinstancestrlisttupledictsetbool)dvs    r   interesting_default_valuer   5   s1    

i"sD%s34Bxr   c                     g }| D ]+  }t        |      dk(  rdnd}|j                  d| | d       - dj                  |      S )N   -z--z``, )lenappendjoin)aliasesfmtedadashess       r   format_aliasesr'   =   sQ    E )A!r&!B'() 99Ur   c           
         g }| j                   }t        | j                  d      j                               D ]  \  }}|j                  j                   }|dz   |j
                  z   }|d|z   dgz  }|j                  j                         xs d}|j                  t        t        |            dz          d|v r>|j                  t        d	d
j                  d |j                  D              z                n|j                  t        d|z                t        |j                        rX	 |j                         }	|	Et#        |	      dkD  r|	dd dz   }	|	j%                  dd      }	|j                  t        d|	z               ||   r+t'        ||         }
|j                  t        d|
z                |j                  d        dj                  |      S # t         $ r d}	Y w xY w)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              3   &   K   | ]	  }d |z    yw)z``%r``N ).0xs     r   	<genexpr>z'class_config_rst_doc.<locals>.<genexpr>X   s     8\!A8\s   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fullnamer;   dvrfmt_aliasess              r   class_config_rst_docrL   E   s   
 EI3++4+8>>@A $5((s?UZZ/%0"55zz  "6&6VF4L)D01 U?LLtyy8\u||8\/\ \]^LL% 789 %U%8%89..0 s8b=cr(U*Ckk%1V$6$<=> "(x)@AKLL+ =>? 	RI$L 99U#  s   *GG$#G$c                    t        t              }| j                  j                         D ]  \  }}||   j	                  |        | j
                  j                         D ]i  \  }\  }}t        |      dk(  st        |      d   }||   }t        |      dk(  s:t        |      d   }	||	   du sP||dz   |	z      j	                  |       k |S )zBProduce a mapping of trait names to lists of command line aliases.r   r   Tr*   )r   r   r#   r8   r!   flagsr    )
r   resaliasrG   flagcfgrF   rE   cls_cfg	traitnames
             r   reverse_aliasesrU   u   s    
d
C))+ !uE
% !
 ))//+ BhsAs8q=S	!I)nG7|q  M!,	9%-	C)34;;DAB Jr   Nc                    t        |      }t        | d      5 }|j                  |dz          |j                  dt        |      z  dz          |j                  d       ||j                  |dz          |j	                         D ].  }|j                  t        ||             |j                  d       0 	 ddd       y# 1 sw Y   yxY w)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

)rU   openwriter    _classes_inc_parentsrL   )pathtitler   preamblerC   fcs          r   	write_docra      s     $C(M	dC 	A		s5z!T)*	GGHv%&))+ 	AGG(M:;GGDM		 	 	s   BB??C)N)__doc__collectionsr   textwrapr   	traitletsr   traitlets.utils.textr   r   r   r'   rL   rU   ra   r/   r   r   <module>rg      s8   !D $   '-`(r   