| 1 | import xml.etree.ElementTree as ET |
|---|
| 2 | import os, ConfigParser |
|---|
| 3 | # Standalone application to extract CAD DETAIL info from |
|---|
| 4 | # incident_script.xml. |
|---|
| 5 | # @author Ally Quan, jdalbey |
|---|
| 6 | # Deploy: zip file is created by package_jars target of NetBeans build.xml. |
|---|
| 7 | # Move unifiedlogger.zip from deploy folder to webapps folder. |
|---|
| 8 | # Usage: PYTHONPATH=webapps/unifiedlogger.zip python -m extract_caddetails |
|---|
| 9 | def extract(): |
|---|
| 10 | config = ConfigParser.ConfigParser() |
|---|
| 11 | config.read('config/logging_service.cfg') |
|---|
| 12 | logfilepath = config.get('Paths', 'UnifiedLogPath') |
|---|
| 13 | |
|---|
| 14 | # read in the incident script in XML format |
|---|
| 15 | tree = ET.parse(logfilepath + 'incident_script.xml') |
|---|
| 16 | # root is TMC_SCRIPT tag |
|---|
| 17 | root = tree.getroot() |
|---|
| 18 | |
|---|
| 19 | # entries contain all of the entry of cad incident from incident script |
|---|
| 20 | entries = "" |
|---|
| 21 | |
|---|
| 22 | # loop through tags under TMC_SCRIPT |
|---|
| 23 | for script_event in root: |
|---|
| 24 | # if found tag SCRIPT_EVENT |
|---|
| 25 | if script_event.tag == 'SCRIPT_EVENT': |
|---|
| 26 | entry_str = "" |
|---|
| 27 | # loop through tags within SCRIPT_EVENT tags |
|---|
| 28 | for info_type in script_event: |
|---|
| 29 | # Add the time index field to entry string |
|---|
| 30 | if info_type.tag == 'TIME_INDEX': |
|---|
| 31 | entry_time = info_type.text + "," |
|---|
| 32 | # Get the incident number to entry string |
|---|
| 33 | if info_type.tag == "INCIDENT": |
|---|
| 34 | incident_num = info_type.attrib["LogNum"] |
|---|
| 35 | entry_incinum = " CAD Log, Incident #" + incident_num |
|---|
| 36 | # Get the cad data info |
|---|
| 37 | if info_type.tag == 'CAD_DATA': |
|---|
| 38 | # Loop through all tags under CAD_DATA |
|---|
| 39 | for event in info_type: |
|---|
| 40 | # Look for CAD_INCIDENT_EVENT |
|---|
| 41 | if event.tag == 'CAD_INCIDENT_EVENT': |
|---|
| 42 | # set flag to check if DETAIL field exist |
|---|
| 43 | detailFlag = False |
|---|
| 44 | detailText = "" |
|---|
| 45 | for info in event: |
|---|
| 46 | # Add the incident detail to entry string |
|---|
| 47 | if info.tag == "DETAIL": |
|---|
| 48 | # replace commas with whitespace |
|---|
| 49 | infotext = info.text.replace(",", " ") |
|---|
| 50 | # if it's the second DETAIL field, |
|---|
| 51 | # we don't need the "Detail" label |
|---|
| 52 | if not detailFlag: |
|---|
| 53 | detailText = ", Detail: " |
|---|
| 54 | detailFlag = True |
|---|
| 55 | detailText += infotext |
|---|
| 56 | # Build the complete line from the header and details |
|---|
| 57 | entry_str += entry_time + entry_incinum + detailText |
|---|
| 58 | # if there's detail field add it to the entries |
|---|
| 59 | if detailFlag: |
|---|
| 60 | entries += entry_str + "\n" |
|---|
| 61 | |
|---|
| 62 | # write all the entries from incident_sript.xml to the output file |
|---|
| 63 | # create a new file for the combined entry from incident script and unifiedlog |
|---|
| 64 | details_file = open(logfilepath + "caddetails.csv", "w") |
|---|
| 65 | details_file.write(entries) |
|---|
| 66 | details_file.close() |
|---|
| 67 | |
|---|
| 68 | if __name__ == '__main__': |
|---|
| 69 | extract() |
|---|
| 70 | |
|---|
| 71 | # The following code is obsolete and has been replaced by a unix script |
|---|
| 72 | |
|---|
| 73 | # Append unifiedlog file to caddetails file |
|---|
| 74 | #details_file = open(logfilepath + "caddetails.csv", "a") |
|---|
| 75 | # read in unifiedlog.csv and append to the caddetails file |
|---|
| 76 | #unified_log = open(logfilepath + "unifiedlog.csv", "r") |
|---|
| 77 | #line = unified_log.readline() |
|---|
| 78 | #while line: |
|---|
| 79 | # details_file.write(line) |
|---|
| 80 | # line = unified_log.readline() |
|---|
| 81 | |
|---|
| 82 | #details_file.close() |
|---|
| 83 | |
|---|
| 84 | # run the unix command line to sorting the file |
|---|
| 85 | #os.system("sort -o " + logfilepath + "caddetails.csv " \ |
|---|
| 86 | # + logfilepath + "caddetails.csv") |
|---|
| 87 | |
|---|