Skip to content

Commit

Permalink
Improve serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Sep 3, 2024
1 parent b11e504 commit 748a39e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 8 additions & 2 deletions foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ def __setitem__(

self._write(
contents[:start]
+ b"\n"
+ (b"\n" if contents[start - 1 : start] != b"\n" else b"")
+ (b" " * (len(keywords) - 1) if keywords else b"")
+ dumpb({keywords[-1]: {}})
+ b"\n"
+ contents[end:]
Expand Down Expand Up @@ -280,7 +281,12 @@ def __setitem__(

self._write(
contents[:start]
+ b"\n"
+ (
b"\n"
if start > 0 and contents[start - 1 : start] != b"\n"
else b""
)
+ (b" " * (len(keywords) - 1) if keywords else b"")
+ dumpb({keywords[-1]: data}, kind=kind)
+ b"\n"
+ contents[end:]
Expand Down
4 changes: 3 additions & 1 deletion foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def __init__(self, contents: bytes) -> None:
Tuple[str, ...],
Tuple[int, Union[FoamFileBase.Data, EllipsisType], int],
] = {}
self._end = len(contents)

for parse_result in _FILE.parse_string(
contents.decode("latin-1"), parse_all=True
):
Expand Down Expand Up @@ -252,7 +254,7 @@ def entry_location(
_, _, end = self._parsed[keywords[:-1]]
end -= 1
else:
end = -1
end = self._end

start = end
else:
Expand Down

3 comments on commit 748a39e

@zzkluck
Copy link

@zzkluck zzkluck commented on 748a39e Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Commit! Just yesterday I was struggling with foamlib's output format in dictionary files and tried to make some similar changes myself. Now, you've officially updated it. Thank you for your outstanding work.

@gerlero
Copy link
Owner Author

@gerlero gerlero commented on 748a39e Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zzkluck Thanks! I'll make a new release with this change so that it is out now while I work on getting v0.4.0 ready.

In the short term I'll keep trying to make file writes prettier while preserving the original format.

@gerlero
Copy link
Owner Author

@gerlero gerlero commented on 748a39e Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zzkluck foamlib v0.4.0 is out now and should be better at preserving the formatting through edits. Let me know though if you find any bugs with that.

Please sign in to comment.