Skip to content

Commit e0aaed2

Browse files
authored
better parsing to avoid bugs when variables name contain whitespaces (gijzelaerr#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
1 parent 63e1599 commit e0aaed2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

snap7/util/db.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,23 @@ def parse_specification(db_specification: str) -> Dict[str, Any]:
149149
Parsed DB specification.
150150
"""
151151
parsed_db_specification = {}
152+
pattern = r"""
153+
(?P<index>\d+(\.\d+)?)\s+ # Match integer or decimal index
154+
(?P<var_name>.*?)\s+ # Non-greedy match for variable name
155+
(?P<_type>\S+)$ # Match type at end of line
156+
"""
157+
regex = re.compile(pattern, re.VERBOSE)
152158

153159
for line in db_specification.split("\n"):
154160
if line and not line.lstrip().startswith("#"):
155-
index, var_name, _type = line.lstrip().split("#")[0].split()
156-
parsed_db_specification[var_name] = (index, _type)
161+
match = regex.match(line.strip())
162+
if match:
163+
index = match.group("index")
164+
var_name = match.group("var_name")
165+
_type = match.group("_type")
166+
var_name = var_name.strip()
167+
168+
parsed_db_specification[var_name] = (index, _type)
157169

158170
return parsed_db_specification
159171

tests/test_util.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
12.0 testbool1 BOOL
5454
12.1 testbool2 BOOL
5555
12.2 testbool3 BOOL
56-
# 12.3 testbool4 BOOL
56+
# 12.3 test bool4 BOOL
5757
# 12.4 testbool5 BOOL
5858
# 12.5 testbool6 BOOL
5959
# 12.6 testbool7 BOOL
@@ -439,13 +439,27 @@ def test_db_creation(self) -> None:
439439
self.assertEqual(row["testbool2"], 1)
440440
self.assertEqual(row["testbool3"], 1)
441441
self.assertEqual(row["testbool4"], 1)
442-
443442
self.assertEqual(row["testbool5"], 0)
444443
self.assertEqual(row["testbool6"], 0)
445444
self.assertEqual(row["testbool7"], 0)
446445
self.assertEqual(row["testbool8"], 0)
447446
self.assertEqual(row["NAME"], "test")
448447

448+
def test_db_creation_vars_with_whitespace(self) -> None:
449+
test_array = bytearray(_bytearray * 1)
450+
test_spec = """
451+
50 testZeroSpaces BYTE
452+
52 testOne Space BYTE
453+
59 testTWo Spaces BYTE
454+
"""
455+
456+
test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=1, layout_offset=0, db_offset=0)
457+
db_export = test_db.export()
458+
for i in db_export:
459+
self.assertTrue("testZeroSpaces" in db_export[i].keys())
460+
self.assertTrue("testOne Space" in db_export[i].keys())
461+
self.assertTrue("testTWo Spaces" in db_export[i].keys())
462+
449463
def test_db_export(self) -> None:
450464
test_array = bytearray(_bytearray * 10)
451465
test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=10, layout_offset=4, db_offset=0)

0 commit comments

Comments
 (0)