check recording status to clean up stack traces

toggle command now invokes stop/start

patch bump
This commit is contained in:
onyx-and-iris 2025-04-20 20:48:23 +01:00
parent a9e8ef8f6d
commit edd3753d4d
2 changed files with 40 additions and 51 deletions

View File

@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online>
#
# SPDX-License-Identifier: MIT
__version__ = "0.6.0"
__version__ = "0.6.1"

View File

@ -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.')