fix form evaluation for optional fields

This commit is contained in:
Jakob Ketterl 2021-03-24 23:17:50 +01:00
parent 6ddced4689
commit 19496d46a3
13 changed files with 40 additions and 31 deletions

View File

@ -275,7 +275,7 @@ class NewSdrDeviceController(SettingsFormController):
return [
Section(
"New device settings",
TextInput("name", "Device name"),
TextInput("name", "Device name", validator=RequiredValidator()),
DropdownInput("type", "Device type", [Option(name, name) for name in SdrDeviceDescription.getTypes()]),
TextInput("id", "Device ID", validator=RequiredValidator()),
)

View File

@ -15,6 +15,7 @@ from owrx.property.filter import ByLambda
from owrx.form import Input, TextInput, NumberInput, CheckboxInput, ModesInput, ExponentialInput
from owrx.form.converter import OptionalConverter
from owrx.form.device import GainInput, SchedulerInput, WaterfallLevelsInput
from owrx.form.validator import RequiredValidator
from owrx.controllers.settings import Section
from typing import List
from enum import Enum
@ -520,7 +521,9 @@ class OptionalSection(Section):
def parse(self, data):
data, errors = super().parse(data)
# remove optional keys if they have been removed from the form
# filter out errors for optional fields
errors = [e for e in errors if e.getKey() not in self.optional or e.getKey() in data]
# remove optional keys if they have been removed from the form by setting them to None
for k in self.optional:
if k not in data:
data[k] = None
@ -550,10 +553,16 @@ class SdrDeviceDescription(object):
return [module_name for _, module_name, _ in pkgutil.walk_packages(__path__) if has_description(module_name)]
def getDeviceInputs(self) -> List[Input]:
return [TextInput("name", "Device name")] + self.getInputs()
keys = self.getDeviceMandatoryKeys() + self.getDeviceOptionalKeys()
return [TextInput("name", "Device name", validator=RequiredValidator())] + [
i for i in self.getInputs() if i.id in keys
]
def getProfileInputs(self) -> List[Input]:
return [TextInput("name", "Profile name")] + self.getInputs()
keys = self.getProfileMandatoryKeys() + self.getProfileOptionalKeys()
return [TextInput("name", "Profile name", validator=RequiredValidator())] + [
i for i in self.getInputs() if i.id in keys
]
def getInputs(self) -> List[Input]:
return [
@ -593,10 +602,10 @@ class SdrDeviceDescription(object):
# default is True since most devices have agc. override in subclasses if agc is not available
return True
def getMandatoryKeys(self):
def getDeviceMandatoryKeys(self):
return ["name", "enabled"]
def getOptionalKeys(self):
def getDeviceOptionalKeys(self):
return [
"ppm",
"always-on",
@ -615,7 +624,7 @@ class SdrDeviceDescription(object):
def getDeviceSection(self):
return OptionalSection(
"Device settings", self.getDeviceInputs(), self.getMandatoryKeys(), self.getOptionalKeys()
"Device settings", self.getDeviceInputs(), self.getDeviceMandatoryKeys(), self.getDeviceOptionalKeys()
)
def getProfileSection(self):

View File

@ -31,8 +31,8 @@ class AirspyDeviceDescription(SoapyConnectorDeviceDescription):
),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["bias_tee", "bitpack"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["bias_tee", "bitpack"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["bias_tee"]

View File

@ -90,8 +90,8 @@ class ConnectorDeviceDescription(SdrDeviceDescription):
),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["rtltcp_compat", "iqswap"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["rtltcp_compat", "iqswap"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["iqswap"]

View File

@ -18,8 +18,8 @@ class HackrfDeviceDescription(SoapyConnectorDeviceDescription):
def getInputs(self) -> List[Input]:
return super().getInputs() + [BiasTeeInput()]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["bias_tee"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["bias_tee"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["bias_tee"]

View File

@ -58,9 +58,9 @@ class PerseussdrDeviceDescription(DirectSourceDeviceDescription):
CheckboxInput("wideband", "Disable analog filters"),
]
def getOptionalKeys(self):
def getDeviceOptionalKeys(self):
# no rf_gain
return [key for key in super().getOptionalKeys() if key != "rf_gain"] + [
return [key for key in super().getDeviceOptionalKeys() if key != "rf_gain"] + [
"attenuator",
"adc_preamp",
"adc_dither",

View File

@ -27,8 +27,8 @@ class RtlSdrDeviceDescription(ConnectorDeviceDescription):
DirectSamplingInput(),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["device", "bias_tee", "direct_sampling"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["device", "bias_tee", "direct_sampling"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["bias_tee", "direct_sampling"]

View File

@ -18,8 +18,8 @@ class RtlSdrSoapyDeviceDescription(SoapyConnectorDeviceDescription):
def getInputs(self) -> List[Input]:
return super().getInputs() + [BiasTeeInput(), DirectSamplingInput()]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["bias_tee", "direct_sampling"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["bias_tee", "direct_sampling"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["bias_tee", "direct_sampling"]

View File

@ -25,5 +25,5 @@ class RtlTcpDeviceDescription(ConnectorDeviceDescription):
def getInputs(self) -> List[Input]:
return super().getInputs() + [RemoteInput()]
def getMandatoryKeys(self):
return super().getMandatoryKeys() + ["device"]
def getDeviceMandatoryKeys(self):
return super().getDeviceMandatoryKeys() + ["device"]

View File

@ -44,8 +44,8 @@ class RundsDeviceDescription(ConnectorDeviceDescription):
CheckboxInput("long", "Use 32-bit sample size (LONG)"),
]
def getMandatoryKeys(self):
return super().getMandatoryKeys() + ["device"]
def getDeviceMandatoryKeys(self):
return super().getDeviceMandatoryKeys() + ["device"]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["protocol", "long"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["protocol", "long"]

View File

@ -54,8 +54,8 @@ class SdrplayDeviceDescription(SoapyConnectorDeviceDescription):
),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["bias_tee", "rf_notch", "dab_notch", "if_mode"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["bias_tee", "rf_notch", "dab_notch", "if_mode"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["bias_tee", "rf_notch", "dab_notch", "if_mode"]

View File

@ -98,8 +98,8 @@ class SoapyConnectorDeviceDescription(ConnectorDeviceDescription):
TextInput("antenna", "Antenna"),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["device", "rf_gain", "antenna"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["device", "rf_gain", "antenna"]
def getProfileOptionalKeys(self):
return super().getProfileOptionalKeys() + ["antenna"]

View File

@ -29,5 +29,5 @@ class SoapyRemoteDeviceDescription(SoapyConnectorDeviceDescription):
),
]
def getOptionalKeys(self):
return super().getOptionalKeys() + ["remote_driver"]
def getDeviceOptionalKeys(self):
return super().getDeviceOptionalKeys() + ["remote_driver"]