Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Comma split for .sym enumerations #463

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/canmatrix/formats/sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from builtins import *

import attr
import re

import canmatrix
import canmatrix.utils
Expand Down Expand Up @@ -323,6 +324,8 @@ class Mode(object):
db.add_signal_defines("HexadecimalOutput", 'BOOL False True')
db.add_signal_defines("DisplayDecimalPlaces", 'INT 0 65535')
db.add_signal_defines("LongName", 'STR')

kvpair_splitting_pattern = re.compile(r'(\",)(\d+=)')

for line_count, line in enumerate(f, 1):
try:
Expand Down Expand Up @@ -360,7 +363,7 @@ class Mode(object):
line = line.split('//')[0]
temp_array = line[5:].strip().rstrip(')').split('(', 1)
val_table_name = temp_array[0]
split = canmatrix.utils.quote_aware_space_split(temp_array[1])
split = canmatrix.utils.quote_aware_space_split(re.sub(kvpair_splitting_pattern, r'\1 \2', temp_array[1]))
temp_array = [s.rstrip(',') for s in split]
temp_val_table = {}
for entry in temp_array:
Expand Down
58 changes: 58 additions & 0 deletions src/canmatrix/tests/test_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,61 @@ def tests_parse_float(variable_type, bit_length):
frame = matrix.frames[0]
signal = frame.signals[0]
assert signal.is_float


def tests_value_tables():
f = io.BytesIO(
textwrap.dedent(
'''\
FormatVersion=5.0 // Do not edit this line!
Title="AFE_CAN_ID0"

{ENUMS}
enum State(0="Power On Reset, and a quoted comma", 1=",Ready,set,go", 2="Following",3="Fault",
4="Forming" , 5="N/A", 6="N/A",7="N/A",8="N/A",9="N/A", 10="N/A",11="N/A",
12="N/A", 13="N/A",14="N/A", 15="N/A")
enum Relay(0="Open", 1="Closed",2="Error",3="N/A")

{SENDRECEIVE}
[StatusBits]
ID=0CFFC3F7h
Type=Extended
DLC=8
Var=State_status unsigned 0,16 /e:State
Var=MX2Permissive_status unsigned 32,16 /e:Relay
'''
).encode('utf-8'),
)

matrix = canmatrix.formats.sym.load(f)

assert matrix.load_errors == []

expected = {
'Relay': {
0: 'Open',
1: 'Closed',
2: 'Error',
3: 'N/A',
},
'State': {
0:"Power On Reset, and a quoted comma",
1: ",Ready,set,go",
2: "Following",
3: "Fault",
4: "Forming",
5: "N/A",
6: "N/A",
7: "N/A",
8: "N/A",
9: "N/A",
10: "N/A",
11: "N/A",
12: "N/A",
13: "N/A",
14: "N/A",
15: "N/A",
}
}

assert matrix.value_tables == expected
7 changes: 7 additions & 0 deletions src/canmatrix/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ def test_utils_guess_value():
assert canmatrix.utils.guess_value("False") == "0"
assert canmatrix.utils.guess_value("faLse") == "0"


def test_decode_number():
assert canmatrix.utils.decode_number("0x10") == 16
assert canmatrix.utils.decode_number("0b10") == 2
assert canmatrix.utils.decode_number("10") == 10


def test_quote_aware_comma_split():
example = '"a,b", c"d,e", f'
split = canmatrix.utils.quote_aware_comma_split(example)
assert split == ['a,b', 'c"d,e"', 'f']