Skip to content

Commit 3bdafa9

Browse files
Tomas NovakTomas Novak
Tomas Novak
authored and
Tomas Novak
committed
Squashed commit of the following:
commit 013449e Author: Eduard Bröcker <[email protected]> Date: Thu Oct 4 15:21:38 2018 +0200 Update canmatrix.py set default factor to 1, this makes more sense commit 5bceb9b Merge: 60a4c8c fe1fe10 Author: Kyle Altendorf <[email protected]> Date: Wed Oct 3 14:29:22 2018 -0400 Merge pull request #199 from altendky/198-error_for_colliding_mux_ids Error for colliding mux ids when loading .sym commit fe1fe10 Author: Kyle Altendorf <[email protected]> Date: Tue Oct 2 08:21:43 2018 -0400 Error for colliding mux ids when loading .sym commit 60a4c8c Author: Eduard Bröcker <[email protected]> Date: Mon Oct 1 15:54:39 2018 +0200 fix compare.py and make it more usefull Fix some missing api-changes Add some command-line switches to ignore custom fields of matrixes
1 parent 08b6fe6 commit 3bdafa9

File tree

4 files changed

+198
-49
lines changed

4 files changed

+198
-49
lines changed

src/canmatrix/canmatrix.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Signal(object):
113113
is_little_endian = attr.ib(type=bool, default = True)
114114
is_signed = attr.ib(type=bool, default = True)
115115
offset = attr.ib(converter = float_factory, default = float_factory(0.0))
116-
factor = attr.ib(converter = float_factory, default = float_factory(0.0))
116+
factor = attr.ib(converter = float_factory, default = float_factory(1.0))
117117

118118
# offset = attr.ib(converter = float_factory, default = 0.0)
119119

@@ -898,6 +898,8 @@ class CanMatrix(object):
898898
valueTables = attr.ib(default = attr.Factory(dict))
899899
envVars = attr.ib(default = attr.Factory(dict))
900900

901+
load_errors = attr.ib(factory=list)
902+
901903
def __iter__(self):
902904
return iter(self.frames)
903905

src/canmatrix/compare.py

+86-47
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,29 @@ def compareDb(db1, db2, ignore=None):
108108
temp._type = "Signal Defines"
109109
result.addChild(temp)
110110

111-
for vt1 in db1.valueTables:
112-
if vt1 not in db2.valueTables:
113-
result.addChild(
114-
compareResult(
115-
"deleted",
116-
"valuetable " + vt1,
117-
db1.valueTables))
118-
else:
119-
result.addChild(
120-
compareValueTable(
121-
db1.valueTables[vt1],
122-
db2.valueTables[vt1]))
111+
if "VALUETABLES" in ignore and ignore["VALUETABLES"]:
112+
pass
113+
else:
114+
for vt1 in db1.valueTables:
115+
if vt1 not in db2.valueTables:
116+
result.addChild(
117+
compareResult(
118+
"deleted",
119+
"valuetable " + vt1,
120+
db1.valueTables))
121+
else:
122+
result.addChild(
123+
compareValueTable(
124+
db1.valueTables[vt1],
125+
db2.valueTables[vt1]))
123126

124-
for vt2 in db2.valueTables:
125-
if vt2 not in db1.valueTables:
126-
result.addChild(
127-
compareResult(
128-
"added",
129-
"valuetable " + vt2,
130-
db2.valueTables))
127+
for vt2 in db2.valueTables:
128+
if vt2 not in db1.valueTables:
129+
result.addChild(
130+
compareResult(
131+
"added",
132+
"valuetable " + vt2,
133+
db2.valueTables))
131134

132135
propagateChanges(result)
133136

@@ -250,11 +253,12 @@ def compareAttributes(ele1, ele2, ignore=None):
250253
def compareBu(bu1, bu2, ignore=None):
251254
result = compareResult("equal", "ECU", bu1)
252255

253-
if bu1.comment != bu2.comment:
254-
result.addChild(
255-
compareResult(
256-
"changed", "ECU", bu1, [
257-
bu1.comment, bu2.comment]))
256+
if not "comment" in ignore:
257+
if bu1.comment != bu2.comment:
258+
result.addChild(
259+
compareResult(
260+
"changed", "ECU", bu1, [
261+
bu1.comment, bu2.comment]))
258262

259263
if ignore is not None and "ATTRIBUTE" in ignore and ignore[
260264
"ATTRIBUTE"] == "*":
@@ -293,15 +297,16 @@ def compareFrame(f1, f2, ignore=None):
293297
"extended-Flag: %d" %
294298
f1.extended, "extended-Flag: %d" %
295299
f2.extended]))
296-
if f2.comment is None:
297-
f2.addComment("")
298-
if f1.comment is None:
299-
f1.addComment("")
300-
if f1.comment != f2.comment:
301-
result.addChild(
302-
compareResult(
303-
"changed", "FRAME", f1, [
304-
"comment: " + f1.comment, "comment: " + f2.comment]))
300+
if not "comment" in ignore:
301+
if f2.comment is None:
302+
f2.addComment("")
303+
if f1.comment is None:
304+
f1.addComment("")
305+
if f1.comment != f2.comment:
306+
result.addChild(
307+
compareResult(
308+
"changed", "FRAME", f1, [
309+
"comment: " + f1.comment, "comment: " + f2.comment]))
305310

306311
for s2 in f2.signals:
307312
s1 = f1.signalByName(s2.name)
@@ -340,14 +345,14 @@ def compareFrame(f1, f2, ignore=None):
340345
def compareSignal(s1, s2, ignore=None):
341346
result = compareResult("equal", "SIGNAL", s1)
342347

343-
if s1.startbit != s2.startbit:
348+
if s1.startBit != s2.startBit:
344349
result.addChild(
345350
compareResult(
346351
"changed", "startbit", s1, [
347352
" %d" %
348353
s1.startbit, " %d" %
349354
s2.startbit]))
350-
if s1.signalsize != s2.signalsize:
355+
if s1.size != s2.size:
351356
result.addChild(
352357
compareResult(
353358
"changed", "signalsize", s1, [
@@ -396,17 +401,18 @@ def compareSignal(s1, s2, ignore=None):
396401
compareResult(
397402
"changed", "unit", s1, [
398403
s1.unit, s2.unit]))
399-
if s1.comment is not None and s2.comment is not None and s1.comment != s2.comment:
400-
if s1.comment.replace("\n", " ") != s2.comment.replace("\n", " "):
401-
result.addChild(
402-
compareResult(
403-
"changed", "comment", s1, [
404-
s1.comment, s2.comment]))
405-
else:
406-
result.addChild(
407-
compareResult(
408-
"changed", "comment", s1, [
409-
"only whitespaces differ", ""]))
404+
if not "comment" in ignore:
405+
if s1.comment is not None and s2.comment is not None and s1.comment != s2.comment:
406+
if s1.comment.replace("\n", " ") != s2.comment.replace("\n", " "):
407+
result.addChild(
408+
compareResult(
409+
"changed", "comment", s1, [
410+
s1.comment, s2.comment]))
411+
else:
412+
result.addChild(
413+
compareResult(
414+
"changed", "comment", s1, [
415+
"only whitespaces differ", ""]))
410416

411417
for receiver in s1.receiver:
412418
if receiver.strip() not in s2.receiver:
@@ -432,7 +438,10 @@ def compareSignal(s1, s2, ignore=None):
432438
else:
433439
result.addChild(compareAttributes(s1, s2, ignore))
434440

435-
result.addChild(compareValueTable(s1.values, s2.values))
441+
if "VALUETABLES" in ignore and ignore["VALUETABLES"]:
442+
pass
443+
else:
444+
result.addChild(compareValueTable(s1.values, s2.values))
436445

437446
return result
438447

@@ -498,6 +507,25 @@ def main():
498507
action="store_true",
499508
help="show list of frames",
500509
default=False)
510+
parser.add_option(
511+
"-c", "--comments",
512+
dest="check_comments",
513+
action="store_true",
514+
help="check changed comments",
515+
default=False)
516+
parser.add_option(
517+
"-a", "--attributes",
518+
dest="check_attributes",
519+
action="store_true",
520+
help="check changed attributes",
521+
default=False)
522+
parser.add_option(
523+
"-t", "--valueTable",
524+
dest="ignore_valuetables",
525+
action="store_true",
526+
help="check changed valuetables",
527+
default=False)
528+
501529
(cmdlineOptions, args) = parser.parse_args()
502530

503531
if len(args) < 2:
@@ -526,6 +554,17 @@ def main():
526554
logger.info("%d Frames found" % (db2.frames.__len__()))
527555

528556
ignore = {}
557+
558+
if not cmdlineOptions.check_comments:
559+
ignore["comment"] = "*"
560+
561+
if not cmdlineOptions.check_attributes:
562+
ignore["ATTRIBUTE"] = "*"
563+
564+
565+
if cmdlineOptions.ignore_valuetables:
566+
ignore["VALUETABLES"] = True
567+
529568
if cmdlineOptions.frames:
530569
onlyInMatrix1 = []
531570
onlyInMatrix2 = []

src/canmatrix/sym.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,48 @@
4141
enums = "{ENUMS}\n"
4242

4343

44+
@attr.s
45+
class ParsingError(Exception):
46+
line_number = attr.ib()
47+
line = attr.ib()
48+
message = attr.ib(default=None)
49+
original = attr.ib(default=None)
50+
51+
def __attrs_post_init__(self):
52+
if self.message is None:
53+
self.message = self.line
54+
55+
self.message = 'line {line_number}: {message}'.format(
56+
line_number=self.line_number,
57+
message=self.message
58+
)
59+
super(ParsingError, self).__init__(self.message)
60+
61+
def __str__(self):
62+
return self.message
63+
64+
65+
@attr.s
66+
class DuplicateMuxIdError(ParsingError):
67+
id = attr.ib(default=None)
68+
old = attr.ib(default=None)
69+
new = attr.ib(default=None)
70+
71+
def __attrs_post_init__(self):
72+
if None in (self.id, self.old, self.new):
73+
raise Exception('ack')
74+
75+
self.message = (
76+
'{id:X}h already in use'
77+
' (old: {old}, new: {new})'
78+
).format(
79+
id=self.id,
80+
old=self.old,
81+
new=self.new,
82+
)
83+
super(DuplicateMuxIdError, self).__attrs_post_init__()
84+
85+
4486
def format_float(f):
4587
s = str(f).upper()
4688
if s.endswith('.0'):
@@ -433,6 +475,14 @@ class Mode(object):
433475
multiplexor = int(multiplexor[:-1], 16)
434476
else:
435477
multiplexor = int(multiplexor)
478+
if multiplexor in frame.mux_names:
479+
raise DuplicateMuxIdError(
480+
id=multiplexor,
481+
old=frame.mux_names[multiplexor],
482+
new=sigName,
483+
line_number=linecount,
484+
line=line,
485+
)
436486
frame.mux_names[multiplexor] = sigName
437487
indexOffset = 2
438488

@@ -568,7 +618,17 @@ class Mode(object):
568618
# print line
569619
# else:
570620
# print "Unrecocniced line: " + l + " (%d) " % i
571-
except:
621+
except Exception as e:
622+
if not isinstance(e, ParsingError):
623+
ParsingError(
624+
message=str(e),
625+
line_number=linecount,
626+
line=line,
627+
original=e,
628+
)
629+
630+
db.load_errors.append(e)
631+
572632
logger.error("Error decoding line %d" % linecount)
573633
logger.error(line)
574634
# TODO: CAMPid 939921818394902983238

src/canmatrix/tests/test_sym.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import io
2+
import textwrap
3+
4+
import canmatrix.sym
5+
6+
7+
def test_colliding_mux_values():
8+
f = io.BytesIO(
9+
textwrap.dedent(
10+
'''\
11+
FormatVersion=5.0 // Do not edit this line!
12+
Title="a file"
13+
14+
{SEND}
15+
16+
[MuxedId]
17+
ID=0h
18+
Mux=TheMux 0,1 0h
19+
Var=Signal unsigned 1,1
20+
21+
[MuxedId]
22+
Mux=FirstMux 0,1 1h
23+
Var=Signal unsigned 1,1
24+
25+
[MuxedId]
26+
Mux=SecondMux 0,1 1h
27+
Var=Signal unsigned 1,1
28+
''',
29+
).encode('utf-8'),
30+
)
31+
32+
matrix = canmatrix.sym.load(f)
33+
error, = matrix.load_errors
34+
line_number = 16
35+
36+
assert len(matrix.load_errors) == 1
37+
38+
assert isinstance(error, canmatrix.sym.DuplicateMuxIdError)
39+
40+
assert error.line_number == line_number
41+
42+
error_string = str(error)
43+
assert error_string.startswith(
44+
'line {line_number}: '.format(line_number=line_number),
45+
)
46+
47+
assert 'FirstMux' in error_string
48+
assert 'SecondMux' in error_string

0 commit comments

Comments
 (0)