Skip to content

Commit

Permalink
Merge pull request #321 from gerlero/fix-320
Browse files Browse the repository at this point in the history
Fix handling of keyword entries without values in FoamFiles
  • Loading branch information
gerlero authored Mar 2, 2025
2 parents 2d71e04 + ea12ca0 commit 7b9d666
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ def __setitem__(self, keywords: str | tuple[str, ...] | None, data: Entry) -> No
self[(*keywords, k)] = v

elif keywords:
val = dumps(data, kind=kind)
parsed.put(
keywords,
normalize(data, kind=kind),
before
+ indentation
+ dumps(keywords[-1])
+ b" "
+ dumps(data, kind=kind)
+ ((b" " + val) if val else b"")
+ (b";" if not keywords[-1].startswith("#") else b"")
+ after,
)
Expand Down
2 changes: 2 additions & 0 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ def _keyword_entry_of(


def parse_data(s: str) -> Data:
if not s.strip():
return ""
return cast(Data, _DATA.parse_string(s, parse_all=True)[0])


Expand Down
5 changes: 4 additions & 1 deletion foamlib/_files/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def dumps(

if isinstance(data, tuple) and kind == Kind.SINGLE_ENTRY and len(data) == 2:
k, v = data
ret = dumps(k) + b" " + dumps(v)
ret = dumps(k)
val = dumps(v)
if val:
ret += b" " + val
if not isinstance(v, Mapping):
ret += b";"
return ret
Expand Down

0 comments on commit 7b9d666

Please sign in to comment.