From 54969edfaac9e6458fbf16922261b3ef7a73c1e3 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 09:38:59 +0200 Subject: [PATCH 01/16] better parsing to avoid bugs when variables name contain whitespaces --- snap7/util/db.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 2b17f72a..417f302d 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -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() + parsed_text = line.lstrip().split("#")[0].split() + + index = parsed_text[0] + _type = parsed_text[-1] + var_name_list = parsed_text[1:-1] + var_name = ''.join(var_name_list) + parsed_db_specification[var_name] = (index, _type) return parsed_db_specification From 99679b80b2e294a1715e556cd6f324f942ce1fa7 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:24:04 +0200 Subject: [PATCH 02/16] better way to parse --- snap7/util/db.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 417f302d..bebe4ac4 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -152,12 +152,7 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: for line in db_specification.split("\n"): if line and not line.lstrip().startswith("#"): - parsed_text = line.lstrip().split("#")[0].split() - - index = parsed_text[0] - _type = parsed_text[-1] - var_name_list = parsed_text[1:-1] - var_name = ''.join(var_name_list) + index, *var_name, _type = line.lstrip().split("#")[0].split() parsed_db_specification[var_name] = (index, _type) From 711b75932b326f7e8c8aa30e7833cc0eb32f9c99 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:24:27 +0200 Subject: [PATCH 03/16] join needed to use as a dict key --- snap7/util/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index bebe4ac4..0a3e1c03 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -153,7 +153,7 @@ 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() - + var_name = "".join(var_name) parsed_db_specification[var_name] = (index, _type) return parsed_db_specification From 84e8cd231d1c6add48f47adac8c13378886a29f0 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:21:55 +0200 Subject: [PATCH 04/16] needed to re-insert the whitespace --- snap7/util/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 0a3e1c03..5f0f321e 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -153,7 +153,7 @@ 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() - var_name = "".join(var_name) + var_name = " ".join(var_name) parsed_db_specification[var_name] = (index, _type) return parsed_db_specification From 09a6a7d1d58bb93b9e4561a3b5b15e9439720523 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:22:09 +0200 Subject: [PATCH 05/16] function to test the whitespace compatibility --- tests/test_util.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index 583ce959..9826623c 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 @@ -445,6 +445,21 @@ def test_db_creation(self) -> None: 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 = """ +52 test Byte BYTE +57 supportByte BYTE +""" + + test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=1, layout_offset=0, db_offset=0) + + exp = test_db.export() + print(exp[0].keys()) + + self.assertTrue("test Byte" in exp[0].keys()) + def test_db_export(self) -> None: test_array = bytearray(_bytearray * 10) From f45d60eda85c5ed56a90b7d571ce6405349c9b77 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:53:26 +0200 Subject: [PATCH 06/16] multiple whitespaces support (with test) --- snap7/util/db.py | 9 +++++++-- tests/test_util.py | 12 +++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 5f0f321e..6996d89b 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -152,8 +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() - var_name = " ".join(var_name) + index, *notUsed, _type = line.lstrip().split("#")[0].split() + + match = re.compile(r'(\s*)(\S+)(\s*)').findall(line) + + var_name = ''.join([m[0] + m[1] + m[2] for m in match[1:-1]]) + 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 9826623c..bd41bfca 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -449,16 +449,18 @@ def test_db_creation(self) -> None: def test_db_creation_vars_with_whitespace(self) -> None: test_array = bytearray(_bytearray * 1) test_spec = """ -52 test Byte BYTE -57 supportByte BYTE + 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) exp = test_db.export() - print(exp[0].keys()) - - self.assertTrue("test Byte" in exp[0].keys()) + + self.assertTrue("testZeroSpaces" in exp[0].keys()) + self.assertTrue("testOne Space" in exp[0].keys()) + self.assertTrue("testTWo Spaces" in exp[0].keys()) def test_db_export(self) -> None: From bb4b2e71e0d2ca2d99171572e241be2d14ef5320 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:26:17 +0200 Subject: [PATCH 07/16] 'text' to "text" --- snap7/util/db.py | 4 ++-- tests/test_util.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 6996d89b..1ffc1269 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -154,9 +154,9 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: if line and not line.lstrip().startswith("#"): index, *notUsed, _type = line.lstrip().split("#")[0].split() - match = re.compile(r'(\s*)(\S+)(\s*)').findall(line) + match = re.compile(r"(\s*)(\S+)(\s*)").findall(line) - var_name = ''.join([m[0] + m[1] + m[2] for m in match[1:-1]]) + var_name = "".join([m[0] + m[1] + m[2] for m in match[1:-1]]) var_name = var_name.strip() parsed_db_specification[var_name] = (index, _type) diff --git a/tests/test_util.py b/tests/test_util.py index bd41bfca..e1e08cc7 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -456,11 +456,12 @@ def test_db_creation_vars_with_whitespace(self) -> None: test_db = DB(1, test_array, test_spec, row_size=len(_bytearray), size=1, layout_offset=0, db_offset=0) - exp = test_db.export() + db_export = test_db.export() - self.assertTrue("testZeroSpaces" in exp[0].keys()) - self.assertTrue("testOne Space" in exp[0].keys()) - self.assertTrue("testTWo Spaces" in exp[0].keys()) + 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: From b19a0e921802bfaefe8ae4a6b096676c16b959f3 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Fri, 19 Jul 2024 08:20:56 +0200 Subject: [PATCH 08/16] fixes whitespaces --- tests/test_util.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index e1e08cc7..ce8716d0 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -438,8 +438,7 @@ 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) @@ -452,12 +451,10 @@ def test_db_creation_vars_with_whitespace(self) -> None: 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()) From 281de6c739e8cfeb6ab5289fd838bc4a69c9eed6 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:05:03 +0200 Subject: [PATCH 09/16] new name for not used variable --- snap7/util/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 1ffc1269..d8573906 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -152,7 +152,7 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: for line in db_specification.split("\n"): if line and not line.lstrip().startswith("#"): - index, *notUsed, _type = line.lstrip().split("#")[0].split() + index, *_, _type = line.lstrip().split("#")[0].split() match = re.compile(r"(\s*)(\S+)(\s*)").findall(line) From 45625f306b89e641eeec3f18d55d431e3db1ed0a Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:22:16 +0200 Subject: [PATCH 10/16] fix to avoid regex being recompiled at each call --- snap7/util/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index d8573906..f2d1a79e 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -154,7 +154,7 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: if line and not line.lstrip().startswith("#"): index, *_, _type = line.lstrip().split("#")[0].split() - match = re.compile(r"(\s*)(\S+)(\s*)").findall(line) + match = re.findall(r"(\s*)(\S+)(\s*)",line) var_name = "".join([m[0] + m[1] + m[2] for m in match[1:-1]]) var_name = var_name.strip() From 1dff2c6764d58592083e44a3761b07b8b651cded Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:28:14 +0200 Subject: [PATCH 11/16] erased useless whitespaces --- tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index ce8716d0..083a7ccf 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -451,7 +451,7 @@ def test_db_creation_vars_with_whitespace(self) -> None: 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() From 711b36046d11ea7ba7515a2a62ee33b16ce5137e Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:16:03 +0200 Subject: [PATCH 12/16] regex to parse specification --- snap7/util/db.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index f2d1a79e..4365fa98 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -149,14 +149,20 @@ 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, *_, _type = line.lstrip().split("#")[0].split() + match = regex.match(line.strip()) + index = match.group('index') + var_name = match.group('var_name') + _type = match.group('_type') - match = re.findall(r"(\s*)(\S+)(\s*)",line) - - var_name = "".join([m[0] + m[1] + m[2] for m in match[1:-1]]) var_name = var_name.strip() parsed_db_specification[var_name] = (index, _type) From fcfe2b214b0c6d1e50e08c74eb6ff9ba20c183a0 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:24:48 +0200 Subject: [PATCH 13/16] check if match is not none --- snap7/util/db.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 4365fa98..993bcbbe 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -159,13 +159,13 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: for line in db_specification.split("\n"): if line and not line.lstrip().startswith("#"): match = regex.match(line.strip()) - index = match.group('index') - var_name = match.group('var_name') - _type = match.group('_type') + if match: + index = match.group('index') + var_name = match.group('var_name') + _type = match.group('_type') + var_name = var_name.strip() - var_name = var_name.strip() - - parsed_db_specification[var_name] = (index, _type) + parsed_db_specification[var_name] = (index, _type) return parsed_db_specification From cd4fa087d8381c8761634e880112e47e83454da1 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:27:33 +0200 Subject: [PATCH 14/16] pre-commit job fix --- snap7/util/db.py | 6 +++--- tests/test_util.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/snap7/util/db.py b/snap7/util/db.py index 993bcbbe..7c84261f 100644 --- a/snap7/util/db.py +++ b/snap7/util/db.py @@ -160,9 +160,9 @@ def parse_specification(db_specification: str) -> Dict[str, Any]: if line and not line.lstrip().startswith("#"): match = regex.match(line.strip()) if match: - index = match.group('index') - var_name = match.group('var_name') - _type = match.group('_type') + 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) diff --git a/tests/test_util.py b/tests/test_util.py index 083a7ccf..1df2dbbd 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -438,7 +438,7 @@ 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) From cd76daf2424fcd4179c884f47cb3ea2a17848532 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:29:22 +0200 Subject: [PATCH 15/16] pre -commit job fix --- tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index 1df2dbbd..71ab79de 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -438,7 +438,7 @@ 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) From 8653cb1a6dd1cde0134470f25e6236226506de43 Mon Sep 17 00:00:00 2001 From: Novecento <61213759+Novecento99@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:41:31 +0200 Subject: [PATCH 16/16] whitespaces --- tests/test_util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 71ab79de..299172ad 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -444,7 +444,7 @@ def test_db_creation(self) -> None: 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 = """ @@ -460,7 +460,6 @@ def test_db_creation_vars_with_whitespace(self) -> None: 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)