
    -PhZ                       d Z ddlmZ ddl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 e
r 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ZdZn# e$ r dZY nw xY wdZd(dZd)dZd*dZd+d!Z  G d" d#e          Z!d,d'Z"dS )-zHImplements the low-level algorithms Sphinx uses for versioning doctrees.    )annotationsN)productzip_longest)
itemgetter)TYPE_CHECKING)uuid4)SphinxTransform)CallableIterator)Any)Node)Sphinx)ExtensionMetadataTFA   doctreer   	conditionCallable[[Node], bool]returnIterator[Node]c              #  r   K   |                      |          D ]}t                      j        |_        |V  dS )a  Add 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)findallr   hexuid)r   r   nodes      Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx/versioning.pyadd_uidsr   !   sE       	**  77;



     oldnewc              #    K   |                      |          }|                     |          }g }g }i }t                      }t          ||          D ]\  }	}
|	|                    |
           t	          |	dd          st                      j        |	_        |
|                    |	           ^t          |	j	        |
j	                  }|dk    r"|	j        |
_        |
                    |
           |||	|
f<   |                    |	           |                    |
           t          ||          D ]Y\  }	}
|
|v s|	|
f|v rt          |	j	        |
j	                  }|dk    r"|	j        |
_        |
                    |
           R|||	|
f<   Zt          |                                t          d                    D ]V\  \  }	}
}|
|v r|
                    |
           |t          k     r|	j        |
_        :t                      j        |
_        |
V  Wt          |          |z
  D ]}
t                      j        |
_        |
V  dS )a1  Merge 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.
    Nr   r      )key)r   setr   appendgetattrr   r   r   	get_ratio	rawsourceaddr   sorteditemsr   VERSIONING_RATIO)r   r   r   old_iternew_iter	old_nodes	new_nodesratiosseenold_nodenew_noderatios               r   merge_doctreesr5   0   s      {{9%%H{{9%%HIIF55D)(H== ' '(X&&&x-- 	' 77;HLX&&&(,h.@AAA::#<HLHHX).F8X%&X&&&X&&&& &i;; / /(t(3v==(,h.@AAA::#<HLHHX).F8X%&& (.fllnn*Q--'P'P'P 	 	#8etHHX####<HLL 77;HLNNNN 	NNT)  ww{ r   strfloatc                    t          | |g          st          S t          r(t          j        | |          t          |           dz  z  S t          | |          t          |           dz  z  S )zReturn a "similarity ratio" (in percent) representing the similarity
    between the two strings where 0 is equal and anything above less than equal.
    g      Y@)allr+   
IS_SPEEDUPLevenshteindistancelenlevenshtein_distance)r   r   s     r   r&   r&   t   sh     Sz??   C#C--SE1ABB#C--SE1ABBr   abintc           	        | |k    rdS t          |           t          |          k     r|| }} | st          |          S t          t          t          |          dz                       }t          |           D ]n\  }}|dz   g}t          |          D ]Q\  }}||dz            dz   }||         dz   }	||         ||k    z   }
|                    t          ||	|
                     R|}o|d         S )zEReturn the Levenshtein edit distance between two strings *a* and *b*.r   r!   )r=   listrange	enumerater$   min)r?   r@   previous_rowicolumn1current_rowjcolumn2
insertions	deletionssubstitutionss              r   r>   r>      s   Avvq
1vvA!1 1vvc!ffqj))**Lll # #
71ug#A,, 	J 	JJAw%a!e,q0J#A*I(Ow'/ABMs:y-HHIIII"r   c                      e Zd ZdZdZd	dZdS )
UIDTransformz#Add UIDs to doctree for versioning.ip  kwargsr   r   Nonec                   | j         }d }|j        }|sd S |j        r`|j        |j         dz  }	 t          |d          5 }t          j        |          }d d d            n# 1 swxY w Y   n# t          $ r Y nw xY w|j        r|$t          t          | j        |                     d S t          t          || j        |                     d S )Nz.doctreerb)envversioning_conditionversioning_compare
doctreedirdocnameopenpickleloadOSErrorrD   r   documentr5   )selfrS   rW   old_doctreerX   filenamefs          r   applyzUIDTransform.apply   s2   h"7# 	F! 	~3;(@(@(@@H(D)) 1Q"(+a..K1 1 1 1 1 1 1 1 1 1 1 1 1 1 1    % 	S)<$-)=>>?????T]<PQQRRRRRs4   A, A A,  A$$A, 'A$(A, ,
A98A9N)rS   r   r   rT   )__name__
__module____qualname____doc__default_priorityre    r   r   rR   rR      s:        --S S S S S Sr   rR   appr   r   c                B    |                      t                     ddddS )NbuiltinT)versionparallel_read_safeparallel_write_safe)add_transformrR   )rl   s    r   setuprs      s0    l### "#  r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r6   r   r6   r   r7   )r?   r6   r@   r6   r   rA   )rl   r   r   r   )#ri   
__future__r   r]   	itertoolsr   r   operatorr   typingr   uuidr   sphinx.transformsr	   collections.abcr
   r   r   docutils.nodesr   sphinx.applicationr   sphinx.util.typingr   r;   r:   ImportErrorr+   r   r5   r&   r>   rR   rs   rk   r   r   <module>r      s   N N " " " " " "  * * * * * * * *                         - - - - - - 522222222######))))))444444JJ   JJJ     A A A AH
C 
C 
C 
C   (S S S S S? S S S8     s   A A! A!