| 1 | import xml.etree.ElementTree as ET |
|---|
| 2 | import os, ConfigParser |
|---|
| 3 | |
|---|
| 4 | config = ConfigParser.ConfigParser() |
|---|
| 5 | config.read('config/logging_service.cfg') |
|---|
| 6 | logfilepath = config.get('Paths', 'UnifiedLogPath') |
|---|
| 7 | |
|---|
| 8 | # read in the incident script in XML format |
|---|
| 9 | tree = ET.parse(logfilepath + 'incident_script_test.xml') |
|---|
| 10 | # root is TMC_SCRIPT tag |
|---|
| 11 | root = tree.getroot() |
|---|
| 12 | |
|---|
| 13 | # entries contain all of the entry of cad incident from incident script |
|---|
| 14 | entries = "" |
|---|
| 15 | # create a new file for the combined entry from incident script and unifiedlog |
|---|
| 16 | out_file = open(logfilepath + "caddetails.csv", "w") |
|---|
| 17 | |
|---|
| 18 | # read from the unifiedlog |
|---|
| 19 | unified_log = open(logfilepath + "unifiedlog.csv", "r") |
|---|
| 20 | |
|---|
| 21 | # loop through tags under TMC_SCRIPT |
|---|
| 22 | for script_event in root: |
|---|
| 23 | # if found tag SCRIPT_EVENT |
|---|
| 24 | if script_event.tag == 'SCRIPT_EVENT': |
|---|
| 25 | entry_str = "" |
|---|
| 26 | # loop through tags within SCRIPT_EVENT tags |
|---|
| 27 | for info_type in script_event: |
|---|
| 28 | # Add the time index field to entry string |
|---|
| 29 | if info_type.tag == 'TIME_INDEX': |
|---|
| 30 | entry_str += info_type.text + "," |
|---|
| 31 | # Get the incident number to entry string |
|---|
| 32 | if info_type.tag == "INCIDENT": |
|---|
| 33 | incident_num = info_type.attrib["LogNum"] |
|---|
| 34 | entry_str += " CAD Log, Incident #" + incident_num |
|---|
| 35 | # Get the cad data info |
|---|
| 36 | if info_type.tag == 'CAD_DATA': |
|---|
| 37 | # Loop through all tags under CAD_DATA |
|---|
| 38 | for event in info_type: |
|---|
| 39 | # Look for CAD_INCIDENT_EVENT |
|---|
| 40 | if event.tag == 'CAD_INCIDENT_EVENT': |
|---|
| 41 | # set flag to check if DETAIL field exist |
|---|
| 42 | detail = False |
|---|
| 43 | for info in event: |
|---|
| 44 | # Add the incident detail to entry string |
|---|
| 45 | if info.tag == "DETAIL": |
|---|
| 46 | # replace commas with whitespace |
|---|
| 47 | text = info.text.replace(",", " ") |
|---|
| 48 | # if it's not the first DETAIL field, |
|---|
| 49 | # append to the end without "Detail" text |
|---|
| 50 | if detail: |
|---|
| 51 | entry_str += "; " + text |
|---|
| 52 | else: |
|---|
| 53 | entry_str += ", Detail: " + text |
|---|
| 54 | detail = True |
|---|
| 55 | |
|---|
| 56 | # if there's detail field |
|---|
| 57 | # add it to the entries |
|---|
| 58 | if detail: |
|---|
| 59 | entries += entry_str + "\n" |
|---|
| 60 | # write all the entries from incident_sript.xml to the output file |
|---|
| 61 | out_file.write(entries) |
|---|
| 62 | |
|---|
| 63 | out_file = open(logfilepath + "caddetails.csv", "a") |
|---|
| 64 | # read in unifiedlog.csv and append to the output file |
|---|
| 65 | line = unified_log.readline() |
|---|
| 66 | while line: |
|---|
| 67 | out_file.write("0"+line) |
|---|
| 68 | line = unified_log.readline() |
|---|
| 69 | |
|---|
| 70 | out_file.close() |
|---|
| 71 | |
|---|
| 72 | # run the unix command line to sorting the file |
|---|
| 73 | os.system("sort -o " + logfilepath + "caddetails.csv " \ |
|---|
| 74 | + logfilepath + "caddetails.csv") |
|---|
| 75 | |
|---|