#!/usr/bin/env python3
#=========================================================================================
# Get data from a file
# convert file from any format to any other format 
# (in the below case CSV to JSON)
# Write to output file
# *Below, update the name of the input file and output file
# *Below, change csv2obj and obj2json function calls to any other source/target formats  
#========================================================================================= 
import sys                                 #to append to path variable
import cgi                                 #cgi for web 

sys.path.insert(0,'/home/s/sultans/web/python/demo/etl/util')
import fileIO                              #import custom functions for file I/O
import convert                             #import custom functions to convert format
import emailFunc                           #import custom function  to email

print("Content-Type: text/plain \n")       #required http response header (w/ extra line) for web

infile   = '/home/s/sultans/web/python/demo/data/csv.txt'
#infile  = '/home/s/sultans/web/python/demo/data/json.txt'
#infile  = '/home/s/sultans/web/python/demo/data/xml.txt'

#outfile = '/home/s/sultans/web/python/demo/data/csv.txt'
outfile  = '/home/s/sultans/web/python/demo/data/json.txt'
#outfile = '/home/s/sultans/web/python/demo/data/xml.txt'

#===============================================================================================
# Main code
#===============================================================================================
input     = fileIO.read(infile)                          #read the input file
list_dict = convert.csv2obj(input)                       #convert input CSV to Python list of dictionaries
output    = convert.obj2json(list_dict)                  #convert Python list of dictionaries to JSON output
status    = fileIO.write(outfile, output)                #write the output to a file

print("Success?", status)

if status == False:
    print('Error in processing')
    emailFunc.send("ss4922@nyu.edu",                         #send email from 
                   "sam.sultan@hbo.com",                     #to   
                   "message subject",
                   "this is the body of the message") 

#===============================================================================================