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

Refactor _VariablesIOWrapper #535

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
32 changes: 12 additions & 20 deletions tdp/core/variables/variables.py
Original file line number Diff line number Diff line change
@@ -113,7 +113,11 @@ def __str__(self):


class _VariablesIOWrapper(VariablesDict):
"""Context manager for file IO operations."""
"""VariablesDict wrapper for file IO operations.

This object can either be used as a context manager or as a regular object. In that
case, it is the responsibility of the user to call the `close` method.
"""

def __init__(self, path: Path, mode: Optional[str] = None):
"""Initializes the _VariablesIOWrapper instance.
@@ -124,8 +128,8 @@ def __init__(self, path: Path, mode: Optional[str] = None):
"""
self._file_path = path
self._file_descriptor = open(self._file_path, mode or "r+")
self._content = from_yaml(self._file_descriptor) or {}
self._name = path.name
# Initialize the content of the variables file
super().__init__(content=from_yaml(self._file_descriptor) or {}, name=path.name)

def __enter__(self) -> "_VariablesIOWrapper":
return proxy(self)
@@ -134,20 +138,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

def _flush_on_disk(self) -> None:
"""Write the content of the variables file on disk.

Raises:
RuntimeError: If the file descriptor is already closed.
"""
"""Write the content of the variables file on disk."""
# Check if the file descriptor is already closed
if not self._file_descriptor or self._file_descriptor.closed:
raise RuntimeError(
f"{self._file_path} is already closed, which shouldn't happen"
)
raise RuntimeError(f"{self._file_path} is already closed.")

# Check if the file is writable
if not self._file_descriptor.writable():
raise RuntimeError(f"{self._file_path} is not writable")
raise RuntimeError(f"{self._file_path} is not writable.")

# Write the content of the variables file on disk
self._file_descriptor.seek(0)
@@ -160,16 +158,10 @@ def _flush_on_disk(self) -> None:
os.fsync(self._file_descriptor.fileno())

def close(self) -> None:
"""Closes the file descriptor.

Raises:
RuntimeError: If the file descriptor is already closed.
"""
"""Closes the file."""
# Check if the file descriptor is already closed
if not self._file_descriptor or self._file_descriptor.closed:
raise RuntimeError(
f"{self._file_path} is already closed, which shouldn't happen"
)
raise RuntimeError(f"{self._file_path} is already closed.")

# Flush to disk only if the file is writable
if self._file_descriptor.writable():