testsAndMisc/linux_configuration/misc/testsAndMisc-bash/tools/_transcribe_output.py
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

102 lines
3.2 KiB
Python

"""Output writers for transcription results (SRT, TXT, RTTM)."""
from __future__ import annotations
from datetime import timedelta
from pathlib import Path
from typing import Any
def format_timestamp(seconds: float) -> str:
"""Format seconds as SRT timestamp HH:MM:SS,mmm."""
td = timedelta(seconds=seconds)
total_seconds = int(td.total_seconds())
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
secs = total_seconds % 60
millis = int((seconds - int(seconds)) * 1000)
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
def write_srt(segments: list[Any], srt_path: str) -> None:
"""Write segments to an SRT subtitle file."""
with Path(srt_path).open("w", encoding="utf-8") as f:
for i, seg in enumerate(segments, start=1):
start = format_timestamp(seg.start)
end = format_timestamp(seg.end)
text = (seg.text or "").strip()
if not text:
continue
f.write(f"{i}\n{start} --> {end}\n{text}\n\n")
def write_txt(segments: list[Any], txt_path: str) -> None:
"""Write segments as plain text, one per line."""
with Path(txt_path).open("w", encoding="utf-8") as f:
for seg in segments:
text = (seg.text or "").strip()
if text:
f.write(text + "\n")
def write_srt_with_speakers(
segments: list[Any],
labels: list[int],
path: str,
) -> None:
"""Write SRT subtitles with speaker labels."""
with Path(path).open("w", encoding="utf-8") as f:
for i, (seg, lab) in enumerate(
zip(segments, labels, strict=False),
start=1,
):
text = (seg.text or "").strip()
if not text:
continue
spk = f"SPK{lab + 1}"
start_ts = format_timestamp(seg.start)
end_ts = format_timestamp(seg.end)
f.write(f"{i}\n{start_ts} --> {end_ts}\n[{spk}] {text}\n\n")
def write_txt_with_speakers(
segments: list[Any],
labels: list[int],
path: str,
) -> None:
"""Write plain text with speaker labels."""
with Path(path).open("w", encoding="utf-8") as f:
for seg, lab in zip(segments, labels, strict=False):
text = (seg.text or "").strip()
if text:
spk = f"SPK{lab + 1}"
f.write(f"[{spk}] {text}\n")
def write_rttm(
segments: list[Any],
labels: list[int],
path: str,
file_id: str = "audio",
) -> None:
"""Write RTTM speaker diarization output."""
with Path(path).open("w", encoding="utf-8") as f:
for seg, lab in zip(segments, labels, strict=False):
start = float(getattr(seg, "start", 0.0) or 0.0)
end = float(getattr(seg, "end", start) or start)
dur = max(0.0, end - start)
name = f"SPK{lab + 1}"
f.write(
f"SPEAKER {file_id} 1 {start:.3f} {dur:.3f} <NA> <NA> {name} <NA>\n"
)
def hhmmss(seconds: float) -> str:
"""Format seconds as HH:MM:SS string."""
seconds = max(0.0, float(seconds))
total_seconds = int(seconds)
h = total_seconds // 3600
m = (total_seconds % 3600) // 60
s = total_seconds % 60
return f"{h:02d}:{m:02d}:{s:02d}"