Skip to content

Commit

Permalink
better parsing to avoid bugs when variables name contain whitespaces (#…
Browse files Browse the repository at this point in the history
…529)

* better parsing to avoid bugs when variables name contain whitespaces

* better way to parse

* join needed to use as a dict key

* needed to re-insert the whitespace

* function to test the whitespace compatibility

* multiple whitespaces support (with test)

* 'text' to "text"

* fixes whitespaces

* new name for not used variable

* fix to avoid regex being recompiled at each call

* erased useless whitespaces

* regex to parse specification

* check if match is not none

* pre-commit job fix

* pre -commit job fix

* whitespaces
  • Loading branch information
Novecento99 authored Jul 25, 2024
1 parent 63e1599 commit e0aaed2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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:
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

0 comments on commit e0aaed2

Please sign in to comment.