Fix unicode issues.

Signed-off-by: Tim 'mithro' Ansell <me@mith.ro>
This commit is contained in:
Tim 'mithro' Ansell 2018-12-13 15:15:11 -08:00
parent 51ed9fee03
commit a09ded0624
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3
import re
import io
import json
import re
import sys
@ -48,9 +49,16 @@ def sort(data):
def pprint(f, data):
detach = False
if not isinstance(f, io.TextIOBase):
detach = True
f = io.TextIOWrapper(f)
sort(data)
json.dump(d, f, sort_keys=True, indent=4)
json.dump(data, f, sort_keys=True, indent=4)
f.write('\n')
f.flush()
if detach:
f.detach()
if __name__ == "__main__":

View File

@ -5,9 +5,13 @@ import yaml
import json
import unittest
from prjxray import xjson
def load(f):
data = f.read()
if isinstance(data, bytes):
data = data.decode('utf-8')
# Strip out of !<tags>
data = re.sub("!<[^>]*>", "", data)
return yaml.load(io.StringIO(data))
@ -15,7 +19,9 @@ def load(f):
def tojson(f):
d = load(f)
return json.dumps(d, sort_keys=True, indent=4)
o = io.StringIO()
xjson.pprint(o, d)
return o.getvalue()
class XYamlTest(unittest.TestCase):