B
    d	                 @   s   d Z ddlmZmZ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eejeddddZeeeef 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.)
    )AnyDictList)nodes)NodeN)Sphinx)SphinxDirective)nested_parse_with_titles)
OptionSpecc               @   s   e Zd ZdS )ifconfigN)__name__
__module____qualname__ r   r   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/ext/ifconfig.pyr      s   r   c               @   s<   e Zd ZU dZdZdZdZi Zee	d< e
e dddZdS )	IfConfigT   r   option_spec)returnc             C   s>   t  }| jj|_| | | jd |d< t| j| j| |gS )Nr   expr)r   statedocumentZset_source_info	argumentsr	   content)selfnoder   r   r   run)   s    

zIfConfig.runN)r   r   r   Zhas_contentZrequired_argumentsZoptional_argumentsZfinal_argument_whitespacer   r
   __annotations__r   r   r   r   r   r   r   r   !   s   
r   )appdoctreedocnamer   c       
      C   s   dd | j D }|| j j  | jj|d< xt|tD ]}yt	|d |}W n^ t
k
r } z@ddlm} d||j|}|jjd| |d	}	||	 W d d }~X Y q>X |s|g  q>||j q>W d S )
Nc             S   s   i | ]}|j |jqS r   )valuename).0Zconfvalr   r   r   
<dictcomp>3   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_nodes2   s    r:   )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   setupH   s    
r?   )__doc__typingr   r   r   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   strr:   r?   r   r   r   r   <module>   s   