diff --git a/snap7/util/db.py b/snap7/util/db.py index 2b17f72a..7c84261f 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -149,11 +149,23 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: Parsed DB specification. """ parsed_db_specification = {} + pattern = r""" + (?P\d+(\.\d+)?)\s+ # Match integer or decimal index + (?P.*?)\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 diff --git a/tests/test_util.py b/tests/test_util.py index 583ce959..299172ad 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -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 @@ -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)