import xml.etree.ElementTree as ET
import os, ConfigParser


config = ConfigParser.ConfigParser()
config.read('config/logging_service.cfg')
logfilepath  = config.get('Paths', 'UnifiedLogPath')

# read in the incident script in XML format
tree = ET.parse(logfilepath + 'incident_script.xml')
# root is TMC_SCRIPT tag 
root = tree.getroot()

# entries contain all of the entry of cad incident from incident script 
entries = ""
# create a new file for the combined entry from incident script and unifiedlog
out_file = open(logfilepath + "caddetails.csv", "w")

# read from the unifiedlog
unified_log = open(logfilepath + "unifiedlog.csv", "r")

# loop through tags under TMC_SCRIPT 
for script_event in root:
    # if found tag SCRIPT_EVENT
    if script_event.tag == 'SCRIPT_EVENT':
        entry_str = ""
        # loop through tags within SCRIPT_EVENT tags
        for info_type in script_event:
            # Add the time index field to entry string
            if info_type.tag == 'TIME_INDEX':
                entry_str +=  info_type.text + ","
            # Get the incident number to entry string 
            if info_type.tag == "INCIDENT":
                incident_num = info_type.attrib["LogNum"]
                entry_str += " CAD Log, Incident #" + incident_num
            # Get the cad data info 
            if info_type.tag == 'CAD_DATA':
                # Loop through all tags under CAD_DATA 
                for event in info_type:
                    # Look for CAD_INCIDENT_EVENT
                    if event.tag == 'CAD_INCIDENT_EVENT':
                        # count variable make sure there is as least a field 
                        # inside CAD_INCIDENT_EVENT 
                        count = 0
                        for info in event:
                            count += 1
                            # Aad the incident detail to entry string 
                            if info.tag == "DETAIL":
                                entry_str += ", Detail: " + info.text 
                            else:
                                # if no detail found then reset string
                                entry_str = ""
                        # if no field inside CAD_INCIDENT_EVENT 
                        # reset the string 
                        if count == 0:
                            entry_str = ""
                        
                        # if entry_str contain some info
                        # add it to the entries 
                        if entry_str != "":
                            entries += entry_str + "\n"
# write all the entries from incident_sript.xml to the output file 
out_file.write(entries)

out_file = open(logfilepath + "caddetails.csv", "a")
# read in unifiedlog.csv and append to the output file 
line = unified_log.readline()
while line:
    out_file.write("0"+line)
    line = unified_log.readline()

out_file.close()

# run the unix command line to sorting the file 
os.system("sort -o " + logfilepath + "caddetails.csv " \
    + logfilepath + "caddetails.csv")
