summaryrefslogtreecommitdiff
path: root/creole/shared/diff_utils.py
diff options
context:
space:
mode:
authorJensDiemer <git@jensdiemer.de>2020-02-13 10:36:50 +0100
committerJensDiemer <git@jensdiemer.de>2020-02-13 10:36:50 +0100
commit51a3622519aa0a04801038a95344f60040cae05c (patch)
tree1eb9b13dd2b7fa6bef7135f01b41df7cd2413efa /creole/shared/diff_utils.py
parent9cc2afe55b8610efdea61f7263ef89ca78a2b473 (diff)
downloadcreole-update.tar.gz
NEW: creole.setup_utils.assert_rst_readmeupdate
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