diff options
Diffstat (limited to 'sphinx/versioning.py')
-rw-r--r-- | sphinx/versioning.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/sphinx/versioning.py b/sphinx/versioning.py index 0631f09ed..c6691d52d 100644 --- a/sphinx/versioning.py +++ b/sphinx/versioning.py @@ -11,9 +11,16 @@ """ from uuid import uuid4 from operator import itemgetter +from itertools import product -from sphinx.util.pycompat import product, zip_longest, all +from six import iteritems +from six.moves import range, zip_longest +try: + import Levenshtein + IS_SPEEDUP = True +except ImportError: + IS_SPEEDUP = False # anything below that ratio is considered equal/changed VERSIONING_RATIO = 65 @@ -55,6 +62,9 @@ def merge_doctrees(old, new, condition): if old_node is None: new_nodes.append(new_node) continue + if not getattr(old_node, 'uid', None): + # maybe config.gettext_uuid has been changed. + old_node.uid = uuid4().hex if new_node is None: old_nodes.append(old_node) continue @@ -80,7 +90,7 @@ def merge_doctrees(old, new, condition): # choose the old node with the best ratio for each new node and set the uid # as long as the ratio is under a certain value, in which case we consider # them not changed but different - ratios = sorted(ratios.iteritems(), key=itemgetter(1)) + ratios = sorted(iteritems(ratios), key=itemgetter(1)) for (old_node, new_node), ratio in ratios: if new_node in seen: continue @@ -104,7 +114,11 @@ def get_ratio(old, new): """ if not all([old, new]): return VERSIONING_RATIO - return levenshtein_distance(old, new) / (len(old) / 100.0) + + if IS_SPEEDUP: + return Levenshtein.distance(old, new) / (len(old) / 100.0) + else: + return levenshtein_distance(old, new) / (len(old) / 100.0) def levenshtein_distance(a, b): @@ -115,7 +129,7 @@ def levenshtein_distance(a, b): a, b = b, a if not a: return len(b) - previous_row = xrange(len(b) + 1) + previous_row = range(len(b) + 1) for i, column1 in enumerate(a): current_row = [i + 1] for j, column2 in enumerate(b): |