feat(pyvtt): add support for optional preset in commands

- Add `preset` argument to `send_cmd` for passing optional presets.
- Modify command formatting to include preset if provided.
- Update CLI to accept an optional `preset` argument.
- Extend socket listener to parse and validate preset values.
- Enhance functionality to allow preset-based dynamic behavior.

Signed-off-by: Max P. <Mail@MPassarello.de>
This commit is contained in:
2025-04-30 15:44:35 +02:00
parent 7719cf429d
commit 495452f7bd
2 changed files with 24 additions and 2 deletions

View File

@@ -2,10 +2,12 @@ import socket
import sys
import argparse
from pyvtt.configuration import read_configurations
from typing import Optional
CONFIGURATION = read_configurations()
def send_cmd(cmd: str, socket_path: str):
def send_cmd(cmd: str, socket_path: str, preset: Optional[str] = None) -> None:
"""
Sends a command to a Unix domain socket server.
@@ -26,6 +28,8 @@ def send_cmd(cmd: str, socket_path: str):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.settimeout(3)
client.connect(socket_path)
if preset:
cmd = f"{cmd}::::{preset}"
client.sendall(cmd.encode())
except FileNotFoundError:
print(f"Error: The socket file '{socket_path}' does not exist.", file=sys.stderr, flush=True)
@@ -47,6 +51,15 @@ def main():
default="toggle",
help="The command to send to the server (default: toggle).",
)
parser.add_argument(
"preset",
nargs="?",
default=None,
help="The preset to use (optional)."
)
args = parser.parse_args()
send_cmd(args.command, CONFIGURATION["socket_path"])
send_cmd(args.command, CONFIGURATION["socket_path"], args.preset)

View File

@@ -166,6 +166,15 @@ class SocketListener(threading.Thread):
conn, _ = self.sock.accept()
with conn:
data = conn.recv(1024).decode().strip()
if data:
cmd = data.split("::::")
if len(cmd) > 1:
data = cmd[0]
preset = cmd[1]
if preset in [p["name"] for p in CONFIGURATION["presets"]]:
self.tray_app.set_preset([p["name"] for p in CONFIGURATION["presets"]].index(preset))
else:
data = cmd[0]
if data == "toggle":
self.tray_app.toggle_recording()
elif data == "start":