classification
Title: Create render_*_diff variants to the *_diff functions in difflib
Type: Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: pablogsal, rhettinger, ruoso, tim.peters
Priority: normal Keywords:

Created on 2020-03-20 14:19 by ruoso, last changed 2020-03-20 15:50 by rhettinger.

Messages (1)
msg364669 - (view) Author: Daniel (ruoso) Date: 2020-03-20 14:19
Currently difflib offers no way to synthesize a diff output without having to assemble the original and modified strings and then asking difflib to calculate the diff.

It would be nice if I could just call a `render_unified_diff(a, b, grouped_opcodes)` and get a diff output. This is useful when I'm synthesizing a patch dynamically and I don't necessarily want to load the entire original file and apply the changes.

One example usage would be something like:

```
    def make_patch(self):
        # simplified input for synthesizing the diff
        a = []
        b = []
        include_lines = []
        for header, _ in self.missing.items():
            include_lines.append(f"#include <{header}>\n")
        while len(b) < self.line:
            b.append(None)
        b.extend(include_lines)
        opcodes = [
            [('insert',
              self.line, self.line,
              self.line, self.line + len(include_lines))]
        ]
        diff = render_unified_diff(
            a, b, opcodes,
            fromfile=os.path.join('a', self.filename),
            tofile=os.path.join('b', self.filename),
        )
        return ''.join(diff)
```
History
Date User Action Args
2020-03-20 15:50:52rhettingersetnosy: + rhettinger
2020-03-20 15:24:52pablogsalsetnosy: + tim.peters
2020-03-20 14:19:02ruosocreate