o $a@ @s>dZddlZddlmZmZddlmZddlmZddl m Z m Z m Z m Z ddlmZddlmZdd lmZe r@dd lmZzddlZd ZWn eySd ZYnwd Zdede de efddZdedede de efddZdededefddZdededefddZ GdddeZ!dd de ee ffd!d"Z"dS)#z sphinx.versioning ~~~~~~~~~~~~~~~~~ Implements the low-level algorithms Sphinx uses for the versioning of doctrees. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N)product zip_longest) itemgetter)path) TYPE_CHECKINGAnyDictIterator)uuid4)Node)SphinxTransform)SphinxTFAdoctree conditionreturnccs&||D] }tj|_|VqdS)aAdd a unique id to every node in the `doctree` which matches the condition and yield the nodes. :param doctree: A :class:`docutils.nodes.document` instance. :param condition: A callable which returns either ``True`` or ``False`` for a given node. N)traverser hexuid)rrnoder3/usr/lib/python3/dist-packages/sphinx/versioning.pyadd_uids#s  roldnewc cs||}||}g}g}i}t}t||D]H\} } | dur'|| qt| dds2tj| _| dur<|| qt| j | j } | dkrQ| j| _| | q| || | f<|| || qt ||D]*\} } | |vsu| | f|vrvqgt| j | j } | dkr| j| _| | qg| || | f<qgt | tdd}|D]!\\} } } | |vrq| | | tkr| j| _qtj| _| Vqt||D] } tj| _| VqdS)a1Merge the `old` doctree with the `new` one while looking at nodes matching the `condition`. Each node which replaces another one or has been added to the `new` doctree will be yielded. :param condition: A callable which returns either ``True`` or ``False`` for a given node. Nrr)key)rsetrappendgetattrr rr get_ratio rawsourceaddrsorteditemsrVERSIONING_RATIO) rrrold_iternew_iter old_nodes new_nodesratiosseenold_nodenew_noderatiorrrmerge_doctrees2sX              r/cCsBt||gstStrt||t|dSt||t|dS)zReturn a "similarity ratio" (in percent) representing the similarity between the two strings where 0 is equal and anything above less than equal. gY@)allr% IS_SPEEDUP Levenshteindistancelenlevenshtein_distance)rrrrrr us r abc Cs||krdSt|t|kr||}}|st|Sttt|d}t|D]3\}}|dg}t|D]#\}}||dd}||d} ||||k} |t|| | q4|}q'|dS)zEReturn the Levenshtein edit distance between two strings *a* and *b*.rr)r4listrange enumeratermin) r6r7 previous_rowicolumn1 current_rowjcolumn2 insertions deletions substitutionsrrrr5s    r5c@s&eZdZdZdZdeddfddZdS) UIDTransformz#Add UIDs to doctree for versioning.ipkwargsrNcKs|j}d}|js dS|jr=z&t|j|jd}t|d }t |}Wdn1s-wYWn t y<Ynw|jrD|durOt t |j |jdSt t||j |jdS)Nz.doctreerb)envversioning_conditionversioning_comparerjoin doctreedirdocnameopenpickleloadOSErrorr9rdocumentr/)selfrGrI old_doctreefilenamefrrrapplys"   zUIDTransform.apply)__name__ __module__ __qualname____doc__default_priorityrrXrrrrrFsrFappr cCs|tddddS)NbuiltinT)versionparallel_read_safeparallel_write_safe) add_transformrF)r^rrrsetups rd)#r\rP itertoolsrroperatorrosrtypingrrrr uuidr docutils.nodesr sphinx.transformsr sphinx.applicationr r2r1 ImportErrorr%rr/strfloatr intr5rFrdrrrrs0        C