| 8 | |
| 9 | |
| 10 | def diff(old, new, limit=False): |
| 11 | matcher = difflib.SequenceMatcher(None, old, new) |
| 12 | actions = [] |
| 13 | size = 0 |
| 14 | for tag, old_from, old_to, new_from, new_to in matcher.get_opcodes(): |
| 15 | if tag == "insert": |
| 16 | new_line = new[new_from:new_to] |
| 17 | actions.append(("+", new_line)) |
| 18 | size += sum(map(len, new_line)) |
| 19 | elif tag == "equal": |
| 20 | actions.append(("=", sumLen(old[old_from:old_to]))) |
| 21 | elif tag == "delete": |
| 22 | actions.append(("-", sumLen(old[old_from:old_to]))) |
| 23 | elif tag == "replace": |
| 24 | actions.append(("-", sumLen(old[old_from:old_to]))) |
| 25 | new_lines = new[new_from:new_to] |
| 26 | actions.append(("+", new_lines)) |
| 27 | size += sumLen(new_lines) |
| 28 | if limit and size > limit: |
| 29 | return False |
| 30 | return actions |
| 31 | |
| 32 | |
| 33 | def patch(old_f, actions): |