swap out pydantic-settings for dotenv (speedup import time)

add short names for root options.
This commit is contained in:
2025-06-03 12:39:39 +01:00
parent 7b94ca2d7d
commit 8a04303af7
6 changed files with 82 additions and 184 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online>
#
# SPDX-License-Identifier: MIT
__version__ = "0.15.1"
__version__ = "0.15.2"

View File

@@ -53,7 +53,7 @@ err_console = Console(stderr=True)
def version_callback(value: bool):
"""Show the version of the CLI."""
if value:
out_console.print(f'obsws_cli version: {obsws_cli_version}')
out_console.print(f'obsws-cli version: {obsws_cli_version}')
raise typer.Exit()
@@ -63,20 +63,39 @@ def main(
host: Annotated[
str,
typer.Option(
envvar='OBS_HOST', help='WebSocket host', show_default='localhost'
'--host',
'-H',
envvar='OBS_HOST',
help='WebSocket host',
show_default='localhost',
),
] = settings.get('HOST'),
] = settings.get('OBS_HOST'),
port: Annotated[
int, typer.Option(envvar='OBS_PORT', help='WebSocket port', show_default=4455)
] = settings.get('PORT'),
int,
typer.Option(
'--port', '-P', envvar='OBS_PORT', help='WebSocket port', show_default=4455
),
] = settings.get('OBS_PORT'),
password: Annotated[
str,
typer.Option(envvar='OBS_PASSWORD', help='WebSocket password', show_default=''),
] = settings.get('PASSWORD'),
typer.Option(
'--password',
'-p',
envvar='OBS_PASSWORD',
help='WebSocket password',
show_default='',
),
] = settings.get('OBS_PASSWORD'),
timeout: Annotated[
int,
typer.Option(envvar='OBS_TIMEOUT', help='WebSocket timeout', show_default=5),
] = settings.get('TIMEOUT'),
typer.Option(
'--timeout',
'-T',
envvar='OBS_TIMEOUT',
help='WebSocket timeout',
show_default=5,
),
] = settings.get('OBS_TIMEOUT'),
version: Annotated[
bool,
typer.Option(

View File

@@ -1,31 +1,51 @@
"""module for settings management."""
"""module for settings management for obsws-cli."""
from collections import UserDict
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
from dotenv import dotenv_values
class Settings(BaseSettings):
class Settings(UserDict):
"""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_',
)
def __init__(self, *args, **kwargs):
"""Initialize the Settings object."""
kwargs.update(
{
**dotenv_values('.env'),
**dotenv_values(Path.home() / '.config' / 'obsws-cli' / 'obsws.env'),
}
)
super().__init__(*args, **kwargs)
HOST: str = 'localhost'
PORT: int = 4455
PASSWORD: str = '' # No password by default
TIMEOUT: int = 5 # Timeout for requests in seconds
def __getitem__(self, key):
"""Get a setting value by key."""
if not key.startswith('OBS_'):
key = f'OBS_{key.upper()}'
return self.data[key]
def __setitem__(self, key, value):
"""Set a setting value by key."""
self.data[key] = value
_settings = Settings().model_dump()
_settings = Settings(
OBS_HOST='localhost', OBS_PORT=4455, OBS_PASSWORD='', OBS_TIMEOUT=5
)
def get(key: str) -> str:
"""Get a setting by key."""
return _settings.get(key)
def get(key: str):
"""Get a setting value by key.
Args:
key (str): The key of the setting to retrieve.
Returns:
The value of the setting.
Raises:
KeyError: If the key does not exist in the settings.
"""
return _settings[key]