ó
öÀ„\c           @   sD  d  Z  d d l Z d d l 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
 m Z 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 n  y d d l Z e Z Wn e k
 rö e Z n Xd Z d „  Z d „  Z d „  Z  d „  Z! d e f d „  ƒ  YZ" d „  Z# d S(   sø   
    sphinx.versioning
    ~~~~~~~~~~~~~~~~~

    Implements the low-level algorithms Sphinx uses for the versioning of
    doctrees.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿN(   t   product(   t
   itemgetter(   t   uuid4(   t	   iteritems(   t   cPickle(   t   ranget   zip_longest(   t   RemovedInSphinx30Warning(   t   SphinxTransform(   t   Anyt   Iterator(   t   nodesiA   c         c   s2   x+ |  j  | ƒ D] } t ƒ  j | _ | Vq Wd S(   s  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(   t   traverseR   t   hext   uid(   t   doctreet	   conditiont   node(    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyt   add_uids'   s    c         c   so  |  j  | ƒ } | j  | ƒ } g  } g  } i  } t ƒ  } xæ t | | ƒ D]Õ \ }	 }
 |	 d k rt | j |
 ƒ qI n  t |	 d d ƒ s˜ t ƒ  j |	 _ n  |
 d k r· | j |	 ƒ qI n  t	 |	 j
 |
 j
 ƒ } | d k rô |	 j |
 _ | j |
 ƒ qI | | |	 |
 f <| j |	 ƒ | j |
 ƒ qI WxŽ t | | ƒ D]} \ }	 }
 |
 | k s2|	 |
 f | k rbq2n  t	 |	 j
 |
 j
 ƒ } | d k rŸ|	 j |
 _ | j |
 ƒ q2| | |	 |
 f <q2Wt t | ƒ d t d ƒ ƒ} xh | D]` \ \ }	 }
 } |
 | k rüqØn | j |
 ƒ | t k  r$|	 j |
 _ qØt ƒ  j |
 _ |
 VqØWx, t | ƒ | D] }
 t ƒ  j |
 _ |
 VqMWd S(   s1  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.
    R   i    t   keyi   N(   R   t   setR   t   Nonet   appendt   getattrR   R   R   t	   get_ratiot	   rawsourcet   addR    t   sortedR   R   t   VERSIONING_RATIO(   t   oldt   newR   t   old_itert   new_itert	   old_nodest	   new_nodest   ratiost   seent   old_nodet   new_nodet   ratio(    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyt   merge_doctrees7   sT    		c         C   sY   t  |  | g ƒ s t St r: t j |  | ƒ t |  ƒ d St |  | ƒ t |  ƒ d Sd S(   s›   Return a "similiarity ratio" (in percent) representing the similarity
    between the two strings where 0 is equal and anything above less than equal.
    g      Y@N(   t   allR   t
   IS_SPEEDUPt   Levenshteint   distancet   lent   levenshtein_distance(   R   R   (    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyR   {   s
    c         C   s   |  | k r d St  |  ƒ t  | ƒ k  r8 | |  }  } n  |  sH t  | ƒ St t  | ƒ d ƒ } x— t |  ƒ D]‰ \ } } | d g } xg t | ƒ D]Y \ } } | | d d } | | d }	 | | | | k }
 | j t | |	 |
 ƒ ƒ q‘ W| } qk W| d S(   sE   Return the Levenshtein edit distance between two strings *a* and *b*.i    i   iÿÿÿÿ(   R-   R   t	   enumerateR   t   min(   t   at   bt   previous_rowt   it   column1t   current_rowt   jt   column2t
   insertionst	   deletionst   substitutions(    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyR.   ‰   s     

t   UIDTransformc           B   s   e  Z d  Z d Z d „  Z RS(   s#   Add UIDs to doctree for versioning.ip  c         C   sÔ   |  j  } d  } | j s d  S| j r‚ yF | j | j | j d ƒ } t | d ƒ  } t j	 | ƒ } Wd  QXWq‚ t
 k
 r~ q‚ Xn  | j s˜ | d  k r´ t t |  j | j ƒ ƒ n t t | |  j | j ƒ ƒ d  S(   Ns   .doctreet   rb(   t   envR   t   versioning_conditiont   versioning_comparet   doc2patht   docnamet
   doctreedirt   opent   picklet   loadt   EnvironmentErrort   listR   t   documentR(   (   t   selfR>   t   old_doctreet   filenamet   f(    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyt   apply¢   s    			(   t   __name__t
   __module__t   __doc__t   default_priorityRN   (    (    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyR<   ž   s   c         C   s0   t  j d t d d ƒt |  ƒ } | j ƒ  d S(   s    Simple wrapper for UIDTransform.s=   versioning.prepare() is deprecated. Use UIDTransform instead.t
   stackleveli   N(   t   warningst   warnR   R<   RN   (   RI   t	   transform(    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyt   prepare¹   s    	($   RQ   RT   t	   itertoolsR    t   operatorR   t   uuidR   t   sixR   t	   six.movesR   RE   R   R   t   sphinx.deprecationR   t   sphinx.transformsR   t   Falset   typingR	   R
   t   docutilsR   R+   t   TrueR*   t   ImportErrorR   R   R(   R   R.   R<   RW   (    (    (    s0   lib/python2.7/site-packages/sphinx/versioning.pyt   <module>   s0   

		D		