Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Pfs comma in string cannot be parsed #476

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions mikeio/pfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,16 +578,17 @@ def _parse_param(self, value: str) -> str:
value = self._parse_token(value)
return value

_COMMA_MATCHER = re.compile(r",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)")
# _COMMA_MATCHER = re.compile(r",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)")

def _split_line_by_comma(self, s: str):
return self._COMMA_MATCHER.split(s)
# import shlex
# lexer = shlex.shlex(s)
# lexer.whitespace += ","
# lexer.quotes += "|"
# lexer.wordchars += ".-"
# return list(lexer)
# return self._COMMA_MATCHER.split(s)
import shlex

lexer = shlex.shlex(s)
lexer.whitespace += ","
lexer.quotes += "|"
lexer.wordchars += ",.-"
return list(lexer)

def _parse_token(self, token: str) -> str:
s = token.strip()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_pfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ def test_difficult_chars_in_str(tmpdir):
C = |sd's\d.dfs0|
D = |str'd.dfs0|
E = |str,s'+-s_d.dfs0|
F = |DT,M.dfs2|
EndSect // ENGINE
"""
with pytest.warns(match="contains a single quote character"):
Expand All @@ -613,9 +614,8 @@ def test_difficult_chars_in_str(tmpdir):
assert isinstance(pfs.ENGINE.A, str)
assert pfs.ENGINE.A == "str,s/d\sd.dfs0"

# NOTE: B will appear wrong as a list with one item
assert isinstance(pfs.ENGINE.B[0], str)
assert pfs.ENGINE.B[0] == "str,sd'sd.dfs0"
assert isinstance(pfs.ENGINE.B, str)
assert pfs.ENGINE.B == "str,sd'sd.dfs0"
assert isinstance(pfs.ENGINE.C, str)
assert pfs.ENGINE.C == "|sd\U0001F600s\d.dfs0|"
assert isinstance(pfs.ENGINE.D, str)
Expand Down