diff2html 1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tim Pillinger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
diff2html-1.0/PKG-INFO ADDED
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: diff2html
3
+ Version: 1.0
4
+ Summary: CLI wrapper for Pythons Difflib builtin
5
+ Author-email: "tim.pillinger" <tim.pillinger@metoffice.gov.uk>
6
+ License-Expression: MIT
7
+ Keywords: diff,html,difftool,difflib,cli
8
+ Classifier: Programming Language :: Python
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE.md
11
+ Provides-Extra: test
12
+ Requires-Dist: pytest; extra == "test"
13
+ Requires-Dist: flake8; extra == "test"
14
+ Dynamic: license-file
15
+
16
+ # Diff2HTML
17
+
18
+ Python's difflib is a great standard library feature.
19
+
20
+ But I wanted a command line version.
21
+
22
+ ## Installation
23
+
24
+ ```
25
+ pip install diff2html
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```
31
+ diff2html fileA fileB --output diff.html
32
+ firefox diff.html
33
+ ```
34
+
35
+ for advanced usage see:
36
+
37
+ ```
38
+ diff2html --help
39
+ ```
@@ -0,0 +1,24 @@
1
+ # Diff2HTML
2
+
3
+ Python's difflib is a great standard library feature.
4
+
5
+ But I wanted a command line version.
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ pip install diff2html
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ diff2html fileA fileB --output diff.html
17
+ firefox diff.html
18
+ ```
19
+
20
+ for advanced usage see:
21
+
22
+ ```
23
+ diff2html --help
24
+ ```
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env python
2
+ """Compare two files and produce a well formatted diff in an HTML file.
3
+
4
+ A command line wrapper for Python's difftool.
5
+ """
6
+
7
+ import argparse
8
+ from difflib import HtmlDiff
9
+ from pathlib import Path
10
+ import re
11
+ from typing import Optional
12
+
13
+
14
+ class DiffDoc:
15
+ """A container to keep HTML bric-a-brac tidy. Provides a single
16
+ class methods convert a diff to an HTML page.
17
+ """
18
+ # Create an additional explanation of the webpage:
19
+ BACKBUTTON = '<br><button onclick="history.back()">Go Back</button><br>'
20
+ HEAD1 = '<h1>{}</h1>'
21
+ HEAD3 = '<h3>{}</h3>'
22
+ HEAD = """
23
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
24
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
25
+
26
+ <html>
27
+
28
+ <head>
29
+ <meta http-equiv="Content-Type"
30
+ content="text/html; charset=utf-8" />
31
+ <title></title>
32
+ <style type="text/css">
33
+ h1, h2, h3 {
34
+ font-family: Consolas, Sans, sans-serif
35
+ }
36
+ #code {
37
+ font-family: 'Jetbrains Mono', consolas, 'Courier New', monospace;
38
+ }
39
+ table {
40
+ font-family: 'Jetbrains Mono', consolas, 'Courier New', monospace;
41
+ border-style: solid;
42
+ border-width: 2px;
43
+ margin-top: 5px;
44
+ }
45
+ .legend,
46
+ .legend th,
47
+ .legend td {
48
+ border-style: none;
49
+ }
50
+ td.diff_header {
51
+ text-align:right;
52
+ }
53
+ .diff_next {
54
+ border-left-style: solid;
55
+ border-left-color: black;
56
+ border-left-width: 3px;
57
+ }
58
+ .diff_add {background-color:#aaffaa}
59
+ .diff_chg {background-color:#ffff77}
60
+ .diff_sub {background-color:#ffaaaa}
61
+ </style>
62
+ </head>
63
+ """
64
+
65
+ KEYTABLE = """
66
+ <table class="diff" summary="Legends" id="legend">
67
+ <tr> <th colspan="2"> Legends </th> </tr>
68
+ <tr> <td> <table border="" summary="Colors">
69
+ <tr><th> Colors </th> </tr>
70
+ <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
71
+ <tr><td class="diff_chg">Changed</td> </tr>
72
+ <tr><td class="diff_sub">Deleted</td> </tr>
73
+ </table></td>
74
+ <td> <table border="" summary="Links">
75
+ <tr><th colspan="2"> Links </th> </tr>
76
+ <tr><td>(f)irst change</td> </tr>
77
+ <tr><td>(n)ext change</td> </tr>
78
+ <tr><td>(t)op</td> </tr>
79
+ </table></td> </tr>
80
+ </table>
81
+ """
82
+
83
+ TAIL = """
84
+ </body>
85
+ </html>
86
+ """
87
+
88
+ @classmethod
89
+ def diff_to_html(
90
+ cls, left, right, title, left_title, right_title, subtitle
91
+ ):
92
+ # Carry out comparison:
93
+ htmldiff = HtmlDiff()
94
+ html_data = htmldiff.make_table(
95
+ left,
96
+ right,
97
+ fromdesc=left_title,
98
+ todesc=right_title
99
+ )
100
+
101
+ output = (
102
+ cls.HEAD
103
+ + cls.BACKBUTTON
104
+ + cls.HEAD1.format(title)
105
+ + f'{cls.HEAD3.format(subtitle) if subtitle else ""}'
106
+ + html_data
107
+ + cls.BACKBUTTON
108
+ + cls.KEYTABLE
109
+ + cls.TAIL
110
+ )
111
+ return output
112
+
113
+
114
+ def parse_args():
115
+ parser = argparse.ArgumentParser(__doc__)
116
+ parser.add_argument('left', help='The first file to compare.')
117
+ parser.add_argument('right', help='The second file to compare.')
118
+ parser.add_argument(
119
+ '-l', '--left-strip',
120
+ help='Regex expression acting on the left hand side of the diff',
121
+ dest='strip_left'
122
+ )
123
+ parser.add_argument(
124
+ '-r', '--right-strip',
125
+ help='Regex expression acting on the right hand side of the diff',
126
+ dest='strip_right'
127
+ )
128
+ parser.add_argument(
129
+ '-t', '--title',
130
+ help=(
131
+ 'Add a title to the page, other than the names of the files'
132
+ ' being compared. If set the names of the files being compared'
133
+ ' will be added as a subtitle.'
134
+ )
135
+ )
136
+ parser.add_argument(
137
+ '-n', '--notes',
138
+ help='Additional notes to be added below the title and subtitle.'
139
+ )
140
+ parser.add_argument(
141
+ '-o', '--output',
142
+ help='Output file path. If unset output will be printed to stdout.',
143
+ dest='output'
144
+ )
145
+ return parser.parse_args()
146
+
147
+
148
+ def read_file(filepath: Path, sub: Optional[str] = None) -> list[str]:
149
+ """Read file and carry out substutions on lines.
150
+
151
+ Returns a list of strings.
152
+ """
153
+ lines = Path(filepath).read_text().split('\n')
154
+ if sub:
155
+ lines = [re.sub(sub, '', line) for line in lines]
156
+ return lines
157
+
158
+
159
+ def main():
160
+ # Parse command line args and environment variables:
161
+ opts = parse_args()
162
+
163
+ parsed_html = DiffDoc.diff_to_html(
164
+ read_file(opts.left, opts.strip_left),
165
+ read_file(opts.right, opts.strip_right),
166
+ opts.title or f'{opts.left} vs {opts.right}',
167
+ opts.left,
168
+ opts.right,
169
+ f'{opts.left} vs {opts.right}' if opts.title else None
170
+ )
171
+
172
+ if opts.output:
173
+ # Write html file
174
+ Path(opts.output).write_text(parsed_html)
175
+ else:
176
+ print(parsed_html)
177
+
178
+
179
+ if __name__ == '__main__':
180
+ main()
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: diff2html
3
+ Version: 1.0
4
+ Summary: CLI wrapper for Pythons Difflib builtin
5
+ Author-email: "tim.pillinger" <tim.pillinger@metoffice.gov.uk>
6
+ License-Expression: MIT
7
+ Keywords: diff,html,difftool,difflib,cli
8
+ Classifier: Programming Language :: Python
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE.md
11
+ Provides-Extra: test
12
+ Requires-Dist: pytest; extra == "test"
13
+ Requires-Dist: flake8; extra == "test"
14
+ Dynamic: license-file
15
+
16
+ # Diff2HTML
17
+
18
+ Python's difflib is a great standard library feature.
19
+
20
+ But I wanted a command line version.
21
+
22
+ ## Installation
23
+
24
+ ```
25
+ pip install diff2html
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```
31
+ diff2html fileA fileB --output diff.html
32
+ firefox diff.html
33
+ ```
34
+
35
+ for advanced usage see:
36
+
37
+ ```
38
+ diff2html --help
39
+ ```
@@ -0,0 +1,11 @@
1
+ LICENSE.md
2
+ README.md
3
+ pyproject.toml
4
+ diff2html/diff2html.py
5
+ diff2html.egg-info/PKG-INFO
6
+ diff2html.egg-info/SOURCES.txt
7
+ diff2html.egg-info/dependency_links.txt
8
+ diff2html.egg-info/entry_points.txt
9
+ diff2html.egg-info/requires.txt
10
+ diff2html.egg-info/top_level.txt
11
+ test/test_e2e.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ diff2html = diff2html.diff2html:main
@@ -0,0 +1,4 @@
1
+
2
+ [test]
3
+ pytest
4
+ flake8
@@ -0,0 +1 @@
1
+ diff2html
@@ -0,0 +1,24 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = 'diff2html'
7
+ version = '1.0'
8
+ authors = [
9
+ {'name' = 'tim.pillinger', 'email' = 'tim.pillinger@metoffice.gov.uk'}
10
+ ]
11
+ description = "CLI wrapper for Pythons Difflib builtin"
12
+ readme = "README.md"
13
+ license = "MIT"
14
+ license-files = ["LICEN[CS]E.*"]
15
+ keywords = ['diff', 'html', 'difftool', 'difflib', 'cli']
16
+ classifiers = [
17
+ "Programming Language :: Python"
18
+ ]
19
+
20
+ [project.optional-dependencies]
21
+ test = ['pytest', 'flake8']
22
+
23
+ [project.scripts]
24
+ diff2html = "diff2html.diff2html:main"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,38 @@
1
+ from pathlib import Path
2
+ from shlex import split
3
+ from subprocess import run
4
+
5
+
6
+ REPO = Path(__file__).parent.parent
7
+ KGO = REPO / 'test/example/example.html'
8
+
9
+ COMMAND = split("""./diff_to_html \
10
+ test/example/foo1 \
11
+ test/example/foo2 \
12
+ --left-strip /some/very/long/path/to/a/ \
13
+ --right-strip /another/very/long/path/to/a/ \
14
+ --title "My Diff"
15
+ """)
16
+
17
+
18
+ def test_example():
19
+ f"""Test example output against KGO
20
+
21
+ Runs, from home of repo:
22
+
23
+ {COMMAND}
24
+
25
+ To update test, copy and add:
26
+
27
+ --output test/example/example.html \
28
+
29
+ """
30
+ result = run(COMMAND, capture_output=True, cwd=str(REPO))
31
+
32
+ assert result.returncode == 0
33
+
34
+ for res, kgo in zip(
35
+ result.stdout.decode().split('\n'),
36
+ (KGO).read_text().split('\n')
37
+ ):
38
+ assert res == kgo, f'{res} != {kgo}'