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:
@@ -2,10 +2,12 @@ import socket
|
|||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
from pyvtt.configuration import read_configurations
|
from pyvtt.configuration import read_configurations
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
CONFIGURATION = read_configurations()
|
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.
|
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:
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
|
||||||
client.settimeout(3)
|
client.settimeout(3)
|
||||||
client.connect(socket_path)
|
client.connect(socket_path)
|
||||||
|
if preset:
|
||||||
|
cmd = f"{cmd}::::{preset}"
|
||||||
client.sendall(cmd.encode())
|
client.sendall(cmd.encode())
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"Error: The socket file '{socket_path}' does not exist.", file=sys.stderr, flush=True)
|
print(f"Error: The socket file '{socket_path}' does not exist.", file=sys.stderr, flush=True)
|
||||||
@@ -47,6 +51,15 @@ def main():
|
|||||||
default="toggle",
|
default="toggle",
|
||||||
help="The command to send to the server (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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
send_cmd(args.command, CONFIGURATION["socket_path"])
|
send_cmd(args.command, CONFIGURATION["socket_path"], args.preset)
|
@@ -166,6 +166,15 @@ class SocketListener(threading.Thread):
|
|||||||
conn, _ = self.sock.accept()
|
conn, _ = self.sock.accept()
|
||||||
with conn:
|
with conn:
|
||||||
data = conn.recv(1024).decode().strip()
|
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":
|
if data == "toggle":
|
||||||
self.tray_app.toggle_recording()
|
self.tray_app.toggle_recording()
|
||||||
elif data == "start":
|
elif data == "start":
|
||||||
|
Reference in New Issue
Block a user