3

T'ícGã@stddlZddlZddlmZddlmZmZmZmZm	Z	ddl
mZddlm
Z
mZddlmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZmZdd
lmZddl m!Z!erÔddl"m#Z#ej$e%ƒZ&Gdd„deƒZ'd"ee(e)e	e(e)fee(dœdd„Z*eee(ej+dœdd„Z,Gdd„deƒZ-Gdd„dƒZ.Gdd„deƒZ/dee(efdœd d!„Z0dS)#éN)Úunified_diff)Ú
TYPE_CHECKINGÚAnyÚDictÚListÚTuple)Únodes)ÚElementÚNode)Ú
directives)Ú
StringList)Úaddnodes)ÚConfig)Úoptional_int)Ú__)ÚloggingÚparselinenos)ÚSphinxDirective)Ú
OptionSpec)ÚSphinxc@sFeZdZUdZdZdZdZdZej	ej
dœZee
edœdd„Zd	S)
Ú	Highlightzt
    Directive to set the highlighting language for code blocks, as well
    as the threshold for line numbers.
    Fér)ÚforceÚlinenothreshold)ÚreturncCsF|jdjƒ}|jjdtjƒ}d|jk}||jjd<tj	|||dgS)NrrrÚhighlight_language)Úlangrr)
Ú	argumentsÚstripÚoptionsÚgetÚsysÚmaxsizeÚenvÚ	temp_datar
Z
highlightlang)ÚselfÚlanguagerr©r'ú8/tmp/pip-build-gk9425m9/sphinx/sphinx/directives/code.pyÚrun(s
z
Highlight.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úhas_contentÚrequired_argumentsÚoptional_argumentsÚfinal_argument_whitespacerÚflagZpositive_intÚoption_specrrr
r)r'r'r'r(rs
r)ÚlinesÚdedentÚlocationrcs†ˆdkrtjdj|ƒƒjdƒSt‡fdd„|DƒƒrFtjtdƒ|dg}x6|D].}|ˆd…}|jdƒrt|rtd}|j	|ƒqPW|S)NÚTc3s|]}|dˆ…jƒVqdS)N)r)Ú.0Ús)r5r'r(ú	<genexpr>7szdedent_lines.<locals>.<genexpr>z!non-whitespace stripped by dedent)r6Ú
)
Útextwrapr5ÚjoinÚ
splitlinesÚanyÚloggerÚwarningrÚendswithÚappend)r4r5r6Ú	new_linesÚlineÚnew_liner')r5r(Údedent_lines3s
rG)Ú	directiveÚliteral_nodeÚcaptionrcCs¾tjdddgd}tjƒ}|jjt|gdd|j|ƒt|dtjƒrdt	d|dj
ƒƒ}t|ƒ‚nVt|dtjƒr¶tj|dj
df|djžŽ}|j|_|j|_||7}||7}|St‚dS)Nr7Tzliteral-block-wrapper)Ú
literal_blockÚclasses)ÚsourcerzInvalid caption: %s)rÚ	containerr	ÚstateZnested_parserZcontent_offsetÚ
isinstanceZsystem_messagerZastextÚ
ValueErrorrJZ	rawsourceÚchildrenrMrEÚRuntimeError)rHrIrJZcontainer_nodeÚparsedÚmsgZcaption_noder'r'r(Úcontainer_wrapperDs"


rVc	@sZeZdZUdZdZdZdZdZej	ej	e
eejejej
ejdœZeeedœdd	„Zd
S)Ú	CodeBlockz^
    Directive for a code block with special highlighting or line numbering
    settings.
    TrrF)rÚlinenosr5zlineno-startzemphasize-linesrJÚclassÚname)rcs8|jj}dj|jƒ}|jj|jƒ}|jjdƒ}|rÆy`t	|jƒ‰t
|ˆƒ}t‡fdd„|Dƒƒr~tj
tdƒˆ|jdf|d‡fdd„|Dƒ}WqÊtk
rÂ}z|jj
||jd	gSd}~XqÊXnd}d
|jkr|jj|jƒ}|jdƒ}t||jd
|d}dj|ƒ}tj||ƒ}d
|jks0d|jkr8d|d
<|d|jjdgƒ7<d|jk|d<|jrx|jd|d<n|jjjd|jjƒ|d<i}	|d<|dk	r®||	d<d|jkrÈ|jd|	d<|j|ƒ|jjdƒ}
|
r(yt|||
ƒ}Wn2tk
r&}z|jj
||jd	gSd}~XnX|j|ƒ|gS)Nr;zemphasize-linesc3s|]}|ˆkVqdS)Nr')r8Úi)Únlinesr'r(r:xsz CodeBlock.run.<locals>.<genexpr>z*line number spec is out of range(1-%d): %r)r6csg|]}|ˆkr|d‘qS)rr')r8Úx)r\r'r(ú
<listcomp>}sz!CodeBlock.run.<locals>.<listcomp>)rEr5Tr7rXzlineno-startrLrYrrr&rÚhighlight_argsÚhl_linesÚlinenostartrJ)rOÚdocumentr=ÚcontentÚ
state_machineÚget_source_and_lineÚlinenorr Úlenrr?r@rArrQÚreporterr>rGrrKrr#r$ÚconfigrÚset_source_inforVÚadd_name)r%rbÚcoder6Úlinespecr`Úerrr4ÚliteralÚ
extra_argsrJÚexcr')r\r(r)nsX


"




 
z
CodeBlock.runN)r*r+r,r-r.r/r0r1rr2rÚintÚunchanged_requiredÚclass_optionÚ	unchangedr3rrr
r)r'r'r'r(rWYs
rWc@s¨eZdZd*d+d,d-d.d/d0d1d2d3d4d5d6g
Zeeedd
œdd„Zddœdd„Zd7ee	ee
feedœdd„Zd8e	ee
fe	ee
fdœdd„Z
d9e	ee
feedœdd„Zd:eee	ee
feedœdd„Zd;eee	ee
feedœdd„Zd<eee	ee
feedœd d!„Zd=eee	ee
feedœd"d#„Zd>eee	ee
feedœd$d%„Zd?eee	ee
feedœd&d'„Zd@eee	ee
feedœd(d)„ZdS)AÚLiteralIncludeReaderúlineno-matchúlineno-startrCÚprependústart-afterústart-atú
end-beforeúend-atÚdiffÚpyobjectr4N)ÚfilenamerrircCs8||_||_|jd|jƒ|_|jjddƒ|_|jƒdS)NÚencodingzlineno-startr)r€rr Zsource_encodingrÚlineno_startÚ
parse_options)r%r€rrir'r'r(Ú__init__½s
zLiteralIncludeReader.__init__)rcCs@x:|jD]0\}}||jkr||jkrttdƒ||fƒ‚qWdS)Nz%Cannot use both "%s" and "%s" options)ÚINVALID_OPTIONS_PAIRrrQr)r%Zoption1Zoption2r'r'r(rƒÅsz"LiteralIncludeReader.parse_options)r€r6rcCsºyHt||jdd.}|jƒ}d|jkr6|j|jdƒ}|jdƒSQRXWnltk
r|}zttdƒ|ƒ|‚WYdd}~Xn:tk
r´}zttdƒ|j|fƒ|‚WYdd}~XnXdS)NÚstrict)rÚerrorsz	tab-widthTz.Include file %r not found or reading it failedz`Encoding %r used for reading included file %r seems to be wrong, try giving an :encoding: option)	ÚopenrÚreadrÚ
expandtabsr>ÚOSErrorrÚUnicodeError)r%r€r6ÚfÚtextrqr'r'r(Ú	read_fileËs
zLiteralIncludeReader.read_file)r6rcCspd|jkr|jƒ}nJ|j|j|j|j|j|j|jg}|j	|j
|d}x|D]}|||d}qJWdj|ƒt|ƒfS)Nr~)r6r7)
rÚ	show_diffÚpyobject_filterÚstart_filterÚ
end_filterÚlines_filterÚprepend_filterÚ
append_filterÚ
dedent_filterrr€r=rg)r%r6r4ÚfiltersÚfuncr'r'r(r‰Ûs


zLiteralIncludeReader.readcCs:|j|jƒ}|jjdƒ}|j|ƒ}t||||jƒ}t|ƒS)Nr~)rr€rr rÚlist)r%r6rDZold_filenameZ	old_linesr~r'r'r(rìs

zLiteralIncludeReader.show_diff)r4r6rc	CsŽ|jjdƒ}|rŠddlm}|j|jdƒ}|jƒ}||krRttdƒ||jfƒ‚n8||d}||d}||d|…}d|jkrŠ||_	|S)	Nrr)ÚModuleAnalyzerr7z,Object named %r not found in include file %rrézlineno-match)
rr Z
sphinx.pycoder›Zfor_filer€Z	find_tagsrQrr‚)	r%r4r6rr›ZanalyzerÚtagsÚstartÚendr'r'r(r‘ós
z$LiteralIncludeReader.pyobject_filtercsÒ|jjdƒ}|rÎt|tˆƒƒ}t‡fdd„|DƒƒrRtjtdƒtˆƒ|f|dd|jkrž|d‰t‡fdd„t	|ƒDƒƒr’|j
|d7_
nttd	ƒƒ‚‡fd
d„|Dƒ‰ˆgkrÎttdƒ||jfƒ‚ˆS)
Nr4c3s|]}|tˆƒkVqdS)N)rg)r8r[)r4r'r(r:	sz4LiteralIncludeReader.lines_filter.<locals>.<genexpr>z*line number spec is out of range(1-%d): %r)r6zlineno-matchrc3s|]\}}ˆ||kVqdS)Nr')r8r[Ún)Úfirstr'r(r:sz8Cannot use "lineno-match" with a disjoint set of "lines"cs g|]}|tˆƒkrˆ|‘qSr')rg)r8r )r4r'r(r^sz5LiteralIncludeReader.lines_filter.<locals>.<listcomp>z2Line spec %r: no lines pulled from include file %r)
rr rrgr?r@rArÚallÚ	enumerater‚rQr€)r%r4r6rmZlinelistr')r¡r4r(r”s 

z!LiteralIncludeReader.lines_filtercCsØd|jkr|jjdƒ}d}n d|jkr8|jjdƒ}d}nd}|rÔxpt|ƒD]d\}}||krJ|rŠd|jkrz|j|d7_||dd…Sd|jkr¢|j|7_||d…SqJW|dkrÈtd|ƒ‚ntd|ƒ‚|S)	Nzstart-atFzstart-afterTzlineno-matchrz!start-after pattern not found: %szstart-at pattern not found: %s)rr r£r‚rQ)r%r4r6ržÚ	inclusiverfrEr'r'r(r’s*



z!LiteralIncludeReader.start_filtercCs®d|jkr|jjdƒ}d}n d|jkr8|jjdƒ}d}nd}|rªxFt|ƒD]:\}}||krJ|rn|d|d…S|dkrxqJ|d|…SqJW|dkržtd|ƒ‚ntd|ƒ‚|S)	Nzend-atTz
end-beforeFrrzend-at pattern not found: %sz end-before pattern not found: %s)rr r£rQ)r%r4r6rŸr¤rfrEr'r'r(r“<s&

zLiteralIncludeReader.end_filtercCs$|jjdƒ}|r |jd|dƒ|S)Nryrr;)rr Úinsert)r%r4r6ryr'r'r(r•Wsz#LiteralIncludeReader.prepend_filtercCs"|jjdƒ}|r|j|dƒ|S)NrCr;)rr rC)r%r4r6rCr'r'r(r–^sz"LiteralIncludeReader.append_filtercCs(d|jkr t||jjdƒ|dS|SdS)Nr5)r6)rrGr )r%r4r6r'r'r(r—es
z"LiteralIncludeReader.dedent_filter)rwrx)rwrC)rwry)rzr{)r|r})r~r)r~rx)r~rw)r~r4)r~rz)r~r|)r~r{)r~r})N)N)N)N)N)N)N)N)N)N)r*r+r,r…Ústrrrr„rƒrrrrrr‰rr‘r”r’r“r•r–r—r'r'r'r(rv¬s2 "$$$$$$rvc@sŒeZdZUdZdZdZdZdZee	j
ee	j
ee	je	j
e	j
e	je	je	je	je	je	je	je	je	je	je	je	je	jdœZeeedœdd	„Zd
S)ÚLiteralIncludez·
    Like ``.. include:: :literal:``, but only warns if the include file is
    not found, and does not raise errors.  Also has several options for
    selecting what to include.
    FrrT)r5rXzlineno-startzlineno-matchz	tab-widthr&rrrr4zstart-afterz
end-beforezstart-atzend-atryrCzemphasize-linesrJrYrZr~)rcs2|jj}|jjs$|jjd|jdgSd|jkrN|jj	|jdƒ\}}||jd<yª|j
j|jƒ}|jj	|jdƒ\}}|jj
|ƒt||j|jƒ}|j|d\}‰tj|||d}	d|jk|	d<|j|	ƒ|jjdƒràd|	d	<nd	|jkrø|jd	|	d	<d
|jksd|jksd|jkr$d
|	d
<|	d|jjdgƒ7<i}
|	d<d|jkr´t|jdˆƒ}t‡fdd„|Dƒƒržtjtdƒˆ|jdf|d‡fdd„|Dƒ|
d<|j|
d<d|jkrì|jdpÞ|jd}t||	|ƒ}	|j|	ƒ|	gStk
r,}
z|jj|
|jdgSd}
~
XnXdS)NzFile insertion disabled)rEr~r)r6)rMrZudiffr&rXzlineno-startzlineno-matchTrLrYr_zemphasize-linesc3s|]}|ˆkVqdS)Nr')r8r[)r4r'r(r:¯sz%LiteralInclude.run.<locals>.<genexpr>z*line number spec is out of range(1-%d): %rcsg|]}|ˆkr|d‘qS)rr')r8r])r4r'r(r^³sz&LiteralInclude.run.<locals>.<listcomp>r`rarJ)rOrbÚsettingsZfile_insertion_enabledrhrArfrr#Z
relfn2pathrdrerZnote_dependencyrvrir‰rrKrjr rr?r@rr‚rVrkÚ	Exception)r%rbÚ_Úpathr6Zrel_filenamer€ÚreaderrŽZretnoderpr`rJrqr')r4r(r)sP







zLiteralInclude.runN)r*r+r,r-r.r/r0r1rrr2rrrsrrurtr3rrr
r)r'r'r'r(r§ls6
r§r)ÚapprcCs<tjdtƒtjdtƒtjdtƒtjdtƒddddœS)NZ	highlightz
code-blockZ
sourcecodeZliteralincludeÚbuiltinT)ÚversionZparallel_read_safeZparallel_write_safe)rZregister_directiverrWr§)r­r'r'r(ÚsetupÃsr°)N)1r!r<ÚdifflibrÚtypingrrrrrZdocutilsrZdocutils.nodesr	r
Zdocutils.parsers.rstrZdocutils.statemachinerZsphinxr
Z
sphinx.configrZsphinx.directivesrZ
sphinx.localerZsphinx.utilrrZsphinx.util.docutilsrZsphinx.util.typingrZsphinx.applicationrÚ	getLoggerr*r@rr¦rrrGrNrVrWrvr§r°r'r'r'r(Ú<module>s2
&SAW