from __future__ import with_statement
import sys, xml.parsers.expat, mml

mml_font = mml.Node('font', '')

def start(name, attrs):
	global mml_font
	if name == 'info':
		mml_font.value = attrs['face']
	if name == 'common':
		mml_font.children.append(mml.Node('height', attrs['lineHeight']))
	if name == 'page':
		mml_font.children.append(mml.Node('texture', attrs['file']))
	if name == 'chars':
		mml_font.children.append(mml.Node('chars', attrs['count']))
	if name == 'char':
		char = mml.Node('char', attrs['id'])
		char.children.append(mml.Node('x', attrs['x']))
		char.children.append(mml.Node('y', attrs['y']))
		char.children.append(mml.Node('w', attrs['width']))
		char.children.append(mml.Node('h', attrs['height']))
		char.children.append(mml.Node('xoff', attrs['xoffset']))
		char.children.append(mml.Node('yoff', attrs['yoffset']))
		char.children.append(mml.Node('xadv', attrs['xadvance']))
		for child in mml_font.children:
			if child.name == 'chars':
				child.children.append(char)

if __name__ == '__main__':
	if len(sys.argv) != 2:
		print 'Provide file to convert'
	else:
		parser = xml.parsers.expat.ParserCreate()
		parser.StartElementHandler = start
		with open(sys.argv[1]) as infile:
			parser.ParseFile(infile)
		with open(sys.argv[1], 'w') as outfile:
			outfile.write(mml.serialize(mml_font))



