#!python3
# -*- coding:utf-8
__author__ = "Přemek Hnilica"
__date__ = "$May 1, 2015 1:04:50 PM$"

import argparse
from xml.etree import ElementTree as et
import time

#########################
# This script is awaiting an inFile containing data in structure:
# <lat>,<lon>,<k>=<v>,...
# 
# For example:
# 49.2265,16.59672,my_point=fit,shop=sport
# 36.2265,08.59672,kct_red=major,ref=322
#########################

parser = argparse.ArgumentParser(description='Tools that creates osm file from nodes definitions.')
parser.add_argument('-o','--output',help='Output file name', required=True)
parser.add_argument('-i','--input',help='Input file name',required=True)
args = parser.parse_args()

counter = -1
current_time = time.strftime("%Y-%d-%mT%H:%M:%SZ", time.localtime(time.time()))

root = et.Element('osm')
root.attrib = {'version':'0.6'}

with open(args.input) as inf:
    for line in inf:
        point = (line.rstrip()).split(',') # strip EOF rom line and split it
        node = et.Element('node') # creation of the node
        node.attrib = {'id':str(counter),'version':'1','lat':point[0],'lon':point[1],'timestamp':current_time} # adding attributes to the node
        for key in point[2:]: # go through all tags
            tag = et.Element('tag') #creation of the tag
            tag.attrib = {'k':key.split('=')[0],'v':key.split('=')[1]} # adding attributes to the tag
            node.append(tag) # appending the attribute to the tag
        root.append(node) # appending node to the root
        counter = counter -1

tree = et.ElementTree(root)

tree.write(args.output, xml_declaration=True, encoding='utf-8')