import xml.etree.ElementTree as ET
import os, ConfigParser
# Standalone application to extract CAD DETAIL info from 
# incident_script.xml.
# @author Ally Quan, jdalbey
# Deploy:  zip file is created by package_jars target of NetBeans build.xml.
#          Move unifiedlogger.zip from deploy folder to webapps folder.
# Usage: PYTHONPATH=webapps/unifiedlogger.zip  python -m extract_caddetails
def extract():
    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 = ""
    
    # 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 
    # create a new file for the combined entry from incident script and unifiedlog
    details_file = open(logfilepath + "caddetails.csv", "w")
    details_file.write(entries)
    details_file.close()

if __name__ == '__main__':
    extract()
    
# The following code is obsolete and has been replaced by a unix script

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

#details_file.close()

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