Debian Codegraph @

src/aiovlc/cli.py

raw
1 """CLI for the VLC client."""
2
3 import asyncio
4 from collections.abc import Awaitable, Callable
5 from typing import Annotated
6
7 from rich import print
8 import typer
9
10 from .client import Client
11 from .exceptions import AIOVLCError
12 from .model.command import Info, Status
13
14 app = typer.Typer()
15
16 ClientFactory = Callable[[], Awaitable[Client]]
17
18
19 @app.command()
20 def client(
21 password: Annotated[
22 str,
23 typer.Option(help="Password of the connection.", prompt=True, hide_input=True),
24 ],
25 host: Annotated[str, typer.Option(help="Host of the connection.")] = "localhost",
26 port: Annotated[int, typer.Option(help="Port of the connection.")] = 4212,
27 ) -> None:
28 """Start a client."""
29
30 async def client_factory() -> Client:
31 """Return a client."""
32 return Client(password, host=host, port=port)
33
34 run_client(client_factory)
35
36
37 def run_client(client_factory: ClientFactory) -> None:
38 """Run a client."""
39 print("Starting client")
40 try:
41 asyncio.run(start_client(client_factory))
42 except KeyboardInterrupt:
43 pass
44 finally:
45 print("Exiting CLI")
46
47
48 async def start_client(client_factory: ClientFactory) -> None:
49 """Start the client."""
50 vlc_client = await client_factory()
51
52 try:
53 async with vlc_client:
54 await vlc_client.login()
55 await handle_client(vlc_client)
56 except AIOVLCError as err:
57 print("Error:", err)
58
59
60 async def handle_client(vlc_client: Client) -> None:
61 """Handle the client calls."""
62 while True:
63 command = Status()
64 output = await command.send(vlc_client)
65 print("Received:", output)
66 info_command = Info()
67 info_output = await info_command.send(vlc_client)
68 print("Received:", info_output)
69 await asyncio.sleep(10)