|
1 | 1 | from datetime import datetime
|
2 | 2 | from dataclasses import fields
|
3 |
| - |
4 | 3 | from typing import Dict, Union, Callable, NamedTuple
|
5 | 4 |
|
| 5 | +from typer import Context, Option, style, colors, echo, Abort |
| 6 | + |
6 | 7 | try:
|
7 | 8 | from paho.mqtt import client
|
8 | 9 | except ModuleNotFoundError: # pragma: no cover
|
9 | 10 | client = None # type: ignore
|
10 |
| -from typer import Context, Option |
11 | 11 |
|
12 | 12 | from pms import logger
|
13 |
| -from pms.optional import missing_optional_module |
14 | 13 | from pms.sensor.base import ObsData
|
15 | 14 |
|
16 | 15 |
|
| 16 | +def __missing_mqtt(): # pragma: no cover |
| 17 | + name = style(__name__, fg=colors.GREEN, bold=True) |
| 18 | + package = style("pypms", fg=colors.GREEN, bold=True) |
| 19 | + module = style("paho-mqtt", fg=colors.RED, bold=True) |
| 20 | + extra = style("mqtt", fg=colors.RED, bold=True) |
| 21 | + pip = style("python3 -m pip instal --upgrade", fg=colors.GREEN) |
| 22 | + pipx = style("pipx inject", fg=colors.GREEN) |
| 23 | + echo( |
| 24 | + f""" |
| 25 | +{name} provides additional functionality to {package}. |
| 26 | +This functionality requires the {module} module, which is not installed. |
| 27 | +You can install this additional dependency with |
| 28 | +\t{pip} {package}[{extra}] |
| 29 | +Or, if you installed {package} with pipx |
| 30 | +\t{pipx} {package} {module} |
| 31 | +""" |
| 32 | + ) |
| 33 | + raise Abort() |
| 34 | + |
| 35 | + |
17 | 36 | def client_pub(
|
18 | 37 | *, topic: str, host: str, port: int, username: str, password: str
|
19 | 38 | ) -> Callable[[Dict[str, Union[int, str]]], None]:
|
20 | 39 | if client is None:
|
21 |
| - missing_optional_module(__name__, "influxdb") |
| 40 | + __missing_mqtt() |
22 | 41 | c = client.Client(topic)
|
23 | 42 | c.enable_logger(logger)
|
24 | 43 | if username:
|
@@ -93,7 +112,7 @@ def on_message(client, userdata, msg):
|
93 | 112 | on_sensordata(data)
|
94 | 113 |
|
95 | 114 | if client is None:
|
96 |
| - missing_optional_module(__name__, "influxdb") |
| 115 | + __missing_mqtt() |
97 | 116 | c = client.Client(topic)
|
98 | 117 | c.enable_logger(logger)
|
99 | 118 | if username:
|
|
0 commit comments