convert group commands

This commit is contained in:
onyx-and-iris 2025-07-24 01:39:02 +01:00
parent de1c604c46
commit ae8ff20cf4

View File

@ -2,42 +2,42 @@
from typing import Annotated, Optional from typing import Annotated, Optional
import typer from cyclopts import App, Argument, Parameter
from rich.table import Table from rich.table import Table
from rich.text import Text from rich.text import Text
from . import console, util, validate from . import console, util, validate
from .alias import SubTyperAliasGroup from .context import Context
from .enum import ExitCode
from .error import OBSWSCLIError
from .protocols import DataclassProtocol from .protocols import DataclassProtocol
app = typer.Typer(cls=SubTyperAliasGroup) app = App(name='group', help='Commands for managing groups in OBS scenes')
@app.callback() @app.command(name=['list', 'ls'])
def main():
"""Control groups in OBS scenes."""
@app.command('list | ls')
def list_( def list_(
ctx: typer.Context,
scene_name: Annotated[ scene_name: Annotated[
Optional[str], Optional[str],
typer.Argument( Argument(
show_default='The current scene', hint='Scene name to list groups for',
help='Scene name to list groups for',
), ),
] = None, ] = None,
/,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""List groups in a scene.""" """List groups in a scene."""
if not scene_name: if not scene_name:
scene_name = ctx.obj['obsws'].get_current_program_scene().scene_name scene_name = ctx.client.get_current_program_scene().scene_name
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f"Scene '{scene_name}' not found.") raise OBSWSCLIError(
raise typer.Exit(1) f'Scene [yellow]{scene_name}[/yellow] not found.',
code=ExitCode.ERROR,
)
resp = ctx.obj['obsws'].get_scene_item_list(scene_name) resp = ctx.client.get_scene_item_list(scene_name)
groups = [ groups = [
(item.get('sceneItemId'), item.get('sourceName'), item.get('sceneItemEnabled')) (item.get('sceneItemId'), item.get('sourceName'), item.get('sceneItemEnabled'))
for item in resp.scene_items for item in resp.scene_items
@ -45,20 +45,20 @@ def list_(
] ]
if not groups: if not groups:
console.out.print( raise OBSWSCLIError(
f'No groups found in scene {console.highlight(ctx, scene_name)}.' f'No groups found in scene {console.highlight(ctx, scene_name)}.',
code=ExitCode.SUCCESS,
) )
raise typer.Exit()
table = Table( table = Table(
title=f'Groups in Scene: {scene_name}', title=f'Groups in Scene: {scene_name}',
padding=(0, 2), padding=(0, 2),
border_style=ctx.obj['style'].border, border_style=ctx.style.border,
) )
columns = [ columns = [
(Text('ID', justify='center'), 'center', ctx.obj['style'].column), (Text('ID', justify='center'), 'center', ctx.style.column),
(Text('Group Name', justify='center'), 'left', ctx.obj['style'].column), (Text('Group Name', justify='center'), 'left', ctx.style.column),
(Text('Enabled', justify='center'), 'center', None), (Text('Enabled', justify='center'), 'center', None),
] ]
for heading, justify, style in columns: for heading, justify, style in columns:
@ -87,30 +87,32 @@ def _get_group(group_name: str, resp: DataclassProtocol) -> dict | None:
return group return group
@app.command('show | sh') @app.command(name=['show', 'sh'])
def show( def show(
ctx: typer.Context,
scene_name: Annotated[ scene_name: Annotated[
str, str,
typer.Argument(..., show_default=False, help='Scene name the group is in'), Argument(hint='Scene name the group is in'),
],
group_name: Annotated[
str, typer.Argument(..., show_default=False, help='Group name to show')
], ],
group_name: Annotated[str, Argument(hint='Group name to show')],
/,
*,
ctx: Annotated[Context, Parameter(parse=False)],
): ):
"""Show a group in a scene.""" """Show a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f"Scene '{scene_name}' not found.") raise OBSWSCLIError(
raise typer.Exit(1) f'Scene [yellow]{scene_name}[/yellow] not found.',
code=ExitCode.ERROR,
resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
console.err.print(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].'
) )
raise typer.Exit(1)
ctx.obj['obsws'].set_scene_item_enabled( resp = ctx.client.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
raise OBSWSCLIError(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].',
code=ExitCode.ERROR,
)
ctx.client.set_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(group.get('sceneItemId')), item_id=int(group.get('sceneItemId')),
enabled=True, enabled=True,
@ -119,29 +121,29 @@ def show(
console.out.print(f'Group {console.highlight(ctx, group_name)} is now visible.') console.out.print(f'Group {console.highlight(ctx, group_name)} is now visible.')
@app.command('hide | h') @app.command(name=['hide', 'h'])
def hide( def hide(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the group is in')],
scene_name: Annotated[ group_name: Annotated[str, Argument(hint='Group name to hide')],
str, typer.Argument(..., show_default=False, help='Scene name the group is in') /,
], *,
group_name: Annotated[ ctx: Annotated[Context, Parameter(parse=False)],
str, typer.Argument(..., show_default=False, help='Group name to hide')
],
): ):
"""Hide a group in a scene.""" """Hide a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.') raise OBSWSCLIError(
raise typer.Exit(1) f'Scene [yellow]{scene_name}[/yellow] not found.',
code=ExitCode.ERROR,
resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
console.err.print(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].'
) )
raise typer.Exit(1)
ctx.obj['obsws'].set_scene_item_enabled( resp = ctx.client.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
raise OBSWSCLIError(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].',
code=ExitCode.ERROR,
)
ctx.client.set_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(group.get('sceneItemId')), item_id=int(group.get('sceneItemId')),
enabled=False, enabled=False,
@ -150,30 +152,30 @@ def hide(
console.out.print(f'Group {console.highlight(ctx, group_name)} is now hidden.') console.out.print(f'Group {console.highlight(ctx, group_name)} is now hidden.')
@app.command('toggle | tg') @app.command(name=['toggle', 'tg'])
def toggle( def toggle(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the group is in')],
scene_name: Annotated[ group_name: Annotated[str, Argument(hint='Group name to toggle')],
str, typer.Argument(..., show_default=False, help='Scene name the group is in') /,
], *,
group_name: Annotated[ ctx: Annotated[Context, Parameter(parse=False)],
str, typer.Argument(..., show_default=False, help='Group name to toggle')
],
): ):
"""Toggle a group in a scene.""" """Toggle a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.') raise OBSWSCLIError(
raise typer.Exit(1) f'Scene [yellow]{scene_name}[/yellow] not found.',
code=ExitCode.ERROR,
resp = ctx.obj['obsws'].get_scene_item_list(scene_name) )
if (group := _get_group(group_name, resp)) is None:
console.err.print( resp = ctx.client.get_scene_item_list(scene_name)
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].' if (group := _get_group(group_name, resp)) is None:
raise OBSWSCLIError(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].',
code=ExitCode.ERROR,
) )
raise typer.Exit(1)
new_state = not group.get('sceneItemEnabled') new_state = not group.get('sceneItemEnabled')
ctx.obj['obsws'].set_scene_item_enabled( ctx.client.set_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(group.get('sceneItemId')), item_id=int(group.get('sceneItemId')),
enabled=new_state, enabled=new_state,
@ -185,29 +187,29 @@ def toggle(
console.out.print(f'Group {console.highlight(ctx, group_name)} is now hidden.') console.out.print(f'Group {console.highlight(ctx, group_name)} is now hidden.')
@app.command('status | ss') @app.command(name=['status', 'ss'])
def status( def status(
ctx: typer.Context, scene_name: Annotated[str, Argument(hint='Scene name the group is in')],
scene_name: Annotated[ group_name: Annotated[str, Argument(hint='Group name to check status')],
str, typer.Argument(..., show_default=False, help='Scene name the group is in') /,
], *,
group_name: Annotated[ ctx: Annotated[Context, Parameter(parse=False)],
str, typer.Argument(..., show_default=False, help='Group name to check status')
],
): ):
"""Get the status of a group in a scene.""" """Get the status of a group in a scene."""
if not validate.scene_in_scenes(ctx, scene_name): if not validate.scene_in_scenes(ctx, scene_name):
console.err.print(f'Scene [yellow]{scene_name}[/yellow] not found.') raise OBSWSCLIError(
raise typer.Exit(1) f'Scene [yellow]{scene_name}[/yellow] not found.',
code=ExitCode.ERROR,
resp = ctx.obj['obsws'].get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
console.err.print(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].'
) )
raise typer.Exit(1)
enabled = ctx.obj['obsws'].get_scene_item_enabled( resp = ctx.client.get_scene_item_list(scene_name)
if (group := _get_group(group_name, resp)) is None:
raise OBSWSCLIError(
f'Group [yellow]{group_name}[/yellow] not found in scene [yellow]{scene_name}[/yellow].',
code=ExitCode.ERROR,
)
enabled = ctx.client.get_scene_item_enabled(
scene_name=scene_name, scene_name=scene_name,
item_id=int(group.get('sceneItemId')), item_id=int(group.get('sceneItemId')),
) )