U
    \Sh	                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
mZ ddlmZ dd	lmZ dd
lmZ G dd dejZG dd deZdddddddZdddddZdS )a  Provides the ``ifconfig`` directive.

The ``ifconfig`` directive enables writing documentation
that is included depending on configuration variables.

Usage::

    .. ifconfig:: releaselevel in ('alpha', 'beta', 'rc')

       This stuff is only included in the built docs for unstable versions.

The argument for ``ifconfig`` is a plain Python expression, evaluated in the
namespace of the project configuration (that is, all variables from
``conf.py`` are available.)
    )annotations)Any)nodes)NodeN)Sphinx)SphinxDirective)nested_parse_with_titles)
OptionSpecc                   @  s   e Zd ZdS )ifconfigN)__name__
__module____qualname__ r   r   F/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/ext/ifconfig.pyr
      s   r
   c                   @  s8   e Zd ZU dZdZdZdZi Zded< dddd	Z	d
S )IfConfigT   r   r	   option_specz
list[Node])returnc                 C  sB   t  }| jj|_| | | jd |d< t| j| j|| j |gS )Nr   expr)r
   stateZdocumentZset_source_info	argumentsr   contentZcontent_offset)selfnoder   r   r   run+   s    

zIfConfig.runN)
r   r   r   Zhas_contentZrequired_argumentsZoptional_argumentsZfinal_argument_whitespacer   __annotations__r   r   r   r   r   r   #   s   
r   r   znodes.documentstrNone)appdoctreedocnamer   c           
      C  s   dd | j D }|| j j  | jj|d< t|tD ]}zt	|d |}W n^ t
k
r } z@ddlm} d||j|}|jjd| |d	}	||	 W 5 d }~X Y q<X |s|g  q<||j q<d S )
Nc                 S  s   i | ]}|j |jqS r   )namevalue).0Zconfvalr   r   r   
<dictcomp>5   s      z*process_ifconfig_nodes.<locals>.<dictcomp>builderr   r   )format_exception_only z.Exception occurred in ifconfig expression: 
%s)Z	base_node)configupdate__dict__copyr%   r!   listfindallr
   eval	Exception	tracebackr&   join	__class__reportererrorZreplace_selfchildren)
r   r   r    nsr   reserrr&   msgZnewnoder   r   r   process_ifconfig_nodes4   s$    r:   zdict[str, Any])r   r   c                 C  s.   |  t | dt | dt tjddS )Nr
   zdoctree-resolvedT)versionZparallel_read_safe)add_noder
   Zadd_directiver   connectr:   sphinxZ__display_version__)r   r   r   r   setupJ   s    
r?   )__doc__
__future__r   typingr   Zdocutilsr   Zdocutils.nodesr   r>   Zsphinx.applicationr   Zsphinx.util.docutilsr   Zsphinx.util.nodesr   Zsphinx.util.typingr	   ZElementr
   r   r:   r?   r   r   r   r   <module>   s   