karenina.utils.file_ops¶
file_ops
¶
File operation utilities for Karenina.
Provides reusable atomic file write functionality.
Functions¶
atomic_write
¶
Write content to file atomically using write-rename pattern.
Writes data to a temporary partial file, then atomically renames it to the target path. This ensures the file is never in a partially written state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Target file path |
required |
|
str
|
Content to write |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If the write or rename fails |
Source code in src/karenina/utils/file_ops.py
atomic_writer
¶
atomic_writer(filepath: Path) -> Iterator[TextIO]
Context-manager twin of atomic_write for streaming writes.
Yields a text file handle pointing at a .partial sidecar of
filepath. On clean exit, fsyncs the handle, closes it, and
atomically renames the sidecar into place. On any exception inside
the with block (including KeyboardInterrupt / SystemExit
via BaseException), removes the .partial file and reraises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Target file path. |
required |
Yields:
| Type | Description |
|---|---|
TextIO
|
An open writable text file handle for the |
Raises:
| Type | Description |
|---|---|
OSError
|
If the partial write, rename, or cleanup fails. |