1
from difflib import SequenceMatcher
1
3
class MultiParent(object):
5
def __init__(self, hunks=None):
12
return "MultiParent(%r)" % self.hunks
14
def __eq__(self, other):
15
if self.__class__ is not other.__class__:
17
return (self.hunks == other.hunks)
7
20
def from_lines(text, parents=()):
9
diff.hunks.append(NewText(text))
22
return SequenceMatcher(None, parent, text).get_matching_blocks()
23
parent_comparisons = [compare(p) for p in parents]
25
new_text = NewText([])
27
block_iter = [iter(i) for i in parent_comparisons]
28
diff = MultiParent([])
31
return block_iter[p].next()
34
cur_block = [next_block(p) for p, i in enumerate(block_iter)]
35
while cur_line < len(text):
36
#import pdb; pdb.set_trace()
38
for p, block in enumerate(cur_block):
42
while j + n < cur_line:
43
block = cur_block[p] = next_block(p)
57
if best_match is None or n > best_match.num_lines:
58
best_match = ParentText(p, i, j, n)
59
if best_match is None:
60
new_text.lines.append(text[cur_line])
63
if len(new_text.lines) > 0:
64
diff.hunks.append(new_text)
65
new_text = NewText([])
66
diff.hunks.append(best_match)
67
cur_line += best_match.num_lines
68
if len(new_text.lines) > 0:
69
diff.hunks.append(new_text)
24
84
if self.__class__ is not other.__class__:
26
86
return (other.lines == self.lines)
89
return 'NewText(%r)' % self.lines
92
class ParentText(object):
94
def __init__(self, parent, parent_pos, child_pos, num_lines):
96
self.parent_pos = parent_pos
97
self.child_pos = child_pos
98
self.num_lines = num_lines
101
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
102
' %(num_lines)r)' % self.__dict__
104
def __eq__(self, other):
105
if self.__class__ != other.__class__:
107
return (self.__dict__ == other.__dict__)