Skip to content

Commit 5b8d802

Browse files
committed
Add support for coded source blocks
1 parent e9b5874 commit 5b8d802

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/foamlib/_files/_parsing/_parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def _parse_field(contents: bytes | bytearray, pos: int) -> tuple[Field, int]:
527527

528528
def _parse_token(contents: bytes | bytearray, pos: int) -> tuple[str, int]:
529529
c = contents[pos : pos + 1]
530-
if c.isalpha() or (c and c in b"_#$"):
530+
if c.isalpha() or (c and c in b"_#$" and contents[pos + 1 : pos + 2] != b"{"):
531531
end = pos + 1
532532
depth = 0
533533
while end < len(contents):
@@ -568,6 +568,17 @@ def _parse_token(contents: bytes | bytearray, pos: int) -> tuple[str, int]:
568568
end += 1
569569
raise FoamFileDecodeError(contents, pos, expected="end of quoted string")
570570

571+
if contents[pos : pos + 2] == b"#{":
572+
end = pos + 2
573+
574+
while end < len(contents) and contents[end : end + 2] != b"#}":
575+
end += 1
576+
577+
if end >= len(contents):
578+
raise FoamFileDecodeError(contents, end, expected="#}")
579+
580+
return contents[pos : end + 2].decode(), end + 2
581+
571582
raise ParseError(contents, pos, expected="token")
572583

573584

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from foamlib import FoamFile
2+
3+
4+
def test_issue_730() -> None:
5+
file = {}
6+
file["functions"] = {}
7+
file["functions"]["difference"] = {
8+
"type": "coded",
9+
"libs": ["utilityFunctionObjects"],
10+
"name": "writeMagU",
11+
"codeWrite": '#{\
12+
const volVectorField& U = mesh().lookupObject<volVectorField>("U");\
13+
mag(U)().write();\
14+
#}',
15+
}
16+
17+
contents = FoamFile.dumps(file)
18+
read = FoamFile.loads(contents)
19+
assert read == file

tests/test_files/test_parsing/test_intermediate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ def test_directives_in_dict() -> None:
310310
""")
311311

312312

313-
@pytest.mark.xfail(reason="Not currently supported")
314313
def test_code() -> None:
315314
ParsedFile(b"""
316315
code_name

0 commit comments

Comments
 (0)