From 1b093f772d68bec9bd2d7577bf39e9716dbf2a79 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Fri, 25 Sep 2020 12:55:14 +0200 Subject: [PATCH] deal with missing extra dependencies --- src/pms/optional.py | 24 ------------------------ src/pms/service/influxdb.py | 26 ++++++++++++++++++++++---- src/pms/service/mqtt.py | 29 ++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 33 deletions(-) delete mode 100644 src/pms/optional.py diff --git a/src/pms/optional.py b/src/pms/optional.py deleted file mode 100644 index 17b6a01..0000000 --- a/src/pms/optional.py +++ /dev/null @@ -1,24 +0,0 @@ -from typer import echo, style, colors, Abort - - -def missing_optional_module(name: str, module: str): - extras = {"paho-mqtt": "mqtt", "influxdb": "influxdb"} - if module not in extras: # pragma: no cover - return - package = style("pypms", fg=colors.GREEN, bold=True) - name = style(name, fg=colors.GREEN, bold=True) - extra = style(extras[module], fg=colors.RED, bold=True) - module = style(module, fg=colors.RED, bold=True) - pip = style("python3 -m pip instal --upgrade", fg=colors.GREEN) - pipx = style("pipx inject", fg=colors.GREEN) - echo( - f""" -{name} provides additional functionality to {package}. -This functionality requires the {module} module, which is not installed. -You can install this additional dependency with -\t{pip} {package}[{extra}] -Or, if you installed {package} with pipx -\t{pipx} {package} {module} -""" - ) - raise Abort() diff --git a/src/pms/service/influxdb.py b/src/pms/service/influxdb.py index 9a1e6d1..3dac8bc 100644 --- a/src/pms/service/influxdb.py +++ b/src/pms/service/influxdb.py @@ -1,16 +1,34 @@ -import os import json from dataclasses import fields from typing import Dict, Callable from mypy_extensions import NamedArg +from typer import Context, Option, style, colors, echo, Abort + try: from influxdb import InfluxDBClient as client except ModuleNotFoundError: # pragma: no cover client = None # type: ignore -from typer import Context, Option -from pms.optional import missing_optional_module + +def __missing_influxdb(): # pragma: no cover + name = style(__name__, fg=colors.GREEN, bold=True) + package = style("pypms", fg=colors.GREEN, bold=True) + module = style("influxdb", fg=colors.RED, bold=True) + extra = style("influxdb", fg=colors.RED, bold=True) + pip = style("python3 -m pip instal --upgrade", fg=colors.GREEN) + pipx = style("pipx inject", fg=colors.GREEN) + echo( + f""" +{name} provides additional functionality to {package}. +This functionality requires the {module} module, which is not installed. +You can install this additional dependency with +\t{pip} {package}[{extra}] +Or, if you installed {package} with pipx +\t{pipx} {package} {module} +""" + ) + raise Abort() def client_pub( @@ -20,7 +38,7 @@ def client_pub( None, ]: if client is None: - missing_optional_module(__name__, "influxdb") + __missing_influxdb() c = client(host, port, username, password, None) databases = c.get_list_database() if len(list(filter(lambda x: x["name"] == db_name, databases))) == 0: diff --git a/src/pms/service/mqtt.py b/src/pms/service/mqtt.py index 1e700b0..36f505c 100644 --- a/src/pms/service/mqtt.py +++ b/src/pms/service/mqtt.py @@ -1,24 +1,43 @@ from datetime import datetime from dataclasses import fields - from typing import Dict, Union, Callable, NamedTuple +from typer import Context, Option, style, colors, echo, Abort + try: from paho.mqtt import client except ModuleNotFoundError: # pragma: no cover client = None # type: ignore -from typer import Context, Option from pms import logger -from pms.optional import missing_optional_module from pms.sensor.base import ObsData +def __missing_mqtt(): # pragma: no cover + name = style(__name__, fg=colors.GREEN, bold=True) + package = style("pypms", fg=colors.GREEN, bold=True) + module = style("paho-mqtt", fg=colors.RED, bold=True) + extra = style("mqtt", fg=colors.RED, bold=True) + pip = style("python3 -m pip instal --upgrade", fg=colors.GREEN) + pipx = style("pipx inject", fg=colors.GREEN) + echo( + f""" +{name} provides additional functionality to {package}. +This functionality requires the {module} module, which is not installed. +You can install this additional dependency with +\t{pip} {package}[{extra}] +Or, if you installed {package} with pipx +\t{pipx} {package} {module} +""" + ) + raise Abort() + + def client_pub( *, topic: str, host: str, port: int, username: str, password: str ) -> Callable[[Dict[str, Union[int, str]]], None]: if client is None: - missing_optional_module(__name__, "influxdb") + __missing_mqtt() c = client.Client(topic) c.enable_logger(logger) if username: @@ -93,7 +112,7 @@ def on_message(client, userdata, msg): on_sensordata(data) if client is None: - missing_optional_module(__name__, "influxdb") + __missing_mqtt() c = client.Client(topic) c.enable_logger(logger) if username: