Skip to content

Commit

Permalink
Fix unicode issues.
Browse files Browse the repository at this point in the history
Signed-off-by: Tim 'mithro' Ansell <me@mith.ro>
  • Loading branch information
mithro committed Dec 14, 2018
1 parent b4545cc commit 7d1d126
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 10 additions & 2 deletions prjxray/xjson.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import re
import io
import json
import re
import sys


Expand Down Expand Up @@ -48,9 +49,16 @@ def f(o):


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__":
Expand Down
8 changes: 7 additions & 1 deletion prjxray/xyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
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))


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):
Expand Down

0 comments on commit 7d1d126

Please sign in to comment.