Skip to content

Commit b0b36bf

Browse files
committed
copy_frame handles default values now (fix #430)
1 parent fcd1b37 commit b0b36bf

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/canmatrix/copy.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,16 @@ def copy_frame(frame_id, source_db, target_db):
170170
copy_ecu(source_ecu, source_db, target_db)
171171

172172
# copy all frame-defines
173-
attributes = frame.attributes
174-
for attribute in attributes:
173+
for attribute in source_db.frame_defines:
175174
if attribute not in target_db.frame_defines:
176175
target_db.add_frame_defines(
177176
copy.deepcopy(attribute), copy.deepcopy(source_db.frame_defines[attribute].definition))
178177
target_db.add_define_default(
179178
copy.deepcopy(attribute), copy.deepcopy(source_db.frame_defines[attribute].defaultValue))
179+
# only default value exists in source but is different to default value in target
180+
if attribute not in frame.attributes and \
181+
frame.attribute(attribute, source_db) != frame.attribute(attribute, target_db):
182+
target_db.frame_by_id(frame.arbitration_id).add_attribute(attribute, frame.attribute(attribute, source_db))
180183
# update enum data types if needed:
181184
if source_db.frame_defines[attribute].type == 'ENUM':
182185
temp_attr = frame.attribute(attribute, db=source_db)
@@ -187,7 +190,7 @@ def copy_frame(frame_id, source_db, target_db):
187190
# trigger all signals of Frame
188191
for sig in frame.signals:
189192
# delete all 'unknown' attributes
190-
for attribute in sig.attributes:
193+
for attribute in source_db.signal_defines:
191194
target_db.add_signal_defines(
192195
copy.deepcopy(attribute), copy.deepcopy(source_db.signal_defines[attribute].definition))
193196
target_db.add_define_default(
@@ -198,5 +201,9 @@ def copy_frame(frame_id, source_db, target_db):
198201
if temp_attr not in target_db.signal_defines[attribute].values:
199202
target_db.signal_defines[attribute].values.append(copy.deepcopy(temp_attr))
200203
target_db.signal_defines[attribute].update()
204+
# only default value exists in source but is different to default value in target
205+
if attribute not in sig.attributes and \
206+
sig.attribute(attribute, source_db) != sig.attribute(attribute, target_db):
207+
target_db.frame_by_id(frame.arbitration_id).signal_by_name(sig.name).add_attribute(attribute, sig.attribute(attribute, source_db))
201208

202209
return True

src/canmatrix/formats/dbc.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
509509
comment += "\n" + l.decode(dbc_comment_encoding).replace('\\"', '"')
510510
except:
511511
logger.error("Error decoding line: %d (%s)" % (i, line))
512-
if re.match('.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
512+
if re.match(r'.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
513513
follow_up = _FollowUps.NOTHING
514514
if signal is not None:
515515
signal.add_comment(comment[:-1].strip()[:-1])
@@ -519,7 +519,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
519519
comment += "\n" + l.decode(dbc_comment_encoding).replace('\\"', '"')
520520
except:
521521
logger.error("Error decoding line: %d (%s)" % (i, line))
522-
if re.match('.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
522+
if re.match(r'.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
523523
follow_up = _FollowUps.NOTHING
524524
if frame is not None:
525525
frame.add_comment(comment[:-1].strip()[:-1])
@@ -530,7 +530,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
530530
l.decode(dbc_comment_encoding).replace('\\"', '"')
531531
except:
532532
logger.error("Error decoding line: %d (%s)" % (i, line))
533-
if re.match('.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
533+
if re.match(r'.*" *;\Z',l.decode(dbc_import_encoding).strip()) is not None:
534534
follow_up = _FollowUps.NOTHING
535535
if board_unit is not None:
536536
board_unit.add_comment(comment[:-1].strip()[:-1])

src/canmatrix/tests/test_copy.py

+28
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,31 @@ def test_copy_ecu_with_attributes():
7575
assert len(matrix1.ecus) == 1
7676
assert matrix1.ecu_by_name("ECU") is not None
7777
assert matrix1.ecu_by_name("ECU").attribute("Node Address") == 42
78+
79+
def test_copy_frame_default_attributes():
80+
source = canmatrix.canmatrix.CanMatrix()
81+
frame1 = canmatrix.canmatrix.Frame("Frame1", arbitration_id=1)
82+
signal = canmatrix.canmatrix.Signal("Signal1")
83+
frame1.add_signal(canmatrix.canmatrix.Signal("SomeSignal"))
84+
frame1.add_signal(signal)
85+
source.add_frame(frame1)
86+
source.add_frame_defines("some_attribute", "STRING")
87+
source.add_define_default("some_attribute", "source_frame_default")
88+
source.add_signal_defines("some_signal_attribute", "STRING")
89+
source.add_define_default("some_signal_attribute", "source_sig_default")
90+
91+
#test if default value only defined in source and copied to target
92+
target = canmatrix.canmatrix.CanMatrix()
93+
canmatrix.copy.copy_frame(frame1.arbitration_id, source, target)
94+
assert target.frames[0].attribute("some_attribute", target) == "source_frame_default"
95+
assert target.frames[0].signals[0].attribute("some_signal_attribute", target) == "source_sig_default"
96+
97+
# test if define already exists, but has another default value:
98+
target2 = canmatrix.canmatrix.CanMatrix()
99+
target2.add_frame_defines("some_attribute", "STRING")
100+
target2.add_define_default("some_attribute", "target_frame_default")
101+
target2.add_signal_defines("some_signal_attribute", "STRING")
102+
target2.add_define_default("some_signal_attribute", "target_sig_default")
103+
canmatrix.copy.copy_frame(frame1.arbitration_id, source, target2)
104+
assert target2.frames[0].attribute("some_attribute", target2) == "source_frame_default"
105+
assert target2.frames[0].signals[0].attribute("some_signal_attribute", target2) == "source_sig_default"

0 commit comments

Comments
 (0)