U
    \Sh                     @  s$  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mZ ddlmZ dd	lmZ dd
lmZ erddlmZ zddlZdZW n ek
r   dZY nX dZddddddZdddddddZddddddZdddddd ZG d!d" d"eZd#d$d%d&d'Z dS )(zHImplements the low-level algorithms Sphinx uses for versioning doctrees.    )annotationsN)productzip_longest)
itemgetter)path)TYPE_CHECKINGAnyIterator)uuid4)Node)SphinxTransform)SphinxTFA   r   r   zIterator[Node])doctree	conditionreturnc                 c  s$   |  |D ]}t j|_|V  q
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   node r   D/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/versioning.pyadd_uids   s    

r   )oldnewr   r   c                 c  s  |  |}| |}g }g }i }t }t||D ]\}	}
|	dkrL||
 q0t|	ddsbt j|	_|
dkrv||	 q0t|	j	|
j	}|dkr|	j|
_|
|
 q0|||	|
f< ||	 ||
 q0t||D ]V\}	}
|
|ks|	|
f|krqt|	j	|
j	}|dkr|	j|
_|
|
 q|||	|
f< qt| tdd}|D ]L\\}	}
}|
|krXq<n
|
|
 |tk rv|	j|
_nt j|
_|
V  q<t|| D ]}
t j|
_|
V  qd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_ratioZ	rawsourceaddr   sorteditemsr   VERSIONING_RATIO)r   r   r   Zold_iterZnew_iterZ	old_nodesZ	new_nodesratiosseenold_nodenew_noderatior   r   r   merge_doctrees+   sT    














r*   strfloat)r   r   r   c                 C  sF   t | |gstS tr,t| |t| d  S t| |t| d  S d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@N)allr$   
IS_SPEEDUPLevenshteinZdistancelenlevenshtein_distance)r   r   r   r   r   r    n   s
    r    int)abr   c                 C  s   | |krdS t | t |k r&||  } }| s2t |S ttt |d }t| D ]f\}}|d g}t|D ]F\}}||d  d }|| d }	|| ||k }
|t||	|
 qh|}qN|d S )zEReturn the Levenshtein edit distance between two strings *a* and *b*.r   r   )r0   listrange	enumerater   min)r3   r4   Zprevious_rowiZcolumn1Zcurrent_rowjZcolumn2
insertionsZ	deletionsZsubstitutionsr   r   r   r1   {   s     

r1   c                   @  s$   e Zd ZdZdZdddddZdS )	UIDTransformz#Add UIDs to doctree for versioning.ip  r   None)kwargsr   c              	   K  s   | j }d }|jsd S |jrhz8t|j|jd }t|d}t	|}W 5 Q R X W n t
k
rf   Y nX |jrv|d krtt| j|j ntt|| j|j d S )Nz.doctreerb)envZversioning_conditionZversioning_comparer   joinZ
doctreedirZdocnameopenpickleloadOSErrorr6   r   Zdocumentr*   )selfr?   rA   Zold_doctreefilenamefr   r   r   apply   s    zUIDTransform.applyN)__name__
__module____qualname____doc__Zdefault_priorityrJ   r   r   r   r   r=      s   r=   r   zdict[str, Any])appr   c                 C  s   |  t ddddS )NbuiltinT)versionZparallel_read_safeZparallel_write_safe)Zadd_transformr=   )rO   r   r   r   setup   s
    
rR   )!rN   
__future__r   rD   	itertoolsr   r   operatorr   osr   typingr   r   r	   uuidr
   Zdocutils.nodesr   Zsphinx.transformsr   Zsphinx.applicationr   r/   r.   ImportErrorr$   r   r*   r    r1   r=   rR   r   r   r   r   <module>   s.   
C