#!/usr/bin/python

import re

# Reads from a file formatted as miles.dat and builds a graph from this data
def buildGraph(fileref):
    cities = []
    g = {}

    for line in fileref.readlines():
        if re.match('\*', line):  # Comments, just ignore them
            pass # ignore

        elif re.match('\d', line):  # Adjacency data, update graph
            dists = line.split()

            # Scan all the distances in this line and insert this data into g            
            for dist in dists:
		# Update the graph with adjacencies
		# Missing Code

        else: # this line provides city data
            data = re.split('[\[\,\]]+', line)

	    # Append to the list of cities
            # Insert the city data into g
	    # Missing Code

    return g


# Takes a graph whose vertices have associated longitude, latitude info. and
# write this into a given kml file
def writeKML(g, filename):
  outfile = open(filename, "wt")
  outfile.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  outfile.write('<kml xmlns="http://www.opengis.net/kml/2.2">\n')
  
  # Write info. corresponding to each vertex in the graph
  for v in g.keys():
    outfile.write('<Placemark>\n')
    outfile.write('<name>' + v + ', ' + g[v][0] + '</name>\n')
    outfile.write('<description>' + v + ', ' + g[v][0] + '</description>\n')
    outfile.write('<Point>\n')
    outfile.write('<coordinates>' + str(-1*g[v][2]) + ', ' + str(g[v][1]) + ', ' + '0</coordinates>\n')
    outfile.write('</Point>\n')
    outfile.write('</Placemark>\n')

  outfile.write('</kml>')
  outfile.close()


def main():

  fileref = open(pickAFile(), 'r')
  g = buildGraph(fileref)
  writeKML(g, "/mnt/nfs/netapp1/fs3/sriram/python/graph.kml")

  for v in g.keys():
    printNow(v)
    printNow(g[v])
