Skip to content

Commit 348d277

Browse files
committed
repair regexp for attributes extraction out of dbc #330
1 parent 3f5297b commit 348d277

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/canmatrix/formats/dbc.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def add_frame_by_id(frame): # type: (canmatrix.Frame) -> None
472472
if l.__len__() == 0:
473473
continue
474474
try:
475-
#if 1==1:
475+
# if 1==1:
476476
if followUp == FollowUps.signalComment:
477477
try:
478478
comment += "\n" + \
@@ -792,18 +792,18 @@ def add_frame_by_id(frame): # type: (canmatrix.Frame) -> None
792792
tempba = regexp.match(decoded)
793793

794794
if tempba.group(1).strip().startswith("BO_ "):
795-
regexp = re.compile(r"^BA_ +\"(.*)\" +BO_ +(.+) +(.+);")
795+
regexp = re.compile(r"^BA_ +\"(.+)\" +BO_ +(\d+) +(.+);")
796796
temp = regexp.match(decoded)
797797
get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(2)))).add_attribute(
798798
temp.group(1), temp.group(3))
799799
elif tempba.group(1).strip().startswith("SG_ "):
800-
regexp = re.compile(r"^BA_ +\"(.*)\" +SG_ +(.+) +(.+) +(.+);")
800+
regexp = re.compile(r"^BA_ +\"(.+)\" +SG_ +(\d+) +(\w+) +(.+);")
801801
temp = regexp.match(decoded)
802802
if temp != None:
803803
get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(2)))).signal_by_name(
804804
temp.group(3)).add_attribute(temp.group(1), temp.group(4))
805805
elif tempba.group(1).strip().startswith("EV_ "):
806-
regexp = re.compile(r"^BA_ +\"(.*)\" +EV_ +(.+) +(.+);")
806+
regexp = re.compile(r"^BA_ +\"(.+)\" +EV_ +(\w+) +(.*);")
807807
temp = regexp.match(decoded)
808808
if temp != None:
809809
db.add_env_attribute(temp.group(2),temp.group(1),temp.group(3))
@@ -867,7 +867,7 @@ def add_frame_by_id(frame): # type: (canmatrix.Frame) -> None
867867
signal.mux_val_min = muxValMin
868868
signal.mux_val_max = muxValMax
869869
elif decoded.startswith("EV_ "):
870-
pattern = r"^EV_ +([\w\-]+) *\: +([0-9]+) +\[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] +\"(\w*)\" +([0-9.+\-eE]+) +([0-9.+\-eE]+) +([\w\-]+) +(.*);"
870+
pattern = r"^EV_ +([\w\-\_]+) *\: +([0-9]+) +\[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] +\"(.*?)\" +([0-9.+\-eE]+) +([0-9.+\-eE]+) +([\w\-]+) +(.*);"
871871
regexp = re.compile(pattern)
872872
temp = regexp.match(decoded)
873873

src/canmatrix/tests/test_dbc.py

+17
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,14 @@ def test_defines_with_spaces():
233233
234234
BO_ 123 someFrame: 1 someOtherEcu
235235
236+
EV_ someEnvVar: 0 [0|0] "" 0 2 DUMMY_NODE_VECTOR0 Vector__XXX;
237+
236238
BA_DEF_ BU_ "Node Address" INT 0 255;
237239
BA_DEF_ BO_ "Period [ms]" INT 0 5000;
240+
BA_DEF_ EV_ "some attrib" STRING;
238241
BA_ "Node Address" BU_ someOtherEcu 42;
239242
BA_ "Period [ms]" BO_ 123 3000;
243+
BA_ "some attrib" EV_ someEnvVar "some space";
240244
''').encode('utf-8'))
241245
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
242246
assert matrix.ecu_defines["Node Address"].type == "INT"
@@ -245,4 +249,17 @@ def test_defines_with_spaces():
245249
assert matrix.frame_defines["Period [ms]"].min == 0
246250
assert matrix.frame_defines["Period [ms]"].max == 5000
247251
assert matrix.frames[0].attributes["Period [ms]"] == '3000'
252+
assert matrix.env_vars["someEnvVar"]["attributes"]["some attrib"] == '"some space"'
248253

254+
def test_defines_with_special_cars():
255+
dbc = io.BytesIO(textwrap.dedent(u'''\
256+
BU_: someOtherEcu
257+
258+
BO_ 123 someFrame: 1 someOtherEcu
259+
SG_ someSignal: 1|2@0+ (1,0) [0|0] "" CCL_TEST
260+
261+
BA_DEF_ SG_ "Accuracy" STRING;
262+
BA_ "Accuracy" SG_ 123 someSignal "+/- 10.2 at 55.1%";
263+
''').encode('utf-8'))
264+
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
265+
assert matrix.frames[0].signals[0].attributes["Accuracy"] == "+/- 10.2 at 55.1%"

0 commit comments

Comments
 (0)