move validation logic into validate module

This commit is contained in:
onyx-and-iris 2025-04-20 21:52:54 +01:00
parent e64b9311d4
commit ab24ca1620
3 changed files with 10 additions and 26 deletions

View File

@ -2,6 +2,7 @@
import typer import typer
from . import validate
from .alias import AliasGroup from .alias import AliasGroup
from .protocols import DataclassProtocol from .protocols import DataclassProtocol
@ -13,16 +14,10 @@ def main():
"""Control groups in OBS scenes.""" """Control groups in OBS scenes."""
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)
@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 groups in a scene.""" """List groups in a scene."""
if not _scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo( typer.echo(
f"Scene '{scene_name}' not found.", f"Scene '{scene_name}' not found.",
err=True, err=True,
@ -52,7 +47,7 @@ def _get_group(group_name: str, resp: DataclassProtocol) -> dict | None:
@app.command() @app.command()
def show(ctx: typer.Context, scene_name: str, group_name: str): def show(ctx: typer.Context, scene_name: str, group_name: str):
"""Show a group in a scene.""" """Show a group in a scene."""
if not _scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo( typer.echo(
f"Scene '{scene_name}' not found.", f"Scene '{scene_name}' not found.",
err=True, err=True,
@ -77,7 +72,7 @@ def show(ctx: typer.Context, scene_name: str, group_name: str):
@app.command() @app.command()
def hide(ctx: typer.Context, scene_name: str, group_name: str): def hide(ctx: typer.Context, scene_name: str, group_name: str):
"""Hide a group in a scene.""" """Hide a group in a scene."""
if not _scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
typer.echo( typer.echo(
f"Scene '{scene_name}' not found.", f"Scene '{scene_name}' not found.",
err=True, err=True,

View File

@ -4,6 +4,7 @@ from typing import Annotated
import typer import typer
from . import validate
from .alias import AliasGroup from .alias import AliasGroup
app = typer.Typer(cls=AliasGroup) app = typer.Typer(cls=AliasGroup)
@ -39,16 +40,10 @@ def list(
typer.echo('\n'.join(input_.get('inputName') for input_ in inputs)) typer.echo('\n'.join(input_.get('inputName') for input_ in inputs))
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)
@app.command() @app.command()
def mute(ctx: typer.Context, input_name: str): def mute(ctx: typer.Context, input_name: str):
"""Mute an input.""" """Mute an input."""
if not _input_in_inputs(ctx, input_name): if not validate.input_in_inputs(ctx, input_name):
typer.echo( typer.echo(
f"Input '{input_name}' not found.", f"Input '{input_name}' not found.",
err=True, err=True,
@ -64,7 +59,7 @@ def mute(ctx: typer.Context, input_name: str):
@app.command() @app.command()
def unmute(ctx: typer.Context, input_name: str): def unmute(ctx: typer.Context, input_name: str):
"""Unmute an input.""" """Unmute an input."""
if not _input_in_inputs(ctx, input_name): if not validate.input_in_inputs(ctx, input_name):
typer.echo( typer.echo(
f"Input '{input_name}' not found.", f"Input '{input_name}' not found.",
err=True, err=True,
@ -80,7 +75,7 @@ def unmute(ctx: typer.Context, input_name: str):
@app.command('toggle | tg') @app.command('toggle | tg')
def toggle(ctx: typer.Context, input_name: str): def toggle(ctx: typer.Context, input_name: str):
"""Toggle an input.""" """Toggle an input."""
if not _input_in_inputs(ctx, input_name): if not validate.input_in_inputs(ctx, input_name):
typer.echo( typer.echo(
f"Input '{input_name}' not found.", f"Input '{input_name}' not found.",
err=True, err=True,

View File

@ -23,12 +23,6 @@ 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,
@ -37,7 +31,7 @@ def current(
] = False, ] = False,
): ):
"""Get the current program scene or preview scene.""" """Get the current program scene or preview scene."""
if preview and not _studio_mode_enabled(ctx): if preview and not validate.studio_mode_enabled(ctx):
typer.echo('Studio mode is not enabled, cannot get preview scene.') typer.echo('Studio mode is not enabled, cannot get preview scene.')
raise typer.Exit(1) raise typer.Exit(1)
@ -59,7 +53,7 @@ def switch(
] = False, ] = False,
): ):
"""Switch to a scene.""" """Switch to a scene."""
if preview and not _studio_mode_enabled(ctx): if preview and not validate.studio_mode_enabled(ctx):
typer.echo('Studio mode is not enabled, cannot set the preview scene.') typer.echo('Studio mode is not enabled, cannot set the preview scene.')
raise typer.Exit(1) raise typer.Exit(1)