add validation logic

add item_in_scene_item_list to validate module
This commit is contained in:
onyx-and-iris 2025-04-20 22:47:18 +01:00
parent bfa657ee71
commit daa7b820d2
2 changed files with 97 additions and 62 deletions

View File

@ -1,10 +1,11 @@
"""module containing commands for manipulating items in scenes.""" """module containing commands for manipulating items in scenes."""
import obsws_python as obsws from typing import Annotated
import typer import typer
from . import validate
from .alias import AliasGroup from .alias import AliasGroup
from .errors import ObswsCliBadParameter
app = typer.Typer(cls=AliasGroup) app = typer.Typer(cls=AliasGroup)
@ -17,20 +18,32 @@ def main():
@app.command('list | ls') @app.command('list | ls')
def list(ctx: typer.Context, scene_name: str): def list(ctx: typer.Context, scene_name: str):
"""List all items in a scene.""" """List all items in a scene."""
try: if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.")
typer.Exit(code=1)
resp = ctx.obj['obsws'].get_scene_item_list(scene_name) resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
items = (item.get('sourceName') for item in resp.scene_items) items = (item.get('sourceName') for item in resp.scene_items)
typer.echo('\n'.join(items)) typer.echo('\n'.join(items))
except obsws.error.OBSSDKRequestError as e:
if e.code == 600:
raise ObswsCliBadParameter(str(e)) from e
raise
@app.command() @app.command()
def show(ctx: typer.Context, scene_name: str, item_name: str): def show(
ctx: typer.Context,
scene_name: str,
item_name: str,
validated: Annotated[bool, validate.skipped_option] = False,
):
"""Show an item in a scene.""" """Show an item in a scene."""
try: if not validated:
if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.")
raise typer.Exit(code=1)
if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
raise typer.Exit(code=1)
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
ctx.obj['obsws'].set_scene_item_enabled( ctx.obj['obsws'].set_scene_item_enabled(
@ -38,55 +51,70 @@ def show(ctx: typer.Context, scene_name: str, item_name: str):
item_id=int(resp.scene_item_id), item_id=int(resp.scene_item_id),
enabled=True, enabled=True,
) )
except obsws.error.OBSSDKRequestError as e:
if e.code == 600:
raise ObswsCliBadParameter(str(e)) from e
raise
@app.command() @app.command()
def hide(ctx: typer.Context, scene_name: str, item_name: str): def hide(
ctx: typer.Context,
scene_name: str,
item_name: str,
validated: Annotated[bool, validate.skipped_option] = False,
):
"""Hide an item in a scene.""" """Hide an item in a scene."""
try: if not validated:
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.")
raise typer.Exit(code=1)
if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
raise typer.Exit(code=1)
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
ctx.obj['obsws'].set_scene_item_enabled( ctx.obj['obsws'].set_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(resp.scene_item_id), item_id=int(resp.scene_item_id),
enabled=False, enabled=False,
) )
except obsws.error.OBSSDKRequestError as e:
if e.code == 600:
raise ObswsCliBadParameter(str(e)) from e
raise
@app.command('toggle | tg') @app.command('toggle | tg')
def toggle(ctx: typer.Context, scene_name: str, item_name: str): def toggle(ctx: typer.Context, scene_name: str, item_name: str):
"""Toggle an item in a scene.""" """Toggle an item in a scene."""
try: if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.")
raise typer.Exit(code=1)
if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
raise typer.Exit(code=1)
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
enabled = ctx.obj['obsws'].get_scene_item_enabled( enabled = ctx.obj['obsws'].get_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(resp.scene_item_id), item_id=int(resp.scene_item_id),
) )
if enabled.scene_item_enabled:
new_state = not enabled.scene_item_enabled ctx.invoke(
ctx.obj['obsws'].set_scene_item_enabled( hide, ctx=ctx, scene_name=scene_name, item_name=item_name, validated=True
scene_name=scene_name, )
item_id=int(resp.scene_item_id), else:
enabled=new_state, ctx.invoke(
show, ctx=ctx, scene_name=scene_name, item_name=item_name, validated=True
) )
except obsws.error.OBSSDKRequestError as e:
if e.code == 600:
raise ObswsCliBadParameter(str(e)) from e
raise
@app.command() @app.command()
def visible(ctx: typer.Context, scene_name: str, item_name: str): def visible(ctx: typer.Context, scene_name: str, item_name: str):
"""Check if an item in a scene is visible.""" """Check if an item in a scene is visible."""
try: if not validate.scene_in_scenes(ctx, scene_name):
typer.echo(f"Scene '{scene_name}' not found.")
raise typer.Exit(code=1)
if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
raise typer.Exit(code=1)
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
enabled = ctx.obj['obsws'].get_scene_item_enabled( enabled = ctx.obj['obsws'].get_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
@ -95,7 +123,3 @@ def visible(ctx: typer.Context, scene_name: str, item_name: str):
typer.echo( typer.echo(
f"Item '{item_name}' in scene '{scene_name}' is currently {'visible' if enabled.scene_item_enabled else 'hidden'}." f"Item '{item_name}' in scene '{scene_name}' is currently {'visible' if enabled.scene_item_enabled else 'hidden'}."
) )
except obsws.error.OBSSDKRequestError as e:
if e.code == 600:
raise ObswsCliBadParameter(str(e)) from e
raise

View File

@ -2,6 +2,9 @@
import typer import typer
# type alias for an option that is skipped when the command is run
skipped_option = typer.Option(parser=lambda _: _, hidden=True, expose_value=False)
def input_in_inputs(ctx: typer.Context, input_name: str) -> bool: def input_in_inputs(ctx: typer.Context, input_name: str) -> bool:
"""Check if an input is in the input list.""" """Check if an input is in the input list."""
@ -29,3 +32,11 @@ def scene_collection_in_scene_collections(
return any( return any(
collection == scene_collection_name for collection in resp.scene_collections collection == scene_collection_name for collection in resp.scene_collections
) )
def item_in_scene_item_list(
ctx: typer.Context, scene_name: str, item_name: str
) -> bool:
"""Check if an item exists in a scene."""
resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
return any(item.get('sourceName') == item_name for item in resp.scene_items)