Skip to content

Commit 961918a

Browse files
committed
fix for #430
1 parent e70399f commit 961918a

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/canmatrix/canmatrix.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,9 @@ def add_attribute(self, attribute, value):
985985
:param any value: attribute value
986986
"""
987987
try:
988-
self.attributes[attribute] = str(value)
988+
self.attributes[attribute] = str(value).strip()
989989
except UnicodeDecodeError:
990-
self.attributes[attribute] = value
990+
self.attributes[attribute] = value.strip()
991991

992992
def del_attribute(self, attribute):
993993
# type: (str) -> typing.Any

src/canmatrix/formats/dbc.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,12 @@ def dump(in_db, f, **options):
250250
if len(db.frames) > 0:
251251
if max([x.cycle_time for x in db.frames]) > 0:
252252
db.add_frame_defines("GenMsgCycleTime", 'INT 0 65535')
253-
if max([x.cycle_time for y in db.frames for x in y.signals]) > 0:
254-
db.add_signal_defines("GenSigCycleTime", 'INT 0 65535')
253+
if len(db.signals) > 0:
254+
if max([x.cycle_time for y in db.frames for x in y.signals]) > 0:
255+
db.add_signal_defines("GenSigCycleTime", 'INT 0 65535')
255256

256-
if max([x.initial_value for y in db.frames for x in y.signals]) > 0 or min([x.initial_value for y in db.frames for x in y.signals]) < 0:
257-
db.add_signal_defines("GenSigStartValue", 'FLOAT 0 100000000000')
257+
if max([x.initial_value for y in db.frames for x in y.signals]) > 0 or min([x.initial_value for y in db.frames for x in y.signals]) < 0:
258+
db.add_signal_defines("GenSigStartValue", 'FLOAT 0 100000000000')
258259

259260

260261

@@ -786,7 +787,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
786787
substring = decoded[7:].strip()
787788
define_type = substring[:3]
788789
substring = substring[3:].strip()
789-
pattern = r"^\"(.+?)\" +(.+);"
790+
pattern = r"^\"(.+?)\" +(.+); *"
790791
regexp = re.compile(pattern)
791792
regexp_raw = re.compile(pattern.encode(dbc_import_encoding))
792793
temp = regexp.match(substring)
@@ -817,45 +818,45 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
817818
tempba = regexp.match(decoded)
818819

819820
if tempba.group(1).strip().startswith("BO_ "):
820-
regexp = re.compile(r"^BA_ +\"(.+?)\" +BO_ +(\d+) +(.+) *;")
821+
regexp = re.compile(r"^BA_ +\"(.+?)\" +BO_ +(\d+) +(.+) *; *")
821822
temp = regexp.match(decoded)
822823
get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(2)))).add_attribute(
823824
temp.group(1), temp.group(3))
824825
elif tempba.group(1).strip().startswith("SG_ "):
825-
regexp = re.compile(r"^BA_ +\"(.+?)\" +SG_ +(\d+) +(\w+) +(.+) *;")
826+
regexp = re.compile(r"^BA_ +\"(.+?)\" +SG_ +(\d+) +(\w+) +(.+) *; *")
826827
temp = regexp.match(decoded)
827828
if temp is not None:
828829
get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(2)))).signal_by_name(
829830
temp.group(3)).add_attribute(temp.group(1), temp.group(4))
830831
elif tempba.group(1).strip().startswith("EV_ "):
831-
regexp = re.compile(r"^BA_ +\"(.+?)\" +EV_ +(\w+) +(.*) *;")
832+
regexp = re.compile(r"^BA_ +\"(.+?)\" +EV_ +(\w+) +(.*) *; *")
832833
temp = regexp.match(decoded)
833834
if temp is not None:
834835
db.add_env_attribute(temp.group(2), temp.group(1), temp.group(3))
835836
elif tempba.group(1).strip().startswith("BU_ "):
836-
regexp = re.compile(r"^BA_ +\"(.*?)\" +BU_ +(\w+) +(.+) *;")
837+
regexp = re.compile(r"^BA_ +\"(.*?)\" +BU_ +(\w+) +(.+) *; *")
837838
temp = regexp.match(decoded)
838839
db.ecu_by_name(
839840
temp.group(2)).add_attribute(
840841
temp.group(1),
841842
temp.group(3))
842843
else:
843844
regexp = re.compile(
844-
r"^BA_ +\"([A-Za-z0-9\-_]+)\" +([\"\w\-\.]+) *;")
845+
r"^BA_ +\"([A-Za-z0-9\-_]+)\" +([\"\w\-\.]+) *; *")
845846
temp = regexp.match(decoded)
846847
if temp:
847848
db.add_attribute(temp.group(1), temp.group(2))
848849

849850
elif decoded.startswith("SIG_GROUP_ "):
850-
regexp = re.compile(r"^SIG_GROUP_ +(\w+) +(\w+) +(\w+) +\:(.*) *;")
851+
regexp = re.compile(r"^SIG_GROUP_ +(\w+) +(\w+) +(\w+) +\:(.*) *; *")
851852
temp = regexp.match(decoded)
852853
frame = get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(1))))
853854
if frame is not None:
854855
signal_array = temp.group(4).split(' ')
855856
frame.add_signal_group(temp.group(2), temp.group(3), signal_array) # todo wrong annotation in canmatrix? Id is a string?
856857

857858
elif decoded.startswith("SIG_VALTYPE_ "):
858-
regexp = re.compile(r"^SIG_VALTYPE_ +(\w+) +(\w+)\s*\:(.*) *;")
859+
regexp = re.compile(r"^SIG_VALTYPE_ +(\w+) +(\w+)\s*\:(.*) *; *")
859860
temp = regexp.match(decoded)
860861
frame = get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(temp.group(1))))
861862
if frame:
@@ -873,7 +874,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
873874
db.add_define_default(temp.group(1),
874875
temp_raw.group(2).decode(dbc_import_encoding))
875876
elif decoded.startswith("SG_MUL_VAL_ "):
876-
pattern = r"^SG_MUL_VAL_ +([0-9]+) +([\w\-]+) +([\w\-]+) +(.*) *;"
877+
pattern = r"^SG_MUL_VAL_ +([0-9]+) +([\w\-]+) +([\w\-]+) +(.*) *; *"
877878
regexp = re.compile(pattern)
878879
temp = regexp.match(decoded)
879880
if temp:
@@ -892,7 +893,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
892893
mux_val_max_number = int(mux_val_max)
893894
signal.mux_val_grp.append([mux_val_min_number, mux_val_max_number])
894895
elif decoded.startswith("EV_ "):
895-
pattern = r"^EV_ +([\w\-\_]+?) *\: +([0-9]+) +\[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] +\"(.*?)\" +([0-9.+\-eE]+) +([0-9.+\-eE]+) +([\w\-]+?) +(.*);"
896+
pattern = r"^EV_ +([\w\-\_]+?) *\: +([0-9]+) +\[([0-9.+\-eE]+)\|([0-9.+\-eE]+)\] +\"(.*?)\" +([0-9.+\-eE]+) +([0-9.+\-eE]+) +([\w\-]+?) +(.*); *"
896897
regexp = re.compile(pattern)
897898
temp = regexp.match(decoded)
898899

src/canmatrix/tests/test_dbc.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,14 @@ def test_j1939_frametype():
333333
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
334334
assert matrix.frames[0].is_j1939 == False
335335

336-
def test_signal_definition_with_spaces_iss358():
337-
dbc = io.BytesIO(textwrap.dedent(u'''\
338-
BU_: someOtherEcu
339336

340-
BO_ 123 someFrame: 1 someOtherEcu
341-
SG_ AccSts : 62|3@0+ (1.0, 0.0) [0.0|0.0] "" VDDM
337+
def test_attributes_with_spaces_before_semicolumn():
338+
dbc = io.BytesIO(textwrap.dedent(u'''\
339+
BO_ 8 Frame_1: 8 Vector__XXX
340+
BA_ "someAttribute" BO_ 8 "str" ;
342341
''').encode('utf-8'))
343342
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
344-
343+
assert matrix.frames[0].attributes["someAttribute"] == '"str"'
345344

346345
def test_cycle_time_handling():
347346
dbc = io.BytesIO(textwrap.dedent(u'''\

0 commit comments

Comments
 (0)