
    Qjf6                    
   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mZ ddlmZ dd	lmZ er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 +w xY w)zHImplements the low-level algorithms Sphinx uses for versioning doctrees.    )annotationsN)productzip_longest)
itemgetter)path)TYPE_CHECKINGAny)uuid4)SphinxTransform)Iterator)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      1lib/python3.12/site-packages/sphinx/versioning.pyadd_uidsr   !   s2      	* 7;;
s   57c              #  P  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
16F'- 	#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    s   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                  rX	 t        j                  |j
                  |j                  dz         }t        |d      5 }t        j                  |      }d d d        |j                  r|*t        t        | j                  |j                               y t        t        || j                  |j                               y # 1 sw Y   lxY w# t        $ r Y zw xY w)Nz.doctreerb)envversioning_conditionversioning_comparer   join
doctreedirdocnameopenpickleloadOSErrorr<   r   documentr1   )selfkwargsrO   old_doctreefilenamefs         r   applyzUIDTransform.apply   s    hh''!!99S^^S[[:5MN(D) 1Q"(++a.K1 %%)<$--)A)ABCT]]C<T<TUV1 1 s)   9C/ "C#8C/ #C,(C/ /	C;:C;N)r[   r	   returnNone)__name__
__module____qualname____doc__default_priorityr_    r9   r   rL   rL      s    -W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   r	   r`   Iterator[Node])r&   r   r'   r   r   r	   r`   rp   )r&   strr'   rq   r`   float)r@   rq   rA   rq   r`   int)rn   r   r`   r   )$re   
__future__r   rV   	itertoolsr   r   operatorr   osr   typingr   r	   uuidr
   sphinx.transformsr   collections.abcr   docutils.nodesr   sphinx.applicationr   sphinx.util.typingr   r5   r4   ImportErrorr%   r   r1   r    r8   rL   ro   rg   r9   r   <module>r      s    N "  *   %  -(#)4J
  @F
C(W? W6k  Js   A8 8BB