kopia lustrzana https://github.com/Yakifo/amqtt
replacing amqtt broker's use of 'docopts' with 'typer'
rodzic
387878d8e8
commit
f6bb6b0497
|
@ -16,9 +16,10 @@ import asyncio
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from docopt import docopt
|
import typer
|
||||||
|
|
||||||
import amqtt
|
import amqtt
|
||||||
|
from amqtt import __version__ as amqtt_version
|
||||||
from amqtt.broker import Broker
|
from amqtt.broker import Broker
|
||||||
from amqtt.utils import read_yaml_config
|
from amqtt.utils import read_yaml_config
|
||||||
|
|
||||||
|
@ -43,15 +44,34 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""Run the MQTT broker."""
|
"""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"
|
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)
|
logging.basicConfig(level=level, format=formatter)
|
||||||
|
|
||||||
config = None
|
if config_file:
|
||||||
if arguments["-c"]:
|
config = read_yaml_config(config_file)
|
||||||
config = read_yaml_config(arguments["-c"])
|
|
||||||
else:
|
else:
|
||||||
config = read_yaml_config(Path(__file__).parent / "default_broker.yaml")
|
config = read_yaml_config(Path(__file__).parent / "default_broker.yaml")
|
||||||
logger.debug("Using default configuration")
|
logger.debug("Using default configuration")
|
||||||
|
|
|
@ -119,7 +119,7 @@ async def do_pub(
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""Entry point for the amqtt publisher."""
|
"""Entry point for the amqtt publisher."""
|
||||||
typer.run(publisher)
|
typer.run(publisher_main)
|
||||||
|
|
||||||
|
|
||||||
def _version(v: bool) -> None:
|
def _version(v: bool) -> None:
|
||||||
|
@ -128,7 +128,7 @@ def _version(v: bool) -> None:
|
||||||
raise typer.Exit(code=0)
|
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: str = typer.Option(
|
||||||
..., "--url", help="Broker connection URL (must conform to MQTT URI scheme: mqtt://<username:password>@HOST:port)"
|
..., "--url", help="Broker connection URL (must conform to MQTT URI scheme: mqtt://<username:password>@HOST:port)"
|
||||||
),
|
),
|
||||||
|
|
|
@ -91,7 +91,7 @@ async def do_sub(client: MQTTClient,
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
typer.run(subscribe)
|
typer.run(subscribe_main)
|
||||||
|
|
||||||
|
|
||||||
def _version(v:bool) -> None:
|
def _version(v:bool) -> None:
|
||||||
|
@ -100,7 +100,7 @@ def _version(v:bool) -> None:
|
||||||
raise typer.Exit(code=0)
|
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),
|
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)"),
|
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"),
|
client_id: str | None = typer.Option(None, "-i", help="Id to use as client ID"),
|
||||||
|
|
Ładowanie…
Reference in New Issue