Skip to content

Commit ea21c5e

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: extract warn helper to consolidate stderr warning messages
Three modules (_agent, _keypress) independently formatted warning messages to stderr with inconsistent methods (print(file=stderr) vs stderr.write()) and manually repeated the "ralphify: warning:" prefix. Extract a shared warn() helper in _output.py so the prefix, output method, and formatting are defined once. Also removes the now-unused sys import from _agent.py. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent a1e8bff commit ea21c5e

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/ralphify/_agent.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import queue as _queue
2121
import signal
2222
import subprocess
23-
import sys
2423
import threading
2524
import time
2625
from collections.abc import Callable
@@ -36,6 +35,7 @@
3635
SUBPROCESS_TEXT_KWARGS,
3736
ProcessResult,
3837
collect_output,
38+
warn,
3939
)
4040

4141
# ── Callback type aliases ──────────────────────────────────────────────
@@ -152,11 +152,7 @@ def _ensure_process_dead(proc: subprocess.Popen[Any]) -> None:
152152
try:
153153
proc.wait(timeout=_PROCESS_WAIT_TIMEOUT)
154154
except subprocess.TimeoutExpired:
155-
print(
156-
"ralphify: warning: agent process did not exit within"
157-
f" {_PROCESS_WAIT_TIMEOUT}s after kill",
158-
file=sys.stderr,
159-
)
155+
warn(f"agent process did not exit within {_PROCESS_WAIT_TIMEOUT}s after kill")
160156

161157

162158
def _close_pipes(proc: subprocess.Popen[Any]) -> None:
@@ -573,10 +569,9 @@ def _drain_readers(
573569
if thread is not None:
574570
thread.join(timeout=timeout)
575571
if thread.is_alive():
576-
print(
577-
f"ralphify: warning: reader thread {thread.name!r} did not"
578-
f" exit within {timeout}s — log output may be incomplete",
579-
file=sys.stderr,
572+
warn(
573+
f"reader thread {thread.name!r} did not exit within"
574+
f" {timeout}s — log output may be incomplete"
580575
)
581576

582577

src/ralphify/_keypress.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from collections.abc import Callable
3434
from types import FrameType
3535

36+
from ralphify._output import warn
37+
3638

3739
_POLL_INTERVAL = 0.1 # seconds between stop-flag checks (POSIX select)
3840
_WIN_POLL_INTERVAL = 0.05 # seconds between kbhit() checks on Windows
@@ -123,8 +125,8 @@ def stop(self) -> None:
123125
# posix loop will still restore the terminal on process
124126
# exit, so the user's shell will not be wedged.
125127
try:
126-
sys.stderr.write(
127-
f"ralphify: keypress listener did not exit within {_THREAD_JOIN_TIMEOUT}s\n"
128+
warn(
129+
f"keypress listener did not exit within {_THREAD_JOIN_TIMEOUT}s"
128130
)
129131
except Exception:
130132
pass

src/ralphify/_output.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ def collect_output(
7878
return "".join(parts)
7979

8080

81+
def warn(message: str) -> None:
82+
"""Print a warning message to stderr with the ``ralphify:`` prefix.
83+
84+
Used by low-level modules (agent, keypress) for operational warnings
85+
that should be visible even when Rich console rendering is not active.
86+
"""
87+
print(f"ralphify: warning: {message}", file=sys.stderr)
88+
89+
8190
_SECONDS_PER_MINUTE = 60
8291
_MINUTES_PER_HOUR = 60
8392

0 commit comments

Comments
 (0)