from __future__ import print_function
import os
import sys

import ruamel.yaml
f = open('AxiStreamDmaRingWrite.yaml', 'r')
data = ruamel.yaml.load(f, ruamel.yaml.RoundTripLoader)
cMap = ruamel.yaml.comments.CommentedMap()
for ch in data["Device"]:
	if "IntField" in ch:
		for d in data["Device"]["IntField"]:
			d.insert(1,'class','IntField')
			if "enums" in d:
				for e in d["enums"]:
					e.insert(1,'class','Enum')
			cMap.insert(100, d["name"], d)
	if "Commands" in ch:
		for d in data["Device"]["Commands"]:
			d.insert(1,'class','SequenceCommand')

childrenMap = ruamel.yaml.comments.CommentedMap()
childrenMap.insert(100,'children', cMap)
#childrenMap.insert(100,'children', data["Device"]["IntField"])

for d in data["Device"]["Commands"]:
	childrenMap["children"].insert(100, d["name"], d)
#	childrenMap["children"].append(d)
new_d = ruamel.yaml.comments.CommentedMap()
for d in data["Device"]:
	if "IntField" in d:
		pass		
	elif "Commands" in d:
		pass
	else:
		if "Device" in new_d:
			new_d["Device"].insert(100,d,data["Device"][d])
		else:
			m = ruamel.yaml.comments.CommentedMap()
			m.insert(100,d,data["Device"][d])
			new_d.insert(100,'Device',m)

new_d["Device"].insert(100,'children',childrenMap['children'])

lic = open('license.txt','r')
print(lic.read())
print(ruamel.yaml.dump(new_d, Dumper=ruamel.yaml.RoundTripDumper), end='')
