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

better parsing to avoid bugs when variables name contain whitespaces #529

Merged
merged 16 commits into from
Jul 25, 2024
Merged
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
16 changes: 14 additions & 2 deletions snap7/util/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,23 @@ def parse_specification(db_specification: str) -> Dict[str, Any]:
Parsed DB specification.
"""
parsed_db_specification = {}
pattern = r"""
(?P<index>\d+(\.\d+)?)\s+ # Match integer or decimal index
(?P<var_name>.*?)\s+ # Non-greedy match for variable name
(?P<_type>\S+)$ # Match type at end of line
"""
regex = re.compile(pattern, re.VERBOSE)

for line in db_specification.split("\n"):
if line and not line.lstrip().startswith("#"):
index, var_name, _type = line.lstrip().split("#")[0].split()
parsed_db_specification[var_name] = (index, _type)
match = regex.match(line.strip())
if match:
index = match.group("index")
var_name = match.group("var_name")
_type = match.group("_type")
var_name = var_name.strip()

parsed_db_specification[var_name] = (index, _type)

return parsed_db_specification

Expand Down
18 changes: 16 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
12.0 testbool1 BOOL
12.1 testbool2 BOOL
12.2 testbool3 BOOL
# 12.3 testbool4 BOOL
# 12.3 test bool4 BOOL
# 12.4 testbool5 BOOL
# 12.5 testbool6 BOOL
# 12.6 testbool7 BOOL
Expand Down Expand Up @@ -439,13 +439,27 @@ def test_db_creation(self) -> None:
self.assertEqual(row["testbool2"], 1)
self.assertEqual(row["testbool3"], 1)
self.assertEqual(row["testbool4"], 1)

self.assertEqual(row["testbool5"], 0)
self.assertEqual(row["testbool6"], 0)
self.assertEqual(row["testbool7"], 0)
self.assertEqual(row["testbool8"], 0)
self.assertEqual(row["NAME"], "test")

def test_db_creation_vars_with_whitespace(self) -> None:
Novecento99 marked this conversation as resolved.
Show resolved Hide resolved
test_array = bytearray(_bytearray * 1)
test_spec = """
50 testZeroSpaces BYTE
52 testOne Space BYTE
59 testTWo Spaces BYTE
"""

test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=1, layout_offset=0, db_offset=0)
db_export = test_db.export()
for i in db_export:
self.assertTrue("testZeroSpaces" in db_export[i].keys())
self.assertTrue("testOne Space" in db_export[i].keys())
self.assertTrue("testTWo Spaces" in db_export[i].keys())

def test_db_export(self) -> None:
test_array = bytearray(_bytearray * 10)
test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=10, layout_offset=4, db_offset=0)
Expand Down
Loading