From bec61465c9f69f4bcf531e92fda4933faa5c93fe Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Fri, 19 Feb 2021 14:44:16 +0100 Subject: [PATCH] move device descriptions to owrx.source --- .../settings/{devices/__init__.py => device.py} | 16 ++++++++++------ owrx/controllers/settings/devices/rtl_sdr.py | 13 ------------- owrx/controllers/settings/sdr.py | 9 +++++---- owrx/source/rtl_sdr.py | 13 +++++++++++++ 4 files changed, 28 insertions(+), 23 deletions(-) rename owrx/controllers/settings/{devices/__init__.py => device.py} (51%) delete mode 100644 owrx/controllers/settings/devices/rtl_sdr.py diff --git a/owrx/controllers/settings/devices/__init__.py b/owrx/controllers/settings/device.py similarity index 51% rename from owrx/controllers/settings/devices/__init__.py rename to owrx/controllers/settings/device.py index 30f1882..803644a 100644 --- a/owrx/controllers/settings/devices/__init__.py +++ b/owrx/controllers/settings/device.py @@ -4,16 +4,20 @@ from abc import ABC, abstractmethod from typing import List -class SdrDeviceType(ABC): +class SdrDeviceDescriptionMissing(Exception): + pass + + +class SdrDeviceDescription(ABC): @staticmethod - def getByType(sdr_type: str) -> "SdrDeviceType": + def getByType(sdr_type: str) -> "SdrDeviceDescription": try: - className = "".join(x for x in sdr_type.title() if x.isalnum()) + "DeviceType" - module = __import__("owrx.controllers.settings.devices.{0}".format(sdr_type), fromlist=[className]) + className = "".join(x for x in sdr_type.title() if x.isalnum()) + "DeviceDescription" + module = __import__("owrx.source.{0}".format(sdr_type), fromlist=[className]) cls = getattr(module, className) return cls() - except ModuleNotFoundError: - return None + except (ModuleNotFoundError, AttributeError): + raise SdrDeviceDescriptionMissing("Device description for type {} not available".format(sdr_type)) @abstractmethod def getInputs(self) -> List[Input]: diff --git a/owrx/controllers/settings/devices/rtl_sdr.py b/owrx/controllers/settings/devices/rtl_sdr.py deleted file mode 100644 index af6673e..0000000 --- a/owrx/controllers/settings/devices/rtl_sdr.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import List -from owrx.controllers.settings.devices import SdrDeviceType -from owrx.form import Input, TextInput - - -class RtlSdrDeviceType(SdrDeviceType): - def getInputs(self) -> List[Input]: - return [ - TextInput( - "test", - "This is a drill" - ), - ] diff --git a/owrx/controllers/settings/sdr.py b/owrx/controllers/settings/sdr.py index 6c2661e..4a45b2e 100644 --- a/owrx/controllers/settings/sdr.py +++ b/owrx/controllers/settings/sdr.py @@ -1,7 +1,7 @@ from owrx.controllers.admin import AuthorizationMixin from owrx.controllers.template import WebpageController from owrx.controllers.settings import SettingsFormController -from owrx.controllers.settings.devices import SdrDeviceType +from owrx.controllers.settings.device import SdrDeviceDescription, SdrDeviceDescriptionMissing from owrx.config import Config from urllib.parse import quote, unquote @@ -53,11 +53,12 @@ class SdrDeviceController(SettingsFormController): self.device = self._get_device() def getSections(self): - device_type = SdrDeviceType.getByType(self.device["type"]) - if device_type is None: + try: + description = SdrDeviceDescription.getByType(self.device["type"]) + return [description.getSection()] + except SdrDeviceDescriptionMissing: # TODO provide a generic interface that allows to switch the type return [] - return [device_type.getSection()] def getTitle(self): return self.device["name"] diff --git a/owrx/source/rtl_sdr.py b/owrx/source/rtl_sdr.py index a6ecbc9..ff1a0df 100644 --- a/owrx/source/rtl_sdr.py +++ b/owrx/source/rtl_sdr.py @@ -1,5 +1,8 @@ from .connector import ConnectorSource from owrx.command import Flag, Option +from owrx.controllers.settings.device import SdrDeviceDescription +from typing import List +from owrx.form import Input, TextInput class RtlSdrSource(ConnectorSource): @@ -10,3 +13,13 @@ class RtlSdrSource(ConnectorSource): .setBase("rtl_connector") .setMappings({"bias_tee": Flag("-b"), "direct_sampling": Option("-e")}) ) + + +class RtlSdrDeviceDescription(SdrDeviceDescription): + def getInputs(self) -> List[Input]: + return [ + TextInput( + "test", + "This is a drill" + ), + ]