#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
#from myModule import myModule
import json
import csv
import os
import datetime
#===============================================================================
def createDirectories(datafile, cfgfile, level1="CVE_ENT", level2="CVE_MUN"):
	with open(cfgfile) as cfg_file:
		cfg = json.load(cfg_file)
	with open(datafile) as csvfile:
		data = csv.DictReader(csvfile, delimiter=',')
		for row in data:

			mun = row[level1]
			ent = row[level2]
			dirname = ent+mun
			print(ent)
#			for k in cfg.keys():
#				p[k]=cfg[k]
			print(os.getcwd())
			cfg["projectDir"]=os.getcwd()+"/"+dirname+"/"
			cfg["endDate"]=datetime.datetime.now().strftime("%Y%m%d")
			cfg["wkt"]=row["WKT"]
			if not os.path.exists(dirname):
				os.mkdir(dirname)
			with open(dirname+'/findProducts.json', 'w') as outfile:
    				json.dump(cfg, outfile)
#===============================================================================
def main(argv):

	if len(sys.argv) != 3:
		print(len(sys.argv))
		print ("Usage: "+argv[0] + " <csv> <cfg>")
	else:
		createDirectories(argv[1], argv[2])


if __name__ == "__main__":
	main(sys.argv)
