diff --git a/prjxray/xyaml.py b/prjxray/xyaml.py new file mode 100644 index 00000000..40e3bc2f --- /dev/null +++ b/prjxray/xyaml.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +import io +import re +import yaml +import json +import unittest + + +def load(f): + data = f.read() + # Strip out of ! + 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) + + +class XYamlTest(unittest.TestCase): + def test(self): + s = io.StringIO("""\ +! +idcode: 0x362d093 +global_clock_regions: + top: ! + rows: + 0: ! + configuration_buses: + CLB_IO_CLK: ! + configuration_columns: + 0: ! + frame_count: 42 +""") + djson = tojson(s) + self.assertMultiLineEqual(djson, """\ +{ + "global_clock_regions": { + "top": { + "rows": { + "0": { + "configuration_buses": { + "CLB_IO_CLK": { + "configuration_columns": { + "0": { + "frame_count": 42 + } + } + } + } + } + } + } + }, + "idcode": 56807571 +}""") + +if __name__ == "__main__": + import sys + if len(sys.argv) == 1: + unittest.main() + else: + assert len(sys.argv) == 2 + print(tojson(open(sys.argv[1])))