replacing amqtt broker's use of 'docopts' with 'typer'

pull/168/head
Andrew Mirsky 2025-05-18 13:34:34 -04:00
rodzic 387878d8e8
commit f6bb6b0497
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: A98E67635CDF2C39
3 zmienionych plików z 30 dodań i 10 usunięć

Wyświetl plik

@ -16,9 +16,10 @@ import asyncio
import logging
from pathlib import Path
from docopt import docopt
import typer
import amqtt
from amqtt import __version__ as amqtt_version
from amqtt.broker import Broker
from amqtt.utils import read_yaml_config
@ -43,15 +44,34 @@ logger = logging.getLogger(__name__)
def main() -> None:
"""Run the MQTT broker."""
arguments = docopt(__doc__, version=amqtt.__version__)
typer.run(broker_main)
def _version(v:bool) -> None:
if v:
typer.echo(f"{amqtt_version}")
raise typer.Exit(code=0)
def broker_main(
config_file: str | None = typer.Option(None, "-c", help="Broker configuration file (YAML format)"),
debug: bool = typer.Option(False, "-d", help="Enable debug messages"),
version: bool = typer.Option( # noqa : ARG001
False,
"--version",
callback=_version,
is_eager=True,
help="Show version and exit",
),
) -> None:
formatter = "[%(asctime)s] :: %(levelname)s - %(message)s"
level = logging.DEBUG if arguments["-d"] else logging.INFO
level = logging.DEBUG if debug else logging.INFO
logging.basicConfig(level=level, format=formatter)
config = None
if arguments["-c"]:
config = read_yaml_config(arguments["-c"])
if config_file:
config = read_yaml_config(config_file)
else:
config = read_yaml_config(Path(__file__).parent / "default_broker.yaml")
logger.debug("Using default configuration")

Wyświetl plik

@ -119,7 +119,7 @@ async def do_pub(
def main() -> None:
"""Entry point for the amqtt publisher."""
typer.run(publisher)
typer.run(publisher_main)
def _version(v: bool) -> None:
@ -128,7 +128,7 @@ def _version(v: bool) -> None:
raise typer.Exit(code=0)
def publisher( # pylint: disable=R0914,R0917 # noqa : PLR0913
def publisher_main( # pylint: disable=R0914,R0917 # noqa : PLR0913
url: str = typer.Option(
..., "--url", help="Broker connection URL (must conform to MQTT URI scheme: mqtt://<username:password>@HOST:port)"
),

Wyświetl plik

@ -91,7 +91,7 @@ async def do_sub(client: MQTTClient,
def main() -> None:
typer.run(subscribe)
typer.run(subscribe_main)
def _version(v:bool) -> None:
@ -100,7 +100,7 @@ def _version(v:bool) -> None:
raise typer.Exit(code=0)
def subscribe( # pylint: disable=R0914,R0917 # noqa : PLR0913
def subscribe_main( # pylint: disable=R0914,R0917 # noqa : PLR0913
url: str = typer.Option(..., help="Broker connection URL (must conform to MQTT URI scheme)", show_default=False),
config_file: str | None = typer.Option(None, "-c", help="Broker configuration file (YAML format)"),
client_id: str | None = typer.Option(None, "-i", help="Id to use as client ID"),