
    Rik                       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dZddZ	 	 	 	 	 	 	 	 ddZddZddZ  G d de      Z!ddZ"y# e$ r dZY 3w xY w)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   c              #  r   K   | j                  |      D ]  }t               j                  |_        | ! yw)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)doctree	conditionnodes      I/home/agent/.friday_env/lib/python3.12/site-packages/sphinx/versioning.pyadd_uidsr   !   s2      	* 7;;
s   57c              #  L  K   | j                  |      }|j                  |      }g }g }i }t               }t        ||      D ]  \  }	}
|	|j                  |
       t	        |	dd      st               j                  |	_        |
|j                  |	       Tt        |	j                  |
j                        }|dk(  r#|	j                  |
_        |j                  |
       |||	|
f<   |j                  |	       |j                  |
        t        ||      D ]_  \  }	}
|
|v s|	|
f|v rt        |	j                  |
j                        }|dk(  r#|	j                  |
_        |j                  |
       Y|||	|
f<   a t        |j                         t        d            D ]V  \  \  }	}
}|
|v r|j                  |
       |t        k  r|	j                  |
_        :t               j                  |
_        |
 X t        |      |z
  D ]  }
t               j                  |
_        |
 ! yw)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)oldnewr   old_iternew_iter	old_nodes	new_nodesratiosseenold_nodenew_noderatios               r   merge_doctreesr1   0   s     {{9%H{{9%HIIF5D)(H= '(X&x- 7;;HLX&(,,h.@.@AA:#<<HLHHX).F8X%&X&X&#'( &i; /(t(3v=(,,h.@.@AA:#<<HLHHX).F8X%&/ (.flln*Q-'P 	#8etHHX###<<HL 7;;HLN	 	NT) w{{s   H"H$c                    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   sT     Sz?##C-SE1ABB#C-SE1ABB    c           	     x   | |k(  ryt        |       t        |      k  r|| }} | st        |      S t        t        t        |      dz               }t        |       D ]Z  \  }}|dz   g}t        |      D ]?  \  }}||dz      dz   }||   dz   }	||   ||k7  z   }
|j	                  t        ||	|
             A |}\ |d   S )zEReturn the Levenshtein edit distance between two strings *a* and *b*.r   r   )r7   listrange	enumerater   min)abprevious_rowicolumn1current_rowjcolumn2
insertions	deletionssubstitutionss              r   r8   r8      s    Av
1vA!11vc!fqj)*Ll #
71ug#A, 	JJAw%a!e,q0J#A*I(Ow'/ABMs:y-HI		J
 ## r9   c                      e Zd ZdZdZddZy)UIDTransformz#Add UIDs to doctree for versioning.ip  c                   | j                   }d }|j                  }|sy |j                  rQ|j                  |j                  j
                   dz  }	 t        |d      5 }t        j                  |      }d d d        |j                  r| t        t        | j                  |             y t        t        || j                  |             y # 1 sw Y   XxY w# t        $ r Y fw xY w)Nz.doctreerb)envversioning_conditionversioning_compare
doctreedircurrent_documentdocnameopenpickleloadOSErrorr<   r   documentr1   )selfkwargsrO   old_doctreerP   filenamefs          r   applyzUIDTransform.apply   s    hh"77#!!~~3+?+?+G+G*H(QQH(D) 1Q"(++a.K1 %%)<$--)=>?T]]<PQR1 1 s*   C C
3C 
CC 	C"!C"N)r[   r   returnNone)__name__
__module____qualname____doc__default_priorityr_    r9   r   rL   rL      s    -Sr9   rL   c                8    | j                  t               ddddS )NbuiltinT)versionparallel_read_safeparallel_write_safe)add_transformrL   )apps    r   setupro      s%    l# "# r9   )r   r   r   Callable[[Node], bool]r`   Iterator[Node])r&   r   r'   r   r   rp   r`   rq   )r&   strr'   rr   r`   float)r@   rr   rA   rr   r`   int)rn   r   r`   r   )#re   
__future__r   rV   	itertoolsr   r   operatorr   typingr   uuidr   sphinx.transformsr	   collections.abcr
   r   r   docutils.nodesr   sphinx.applicationr   sphinx.util.typingr   r5   r4   ImportErrorr%   r   r1   r    r8   rL   ro   rg   r9   r   <module>r      s    N "  *     -2#)4J
  A	AA%;AAH
C(S? S8o  Js   B   B
	B
