From edd3753d4d2a8de6832ba2f2535f771e9eb207b3 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 20 Apr 2025 20:48:23 +0100 Subject: [PATCH] check recording status to clean up stack traces toggle command now invokes stop/start patch bump --- obsws_cli/__about__.py | 2 +- obsws_cli/record.py | 89 ++++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/obsws_cli/__about__.py b/obsws_cli/__about__.py index ff02fe1..8349500 100644 --- a/obsws_cli/__about__.py +++ b/obsws_cli/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2025-present onyx-and-iris # # SPDX-License-Identifier: MIT -__version__ = "0.6.0" +__version__ = "0.6.1" diff --git a/obsws_cli/record.py b/obsws_cli/record.py index 7608ca2..f6a7eb1 100644 --- a/obsws_cli/record.py +++ b/obsws_cli/record.py @@ -1,11 +1,10 @@ """module for controlling OBS recording functionality.""" -import obsws_python as obsws import typer -from .errors import ObswsCliError +from .alias import AliasGroup -app = typer.Typer() +app = typer.Typer(cls=AliasGroup) @app.callback() @@ -13,36 +12,38 @@ def main(): """Control OBS recording functionality.""" +def _get_recording_status(ctx: typer.Context) -> tuple: + """Get recording status.""" + resp = ctx.obj['obsws'].get_record_status() + return resp.output_active, resp.output_paused + + @app.command() def start(ctx: typer.Context): """Start recording.""" - try: - ctx.obj['obsws'].start_record() - typer.echo('Recording started successfully.') - except obsws.error.OBSSDKRequestError as e: - if e.code == 500: - raise ObswsCliError( - 'Recording is already in progress, cannot start.' - ) from e - raise + active, paused = _get_recording_status(ctx) + if active: + err_msg = 'Recording is already in progress, cannot start.' + if paused: + err_msg += ' Try resuming it.' + + typer.echo(err_msg) + raise typer.Exit(1) + + ctx.obj['obsws'].start_record() + typer.echo('Recording started successfully.') @app.command() def stop(ctx: typer.Context): """Stop recording.""" - try: - ctx.obj['obsws'].stop_record() - typer.echo('Recording stopped successfully.') - except obsws.error.OBSSDKRequestError as e: - if e.code == 501: - raise ObswsCliError('Recording is not in progress, cannot stop.') from e - raise + active, _ = _get_recording_status(ctx) + if not active: + typer.echo('Recording is not in progress, cannot stop.') + raise typer.Exit(1) - -def _get_recording_status(ctx: typer.Context) -> tuple: - """Get recording status.""" - resp = ctx.obj['obsws'].get_record_status() - return resp.output_active, resp.output_paused + ctx.obj['obsws'].stop_record() + typer.echo('Recording stopped successfully.') @app.command() @@ -63,17 +64,9 @@ def toggle(ctx: typer.Context): """Toggle recording.""" active, _ = _get_recording_status(ctx) if active: - try: - ctx.obj['obsws'].stop_record() - typer.echo('Recording stopped successfully.') - except obsws.error.OBSSDKRequestError as e: - raise ObswsCliError(str(e)) from e + ctx.invoke(stop, ctx=ctx) else: - try: - ctx.obj['obsws'].start_record() - typer.echo('Recording started successfully.') - except obsws.error.OBSSDKRequestError as e: - raise ObswsCliError(str(e)) from e + ctx.invoke(start, ctx=ctx) @app.command() @@ -81,15 +74,14 @@ def resume(ctx: typer.Context): """Resume recording.""" active, paused = _get_recording_status(ctx) if not active: - raise ObswsCliError('Recording is not in progress, cannot resume.') + typer.echo('Recording is not in progress, cannot resume.') + raise typer.Exit(1) if not paused: - raise ObswsCliError('Recording is in progress but not paused, cannot resume.') + typer.echo('Recording is in progress but not paused, cannot resume.') + raise typer.Exit(1) - try: - ctx.obj['obsws'].resume_record() - typer.echo('Recording resumed successfully.') - except obsws.error.OBSSDKRequestError as e: - raise ObswsCliError(str(e)) from e + ctx.obj['obsws'].resume_record() + typer.echo('Recording resumed successfully.') @app.command() @@ -97,14 +89,11 @@ def pause(ctx: typer.Context): """Pause recording.""" active, paused = _get_recording_status(ctx) if not active: - raise ObswsCliError('Recording is not in progress, cannot pause.') + typer.echo('Recording is not in progress, cannot pause.') + raise typer.Exit(1) if paused: - raise ObswsCliError( - 'Recording is in progress but already paused, cannot pause.' - ) + typer.echo('Recording is in progress but already paused, cannot pause.') + raise typer.Exit(1) - try: - ctx.obj['obsws'].pause_record() - typer.echo('Recording paused successfully.') - except obsws.error.OBSSDKRequestError as e: - raise ObswsCliError(str(e)) from e + ctx.obj['obsws'].pause_record() + typer.echo('Recording paused successfully.')