-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·226 lines (197 loc) · 7.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# # # # #
# Controller Area Network debugger
# # #
# Reads data from files filled with CAN data dump in specific format (see docs)
# and allows manipulation with the data (several types)
# # # # #
import sys
import getopt # reading command line
import re
import csv
from datetime import datetime
from src.data_structure import CANNode, CANTestCase
from src.data_diff import CANDiff
def get_diff_data_str(src_tc, trg_tc, addr, outputgrp):
"""Also simple
"""
(src_diff, trg_diff) = CANDiff().get_diff_data(
src_tc, trg_tc, addr)
string = ''
if src_diff is not None:
string += "\n--- SOURCE: {0} ---\n".format(src_diff.addr)
for src_data in src_diff.data:
string += "{0}\n".format(src_data)
else:
string += "\n--- SOURCE: NONE ---\n"
if trg_diff is not None:
string += "\n--- TARGET: {0} ---\n".format(trg_diff.addr)
for trg_data in trg_diff.data:
string += "{0}\n".format(trg_data)
else:
string += "\n--- TARGET: NONE ---\n"
return string
def get_diffs_data_str(src_tc, trg_tc, outputgrp):
"""A simple wrap up for receiving basic diffs
Args:
src_tc: source test case
trg_tc: target test case
Returns:
formatted string of diff data
"""
diff_addrs = CANDiff().get_diff_addrs(src_tc, trg_tc)
string = ''
for addr in diff_addrs:
string += get_diff_data_str(src_tc, trg_tc, addr, outputgrp)
return string
def quote(string):
return '"{}"'.format(string)
def get_diff_data_csv(src_tc, trg_tc, outputgrp):
"""A little less simple wrap up for receiving basic diffs for csv
Args:
src_tc: source test case
trg_tc: target test case
Returns:
formatted array for csv writer
"""
data = CANDiff().get_diffs_data(src_tc, trg_tc)
if outputgrp:
data_list = [
[src_tc.filename, trg_tc.filename, "Address", "Data",
"Count", "Time"]]
else:
data_list = [
[src_tc.filename, trg_tc.filename, "Address", "Data", "Time"]]
# dirty - needs refactoring
for src_diff, trg_diff in data:
if src_diff is not None:
if outputgrp:
last_data = src_diff.data[0].data
last_data_count = 1
for src_data in src_diff.data:
if src_data.data == last_data:
last_data_count += 1
else:
data_list.append(["Yes", "No", src_diff.addr,
src_data.data, last_data_count,
src_data.time])
last_data_count = 1
last_data = src_data.data
else:
for src_data in src_diff.data:
data_list.append(
["Yes", "No", src_diff.addr, src_data.data,
src_data.time])
if trg_diff is not None:
if outputgrp:
last_data = trg_diff.data[0].data
last_data_count = 1
for trg_data in trg_diff.data:
if trg_data.data == last_data:
last_data_count += 1
else:
data_list.append(["No", "Yes", trg_diff.addr,
trg_data.data, last_data_count,
trg_data.time])
last_data_count = 1
last_data = trg_data.data
else:
for trg_data in trg_diff.data:
data_list.append(
["No", "Yes", trg_diff.addr, trg_data.data,
trg_data.time])
return data_list
def save_as_text(f, src_tc, trg_tc, outputgrp):
f.write("Addresses that differ:\n")
f.write(str(diff_addrs) + "\n")
f.write(get_diffs_data_str(src_tc, trg_tc, outputgrp))
def save_as_csv(f, src_tc, trg_tc, outputgrp):
writer = csv.writer(f, quotechar='\"', quoting=csv.QUOTE_ALL)
writer.writerows(get_diff_data_csv(src_tc, trg_tc, outputgrp))
def console_interactive_mode(src_tc, trg_tc, diff_addrs, outputgrp):
"""Interactive mode via console
"""
print("Addresses that have different data:")
print(diff_addrs)
addr_chosen = raw_input("Which address to display?: ")
while addr_chosen in diff_addrs or addr_chosen == "all":
if addr_chosen == "all":
print(get_diffs_data_str(src_tc, trg_tc, outputgrp))
else:
print(get_diff_data_str(src_tc, trg_tc, addr_chosen, outputgrp))
print(diff_addrs)
addr_chosen = raw_input("Which address to display?: ")
def print_usage():
print(
"{0} [-s <sourcepath>] [-t <targetpath>] \
[-o <outputpath>] [-f <format>]".format(__file__))
print("\t-s source (root) file path")
print("\t-t comparable file path")
print("\t-o difference output file path")
print("\t-f format for output file. Formats:")
print("\t\ttext - textual information format")
print("\t\tcsv - spreadsheet information format")
print("\t-h display this help")
print("\t-g group data")
if __name__ == "__main__":
sourcefile = ''
targetfile = ''
outputfile = ''
outputfrmt = 'text'
outputgrp = False
"""
Option list:
-s or --source for sourcefile
-t or --target for targetfile
-o or --output for outputfile
-f or --format for output file format
-h for help
"""
try:
optlist, args = getopt.getopt(sys.argv[1:], "s:t:o:f:hg",
["source=", "target=",
"output=", "format=",
"help", "group"])
except getopt.GetoptError as err:
print str(err)
print_usage()
sys.exit(2)
for o, a in optlist:
if o in ("-s", "--source"):
sourcefile = a
elif o in ("-t", "--target"):
targetfile = a
elif o in ("-o", "--output"):
outputfile = a
elif o in ("-f", "--format"):
# since there are only two formats currently,
# set text to default
if a == "csv":
outputfrmt = a
elif o in ("-g", "--group"):
outputgrp = True
else:
print_usage()
if o in ("-h", "--help"):
sys.exit(0)
else:
sys.exit(2)
# mandatory
while not sourcefile:
sourcefile = raw_input("Source file:")
# mandatory
while not targetfile:
targetfile = raw_input("Target file:")
source_tc = CANTestCase(sourcefile)
target_tc = CANTestCase(targetfile)
diff_addrs = CANDiff().get_diff_addrs(source_tc, target_tc)
if outputfile:
with open(outputfile, "wb") as f:
if outputfrmt == "text":
save_as_text(f, source_tc, target_tc, outputgrp)
else:
save_as_csv(f, source_tc, target_tc, outputgrp)
print("Saved as '{1}' type to {0}".format(outputfile, outputfrmt))
else:
console_interactive_mode(source_tc, target_tc, diff_addrs, outputgrp)