Skip to content

Commit

Permalink
multiple whitespaces support (with test)
Browse files Browse the repository at this point in the history
  • Loading branch information
Novecento99 committed Jul 17, 2024
1 parent 09a6a7d commit f45d60e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 7 additions & 2 deletions snap7/util/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f45d60e

Please sign in to comment.