From f74ec9cd93900600cc3ad8c5c00f1aeef64caccc Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 9 Jan 2026 23:14:34 +0000 Subject: [PATCH] scene_collection validation logic now in callbacks --- obsws_cli/scenecollection.py | 26 ++++++++++++-------------- obsws_cli/validate.py | 28 ++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/obsws_cli/scenecollection.py b/obsws_cli/scenecollection.py index 4680b16..1792a42 100644 --- a/obsws_cli/scenecollection.py +++ b/obsws_cli/scenecollection.py @@ -53,16 +53,15 @@ def current(ctx: typer.Context): def switch( ctx: typer.Context, scene_collection_name: Annotated[ - str, typer.Argument(..., help='Name of the scene collection to switch to') + str, + typer.Argument( + ..., + help='Name of the scene collection to switch to', + callback=validate.scene_collection_in_scene_collections, + ), ], ): """Switch to a scene collection.""" - if not validate.scene_collection_in_scene_collections(ctx, scene_collection_name): - console.err.print( - f'Scene collection [yellow]{scene_collection_name}[/yellow] not found.' - ) - raise typer.Exit(1) - current_scene_collection = ( ctx.obj['obsws'].get_scene_collection_list().current_scene_collection_name ) @@ -82,16 +81,15 @@ def switch( def create( ctx: typer.Context, scene_collection_name: Annotated[ - str, typer.Argument(..., help='Name of the scene collection to create') + str, + typer.Argument( + ..., + help='Name of the scene collection to create', + callback=validate.scene_collection_not_in_scene_collections, + ), ], ): """Create a new scene collection.""" - if validate.scene_collection_in_scene_collections(ctx, scene_collection_name): - console.err.print( - f'Scene collection [yellow]{scene_collection_name}[/yellow] already exists.' - ) - raise typer.Exit(1) - ctx.obj['obsws'].create_scene_collection(scene_collection_name) console.out.print( f'Created scene collection {console.highlight(ctx, scene_collection_name)}.' diff --git a/obsws_cli/validate.py b/obsws_cli/validate.py index e16a317..feb1915 100644 --- a/obsws_cli/validate.py +++ b/obsws_cli/validate.py @@ -43,12 +43,32 @@ def studio_mode_enabled(ctx: typer.Context) -> bool: def scene_collection_in_scene_collections( ctx: typer.Context, scene_collection_name: str -) -> bool: - """Check if a scene collection exists.""" +) -> str: + """Ensure a scene collection exists in the list of scene collections.""" resp = ctx.obj['obsws'].get_scene_collection_list() - return any( + if not any( collection == scene_collection_name for collection in resp.scene_collections - ) + ): + console.err.print( + f'Scene collection [yellow]{scene_collection_name}[/yellow] not found.' + ) + raise typer.Exit(1) + return scene_collection_name + + +def scene_collection_not_in_scene_collections( + ctx: typer.Context, scene_collection_name: str +) -> str: + """Ensure a scene collection does not already exist in the list of scene collections.""" + resp = ctx.obj['obsws'].get_scene_collection_list() + if any( + collection == scene_collection_name for collection in resp.scene_collections + ): + console.err.print( + f'Scene collection [yellow]{scene_collection_name}[/yellow] already exists.' + ) + raise typer.Exit(1) + return scene_collection_name def item_in_scene_item_list(