From 618cdefc174e84bc4577e63227451fd042a5de84 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 20 Apr 2025 11:29:26 +0100 Subject: [PATCH] add --preview flag to scene current and scene switch. minor bump --- obsws_cli/__about__.py | 2 +- obsws_cli/scene.py | 52 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/obsws_cli/__about__.py b/obsws_cli/__about__.py index 081c3ae..6a444f2 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.1.0" +__version__ = "0.2.0" diff --git a/obsws_cli/scene.py b/obsws_cli/scene.py index 077c41a..e41c406 100644 --- a/obsws_cli/scene.py +++ b/obsws_cli/scene.py @@ -1,10 +1,12 @@ """module containing commands for controlling OBS scenes.""" +from typing import Annotated + import obsws_python as obsws import typer from .alias import AliasGroup -from .errors import ObswsCliBadParameter +from .errors import ObswsCliBadParameter, ObswsCliError app = typer.Typer(cls=AliasGroup) @@ -23,18 +25,50 @@ def list(ctx: typer.Context): @app.command('current | get') -def current(ctx: typer.Context): - """Get the current program scene.""" - resp = ctx.obj['obsws'].get_current_program_scene() - typer.echo(resp.current_program_scene_name) +def current( + ctx: typer.Context, + preview: Annotated[ + bool, typer.Option(help='Get the preview scene instead of the program scene') + ] = False, +): + """Get the current program scene or preview scene.""" + try: + if preview: + resp = ctx.obj['obsws'].get_current_preview_scene() + typer.echo(resp.current_preview_scene_name) + else: + resp = ctx.obj['obsws'].get_current_program_scene() + typer.echo(resp.current_program_scene_name) + except obsws.error.OBSSDKRequestError as e: + match e.code: + case 506: + raise ObswsCliError( + 'Not in studio mode, cannot get preview scene.' + ) from e + raise @app.command('switch | set') -def switch(ctx: typer.Context, scene_name: str): +def switch( + ctx: typer.Context, + scene_name: str, + preview: Annotated[ + bool, + typer.Option(help='Switch to the preview scene instead of the program scene'), + ] = False, +): """Switch to a scene.""" try: - ctx.obj['obsws'].set_current_program_scene(scene_name) + if preview: + ctx.obj['obsws'].set_current_preview_scene(scene_name) + else: + ctx.obj['obsws'].set_current_program_scene(scene_name) except obsws.error.OBSSDKRequestError as e: - if e.code == 600: - raise ObswsCliBadParameter(f"Scene '{scene_name}' not found.") + match e.code: + case 506: + raise ObswsCliError( + 'Not in studio mode, cannot set preview scene.' + ) from e + case 600: + raise ObswsCliBadParameter(f"Scene '{scene_name}' not found.") from e raise