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 file writing #244

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
6 changes: 4 additions & 2 deletions foamlib/_files/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ def __exit__(
self.__defer_io -= 1
if self.__defer_io == 0:
assert self.__parsed is not None
if self.__parsed.modified or self.__missing:
if self.__parsed.modified:
contents = self.__parsed.contents

if self.path.suffix == ".gz":
contents = gzip.compress(contents)

self.path.write_bytes(contents)
self.__parsed.modified = False
self.__missing = False

def _get_parsed(self, *, missing_ok: bool = False) -> Parsed:
if not self.__defer_io:
Expand All @@ -69,7 +71,7 @@ def _get_parsed(self, *, missing_ok: bool = False) -> Parsed:
assert self.__parsed is not None
assert self.__missing is not None

if self.__missing and not missing_ok:
if self.__missing and not self.__parsed.modified and not missing_ok:
raise FileNotFoundError(self.path)

return self.__parsed
Expand Down
18 changes: 10 additions & 8 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import array
import contextlib
import sys
from typing import Tuple, Union, cast

Expand Down Expand Up @@ -273,22 +272,24 @@ def put(
) -> None:
assert not isinstance(data, Mapping)

with contextlib.suppress(KeyError):
del self[keywords]

start, end = self.entry_location(keywords, missing_ok=True)
diff = len(content) - (end - start)

self._parsed[keywords] = (start, data, end + diff)
diff = len(content) - (end - start)
for k, (s, d, e) in self._parsed.items():
if s > end:
self._parsed[k] = (s + diff, d, e + diff)
elif e > start:
self._parsed[k] = (s, d, e + diff)

self._parsed[keywords] = (start, data, end + diff)

self.contents = self.contents[:start] + content + self.contents[end:]
self.modified = True

for k in list(self._parsed):
if keywords != k and keywords == k[: len(keywords)]:
del self._parsed[k]

def __delitem__(self, keywords: Union[str, Tuple[str, ...]]) -> None:
if isinstance(keywords, str):
keywords = (keywords,)
Expand Down Expand Up @@ -327,8 +328,9 @@ def entry_location(
except KeyError:
if missing_ok:
if len(keywords) > 1:
_, _, end = self._parsed[keywords[:-1]]
end -= 1
assert self[keywords[:-1]] is ...
start, end = self.entry_location(keywords[:-1])
end = self.contents.rindex(b"}", start, end)
else:
end = len(self.contents)

Expand Down
Loading