summaryrefslogtreecommitdiff
path: root/creole/shared/diff_utils.py
diff options
context:
space:
mode:
authorJens Diemer <github.com@jensdiemer.de>2020-02-13 10:41:24 +0100
committerGitHub <noreply@github.com>2020-02-13 10:41:24 +0100
commit1e2e2c4edb888b5c9a46f057880d441922878f5b (patch)
tree1eb9b13dd2b7fa6bef7135f01b41df7cd2413efa /creole/shared/diff_utils.py
parent00cbba43f0fcc8648bc1d2bb461647741f00bc4a (diff)
parent51a3622519aa0a04801038a95344f60040cae05c (diff)
downloadcreole-1e2e2c4edb888b5c9a46f057880d441922878f5b.tar.gz
Merge pull request #47 from jedie/update
Update
Diffstat (limited to 'creole/shared/diff_utils.py')
-rw-r--r--creole/shared/diff_utils.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/creole/shared/diff_utils.py b/creole/shared/diff_utils.py
new file mode 100644
index 0000000..b6fa05d
--- /dev/null
+++ b/creole/shared/diff_utils.py
@@ -0,0 +1,34 @@
+"""
+ :copyleft: 2020 by python-creole team, see AUTHORS for more details.
+ :license: GNU GPL v3 or above, see LICENSE for more details.
+"""
+
+import difflib
+
+
+def unified_diff(old, new, filename=None):
+ """
+ Return text of unified diff between old and new.
+ """
+ if filename is None:
+ fromfile = 'old'
+ tofile = 'new'
+ else:
+ fromfile = f'old / {filename}'
+ tofile = f'new / {filename}'
+
+ if isinstance(old, str) and isinstance(new, str):
+ old = old.splitlines(keepends=True)
+ new = new.splitlines(keepends=True)
+
+ diff = difflib.unified_diff(old, new, fromfile=fromfile, tofile=tofile)
+
+ text = ''
+ for line in diff:
+ text += line
+
+ # Work around missing newline (http://bugs.python.org/issue2142).
+ if text and not line.endswith('\n'):
+ text += 'n\\ No newline at end of file\n'
+
+ return text