#!/usr/bin/env python3
import io
import re
import yaml
import json
import unittest


def load(f):
    data = f.read()
    # 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)


class XYamlTest(unittest.TestCase):
    def test(self):
        s = io.StringIO("""\
!<xilinx/xc7series/part>
idcode: 0x362d093
global_clock_regions:
  top: !<xilinx/xc7series/global_clock_region>
    rows:
      0: !<xilinx/xc7series/row>
        configuration_buses:
          CLB_IO_CLK: !<xilinx/xc7series/configuration_bus>
            configuration_columns:
              0: !<xilinx/xc7series/configuration_column>
                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])))

Add wrapper to allow easy loading of yaml files.
Add wrapper to allow easy loading of yaml files.

Fixes #327.

Signed-off-by: Tim 'mithro' Ansell <b1c1d8736f20db3fb6c1c66bb1455ed43909f0d8@mith.ro>
