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,85 +18,108 @@ 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):
resp = ctx.obj['obsws'].get_scene_item_list(scene_name) typer.echo(f"Scene '{scene_name}' not found.")
items = (item.get('sourceName') for item in resp.scene_items) typer.Exit(code=1)
typer.echo('\n'.join(items))
except obsws.error.OBSSDKRequestError as e: resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
if e.code == 600: items = (item.get('sourceName') for item in resp.scene_items)
raise ObswsCliBadParameter(str(e)) from e typer.echo('\n'.join(items))
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:
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)
ctx.obj['obsws'].set_scene_item_enabled( if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
scene_name=scene_name, typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
item_id=int(resp.scene_item_id), raise typer.Exit(code=1)
enabled=True,
) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
except obsws.error.OBSSDKRequestError as e:
if e.code == 600: ctx.obj['obsws'].set_scene_item_enabled(
raise ObswsCliBadParameter(str(e)) from e scene_name=scene_name,
raise item_id=int(resp.scene_item_id),
enabled=True,
)
@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)
ctx.obj['obsws'].set_scene_item_enabled( if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
scene_name=scene_name, typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
item_id=int(resp.scene_item_id), raise typer.Exit(code=1)
enabled=False,
) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
except obsws.error.OBSSDKRequestError as e: ctx.obj['obsws'].set_scene_item_enabled(
if e.code == 600: scene_name=scene_name,
raise ObswsCliBadParameter(str(e)) from e item_id=int(resp.scene_item_id),
raise enabled=False,
)
@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):
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) typer.echo(f"Scene '{scene_name}' not found.")
enabled = ctx.obj['obsws'].get_scene_item_enabled( raise typer.Exit(code=1)
scene_name=scene_name,
item_id=int(resp.scene_item_id),
)
new_state = not enabled.scene_item_enabled if not validate.item_in_scene_item_list(ctx, scene_name, item_name):
ctx.obj['obsws'].set_scene_item_enabled( typer.echo(f"Item '{item_name}' not found in scene '{scene_name}'.")
scene_name=scene_name, raise typer.Exit(code=1)
item_id=int(resp.scene_item_id),
enabled=new_state, resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
enabled = ctx.obj['obsws'].get_scene_item_enabled(
scene_name=scene_name,
item_id=int(resp.scene_item_id),
)
if enabled.scene_item_enabled:
ctx.invoke(
hide, ctx=ctx, scene_name=scene_name, item_name=item_name, validated=True
)
else:
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):
resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name) typer.echo(f"Scene '{scene_name}' not found.")
enabled = ctx.obj['obsws'].get_scene_item_enabled( raise typer.Exit(code=1)
scene_name=scene_name,
item_id=int(resp.scene_item_id), 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}'.")
typer.echo( raise typer.Exit(code=1)
f"Item '{item_name}' in scene '{scene_name}' is currently {'visible' if enabled.scene_item_enabled else 'hidden'}."
) resp = ctx.obj['obsws'].get_scene_item_id(scene_name, item_name)
except obsws.error.OBSSDKRequestError as e: enabled = ctx.obj['obsws'].get_scene_item_enabled(
if e.code == 600: scene_name=scene_name,
raise ObswsCliBadParameter(str(e)) from e item_id=int(resp.scene_item_id),
raise )
typer.echo(
f"Item '{item_name}' in scene '{scene_name}' is currently {'visible' if enabled.scene_item_enabled else 'hidden'}."
)

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)