mirror of
https://github.com/onyx-and-iris/obsws-cli.git
synced 2025-05-20 16:10:26 +01:00
add env var and defaults to --help
move Settings into settings module patch bump
This commit is contained in:
parent
34fbc77182
commit
81762508a7
@ -1,4 +1,4 @@
|
||||
# SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
__version__ = "0.10.3"
|
||||
__version__ = "0.10.4"
|
||||
|
@ -1,11 +1,9 @@
|
||||
"""Command line interface for the OBS WebSocket API."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Optional
|
||||
from typing import Annotated
|
||||
|
||||
import obsws_python as obsws
|
||||
import typer
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
from . import (
|
||||
group,
|
||||
@ -16,31 +14,13 @@ from . import (
|
||||
scene,
|
||||
scenecollection,
|
||||
sceneitem,
|
||||
settings,
|
||||
stream,
|
||||
studiomode,
|
||||
virtualcam,
|
||||
)
|
||||
from .alias import AliasGroup
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Settings for the OBS WebSocket client."""
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=(
|
||||
'.env',
|
||||
Path.home() / '.config' / 'obsws-cli' / 'obsws.env',
|
||||
),
|
||||
env_file_encoding='utf-8',
|
||||
env_prefix='OBS_',
|
||||
)
|
||||
|
||||
HOST: str = 'localhost'
|
||||
PORT: int = 4455
|
||||
PASSWORD: str = '' # No password by default
|
||||
TIMEOUT: int = 5 # Timeout for requests in seconds
|
||||
|
||||
|
||||
app = typer.Typer(cls=AliasGroup)
|
||||
for module in (
|
||||
group,
|
||||
@ -61,31 +41,26 @@ for module in (
|
||||
@app.callback()
|
||||
def main(
|
||||
ctx: typer.Context,
|
||||
host: Annotated[Optional[str], typer.Option(help='WebSocket host')] = None,
|
||||
port: Annotated[Optional[int], typer.Option(help='WebSocket port')] = None,
|
||||
password: Annotated[Optional[str], typer.Option(help='WebSocket password')] = None,
|
||||
timeout: Annotated[Optional[int], typer.Option(help='WebSocket timeout')] = None,
|
||||
host: Annotated[
|
||||
str,
|
||||
typer.Option(
|
||||
envvar='OBS_HOST', help='WebSocket host', show_default='localhost'
|
||||
),
|
||||
] = settings.get('HOST'),
|
||||
port: Annotated[
|
||||
int, typer.Option(envvar='OBS_PORT', help='WebSocket port', show_default=4455)
|
||||
] = settings.get('PORT'),
|
||||
password: Annotated[
|
||||
str,
|
||||
typer.Option(envvar='OBS_PASSWORD', help='WebSocket password', show_default=''),
|
||||
] = settings.get('PASSWORD'),
|
||||
timeout: Annotated[
|
||||
int,
|
||||
typer.Option(envvar='OBS_TIMEOUT', help='WebSocket timeout', show_default=5),
|
||||
] = settings.get('TIMEOUT'),
|
||||
):
|
||||
"""obsws_cli is a command line interface for the OBS WebSocket API."""
|
||||
settings = Settings()
|
||||
# Allow overriding settings with command line options
|
||||
if host:
|
||||
settings.HOST = host
|
||||
if port:
|
||||
settings.PORT = port
|
||||
if password:
|
||||
settings.PASSWORD = password
|
||||
if timeout:
|
||||
settings.TIMEOUT = timeout
|
||||
|
||||
ctx.obj = ctx.with_resource(
|
||||
obsws.ReqClient(
|
||||
host=settings.HOST,
|
||||
port=settings.PORT,
|
||||
password=settings.PASSWORD,
|
||||
timeout=settings.TIMEOUT,
|
||||
)
|
||||
)
|
||||
ctx.obj = ctx.with_resource(obsws.ReqClient(**ctx.params))
|
||||
|
||||
|
||||
@app.command()
|
||||
|
31
obsws_cli/settings.py
Normal file
31
obsws_cli/settings.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""module for settings management."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Settings for the OBS WebSocket client."""
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=(
|
||||
'.env',
|
||||
Path.home() / '.config' / 'obsws-cli' / 'obsws.env',
|
||||
),
|
||||
env_file_encoding='utf-8',
|
||||
env_prefix='OBS_',
|
||||
)
|
||||
|
||||
HOST: str = 'localhost'
|
||||
PORT: int = 4455
|
||||
PASSWORD: str = '' # No password by default
|
||||
TIMEOUT: int = 5 # Timeout for requests in seconds
|
||||
|
||||
|
||||
_settings = Settings().model_dump()
|
||||
|
||||
|
||||
def get(key: str) -> str:
|
||||
"""Get a setting by key."""
|
||||
return _settings.get(key)
|
Loading…
x
Reference in New Issue
Block a user