B
    \1                 @   s  d Z ddlmZ ddl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 ddlmZ ddlZddlmZ dd	lmZ dd
lmZmZ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l!m"Z" e"#e$Z%e&dZ'e(eej)j*Z+e, Z-e	dd Z.dd Z/dd Z0dd Z1e	dd Z2e	dd Z3e	d6ddZ4G d d! d!e5Z6G d"d# d#e7Z8G d$d% d%e7Z9G d&d' d'eZ:G d(d) d)eZ;d*d+ Z<d7d,d-Z=e	d.d/ Z>G d0d1 d1eZ?G d2d3 d3eZ@daAd8d4d5ZBdS )9z
    sphinx.util.docutils
    ~~~~~~~~~~~~~~~~~~~~

    Utility functions for docutils.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    )absolute_importN)contextmanager)copy)LooseVersion)path)nodes)
FileOutput)	Directive
directivesrolesconvert_directive_function)StateMachine)Reporter)RemovedInSphinx30Warning)ExtensionError)__)loggingz>^(.+?:(?:\d+)?): \((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\d+)?\) c           
   c   sV   zt tj} t tj}dV  W d| t_|t_x"ttD ]}t| t| q6W X dS )z"Create namespace for reST parsers.N)	r   r
   _directivesr   _roleslistadditional_nodesunregister_nodediscard)r   r   node r   3lib/python3.7/site-packages/sphinx/util/docutils.pydocutils_namespace3   s    


r   c             C   s   t tjd| j S )z'Check the *node* is already registered.visit_)hasattrr   GenericNodeVisitor__name__)r   r   r   r   is_node_registeredE   s    r!   c             C   s.   t tjd| j s*t| jg t|  dS )zRegister a node to docutils.

    This modifies global state of some visitors.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    r   N)r   r   r   r    Z_add_node_class_namesr   add)r   r   r   r   register_nodeK   s    r#   c             C   s^   t tjd| j rZttjd| j  ttjd| j  ttjd| j  ttjd| j  dS )zaUnregister a node from docutils.

    This is inverse of ``nodes._add_nodes_class_names()``.
    r   Zdepart_N)r   r   r   r    delattrZSparseNodeVisitor)r   r   r   r   r   W   s
    r   c              #   s<   ddl m  d fdd	} z| tj_dV  W d tj_X dS )zPatch docutils.languages.get_language() temporarily.

    This ignores the second argument ``reporter`` to suppress warnings.
    refs: https://github.com/sphinx-doc/sphinx/issues/3788
    r   )get_languageNc                s    | S )Nr   )Zlanguage_codereporter)r%   r   r   patched_get_languagen   s    z2patched_get_language.<locals>.patched_get_language)N)Zdocutils.languagesr%   docutilsZ	languages)r'   r   )r%   r   r'   d   s    
r'   c          
   c   s^   z4t jdd}| r,tt| dt jd< dV  W d|dkrNt jdd n
|t jd< X dS )z?Let docutils know the location of ``docutils.conf`` for Sphinx.ZDOCUTILSCONFIGNzdocutils.conf)osenvirongetr   joinabspathpop)confdirZdocutilsconfigr   r   r   using_user_docutils_confz   s    
r0   c          
   c   s0   t    t|  dV  W dQ R X W dQ R X dS )zPatch to docutils temporarily.N)r'   r0   )r/   r   r   r   patch_docutils   s    r1   c               @   s   e Zd ZdS )ElementLookupErrorN)r    
__module____qualname__r   r   r   r   r2      s   r2   c               @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )sphinx_domainszcMonkey-patch directive and role dispatch, so that domain-specific
    markup takes precedence.
    c             C   s   || _ d | _d | _d S )N)envdirective_funcZ
roles_func)selfr6   r   r   r   __init__   s    zsphinx_domains.__init__c             C   s   |    d S )N)enable)r8   r   r   r   	__enter__   s    zsphinx_domains.__enter__c             C   s   |    d S )N)disable)r8   typevalue	tracebackr   r   r   __exit__   s    zsphinx_domains.__exit__c             C   s$   t j| _tj| _| jt _| jt_d S )N)r
   	directiver7   r   role	role_funclookup_directivelookup_role)r8   r   r   r   r:      s    zsphinx_domains.enablec             C   s   | j t_| jt_d S )N)r7   r
   rA   rC   r   rB   )r8   r   r   r   r<      s    zsphinx_domains.disablec             C   s   |  }d|krX|dd\}}|| jjkr| j|}t|||}|dk	r|g fS n4| jjd}|dk	rt|||}|dk	r|g fS t| jd||}|dk	r|g fS tdS )ztLookup a markup element (directive or role), given its name which can
        be a full name (with domain).
        :   NZdefault_domainZstd)	lowersplitr6   ZdomainsZ
get_domaingetattrZ	temp_datar+   r2   )r8   r=   nameZdomain_nameZdomainelementZ
def_domainr   r   r   lookup_domain_element   s"    
z$sphinx_domains.lookup_domain_elementc             C   s0   y|  d|S  tk
r*   | |||S X d S )NrA   )rM   r2   r7   )r8   rK   lang_moduledocumentr   r   r   rD      s    zsphinx_domains.lookup_directivec          	   C   s2   y|  d|S  tk
r,   | ||||S X d S )NrB   )rM   r2   rC   )r8   rK   rN   linenor&   r   r   r   rE      s    zsphinx_domains.lookup_roleN)r    r3   r4   __doc__r9   r;   r@   r:   r<   rM   rD   rE   r   r   r   r   r5      s   r5   c               @   s   e Zd Zdd ZdS )WarningStreamc             C   sR   t |}|s t|d n.| \}}}t d| }tj|||d d S )Nz
 )location)	report_researchloggerZwarningrstripgroupssublog)r8   textZmatchedrT   r=   levelmessager   r   r   write   s    
zWarningStream.writeN)r    r3   r4   r_   r   r   r   r   rR      s   rR   c               @   s.   e Zd Zedd ZejejddfddZdS )LoggingReporterc             C   s   | |j |j|j|j|jS )zACreate an instance of LoggingReporter from other reporter object.)sourcereport_level
halt_levelZ
debug_flagerror_handler)clsr&   r   r   r   from_reporter   s    zLoggingReporter.from_reporterFbackslashreplacec          	   C   s"   t  }tj| ||||||d d S )N)rd   )rR   r   r9   )r8   ra   rb   rc   debugrd   streamr   r   r   r9      s    zLoggingReporter.__init__N)	r    r3   r4   classmethodrf   r   ZWARNING_LEVELZSEVERE_LEVELr9   r   r   r   r   r`      s   r`   c               @   s   e Zd ZdZdd ZdS )NullReporterz A dummy reporter; write nothing.c             C   s   t | ddd d S )NrS   i     )r   r9   )r8   r   r   r   r9     s    zNullReporter.__init__N)r    r3   r4   rQ   r9   r   r   r   r   rk      s   rk   c               C   s   t dkS )N)r      r   )__version_info__r   r   r   r   is_html5_writer_available  s    ro   c             K   s\   t dt t| tjtjfr<|| _|p*d| _|| _	t
| S |sH|sH|rTttd| S d S )NzVfunction based directive support is now deprecated. Use class based directive instead.)r   r   FzCwhen adding directive classes, no additional arguments may be given)warningswarnr   
isinstancetypesFunctionType
MethodTypecontentZ	argumentsZoptionsr   r   r   )objZhas_contentZargument_specZoption_specr   r   r   directive_helper  s    
rx   c             c   sB   z0| j jj}tg d}||_|j| j j_dV  W d|| j j_X dS )z1Switch current source input of state temporarily.N)memor&   get_source_and_liner   Zinput_lines)staterv   rz   Zstate_machiner   r   r   switch_source_input  s    


r|   c               @   s    e Zd ZdZdd Zdd ZdS )SphinxFileOutputz#Better FileOutput class for Sphinx.c             K   s    | dd| _tj| f| d S )Noverwrite_if_changedF)r.   r~   r   r9   )r8   kwargsr   r   r   r9   4  s    zSphinxFileOutput.__init__c          	   C   sd   | j rX| jrXd| jkrX| jrXtj| j rXtj| j | j	d}|
 |krN|S W d Q R X t| |S )Nb)encoding)Zdestination_pathZ	autoclosemoder~   r)   r   existscodecsopenr   readr   r_   )r8   datafr   r   r   r_   9  s    zSphinxFileOutput.writeN)r    r3   r4   rQ   r9   r_   r   r   r   r   r}   1  s   r}   c               @   s(   e Zd ZdZedd Zedd ZdS )SphinxDirectivezA base class for Sphinx directives.

    This class provides helper methods for Sphinx directives.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    c             C   s   | j jjjS )z3Reference to the :class:`.BuildEnvironment` object.)r{   rO   settingsr6   )r8   r   r   r   r6   N  s    zSphinxDirective.envc             C   s   | j jS )z)Reference to the :class:`.Config` object.)r6   config)r8   r   r   r   r   T  s    zSphinxDirective.configN)r    r3   r4   rQ   propertyr6   r   r   r   r   r   r   E  s   r   c             C   sH   t dkrtj| a |dkr&tt j}tj|t j| d}|	| d |S )a&  Return a new empty document object.  This is an alternative of docutils'.

    This is a simple wrapper for ``docutils.utils.new_document()``.  It
    caches the result of docutils' and use it on second call for instanciation.
    This makes an instantiation of document nodes much faster.
    N)ra   )
__document_cache__r(   Zutilsnew_documentr   r   r   rO   r&   Znote_source)source_pathr   rO   r   r   r   r   `  s    	
r   )N)NN)N)CrQ   Z
__future__r   r   r)   rers   rp   
contextlibr   r   Zdistutils.versionr   r   r(   r   Zdocutils.ior   Zdocutils.parsers.rstr	   r
   r   r   Zdocutils.statemachiner   Zdocutils.utilsr   Zsphinx.deprecationr   Zsphinx.errorsr   Zsphinx.localer   Zsphinx.utilr   Z	getLoggerr    rW   compilerU   tuple__version__versionrn   setr   r   r!   r#   r   r'   r0   r1   	Exceptionr2   objectr5   rR   r`   rk   ro   rx   r|   r}   r   r   r   r   r   r   r   <module>
   sX   

K
