Warning: Can't use blame annotator:
svn blame failed on trunk/src/python/unifiedlogger/extend_unifiedlogger.py: ("Can't find a temporary directory: Internal error", 20014)

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

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

Replace commas in DETAIL text with whitespace. Remove the text "Detail" for the second DETAIL field.

RevLine 
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_test.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_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
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.