fileio
fileio
¶
Atomic file I/O helpers shared across the data pipelines.
The frontend and backend processes share one cache directory (a Docker bind
mount in production) and the frontend's file watcher reloads pipeline outputs
whenever their mtime changes. Plain writes leave a window where the file is
truncated or half-written, so a concurrent reader can crash on a torn parquet
or a crash mid-write can corrupt the file permanently. These helpers always
write to a temp file in the destination's directory and then move it into
place with os.replace (atomic within one filesystem).
write_parquet_atomic(df, path)
¶
Write a DataFrame as parquet without exposing a torn file to readers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to persist. |
required |
path
|
Path
|
Destination parquet path. |
required |
Source code in pspcz_analyzer/fileio.py
write_json_atomic(data, path)
¶
Write a JSON-serializable object atomically (indented, trailing newline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
JSON-serializable payload. |
required |
path
|
Path
|
Destination JSON path. |
required |
Source code in pspcz_analyzer/fileio.py
stream_to_atomic(response, dest, chunk_size=_CHUNK_SIZE)
¶
Stream an open httpx response body to dest atomically.
A failure mid-stream (connection reset, timeout) removes the temp file and
leaves dest absent rather than truncated, so the next run downloads
cleanly instead of tripping over a partial file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
Response from |
required |
dest
|
Path
|
Destination file path. |
required |
chunk_size
|
int
|
Read chunk size in bytes. |
_CHUNK_SIZE
|
Source code in pspcz_analyzer/fileio.py
assert_zip_intact(path)
¶
Verify that a ZIP archive is complete and all members pass CRC checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the ZIP file. |
required |
Raises:
| Type | Description |
|---|---|
BadZipFile
|
If the archive structure is corrupt or a member fails its CRC check. |