fix display and parsing issues

This commit is contained in:
Jakob Ketterl 2021-02-20 00:36:18 +01:00
parent bd7e5b7166
commit 058463a9b3

View File

@ -8,23 +8,29 @@ class GainInput(Input):
self.gain_stages = gain_stages self.gain_stages = gain_stages
def render_input(self, value): def render_input(self, value):
try:
display_value = float(value)
except (ValueError, TypeError):
display_value = "0.0"
return """ return """
<div id="{id}"> <div id="{id}">
<select class="{classes}" id="{id}-select" name="{id}-select"> <select class="{classes}" id="{id}-select" name="{id}-select">
{options} {options}
</select> </select>
<div class="option manual" style="display: none;"> <div class="option manual" style="display: none;">
<input type="number" id="{id}-manual" name="{id}-manual" value="{value}" class="{classes}" placeholder="Manual device gain" step="any"> <input type="number" id="{id}-manual" name="{id}-manual" value="{value}" class="{classes}"
placeholder="Manual device gain" step="any">
</div> </div>
{stageoption} {stageoption}
</div> </div>
""".format( """.format(
id=self.id, id=self.id,
classes=self.input_classes(), classes=self.input_classes(),
value="0.0" if value is None else value, value=display_value,
label=self.label, label=self.label,
options=self.render_options(value), options=self.render_options(value),
stageoption=self.render_stage_option(value), stageoption="" if self.gain_stages is None else self.render_stage_option(value),
) )
def render_options(self, value): def render_options(self, value):
@ -41,9 +47,7 @@ class GainInput(Input):
""" """
<option value="{value}" {selected}>{text}</option> <option value="{value}" {selected}>{text}</option>
""".format( """.format(
value=v[0], value=v[0], text=v[1], selected="selected" if mode == v[0] else ""
text=v[1],
selected="selected" if mode == v[0] else ""
) )
for v in options for v in options
) )
@ -55,7 +59,7 @@ class GainInput(Input):
try: try:
float(value) float(value)
return "manual" return "manual"
except ValueError: except (ValueError, TypeError):
pass pass
return "stages" return "stages"
@ -75,7 +79,8 @@ class GainInput(Input):
""" """
<div class="row"> <div class="row">
<div class="col-3">{stage}</div> <div class="col-3">{stage}</div>
<input type="number" id="{id}-{stage}" name="{id}-{stage}" value="{value}" class="col-9 {classes}" placeholder="{stage}" step="any"> <input type="number" id="{id}-{stage}" name="{id}-{stage}" value="{value}"
class="col-9 {classes}" placeholder="{stage}" step="any">
</div> </div>
""".format( """.format(
id=self.id, id=self.id,
@ -93,7 +98,7 @@ class GainInput(Input):
if input_id in data: if input_id in data:
return data[input_id][0] return data[input_id][0]
else: else:
return 0.0 return None
select_id = "{id}-select".format(id=self.id) select_id = "{id}-select".format(id=self.id)
if select_id in data: if select_id in data:
@ -102,12 +107,14 @@ class GainInput(Input):
value = 0.0 value = 0.0
if input_id in data: if input_id in data:
try: try:
value = float(float(data[input_id][0])) value = float(data[input_id][0])
except ValueError: except ValueError:
pass pass
return {self.id: value} return {self.id: value}
if data[select_id][0] == "stages": if self.gain_stages is not None and data[select_id][0] == "stages":
settings_dict = [{s: getStageValue(s)} for s in self.gain_stages] settings_dict = [{s: getStageValue(s)} for s in self.gain_stages]
# filter out empty ones
settings_dict = [s for s in settings_dict if next(iter(s.values()))]
return {self.id: SoapySettings.encode(settings_dict)} return {self.id: SoapySettings.encode(settings_dict)}
return {self.id: None} return {self.id: None}