#!/usr/bin/python


import xml.dom.minidom


def PrettyPrintXmlString(string):
	xmlVal = xml.dom.minidom.parseString(string)
	return xmlVal.toprettyxml()


def PrettyPrintXmlFile(filename):
	xmlVal = xml.dom.minidom.parse(filename)
	return xmlVal.toprettyxml()


def TableToXmlWorksheet(table, name='Worksheet1'):
	columnCount = len(table["columns"])
	maxColWidth = 2
	for col in table["columns"]:
		maxColWidth = max(maxColWidth, len(col))
	maxRowWidth = 2
	for row in table["rows"]:
		maxRowWidth = max(maxRowWidth, len(row))
	lines = []
	lines.append('<ss:Worksheet ss:Name="' + name + '">')
	lines.append('<Table ss:StyleID="table">')
	# Width needs to be as wide as widest row name
	lines.append('<Column ss:Width="50.0"/>')
	lines.append('<Column ss:Width="' + str(maxRowWidth*8) + '.0"/>')
	for col in table["columns"]:
		lines.append('<Column ss:Width="20.0"/>')
	lines.append('<Column ss:Width="50.0"/>')
	lines.append('<Row ss:Height="50.0"><Cell ss:StyleID="Blank"/>')
	lines.append('<Cell ss:MergeAcross="' + str(columnCount) + '" ss:StyleID="Heading"> <Data ss:Type="String">' + name + '</Data> </Cell>')
	lines.append('<Cell ss:StyleID="Blank"/></Row>')
 	# Height needs to be as high as widest column name
	lines.append('<Row ss:Height="' + str(maxColWidth*6) + '.0"><Cell ss:StyleID="Blank"/><Cell ss:StyleID="ColumnHeader"/>')
	for col in table["columns"]:
		lines.append('<Cell ss:StyleID="ColumnHeader"> <Data ss:Type="String">' + col + '</Data> </Cell>')
	lines.append('<Cell ss:StyleID="Blank"/></Row>')
	alt = 0
	for row in table["rows"]:
		lines.append('<Row ss:AutoFitHeight="0" ss:Height="13.0">')
		lines.append('<Cell ss:StyleID="Left"/>')
		lines.append('<Cell ss:StyleID="Row' + str(alt) + '"> <Data ss:Type="String">' + str(row) + '</Data> </Cell>')
		for col in table["columns"]:
			val = ''
			if col in table["data"][row]:
				val = table["data"][row][col]
			lines.append('<Cell ss:StyleID="Value' + str(alt) + '"> <Data ss:Type="String">' + str(val) + '</Data> </Cell>')
		lines.append('<Cell ss:StyleID="Right"/>')
		lines.append('</Row>')
		alt = 1 - alt
	lines.append('<Row ss:Height="50.0"><Cell ss:StyleID="Blank"/>')
	lines.append('<Cell ss:MergeAcross="' + str(columnCount) + '" ss:StyleID="Bottom"> </Cell>')
	lines.append('<Cell ss:StyleID="Blank"/></Row>')
	lines.append('</Table>')
	lines.append('<x:WorksheetOptions/>')
	lines.append('</ss:Worksheet>')
	return '\n'.join(lines)


def ConvertToExcelFile(data):
	lines = [ '<?xml version="1.0" ?>',
		 '<?mso-application progid="Excel.Sheet"?>',
		 '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"',
		 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">',
		 '<Styles>', '<Style ss:ID="Default" ss:Name="Default"/>',
		
		 '<Style ss:ID="Heading"><Alignment ss:Vertical="Center" ss:Horizontal="Center"/>',
		 '<Interior ss:Color="#dddddd" ss:Pattern="Solid"/>',
		 '<Font ss:Bold="1" ss:Size="16"/></Style>',

		 '<Style ss:ID="ColumnHeader"> <Alignment ss:Horizontal="Left" ss:Rotate="45"/><Borders>',
		 '<Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Bottom" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Left"><Borders>',
		 '<Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Right" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Right"><Borders>',
		 '<Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Left" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Bottom"><Borders>',
		 '<Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Top" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Blank"><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Row0"><Interior ss:Color="#dddddd" ss:Pattern="Solid"/></Style>',
		 '<Style ss:ID="Row1"><Interior ss:Color="#eeeeee" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Value0"><Borders><Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Left" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#ddddee" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="Value1"><Borders><Border ss:Color="#000000" ss:LineStyle="Continuous" ss:Position="Left" ss:Weight="1"/>',
		 '</Borders><Interior ss:Color="#eeeeff" ss:Pattern="Solid"/></Style>',

		 '<Style ss:ID="table"> <Font ss:Size="10" ss:VerticalAlign="Subscript"/> </Style>',

		 '</Styles>' ]
	for worksheet in sorted(data.keys()):
		lines.append(TableToXmlWorksheet(DictToTable(data[worksheet]), worksheet))
	lines.append('</Workbook>')
	return '\n'.join(lines)



