-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
diff.py
105 lines (80 loc) · 3.11 KB
/
diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""A command line tool for diffing files.
The Python 2.7 documentation demonstrates how to make a command line interface
for the difflib library using optparse:
https://docs.python.org/2/library/difflib.html#a-command-line-interface-to-difflib
This file demonstrates how to create a command line interface providing the same
functionality using Python Fire.
Usage:
diff FROMFILE TOFILE COMMAND [LINES]
Arguments can be passed positionally or via the Flag syntax.
Using positional arguments, the usage is:
diff FROMFILE TOFILE
diff FROMFILE TOFILE context-diff [LINES]
diff FROMFILE TOFILE unified-diff [LINES]
diff FROMFILE TOFILE ndiff
diff FROMFILE TOFILE make-file [CONTEXT] [LINES]
Using the Flag syntax, the usage is:
diff --fromfile=FROMFILE --tofile=TOFILE
diff --fromfile=FROMFILE --tofile=TOFILE context-diff [--lines=LINES]
diff --fromfile=FROMFILE --tofile=TOFILE unified-diff [--lines=LINES]
diff --fromfile=FROMFILE --tofile=TOFILE ndiff
diff --fromfile=FROMFILE --tofile=TOFILE make-file \
[--context=CONTEXT] [--lines LINES]
As with any Fire CLI, you can append '--' followed by any Flags to any command.
The Flags available for all Fire CLIs are:
--help
--interactive
--trace
--separator=SEPARATOR
--completion
--verbose
"""
import difflib
import os
import time
import fire
class DiffLibWrapper(object):
"""Provides a simple interface to the difflib module.
The purpose of this simple interface is to offer a limited subset of the
difflib functionality as a command line interface.
"""
def __init__(self, fromfile, tofile):
self._fromfile = fromfile
self._tofile = tofile
self.fromdate = time.ctime(os.stat(fromfile).st_mtime)
self.todate = time.ctime(os.stat(tofile).st_mtime)
with open(fromfile) as f:
self.fromlines = f.readlines()
with open(tofile) as f:
self.tolines = f.readlines()
def unified_diff(self, lines=3):
return difflib.unified_diff(
self.fromlines, self.tolines, self._fromfile,
self._tofile, self.fromdate, self.todate, n=lines)
def ndiff(self):
return difflib.ndiff(self.fromlines, self.tolines)
def make_file(self, context=False, lines=3):
return difflib.HtmlDiff().make_file(
self.fromlines, self.tolines, self._fromfile, self._tofile,
context=context, numlines=lines)
def context_diff(self, lines=3):
return difflib.context_diff(
self.fromlines, self.tolines, self._fromfile,
self._tofile, self.fromdate, self.todate, n=lines)
def main():
fire.Fire(DiffLibWrapper, name='diff')
if __name__ == '__main__':
main()