Skip to content

Commit

Permalink
Fix for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
rasa committed Feb 19, 2017
1 parent 12a4ffb commit b8cb78a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions jsonfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import os
import sys

if sys.version_info >= (3,0):
sys.exit("Sorry, this script has only been tested with Python 2.x")

def decode(s):
for encoding in 'utf-8-sig', 'utf-16':
try:
Expand All @@ -27,17 +30,22 @@ def touch(filename, mtime):

mtime = os.path.getmtime(file)

with open(file, 'rb') as f:
with open(file, 'r') as f:
jstr = f.read(os.path.getsize(file))
jstr_no_bom = decode(jstr)

parser = JsonComment(json)
json_data = parser.loads(jstr_no_bom)

new_data = json.dumps(json_data, sort_keys=True, indent=4, separators=(',', ': '))
with open(file, 'wb') as f:
with open(file + '.tmp', 'w') as f:
f.write(new_data+"\n")

if os.path.isfile(file + '.bak'):
os.remove(file + '.bak')
os.rename(file, file + '.bak')
os.rename(file + '.tmp', file)

touch(file, mtime)

sys.exit(0)
4 changes: 2 additions & 2 deletions validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def decode(s):

print('Validating', file)

with open(schema_name, 'rb') as f:
with open(schema_name, 'r') as f:
schema_data = json.load(f)

with open(file, 'rb') as f:
with open(file, 'r') as f:
jstr = f.read(os.path.getsize(file))

jstr_no_bom = decode(jstr)
Expand Down

0 comments on commit b8cb78a

Please sign in to comment.