summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.com>2020-03-29 18:46:51 +0200
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2020-04-09 20:03:18 +0200
commita8a15be369515e52a621ae0cdb70f2d6fe97b2b4 (patch)
tree6e23ea56d25a08c6cd8f7ffc8b8668f53afafbeb
parentRename driver.GlucometerDriver to driver.GlucometerDevice. (diff)
downloadglucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar.gz
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar.bz2
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar.lz
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar.xz
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.tar.zst
glucometerutils-a8a15be369515e52a621ae0cdb70f2d6fe97b2b4.zip
-rw-r--r--glucometerutils/driver.py19
-rwxr-xr-xglucometerutils/glucometer.py10
2 files changed, 22 insertions, 7 deletions
diff --git a/glucometerutils/driver.py b/glucometerutils/driver.py
index 641a5b6..88556d8 100644
--- a/glucometerutils/driver.py
+++ b/glucometerutils/driver.py
@@ -4,8 +4,11 @@
# SPDX-License-Identifier: MIT
import abc
+import dataclasses
import datetime
-from typing import Generator, Optional, Text
+import importlib
+import inspect
+from typing import Generator, Optional, Text, Type
from glucometerutils import common
@@ -65,3 +68,17 @@ class GlucometerDevice(abc.ABC):
@abc.abstractmethod
def get_readings(self) -> Generator[common.AnyReading, None, None]:
pass
+
+
+@dataclasses.dataclass
+class Driver:
+ device: Type[GlucometerDevice]
+ help: str
+
+
+def load_driver(driver_name: str) -> Driver:
+ driver_module = importlib.import_module(f"glucometerutils.drivers.{driver_name}")
+ help_string = inspect.getdoc(driver_module)
+ assert help_string is not None
+
+ return Driver(getattr(driver_module, "Device"), help_string)
diff --git a/glucometerutils/glucometer.py b/glucometerutils/glucometer.py
index c13b6b1..7b5803b 100755
--- a/glucometerutils/glucometer.py
+++ b/glucometerutils/glucometer.py
@@ -6,12 +6,10 @@
"""Utility to manage glucometers' data."""
import argparse
-import importlib
-import inspect
import logging
import sys
-from glucometerutils import common, exceptions
+from glucometerutils import common, driver, exceptions
def main():
@@ -101,7 +99,7 @@ def main():
logging.basicConfig(level=args.vlog)
try:
- driver = importlib.import_module("glucometerutils.drivers." + args.driver)
+ requested_driver = driver.load_driver(args.driver)
except ImportError as e:
logging.error(
'Error importing driver "%s", please check your --driver parameter:\n%s',
@@ -113,10 +111,10 @@ def main():
# This check needs to happen before we try to initialize the device, as the
# help action does not require a --device at all.
if args.action == "help":
- print(inspect.getdoc(driver))
+ print(requested_driver.help)
return 0
- device = driver.Device(args.device)
+ device = requested_driver.device(args.device)
device.connect()
device_info = device.get_meter_info()