B
    \                 @   s8  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddddgZd%ddZd&ddZdd Zd'ddZdd Z G dd de!Z"G dd dej#Z$G dd de!Z%e&dZ'd d Z(d(d!d"Z)d#d$ Z*dS ))z
    sphinx.testing.util
    ~~~~~~~~~~~~~~~~~~~

    Sphinx test suite utilities

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)ElementTree)nodes)
directivesroles)string_types)applicationlocale)LaTeXBuilder)AutoDirective)ModuleAnalyzer)path)relpathStructSphinxTestApp#SphinxTestAppWrapperForSkipBuildingremove_unicode_literalsc             C   s&   t | ||s"ds"td| |f d S )NFz%r did not match %r)researchAssertionError)regextextflags r   2lib/python3.7/site-packages/sphinx/testing/util.pyassert_re_search)   s    r   c             C   s&   t | ||r"ds"td| |f d S )NFz%r did match %r)r   r   r   )r   r   r   r   r   r   assert_not_re_search/   s    r   c             C   s"   |  |sdstd| |f d S )NFz%r does not start with %r)
startswithr   )thingprefixr   r   r   assert_startswith5   s    
r    c       	      K   s  |rdt |trt| |d fd|i| |dd  rt |d tr`t| |d fd|i| nFt| dkstd|t| f t| d |dd  fd|d i| nt |trt| t|kstd|t| t|f xt|D ].\}}|d|  }t| | |fd|i| qW nHt |trF| |ksdtd||| f nt | |sdtd	||| f xX| D ]L\}}|| kstd
||| f | | |ksntd|||| | f qnW d S )Nr   xpath   z&The node%s has %d child nodes, not onez[0]z%The node%s has %d child nodes, not %rz[%d]zThe node %r is not %r: %rz$The node%s is not subclass of %r: %rz)The node%s does not have %r attribute: %rzThe node%s[%s] is not %r: %r)	
isinstancelistassert_nodetuplelenr   	enumerater   items)	Znodeclsr!   kwargsiZnodeclsr   keyvaluer   r   r   r%   ;   s.    
( r%   c          	   C   s0   t jdd t jdtd t| S Q R X d S )NF)recordignore)category)warningscatch_warningsfilterwarningsDeprecationWarningr   parse)r   r   r   r   etree_parseY   s    r7   c               @   s   e Zd Zdd ZdS )r   c             K   s   | j | d S )N)__dict__update)selfkwdsr   r   r   __init__a   s    zStruct.__init__N)__name__
__module____qualname__r<   r   r   r   r   r   `   s   c               @   s,   e Zd ZdZdddZdddZd	d
 ZdS )r   z
    A subclass of :class:`Sphinx` that runs on the test root, with some
    better default values for the initialization parameters.
    htmlNFc	             C   s   |d k	r|d  | |d }	|}
|	|}| s<|  |	d}| sV|  |d krbi }d}tjd d  | _tj	 | _
tj	 | _tdd ttjD | _y&tj| ||
||||||||| W n tk
r   |    Y nX d S )Nzdocutils.confZ_builddoctreesFc             s   s   | ]}| d r|V  qdS )visit_N)r   ).0vr   r   r   	<genexpr>   s    z)SphinxTestApp.__init__.<locals>.<genexpr>)Z
write_textZjoinpathisdirmakedirssysr   _saved_pathr   _directivescopy_saved_directivesr   _roles_saved_rolessetdirr   GenericNodeVisitor_saved_nodeclassesr   Sphinxr<   	Exceptioncleanup)r:   ZbuildernameZsrcdirZfreshenvZconfoverridesZstatusZwarningZtagsZdocutilsconfZbuilddirZconfdiroutdirZ
doctreedirZwarningiserrorr   r   r   r<   l   s0    

zSphinxTestApp.__init__c             C   s   t j  tj  g t_tj  | j	t
jd d < t
jdd  | jt_| jt_xVttjD ]H}|dr^|| jkr^ttjd|dd    ttjd|dd    q^W d S )NZautodoc_fodderrB      Zdepart_)r
   	_registryclearr   cacher	   Zusepackagesr   ZtranslatorsrI   rH   r   modulespoprL   r   rJ   rN   r   rM   rP   r   rQ   r   rR   delattr)r:   rA   methodr   r   r   rU      s    




zSphinxTestApp.cleanupc             C   s   d| j j| jjf S )Nz<%s buildername=%r>)	__class__r=   Zbuildername)r:   r   r   r   __repr__   s    zSphinxTestApp.__repr__)r@   NFNNNNN)F)r=   r>   r?   __doc__r<   rU   ra   r   r   r   r   r   f   s     
%
c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   z
    This class is a wrapper for SphinxTestApp to speed up the test by skipping
    `app.build` process if it is already built and there is even one output
    file.
    c             C   s
   || _ d S )N)app)r:   Zapp_r   r   r   r<      s    z,SphinxTestAppWrapperForSkipBuilding.__init__c             C   s   t | j|S )N)getattrrc   )r:   r`   r   r   r   __getattr__   s    z/SphinxTestAppWrapperForSkipBuilding.__getattr__c             O   s   | j j s| j j|| d S )N)rc   rV   listdirbuild)r:   argskwr   r   r   rg      s    z)SphinxTestAppWrapperForSkipBuilding.buildN)r=   r>   r?   rb   r<   re   rg   r   r   r   r   r      s   zu(".*?")|u(\'.*?\')c             C   s   t dd | S )Nc             S   s   |  dp|  dS )Nr"      )group)xr   r   r   <lambda>   s    z)remove_unicode_literals.<locals>.<lambda>)_unicode_literals_resub)sr   r   r   r      s    c             #   sZ   xTt j| ddD ]B\}}}t|}x. fdd|D D ]}|| }t|| V  q6W qW d S )NT)followlinksc                s   g | ]} r|  r|qS r   )endswith)rC   f)suffixr   r   
<listcomp>   s    zfind_files.<locals>.<listcomp>)oswalkr   r   )rootrt   dirpathdirsfilesrs   Zfpathr   )rt   r   
find_files   s
    r|   c             C   s   t dd| S )Nz.*?mr    )r   ro   )r   r   r   r   strip_escseq   s    r}   )r   )r   )Nr    )N)+rb   rv   r   rH   r2   Z	xml.etreer   Zdocutilsr   Zdocutils.parsers.rstr   r   Zsixr   Zsphinxr   r   Zsphinx.builders.latexr	   Zsphinx.ext.autodocr
   Zsphinx.pycoder   Zsphinx.testing.pathr   Zsphinx.util.osutilr   __all__r   r   r   r%   r7   objectr   rS   r   r   compilern   r   r|   r}   r   r   r   r   <module>
   s:   	


B

	