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_time =  info_type.text + ","
            # Get the incident number to entry string 
            if info_type.tag == "INCIDENT":
                incident_num = info_type.attrib["LogNum"]
                entry_incinum = " 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':
                        # set flag to check if DETAIL field exist
                        detailFlag = False
                        detailText = ""
                        for info in event:
                            # Add the incident detail to entry string 
                            if info.tag == "DETAIL":
                                # replace commas with whitespace
                                infotext = info.text.replace(",", " ")
                                # if it's the second DETAIL field, 
                                # we don't need the "Detail" label
                                if not detailFlag:
                                    detailText = ", Detail: "
                                    detailFlag = True
                                detailText += infotext                                
                        # Build the complete line from the header and details
                        entry_str += entry_time + entry_incinum + detailText                                    
                        # if there's detail field add it to the entries 
                        if detailFlag:
                            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")
                            
