community-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1867570 - /comdev/reporter.apache.org/trunk/scripts/prettify.py
Date Thu, 26 Sep 2019 09:53:19 GMT
Author: sebb
Date: Thu Sep 26 09:53:19 2019
New Revision: 1867570

URL: http://svn.apache.org/viewvc?rev=1867570&view=rev
Log:
Oops, py2 has no encoding

Modified:
    comdev/reporter.apache.org/trunk/scripts/prettify.py

Modified: comdev/reporter.apache.org/trunk/scripts/prettify.py
URL: http://svn.apache.org/viewvc/comdev/reporter.apache.org/trunk/scripts/prettify.py?rev=1867570&r1=1867569&r2=1867570&view=diff
==============================================================================
--- comdev/reporter.apache.org/trunk/scripts/prettify.py (original)
+++ comdev/reporter.apache.org/trunk/scripts/prettify.py Thu Sep 26 09:53:19 2019
@@ -1,6 +1,10 @@
 #!/usr/bin/env python
 # Prettify input json file: indent, sort
+
+import sys
 import argparse
+import json
+
 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)
@@ -10,14 +14,14 @@ args = parser.parse_args()
 
 sorted = "unsorted" if args.nosort else "sorted"
 
-import sys
+python2 = (sys.hexversion < 0x030000F0)
+
 # Any files provided?
 if len(args.file) > 0:
-  if sys.hexversion < 0x030000F0:
+  if python2:
     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
 
@@ -29,8 +33,12 @@ for arg in args.file:
             input = json.loads(f.read().decode('UTF-8', errors='replace'))
         out = arg if args.clobber else arg + ".out"
         print("Writing " + out)
-        with open(out, "w", encoding='utf-8') as f:
-            json.dump(input, f, indent=args.indent, sort_keys=sort_keys, ensure_ascii=False)
+        if python2:
+            with open(out, "w") as f:
+                json.dump(input, f, indent=args.indent, sort_keys=sort_keys, ensure_ascii=False)
+        else:
+            with open(out, "w", encoding='utf-8') as f:
+                json.dump(input, f, indent=args.indent, sort_keys=sort_keys, ensure_ascii=False)
      # we catch exception so can continue to process other files
     except Exception as ex:
         print(ex)



Mime
View raw message