source: tmcsimulator/trunk/src/python/unifiedlogger/extend_unifiedlogger.py @ 545

Revision 545, 3.2 KB checked in by jdalbey, 6 years ago (diff)

extend_unifiedlogger.py Fixed defect - wasn't including time and incident fields.

Line 
1import xml.etree.ElementTree as ET
2import os, ConfigParser
3
4config = ConfigParser.ConfigParser()
5config.read('config/logging_service.cfg')
6logfilepath  = config.get('Paths', 'UnifiedLogPath')
7
8# read in the incident script in XML format
9tree = ET.parse(logfilepath + 'incident_script.xml')
10# root is TMC_SCRIPT tag
11root = tree.getroot()
12
13# entries contain all of the entry of cad incident from incident script
14entries = ""
15# create a new file for the combined entry from incident script and unifiedlog
16out_file = open(logfilepath + "caddetails.csv", "w")
17
18# read from the unifiedlog
19unified_log = open(logfilepath + "unifiedlog.csv", "r")
20
21# loop through tags under TMC_SCRIPT
22for 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_time =  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_incinum = " 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                        detailFlag = False
43                        detailText = ""
44                        for info in event:
45                            # Add the incident detail to entry string
46                            if info.tag == "DETAIL":
47                                # replace commas with whitespace
48                                infotext = info.text.replace(",", " ")
49                                # if it's the second DETAIL field,
50                                # we don't need the "Detail" label
51                                if not detailFlag:
52                                    detailText = ", Detail: "
53                                    detailFlag = True
54                                detailText += infotext                               
55                        # Build the complete line from the header and details
56                        entry_str += entry_time + entry_incinum + detailText                                   
57                        # if there's detail field add it to the entries
58                        if detailFlag:
59                            entries += entry_str + "\n"
60# write all the entries from incident_sript.xml to the output file
61out_file.write(entries)
62
63out_file = open(logfilepath + "caddetails.csv", "a")
64# read in unifiedlog.csv and append to the output file
65line = unified_log.readline()
66while line:
67    out_file.write("0"+line)
68    line = unified_log.readline()
69
70out_file.close()
71
72# run the unix command line to sorting the file
73os.system("sort -o " + logfilepath + "caddetails.csv " \
74    + logfilepath + "caddetails.csv")
75                           
Note: See TracBrowser for help on using the repository browser.