
    Fe                        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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)SphinxTFA   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      3/usr/lib/python3/dist-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_doctreesr0   -   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   p   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   )r6   listrange	enumerater   min)abprevious_rowicolumn1current_rowjcolumn2
insertions	deletionssubstitutionss              r   r7   r7   }   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
 ## r8   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   documentr0   )selfkwargsrN   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)rZ   r	   returnNone)__name__
__module____qualname____doc__default_priorityr^    r8   r   rK   rK      s    -Wr8   rK   c                8    | j                  t               ddddS )NbuiltinT)versionparallel_read_safeparallel_write_safe)add_transformrK   )apps    r   setuprn      s%    l# "# r8   )r   r   r   r	   r_   Iterator[Node])r%   r   r&   r   r   r	   r_   ro   )r%   strr&   rp   r_   float)r?   rp   r@   rp   r_   int)rm   r   r_   zdict[str, Any])"rd   
__future__r   rU   	itertoolsr   r   operatorr   osr   typingr   r	   uuidr
   sphinx.transformsr   collections.abcr   docutils.nodesr   sphinx.applicationr   r4   r3   ImportErrorr$   r   r0   r   r7   rK   rn   rf   r8   r   <module>r~      s    N "  *   %  -(#)J
  @F
C(W? W4i  Js   
A2 2A<;A<