Skip to content

Commit 1b093f7

Browse files
committed
deal with missing extra dependencies
1 parent ca28304 commit 1b093f7

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/pms/optional.py

-24
This file was deleted.

src/pms/service/influxdb.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
import os
21
import json
32
from dataclasses import fields
43
from typing import Dict, Callable
54
from mypy_extensions import NamedArg
65

6+
from typer import Context, Option, style, colors, echo, Abort
7+
78
try:
89
from influxdb import InfluxDBClient as client
910
except ModuleNotFoundError: # pragma: no cover
1011
client = None # type: ignore
11-
from typer import Context, Option
1212

13-
from pms.optional import missing_optional_module
13+
14+
def __missing_influxdb(): # pragma: no cover
15+
name = style(__name__, fg=colors.GREEN, bold=True)
16+
package = style("pypms", fg=colors.GREEN, bold=True)
17+
module = style("influxdb", fg=colors.RED, bold=True)
18+
extra = style("influxdb", fg=colors.RED, bold=True)
19+
pip = style("python3 -m pip instal --upgrade", fg=colors.GREEN)
20+
pipx = style("pipx inject", fg=colors.GREEN)
21+
echo(
22+
f"""
23+
{name} provides additional functionality to {package}.
24+
This functionality requires the {module} module, which is not installed.
25+
You can install this additional dependency with
26+
\t{pip} {package}[{extra}]
27+
Or, if you installed {package} with pipx
28+
\t{pipx} {package} {module}
29+
"""
30+
)
31+
raise Abort()
1432

1533

1634
def client_pub(
@@ -20,7 +38,7 @@ def client_pub(
2038
None,
2139
]:
2240
if client is None:
23-
missing_optional_module(__name__, "influxdb")
41+
__missing_influxdb()
2442
c = client(host, port, username, password, None)
2543
databases = c.get_list_database()
2644
if len(list(filter(lambda x: x["name"] == db_name, databases))) == 0:

src/pms/service/mqtt.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
from datetime import datetime
22
from dataclasses import fields
3-
43
from typing import Dict, Union, Callable, NamedTuple
54

5+
from typer import Context, Option, style, colors, echo, Abort
6+
67
try:
78
from paho.mqtt import client
89
except ModuleNotFoundError: # pragma: no cover
910
client = None # type: ignore
10-
from typer import Context, Option
1111

1212
from pms import logger
13-
from pms.optional import missing_optional_module
1413
from pms.sensor.base import ObsData
1514

1615

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+
1736
def client_pub(
1837
*, topic: str, host: str, port: int, username: str, password: str
1938
) -> Callable[[Dict[str, Union[int, str]]], None]:
2039
if client is None:
21-
missing_optional_module(__name__, "influxdb")
40+
__missing_mqtt()
2241
c = client.Client(topic)
2342
c.enable_logger(logger)
2443
if username:
@@ -93,7 +112,7 @@ def on_message(client, userdata, msg):
93112
on_sensordata(data)
94113

95114
if client is None:
96-
missing_optional_module(__name__, "influxdb")
115+
__missing_mqtt()
97116
c = client.Client(topic)
98117
c.enable_logger(logger)
99118
if username:

0 commit comments

Comments
 (0)