#!/usr/bin/env python

import os
import shutil
import csv

def read_lookup(ref_path):
    ref_dict = {}

    header_read = False
    full_ref_path = os.path.join(ref_path, 'Referenz_Matching_ID.csv')
    with open(full_ref_path, 'rb') as f:
        reader = csv.reader(f, delimiter=';')
        for row in reader:
            if header_read:
                ref_dict[row[1]] = row[0]
            else:
                header_read = True
    return ref_dict

def convert(in_file, out_file, ref_dict):
    header_read = False
    no_index = -1
	
    with open(in_file, 'rb') as f, open(out_file,'wb') as of:
        reader = csv.reader(f, delimiter=';')
        writer = csv.writer(of, delimiter=';')
        for row in reader:
            if any(row):
                if header_read:
                    item_id = "ERROR"
                    if row[no_index] in ref_dict:
                        item_id = ref_dict[row[no_index]]
                    row.insert(0,item_id)
                else:
                    header_read = True
                    no_index = row.index('ItemNo')
                    row.insert(0,'ItemId')
                writer.writerow(row)                
    
def main():
    #data_root = r"/data/thelen"
    data_root = r"/ftproot"
    path_ref = os.path.join(data_root,"ref")
    path_in = os.path.join(data_root,"in")
    path_out = os.path.join(data_root,"out")
    path_proc = os.path.join(data_root,"processed")
    path_err = os.path.join(data_root,"error")
    
    ref_dict =  read_lookup(path_ref)

    for f in os.listdir(path_in):
        in_file = os.path.join(path_in, f)
        out_file = os.path.join(path_out, f)
        proc_file = os.path.join(path_proc, f)
        err_file = os.path.join(path_err, f)
        
        if os.path.isfile(in_file):
            print("Converting {0} to {1}...".format(in_file, out_file))
            try:
            	convert(in_file,out_file, ref_dict)
            	shutil.move(in_file, proc_file)
            except:
            	shutil.move(in_file, err_file)

if __name__ == "__main__":
    main()


