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 8 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
8 changes: 7 additions & 1 deletion snap7/util/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ def parse_specification(db_specification: str) -> Dict[str, Any]:

for line in db_specification.split("\n"):
if line and not line.lstrip().startswith("#"):
index, var_name, _type = line.lstrip().split("#")[0].split()
index, *notUsed, _type = line.lstrip().split("#")[0].split()
Novecento99 marked this conversation as resolved.
Show resolved Hide resolved

match = re.compile(r"(\s*)(\S+)(\s*)").findall(line)
Novecento99 marked this conversation as resolved.
Show resolved Hide resolved

var_name = "".join([m[0] + m[1] + m[2] for m in match[1:-1]])
Novecento99 marked this conversation as resolved.
Show resolved Hide resolved
var_name = var_name.strip()

parsed_db_specification[var_name] = (index, _type)

return parsed_db_specification
Expand Down
21 changes: 18 additions & 3 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 @@ -438,13 +438,28 @@ def test_db_creation(self) -> None:
self.assertEqual(row["testbool1"], 1)
self.assertEqual(row["testbool2"], 1)
self.assertEqual(row["testbool3"], 1)
self.assertEqual(row["testbool4"], 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)
Expand Down