From a8a15be369515e52a621ae0cdb70f2d6fe97b2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sun, 29 Mar 2020 17:46:51 +0100 Subject: Move driver loading to glucometerutils.driver. This allows wrapping around some of the interfaces, and make typing more strict. --- glucometerutils/driver.py | 19 ++++++++++++++++++- glucometerutils/glucometer.py | 10 ++++------ 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() -- cgit v1.2.3