validate preview mode enabled to clean up stack traces

This commit is contained in:
onyx-and-iris 2025-04-20 21:52:25 +01:00
parent 2175fb8788
commit e64b9311d4
2 changed files with 53 additions and 30 deletions

View File

@ -2,11 +2,10 @@
from typing import Annotated from typing import Annotated
import obsws_python as obsws
import typer import typer
from . import validate
from .alias import AliasGroup from .alias import AliasGroup
from .errors import ObswsCliBadParameter, ObswsCliError
app = typer.Typer(cls=AliasGroup) app = typer.Typer(cls=AliasGroup)
@ -24,6 +23,12 @@ def list(ctx: typer.Context):
typer.echo('\n'.join(scenes)) typer.echo('\n'.join(scenes))
def _studio_mode_enabled(ctx: typer.Context) -> bool:
"""Check if studio mode is enabled."""
resp = ctx.obj['obsws'].get_studio_mode_enabled()
return resp.studio_mode_enabled
@app.command('current | get') @app.command('current | get')
def current( def current(
ctx: typer.Context, ctx: typer.Context,
@ -32,20 +37,16 @@ def current(
] = False, ] = False,
): ):
"""Get the current program scene or preview scene.""" """Get the current program scene or preview scene."""
try: if preview and not _studio_mode_enabled(ctx):
if preview: typer.echo('Studio mode is not enabled, cannot get preview scene.')
resp = ctx.obj['obsws'].get_current_preview_scene() raise typer.Exit(1)
typer.echo(resp.current_preview_scene_name)
else: if preview:
resp = ctx.obj['obsws'].get_current_program_scene() resp = ctx.obj['obsws'].get_current_preview_scene()
typer.echo(resp.current_program_scene_name) typer.echo(resp.current_preview_scene_name)
except obsws.error.OBSSDKRequestError as e: else:
match e.code: resp = ctx.obj['obsws'].get_current_program_scene()
case 506: typer.echo(resp.current_program_scene_name)
raise ObswsCliError(
'Not in studio mode, cannot get preview scene.'
) from e
raise
@app.command('switch | set') @app.command('switch | set')
@ -58,17 +59,18 @@ def switch(
] = False, ] = False,
): ):
"""Switch to a scene.""" """Switch to a scene."""
try: if preview and not _studio_mode_enabled(ctx):
if preview: typer.echo('Studio mode is not enabled, cannot set the preview scene.')
ctx.obj['obsws'].set_current_preview_scene(scene_name) raise typer.Exit(1)
else:
ctx.obj['obsws'].set_current_program_scene(scene_name) if not validate.scene_in_scenes(ctx, scene_name):
except obsws.error.OBSSDKRequestError as e: typer.echo(
match e.code: f"Scene '{scene_name}' not found.",
case 506: err=True,
raise ObswsCliError( )
'Not in studio mode, cannot set preview scene.' raise typer.Exit(code=1)
) from e
case 600: if preview:
raise ObswsCliBadParameter(f"Scene '{scene_name}' not found.") from e ctx.obj['obsws'].set_current_preview_scene(scene_name)
raise else:
ctx.obj['obsws'].set_current_program_scene(scene_name)

21
obsws_cli/validate.py Normal file
View File

@ -0,0 +1,21 @@
"""module containing functions to validate input parameters."""
import typer
def input_in_inputs(ctx: typer.Context, input_name: str) -> bool:
"""Check if an input is in the input list."""
inputs = ctx.obj['obsws'].get_input_list().inputs
return any(input_.get('inputName') == input_name for input_ in inputs)
def scene_in_scenes(ctx: typer.Context, scene_name: str) -> bool:
"""Check if a scene exists in the list of scenes."""
resp = ctx.obj['obsws'].get_scene_list()
return any(scene.get('sceneName') == scene_name for scene in resp.scenes)
def studio_mode_enabled(ctx: typer.Context) -> bool:
"""Check if studio mode is enabled."""
resp = ctx.obj['obsws'].get_studio_mode_enabled()
return resp.studio_mode_enabled