You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@community.apache.org by se...@apache.org on 2015/10/10 23:50:08 UTC
svn commit: r1707916 - /comdev/reporter.apache.org/trunk/prettify.py
Author: sebb
Date: Sat Oct 10 21:50:07 2015
New Revision: 1707916
URL: http://svn.apache.org/viewvc?rev=1707916&view=rev
Log:
Default to python2 (as used for data/releases)
Allow sort and indent to be separately overridden
Modified:
comdev/reporter.apache.org/trunk/prettify.py
Modified: comdev/reporter.apache.org/trunk/prettify.py
URL: http://svn.apache.org/viewvc/comdev/reporter.apache.org/trunk/prettify.py?rev=1707916&r1=1707915&r2=1707916&view=diff
==============================================================================
--- comdev/reporter.apache.org/trunk/prettify.py (original)
+++ comdev/reporter.apache.org/trunk/prettify.py Sat Oct 10 21:50:07 2015
@@ -1,7 +1,25 @@
+#!/usr/bin/env python
# Prettify input json file: indent, sort
-import json
+import argparse
+parser = argparse.ArgumentParser()
+parser.add_argument("--nosort", action='store_true', help="Don't sort the output", default=False)
+parser.add_argument("--clobber",action='store_true', help="Overwrite the input file", default=False)
+parser.add_argument("--indent", type=int, help="Indentation to use for the output file (default 1)", default=1)
+parser.add_argument("file", help="Input file(s)", nargs='*')
+args = parser.parse_args()
+
+sorted = "unsorted" if args.nosort else "sorted"
+
import sys
-for arg in sys.argv[1:]:
+if sys.hexversion < 0x030000F0:
+ print("Using Python2 (adds trailing spaces), output will be " + sorted + ". Indent = " + str(args.indent))
+else:
+ print("Using Python3 (strips trailing spaces), output will be " + sorted + ". Indent = " + str(args.indent))
+import json
+
+sort_keys = not args.nosort
+
+for arg in args.file:
print("Reading " + arg)
input = {}
try:
@@ -12,8 +30,8 @@ for arg in sys.argv[1:]:
print(ex)
pass
else:
- out = arg + ".out"
+ out = arg if args.clobber else arg + ".out"
print("Writing " + out)
with open(out, "w") as f:
- json.dump(input, f, indent=1, sort_keys=True)
+ json.dump(input, f, indent=args.indent, sort_keys=sort_keys)
f.close()