| 1 | import xml.etree.ElementTree as ET |
|---|
| 2 | import os, ConfigParser |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | config = ConfigParser.ConfigParser() |
|---|
| 6 | config.read('config/logging_service.cfg') |
|---|
| 7 | logfilepath = config.get('Paths', 'UnifiedLogPath') |
|---|
| 8 | |
|---|
| 9 | # read in the incident script in XML format |
|---|
| 10 | tree = ET.parse(logfilepath + 'incident_script.xml') |
|---|
| 11 | # root is TMC_SCRIPT tag |
|---|
| 12 | root = tree.getroot() |
|---|
| 13 | |
|---|
| 14 | # entries contain all of the entry of cad incident from incident script |
|---|
| 15 | entries = "" |
|---|
| 16 | # create a new file for the combined entry from incident script and unifiedlog |
|---|
| 17 | out_file = open(logfilepath + "caddetails.csv", "w") |
|---|
| 18 | |
|---|
| 19 | # read from the unifiedlog |
|---|
| 20 | unified_log = open(logfilepath + "unifiedlog.csv", "r") |
|---|
| 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_str += 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_str += " 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 | # count variable make sure there is as least a field |
|---|
| 43 | # inside CAD_INCIDENT_EVENT |
|---|
| 44 | count = 0 |
|---|
| 45 | for info in event: |
|---|
| 46 | count += 1 |
|---|
| 47 | # Aad the incident detail to entry string |
|---|
| 48 | if info.tag == "DETAIL": |
|---|
| 49 | entry_str += ", Detail: " + info.text |
|---|
| 50 | else: |
|---|
| 51 | # if no detail found then reset string |
|---|
| 52 | entry_str = "" |
|---|
| 53 | # if no field inside CAD_INCIDENT_EVENT |
|---|
| 54 | # reset the string |
|---|
| 55 | if count == 0: |
|---|
| 56 | entry_str = "" |
|---|
| 57 | |
|---|
| 58 | # if entry_str contain some info |
|---|
| 59 | # add it to the entries |
|---|
| 60 | if entry_str != "": |
|---|
| 61 | entries += entry_str + "\n" |
|---|
| 62 | # write all the entries from incident_sript.xml to the output file |
|---|
| 63 | out_file.write(entries) |
|---|
| 64 | |
|---|
| 65 | out_file = open(logfilepath + "caddetails.csv", "a") |
|---|
| 66 | # read in unifiedlog.csv and append to the output file |
|---|
| 67 | line = unified_log.readline() |
|---|
| 68 | while line: |
|---|
| 69 | out_file.write("0"+line) |
|---|
| 70 | line = unified_log.readline() |
|---|
| 71 | |
|---|
| 72 | out_file.close() |
|---|
| 73 | |
|---|
| 74 | # run the unix command line to sorting the file |
|---|
| 75 | os.system("sort -o " + logfilepath + "caddetails.csv " \ |
|---|
| 76 | + logfilepath + "caddetails.csv") |
|---|