Skip to content

Instantly share code, notes, and snippets.

@afaqk9394
Created November 3, 2019 07:41
Show Gist options
  • Select an option

  • Save afaqk9394/36000a7543943e61f3bfc5a15a327367 to your computer and use it in GitHub Desktop.

Select an option

Save afaqk9394/36000a7543943e61f3bfc5a15a327367 to your computer and use it in GitHub Desktop.
Modified Python Code -Interpret a unified diff
import sys, os, difflib, argparse
from datetime import datetime, timezone
def file_mtime(path):
t = datetime.fromtimestamp(os.stat(path).st_mtime,
timezone.utc)
return t.astimezone().isoformat()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true', default=False,
help='Produce a context format diff (default)')
parser.add_argument('-u', action='store_true', default=False,
help='Produce a unified format diff')
parser.add_argument('-m', action='store_true', default=False,
help='Produce HTML side by side diff '
'(can use -c and -l in conjunction)')
parser.add_argument('-n', action='store_true', default=False,
help='Produce a ndiff format diff')
parser.add_argument('-l', '--lines', type=int, default=3,
help='Set number of context lines (default 3)')
parser.add_argument('fromfile')
parser.add_argument('tofile')
options = parser.parse_args()
parser = argparse.ArgumentParser(description="pydiff - Tkinter GUI tool based on Python's difflib")
parser.add_argument('-p', '--paths', metavar=('path1', 'path2'), nargs=2, help='Two paths to compare', required=False)
args = parser.parse_args()
leftpath = args.paths[0] if args.paths else None
rightpath = args.paths[1] if args.paths else None
main_window = MainWindow()
main_window.start(leftpath, rightpath)
if options.u:
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
elif options.n:
diff = difflib.ndiff(fromlines, tolines)
elif options.m:
diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
else:
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
sys.stdout.writelines(diff)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment