def PatchChunks(old_lines, chunks):
  if not chunks:
    # The patch is a no-op
    yield ("equal", old_lines, old_lines)
    return

  old_pos = 0
  for (old_i, old_j), _, old_chunk, new_chunk in chunks:
    eq = old_lines[old_pos:old_i]
    if eq:
      yield "equal", eq, eq
    old_pos = old_i
    # Check that the patch matches the target file
    if old_lines[old_i:old_j] != old_chunk:
      logging.warn("mismatch:%s.%s.", old_lines[old_i:old_j], old_chunk)
      yield ("error: old chunk mismatch", old_lines[old_i:old_j], old_chunk)
      return
    # TODO(guido): ParsePatch knows the diff details, but throws the info away
    sm = patiencediff.PseudoPatienceSequenceMatcher(None, old_chunk, new_chunk)
    for tag, i1, i2, j1, j2 in sm.get_opcodes():
      yield tag, old_chunk[i1:i2], new_chunk[j1:j2]
    old_pos = old_j

  # Copy the final matching chunk if any.
  eq = old_lines[old_pos:]
  if eq:
    yield ("equal", eq, eq)
Patche old_lines with chunks.
dataset/ETHPy150Open rietveld-codereview/rietveld/codereview/patching.py/PatchChunks