3

T'ícéQã@s¸UdZddlZddlZddlmZddlmZddlmZddlmZddl	m
Z
mZmZm
Z
mZmZmZmZmZmZmZmZddlZddlmZdd	lmZdd
lmZmZmZddlmZm Z m!Z!ddl"m#Z#dd
l$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1ddl2m3Z3ddl4m5Z5e3j6e7ƒZ8ej9dƒZ:ertddl;m<Z<ddl=m>Z>ddl?m@Z@e,jAejBƒjCZDeEƒZFeeeFeedYdœdd„ƒZGeHeIdœdd„ZJeHeeddœdd „ZKeHeIdœd!d"„ZLeHe5dd#œd$d%„ZMeHddœd&d'„ZNeeeId(œd)d*„ZOeedd(œd+d,„ZPeedd(œd-d.„ZQeedZdœd/d0„ƒZReed[dœd1d2„ƒZSeeeHed\d3œd4d5„ƒZTed]eeHed^d3œd6d7„ƒZUGd8d9„d9ƒZVGd:d;„d;eWƒZXGd<d=„d=eVƒZYGd>d?„d?ƒZZGd@dA„dAe)ƒZ[GdBdC„dCe)ƒZ\eIdœdDdE„Z]ee%e'ed_dFœdGdH„ƒZ^GdIdJ„dJeƒZ_GdKdL„dLeƒZ`GdMdN„dNƒZaGdOdP„dPeaƒZbGdQdR„dRejcƒZdeDd`krdTdU„Zeeee_edafeejgfdaeHeejgdVœdWdX„ZhdS)bzUtility functions for docutils.éN)Úcontextmanager)Úcopy)Úpath)Ú
ModuleType)ÚIOÚ
TYPE_CHECKINGÚAnyÚCallableÚDictÚ	GeneratorÚListÚOptionalÚSetÚTupleÚTypeÚcast)Únodes)Ú
FileOutput)ÚElementÚNodeÚsystem_message)Ú	DirectiveÚ
directivesÚroles)ÚInliner)ÚStateÚStateMachineÚ
StringList)ÚReporterÚunescape)Úversion)ÚSphinxError)Ú_Ú__)Úlogging)ÚRoleFunctionz>^(.+?:(?:\d+)?): \((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\d+)?\) )ÚBuilder)ÚConfig)ÚBuildEnvironment)ÚreturnccsVzttjƒ}ttjƒ}dVWd|t_|t_x"ttƒD]}t|ƒtj|ƒq6WXdS)z"Create namespace for reST parsers.N)	rrÚ_directivesrÚ_rolesÚlistÚadditional_nodesÚunregister_nodeÚdiscard)r*r+Únode©r1ú6/tmp/pip-build-gk9425m9/sphinx/sphinx/util/docutils.pyÚdocutils_namespace(s


r3)Únamer)cCs
|tjkS)z1Check the *name* directive is already registered.)rr*)r4r1r1r2Úis_directive_registered9sr5)r4Ú	directiver)cCstj||ƒdS)z­Register a directive to docutils.

    This modifies global state of docutils.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    N)rÚregister_directive)r4r6r1r1r2r7>sr7cCs
|tjkS)z,Check the *name* role is already registered.)rr+)r4r1r1r2Úis_role_registeredGsr8)r4Úroler)cCstj||ƒdS)z¨Register a role to docutils.

    This modifies global state of docutils.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    N)rZregister_local_role)r4r9r1r1r2Ú
register_roleLsr:cCstjj|dƒdS)z Unregister a role from docutils.N)rr+Úpop)r4r1r1r2Úunregister_roleUsr<)r0r)cCsttjd|jƒS)z'Check the *node* is already registered.Úvisit_)ÚhasattrrÚGenericNodeVisitorÚ__name__)r0r1r1r2Úis_node_registeredZsrAcCs.ttjd|jƒs*tj|jgƒtj|ƒdS)z­Register 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>rr?r@Z_add_node_class_namesr-Úadd)r0r1r1r2Ú
register_node_srCcCs^ttjd|jƒrZttjd|jƒttjd|jƒttjd|jƒttjd|jƒdS)zaUnregister a node from docutils.

    This is inverse of ``nodes._add_nodes_class_names()``.
    r=Zdepart_N)r>rr?r@ÚdelattrZSparseNodeVisitor)r0r1r1r2r.js
r.c#sFddlm‰dtttdœ‡fdd„
}z|tj_dVWdˆtj_XdS)z½Patch 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_languageN)Ú
language_codeÚreporterr)csˆ|ƒS)Nr1)rFrG)rEr1r2Úpatched_get_languagesz2patched_get_language.<locals>.patched_get_language)N)Zdocutils.languagesrEÚstrrrÚdocutilsÚ	languages)rHr1)rEr2rHvs
rHc#sNddlm‰dtttdœ‡fdd„
}z|tjjj_dVWdˆtjjj_XdS)a^Patch docutils.parsers.rst.languages.get_language().
    Starting from docutils 0.17, get_language() in ``rst.languages``
    also has a reporter, which needs to be disabled temporarily.

    This should also work for old versions of docutils,
    because reporter is none by default.

    refs: https://github.com/sphinx-doc/sphinx/issues/10179
    r)rEN)rFrGr)csˆ|ƒS)Nr1)rFrG)rEr1r2rH—sz6patched_rst_get_language.<locals>.patched_get_language)N)	Zdocutils.parsers.rst.languagesrErIrrrJÚparsersZrstrK)rHr1)rEr2Úpatched_rst_get_languageŠs
rM)Úconfdirr)ccs^z4tjjddƒ}|r,tjtj|ƒdƒtjd<dVWd|dkrNtjjddƒn
|tjd<XdS)z?Let docutils know the location of ``docutils.conf`` for Sphinx.ZDOCUTILSCONFIGNz
docutils.conf)ÚosÚenvironÚgetrÚjoinÚabspathr;)rNZdocutilsconfigr1r1r2Úusing_user_docutils_conf¢s
rTccsBtƒ2tƒ t|ƒdVWdQRXWdQRXWdQRXdS)zPatch to docutils temporarily.N)rHrMrT)rNr1r1r2Úpatch_docutils²srUc@s¬eZdZdZddœdd„Zddœdd„Zeeeeddœd	d
„Z	ddœdd„Z
ddœd
d„Zee
ejeeeeeefdœdd„Zee
eeeeeefdœdd„ZdS)ÚCustomReSTDispatcherzCustom reST's mark-up dispatcher.

    This replaces docutils's directives and roles dispatch mechanism for reST parser
    by original one temporarily.
    N)r)cCsdd„|_dd„|_dS)NcWsdgfS)Nr1)Úargsr1r1r2Ú<lambda>Ász/CustomReSTDispatcher.__init__.<locals>.<lambda>cWsdgfS)Nr1)rWr1r1r2rXÂs)Údirective_funcZ
roles_func)Úselfr1r1r2Ú__init__Às
zCustomReSTDispatcher.__init__cCs|jƒdS)N)Úenable)rZr1r1r2Ú	__enter__ÄszCustomReSTDispatcher.__enter__)Úexc_typeÚ	exc_valueÚ	tracebackr)cCs|jƒdS)N)Údisable)rZr^r_r`r1r1r2Ú__exit__ÇszCustomReSTDispatcher.__exit__cCs$tj|_tj|_|jt_|jt_dS)N)rr6rYrr9Ú	role_func)rZr1r1r2r\ÊszCustomReSTDispatcher.enablecCs|jt_|jt_dS)N)rYrr6rcrr9)rZr1r1r2raÑszCustomReSTDispatcher.disable)Údirective_nameÚlanguage_moduleÚdocumentr)cCs|j|||ƒS)N)rY)rZrdrerfr1r1r2r6ÕszCustomReSTDispatcher.directive)Ú	role_namereÚlinenorGr)cCs|j||||ƒS)N)rc)rZrgrerhrGr1r1r2r9ÚszCustomReSTDispatcher.role)r@Ú
__module__Ú__qualname__Ú__doc__r[r]rÚ	Exceptionrrbr\rarIrrrfrr
rrrr6Úintrr%r9r1r1r1r2rV¹s"rVc@seZdZdS)ÚElementLookupErrorN)r@rirjr1r1r1r2rnßsrncsŽeZdZdZdddœ‡fdd„Zeeedœdd	„Zeee	j
eee
eeefd
œ‡fdd„Zeeeeeeeefd
œ‡fdd„Z‡ZS)Úsphinx_domainszcMonkey-patch directive and role dispatch, so that domain-specific
    markup takes precedence.
    r(N)Úenvr)cs||_tƒjƒdS)N)rpÚsuperr[)rZrp)Ú	__class__r1r2r[çszsphinx_domains.__init__)Útyper4r)cCsÎ|jƒ}d|krl|jddƒ\}}||jjkrX|jj|ƒ}t||ƒ|ƒ}|dk	rj|gfSq tjtdƒ||ƒn4|jj	j
dƒ}|dk	r t||ƒ|ƒ}|dk	r |gfSt|jjdƒ|ƒ|ƒ}|dk	rÆ|gfSt‚dS)ztLookup a markup element (directive or role), given its name which can
        be a full name (with domain).
        ú:éNz%unknown directive or role name: %s:%sZdefault_domainZstd)ÚlowerÚsplitrpÚdomainsZ
get_domainÚgetattrÚloggerÚwarningr"Ú	temp_datarQrn)rZrsr4Zdomain_nameÚdomainÚelementZ
def_domainr1r1r2Úlookup_domain_elementës$
z$sphinx_domains.lookup_domain_element)rdrerfr)cs2y|jd|ƒStk
r,tƒj|||ƒSXdS)Nr6)rrnrqr6)rZrdrerf)rrr1r2r6	szsphinx_domains.directive)rgrerhrGr)cs4y|jd|ƒStk
r.tƒj||||ƒSXdS)Nr9)rrnrqr9)rZrgrerhrG)rrr1r2r9szsphinx_domains.role)r@rirjrkr[rIrrrrrfrr
rrrrr6rmrr%r9Ú
__classcell__r1r1)rrr2roãs&roc@seZdZeddœdd„ZdS)Ú
WarningStreamN)Útextr)cCsRtj|ƒ}|s tj|jdƒƒn.|jƒ\}}}tjd|ƒjƒ}tj|||ddS)Nz
Ú)Úlocation)Ú	report_reÚsearchrzr{ÚrstripÚgroupsÚsubÚlog)rZr‚Úmatchedr„rsÚlevelÚmessager1r1r2Úwrites
zWarningStream.write)r@rirjrIrŽr1r1r1r2rsrcsNeZdZeeddœdd„ƒZejejddfee	e	e
eddœ‡fdd	„
Z‡ZS)
ÚLoggingReporter)rGr)cCs||j|j|j|j|jƒS)zACreate an instance of LoggingReporter from other reporter object.)ÚsourceÚreport_levelÚ
halt_levelZ
debug_flagÚ
error_handler)ÚclsrGr1r1r2Ú
from_reporter%szLoggingReporter.from_reporterFÚbackslashreplaceN)rr‘r’Údebugr“r)cs(tttƒƒ}tƒj||||||ddS)N)r“)rrrrqr[)rZrr‘r’r—r“Ústream)rrr1r2r[+szLoggingReporter.__init__)
r@rirjÚclassmethodrr•Z
WARNING_LEVELZSEVERE_LEVELrIrmÚboolr[r€r1r1)rrr2r$s
rcs&eZdZdZddœ‡fdd„Z‡ZS)ÚNullReporterz A dummy reporter; write nothing.N)r)cstƒjdddƒdS)Nrƒiçé)rqr[)rZ)rrr1r2r[6szNullReporter.__init__)r@rirjrkr[r€r1r1)rrr2r›3sr›cCstdkS)Nré
)rrr)Ú__version_info__r1r1r1r2Úis_html5_writer_available:srŸ)ÚstateÚcontentr)ccsBz0|jjj}tgdƒ}||_|j|jj_dVWd||jj_XdS)z1Switch current source input of state temporarily.N)ÚmemorGÚget_source_and_linerZinput_lines)r r¡r£Ú
state_machiner1r1r2Úswitch_source_input>s


r¥cs<eZdZdZeddœ‡fdd„Zeedœ‡fdd„Z‡ZS)	ÚSphinxFileOutputz#Better FileOutput class for Sphinx.N)Úkwargsr)cs |jddƒ|_tƒjf|ŽdS)NÚoverwrite_if_changedF)r;r¨rqr[)rZr§)rrr1r2r[SszSphinxFileOutput.__init__)Údatar)c	sb|jrV|jrVd|jkrV|jrVtjj|jƒrVt|j|jd}|j	ƒ|krL|SWdQRXt
ƒj|ƒS)NÚb)Úencoding)Zdestination_pathZ	autocloseÚmoder¨rOrÚexistsÚopenr«ÚreadrqrŽ)rZr©Úf)rrr1r2rŽWszSphinxFileOutput.write)	r@rirjrkrr[rIrŽr€r1r1)rrr2r¦Psr¦c@sheZdZdZeddœdd„ƒZeddœdd„ƒZeee	fdœd	d
„Z
eddœd
d„Zedœdd„Z
dS)ÚSphinxDirectivezëA 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.
    r()r)cCs|jjjjS)z3Reference to the :class:`.BuildEnvironment` object.)r rfÚsettingsrp)rZr1r1r2rpkszSphinxDirective.envr'cCs|jjS)z)Reference to the :class:`.Config` object.)rpÚconfig)rZr1r1r2r³pszSphinxDirective.configcCs|jj|jƒS)zGet source and line number.)r¤r£rh)rZr1r1r2Úget_source_infouszSphinxDirective.get_source_infoN)r0r)cCs|jƒ\|_|_dS)z'Set source and line number to the node.N)r´rÚline)rZr0r1r1r2Úset_source_infoyszSphinxDirective.set_source_infocCsdjdd„|jƒDƒƒS)z&Get current location info for logging.rtcss|]}t|ƒVqdS)N)rI)Ú.0Úsr1r1r2ú	<genexpr>sz/SphinxDirective.get_location.<locals>.<genexpr>)rRr´)rZr1r1r2Úget_location}szSphinxDirective.get_location)r@rirjrkÚpropertyrpr³rrIrmr´rr¶rºr1r1r1r2r±bsr±c@sæeZdZUdZeeee	e
e
eeigfeeeee
eeeeeeeefdœdd„Zeeeeefdœdd„Zeddœd	d
„ƒZeddœdd
„ƒZdeeeefdœdd„Zdeeddœdd„Zedœdd„ZdS)Ú
SphinxRolezáA base class for Sphinx roles.

    This class provides helper methods for Sphinx roles.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    )r4Úrawtextr‚rhÚinlinerÚoptionsr¡r)cCsr||_t|ƒ|_||_||_||_||_|r8|jƒ|_n2|j	j
jddƒ|_|js\|j	jj
|_|jsjtdƒ‚|jƒS)NÚdefault_rolerƒzcannot determine default role!)r½rr‚rhr¾r¿r¡rvr4rpr|rQr³rÀr!Úrun)rZr4r½r‚rhr¾r¿r¡r1r1r2Ú__call__”s
zSphinxRole.__call__)r)cCst‚dS)N)ÚNotImplementedError)rZr1r1r2rÁªszSphinxRole.runr(cCs|jjjjS)z3Reference to the :class:`.BuildEnvironment` object.)r¾rfr²rp)rZr1r1r2rp­szSphinxRole.envr'cCs|jjS)z)Reference to the :class:`.Config` object.)rpr³)rZr1r1r2r³²szSphinxRole.configN)rhr)cCs|dkr|j}|jjj|ƒS)N)rhr¾rGr£)rZrhr1r1r2r´·szSphinxRole.get_source_info)r0rhr)cCs|j|ƒ\|_|_dS)N)r´rrµ)rZr0rhr1r1r2r¶¼szSphinxRole.set_source_infocCsdjdd„|jƒDƒƒS)z&Get current location info for logging.rtcss|]}t|ƒVqdS)N)rI)r·r¸r1r1r2r¹Ász*SphinxRole.get_location.<locals>.<genexpr>)rRr´)rZr1r1r2rº¿szSphinxRole.get_location)N)N)r@rirjrkrIr4r½r‚rmrhrr¾r
r¿rr¡rrrrÂrÁr»rpr³r´r¶rºr1r1r1r2r¼‚s$
r¼csneZdZUdZeeee	e
jde
jƒZ
igfeeeeeeeeeeeeefdœ‡fdd„
Z‡ZS)Ú
ReferenceRolezèA base class for reference roles.

    The reference roles can accept ``link title <target>`` style as a text for
    the role.  The parsed result; link title and target will be stored to
    ``self.title`` and ``self.target``.
    z^(.+?)\s*(?<!\x00)<(.*?)>$)r4r½r‚rhr¾r¿r¡r)c	sv|jdƒ|_|jj|ƒ}|rDd|_t|jdƒƒ|_t|jdƒƒ|_nd|_t|ƒ|_t|ƒ|_t	ƒj
|||||||ƒS)Nú!TruéF)Ú
startswithÚdisabledÚexplicit_title_reÚmatchÚhas_explicit_titlerÚgroupÚtitleÚtargetrqrÂ)	rZr4r½r‚rhr¾r¿r¡r‹)rrr1r2rÂÓs

zReferenceRole.__call__)r@rirjrkršrËrÈrIrÍrÎÚreÚcompileÚDOTALLrÉrmrr
rrrrrÂr€r1r1)rrr2rÄÄs
rÄcsdeZdZdZejdddœ‡fdd„Zeddœ‡fdd	„Zeddœ‡fd
d„Z	eddœdd
„Z
‡ZS)ÚSphinxTranslatoraxA base class for Sphinx translators.

    This class adds a support for visitor/departure method for super node class
    if visitor/departure method for node class is not found.

    It also provides helper methods for Sphinx translators.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r&N)rfÚbuilderr)cs&tƒj|ƒ||_|j|_|j|_dS)N)rqr[rÓr³r²)rZrfrÓ)rrr1r2r[òszSphinxTranslator.__init__)r0r)csBx<|jjD]$}t|d|jdƒ}|r
||ƒPq
Wtƒj|ƒdS)zæ
        Dispatch node to appropriate visitor method.
        The priority of visitor method is:

        1. ``self.visit_{node_class}()``
        2. ``self.visit_{super_node_class}()``
        3. ``self.unknown_visit()``
        zvisit_%sN)rrÚ__mro__ryr@rqÚdispatch_visit)rZr0Ú
node_classÚmethod)rrr1r2rÕøs	zSphinxTranslator.dispatch_visitcsBx<|jjD]$}t|d|jdƒ}|r
||ƒPq
Wtƒj|ƒdS)zð
        Dispatch node to appropriate departure method.
        The priority of departure method is:

        1. ``self.depart_{node_class}()``
        2. ``self.depart_{super_node_class}()``
        3. ``self.unknown_departure()``
        z	depart_%sN)rrrÔryr@rqÚdispatch_departure)rZr0rÖr×)rrr1r2rØ	s	z#SphinxTranslator.dispatch_departurecCstjtdƒ||ddS)Nzunknown node type: %r)r„)rzr{r#)rZr0r1r1r2Ú
unknown_visitszSphinxTranslator.unknown_visit)r@rirjrkrrfr[rrÕrØrÙr€r1r1)rrr2rÒæs

rÒécOst|j||ŽƒS)N)ÚiterZtraverse)rZrWr§r1r1r2Úfindall"srÜ)Úsource_pathr²r)cCsTtdkrtjj|ƒa|dkr&ttjƒ}ddlm}|j|tj	|d}|j
|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 instantiation.
    This makes an instantiation of document nodes much faster.
    Nr)Úaddnodes)rruéÿÿÿÿ)Ú__document_cache__rJÚutilsÚnew_documentrr²ZsphinxrÞrfrGZnote_source)rÝr²rÞrfr1r1r2râ-s
râ)NNN)NNN)NNN)NNN)N)NNN)NNN)rrÚ)N)irkrOrÏÚ
contextlibrrrÚtypesrÚtypingrrrr	r
rrr
rrrrrJrZdocutils.iorZdocutils.nodesrrrZdocutils.parsers.rstrrrZdocutils.parsers.rst.statesrZdocutils.statemachinerrrZdocutils.utilsrrÚ	packagingr Z
sphinx.errorsr!Z
sphinx.localer"r#Zsphinx.utilr$Zsphinx.util.typingr%Ú	getLoggerr@rzrÐr…Zsphinx.buildersr&Z
sphinx.configr'Zsphinx.environmentr(ÚparseÚ__version__ÚreleaseržÚsetr-r3rIršr5r7r8r:r<rArCr.rHrMrTrUrVrlrnrorrr›rŸr¥r¦r±r¼rÄÚNodeVisitorrÒrÜràrfrâr1r1r1r2Ú<module>s‚8

		&6 B";