Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

refactor: expose the stitch service's chunk listing and byte-join helpers

François Catuhe43f6ace72991
diff --git a/src/services/recording_stitch.py b/src/services/recording_stitch.py
index 8187d1fa..7a89c4ec 100644
--- a/src/services/recording_stitch.py
+++ b/src/services/recording_stitch.py
@@ -67,7 +67,7 @@ def _session_dir(upload_folder: str, session_id: str) -> str:
return os.path.join(upload_folder, '_sessions', session_id)
-def _chunk_paths(session_dir: str) -> list:
+def session_chunk_paths(session_dir: str) -> list:
"""Return chunk file paths in monotonic order. ``chunk-NNNNNN.bin``
naming sorts lexicographically by index because of the zero pad."""
if not os.path.isdir(session_dir):
@@ -130,7 +130,7 @@ def _partition_into_segments(chunk_paths: list) -> list:
return segments
-def _byte_join(chunk_paths: list, output_path: str) -> None:
+def byte_join(chunk_paths: list, output_path: str) -> None:
"""Raw byte concatenation in order, streamed for flat memory use."""
with open(output_path, 'wb') as out:
for p in chunk_paths:
@@ -212,7 +212,7 @@ def _assemble_session_audio(chunk_paths: list, output_path: str, mime_type: str)
segment_files = []
for i, seg_chunks in enumerate(segments):
seg_path = os.path.join(work_dir, f'segment-{i:04d}.bin')
- _byte_join(seg_chunks, seg_path)
+ byte_join(seg_chunks, seg_path)
segment_files.append(seg_path)
if len(segment_files) == 1:
@@ -326,7 +326,7 @@ def stitch_recording_session(session_id: str) -> Tuple[int, str, dict]:
upload_folder = current_app.config.get('UPLOAD_FOLDER') or '/data/uploads'
sess_dir = _session_dir(upload_folder, session_id)
- chunk_paths = _chunk_paths(sess_dir)
+ chunk_paths = session_chunk_paths(sess_dir)
if not chunk_paths:
raise StitchError(f'session {session_id} has no chunks on disk')
diff --git a/tests/test_recording_stitch.py b/tests/test_recording_stitch.py
index f6e97d77..5fa1365d 100644
--- a/tests/test_recording_stitch.py
+++ b/tests/test_recording_stitch.py
@@ -37,7 +37,7 @@ from src.models import User, Recording, RecordingSession
from src.services.recording_stitch import (
stitch_recording_session,
StitchError,
- _chunk_paths,
+ session_chunk_paths,
_mime_to_extension,
_chunk_starts_segment,
_partition_into_segments,
@@ -208,7 +208,7 @@ def test_chunk_paths_returns_sorted_chunks():
f.write(b'{}')
with open(os.path.join(tmp, 'chunk-bad.txt'), 'wb') as f:
f.write(b'.')
- paths = _chunk_paths(tmp)
+ paths = session_chunk_paths(tmp)
assert [os.path.basename(p) for p in paths] == ['chunk-000001.bin', 'chunk-000002.bin', 'chunk-000003.bin']
finally:
shutil.rmtree(tmp, ignore_errors=True)
@@ -217,7 +217,7 @@ def test_chunk_paths_returns_sorted_chunks():
def test_chunk_paths_empty_dir_returns_empty_list():
tmp = tempfile.mkdtemp(prefix="speakr-stitch-empty-")
try:
- assert _chunk_paths(tmp) == []
+ assert session_chunk_paths(tmp) == []
finally:
shutil.rmtree(tmp, ignore_errors=True)