B
    .Kc	                 @   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   2lib/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_infoZ	argumentsr	   Z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)Zconfigupdate__dict__copyr#   r    listfindallr   eval	Exception	tracebackr$   join	__class__ZreportererrorZreplace_selfZchildren)
r   r   r   nsr   Zreserrr$   msgZnewnoder   r   r   process_ifconfig_nodes2   s    r4   )r   r   c             C   s.   |  t | dt | dt tjddS )Nr   zdoctree-resolvedT)versionZparallel_read_safe)Zadd_noder   Zadd_directiver   Zconnectr4   sphinxZ__display_version__)r   r   r   r   setupH   s    
r7   )__doc__typingr   r   r   Zdocutilsr   Zdocutils.nodesr   r6   Zsphinx.applicationr   Zsphinx.util.docutilsr   Zsphinx.util.nodesr	   Zsphinx.util.typingr
   ZElementr   r   r   strr4   r7   r   r   r   r   <module>   s   