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

Revision 542, 3.0 KB checked in by liquan, 6 years ago (diff)

Added extend_unifiedlogger.py for combining incident_script.xml and unifiedlog.csv entries.

Line 
1import xml.etree.ElementTree as ET
2import os, ConfigParser
3
4
5config = ConfigParser.ConfigParser()
6config.read('config/logging_service.cfg')
7logfilepath  = config.get('Paths', 'UnifiedLogPath')
8
9# read in the incident script in XML format
10tree = ET.parse(logfilepath + 'incident_script.xml')
11# root is TMC_SCRIPT tag
12root = tree.getroot()
13
14# entries contain all of the entry of cad incident from incident script
15entries = ""
16# create a new file for the combined entry from incident script and unifiedlog
17out_file = open(logfilepath + "caddetails.csv", "w")
18
19# read from the unifiedlog
20unified_log = open(logfilepath + "unifiedlog.csv", "r")
21
22# loop through tags under TMC_SCRIPT
23for 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
63out_file.write(entries)
64
65out_file = open(logfilepath + "caddetails.csv", "a")
66# read in unifiedlog.csv and append to the output file
67line = unified_log.readline()
68while line:
69    out_file.write("0"+line)
70    line = unified_log.readline()
71
72out_file.close()
73
74# run the unix command line to sorting the file
75os.system("sort -o " + logfilepath + "caddetails.csv " \
76    + logfilepath + "caddetails.csv")
Note: See TracBrowser for help on using the repository browser.