Skip to content

Commit b3b13a9

Browse files
committed
(tweak) Add input validation
1 parent 58420da commit b3b13a9

File tree

4 files changed

+102
-107
lines changed

4 files changed

+102
-107
lines changed

BabbleApp/algo_settings_widget.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from queue import Queue
55
from threading import Event
66
import re
7-
from utils.misc_utils import bg_color_highlight, bg_color_clear
7+
from utils.misc_utils import bg_color_highlight, bg_color_clear, is_valid_float_input, is_valid_int_input
88
from lang_manager import LocaleStringManager as lang
99

1010

@@ -175,55 +175,47 @@ def stop(self):
175175
return
176176
self.cancellation_event.set()
177177

178-
def is_valid_float_input(self, value):
179-
# Allow empty string, negative sign, or a float number
180-
return bool(re.match(r"^-?\d*\.?\d*$", value))
181-
182-
def is_valid_int_input(self, value):
183-
# Allow empty string, negative sign, or an integer number
184-
return bool(re.match(r"^-?\d*$", value))
185-
186178
def render(self, window, event, values):
187179
# Input validation for the fields
188180
if event == self.gui_multiply:
189181
value = values[self.gui_multiply]
190-
if not self.is_valid_float_input(value):
182+
if not is_valid_float_input(value):
191183
# Invalid input, remove last character
192184
value = value[:-1]
193185
window[self.gui_multiply].update(value)
194186
values[self.gui_multiply] = value
195187

196188
elif event == self.calib_deadzone:
197189
value = values[self.calib_deadzone]
198-
if not self.is_valid_float_input(value):
190+
if not is_valid_float_input(value):
199191
value = value[:-1]
200192
window[self.calib_deadzone].update(value)
201193
values[self.calib_deadzone] = value
202194

203195
elif event == self.gui_inference_threads:
204196
value = values[self.gui_inference_threads]
205-
if not self.is_valid_int_input(value):
197+
if not is_valid_int_input(value):
206198
value = value[:-1]
207199
window[self.gui_inference_threads].update(value)
208200
values[self.gui_inference_threads] = value
209201

210202
elif event == self.gui_gpu_index:
211203
value = values[self.gui_gpu_index]
212-
if not self.is_valid_int_input(value):
204+
if not is_valid_int_input(value):
213205
value = value[:-1]
214206
window[self.gui_gpu_index].update(value)
215207
values[self.gui_gpu_index] = value
216208

217209
elif event == self.gui_min_cutoff:
218210
value = values[self.gui_min_cutoff]
219-
if not self.is_valid_float_input(value):
211+
if not is_valid_float_input(value):
220212
value = value[:-1]
221213
window[self.gui_min_cutoff].update(value)
222214
values[self.gui_min_cutoff] = value
223215

224216
elif event == self.gui_speed_coefficient:
225217
value = values[self.gui_speed_coefficient]
226-
if not self.is_valid_float_input(value):
218+
if not is_valid_float_input(value):
227219
value = value[:-1]
228220
window[self.gui_speed_coefficient].update(value)
229221
values[self.gui_speed_coefficient] = value

BabbleApp/calib_settings_widget.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from threading import Event
77
import numpy as np
88
from calib_settings_values import set_shapes
9-
from utils.misc_utils import bg_color_highlight, bg_color_clear
9+
from utils.misc_utils import bg_color_highlight, bg_color_clear, is_valid_float_input
1010
from lang_manager import LocaleStringManager as lang
1111

1212

@@ -164,12 +164,14 @@ def double_shape(self, shapename):
164164
key=self.shape[0][indexl],
165165
size=(8),
166166
tooltip=lang._instance.get_string("calibration.minLeftValue"),
167+
enable_events=True,
167168
),
168169
sg.InputText(
169170
default_text=self.array[1][indexl],
170171
key=self.shape[1][indexl],
171172
size=(8),
172173
tooltip=lang._instance.get_string("calibration.maxLeftValue"),
174+
enable_events=True,
173175
),
174176
sg.Text(
175177
f"{shapename}Left/Right",
@@ -181,12 +183,14 @@ def double_shape(self, shapename):
181183
key=self.shape[1][indexr],
182184
size=(8),
183185
tooltip=lang._instance.get_string("calibration.maxRightValue"),
186+
enable_events=True,
184187
),
185188
sg.InputText(
186189
default_text=self.array[0][indexr],
187190
key=self.shape[0][indexr],
188191
size=(8),
189192
tooltip=lang._instance.get_string("calibration.minRightValue"),
193+
enable_events=True,
190194
),
191195
]
192196
return double_shape
@@ -199,12 +203,14 @@ def single_shape(self, shapename):
199203
key=self.shape[0][index],
200204
size=(8),
201205
tooltip=lang._instance.get_string("calibration.minLeftValue"),
206+
enable_events=True,
202207
),
203208
sg.InputText(
204209
default_text=self.array[1][index],
205210
key=self.shape[1][index],
206211
size=(8),
207212
tooltip=lang._instance.get_string("calibration.maxLeftValue"),
213+
enable_events=True,
208214
),
209215
sg.Text(f"{shapename}", background_color=bg_color_highlight, expand_x=True),
210216
]
@@ -247,20 +253,25 @@ def render(self, window, event, values):
247253

248254
for count1, element1 in enumerate(self.shape):
249255
for count2, element2 in enumerate(element1):
250-
if values[element2] != "":
251-
try:
252-
if float(self.array[count1][count2]) != float(values[element2]):
253-
self.array[count1][count2] = float(values[element2])
256+
if values[element2] != "":
257+
value = values[element2]
258+
if is_valid_float_input(value):
259+
value = float(values[element2])
260+
if float(self.array[count1][count2]) != value:
261+
self.array[count1][count2] = value
254262
changed = True
255-
except:
256-
print(lang._instance.get_string("error.notAFloat"))
263+
else:
264+
value = float(value[:-1])
265+
window[element2].update(value)
266+
values[element2] = value
257267

258268
if event == self.gui_reset_min:
259269
for count1, element1 in enumerate(self.shape):
260270
for count2, element2 in enumerate(element1):
261271
self.array[0][count2] = float(0)
262272
changed = True
263273
self.refreshed = False
274+
264275
elif event == self.gui_reset_max:
265276
for count1, element1 in enumerate(self.shape):
266277
for count2, element2 in enumerate(element1):

BabbleApp/general_settings_widget.py

Lines changed: 69 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from osc import Tab
55
from queue import Queue
66
from threading import Event
7-
from utils.misc_utils import bg_color_highlight, bg_color_clear
8-
7+
from utils.misc_utils import bg_color_highlight, bg_color_clear, is_valid_int_input
98

109
class SettingsWidget:
1110
def __init__(
@@ -84,6 +83,7 @@ def __init__(
8483
key=self.gui_osc_port,
8584
size=(0, 10),
8685
tooltip=f'{lang._instance.get_string("general.portTooltip")}.',
86+
enable_events=True,
8787
),
8888
],
8989
[
@@ -110,6 +110,7 @@ def __init__(
110110
key=self.gui_osc_receiver_port,
111111
size=(0, 10),
112112
tooltip=f'{lang._instance.get_string("general.receiverPortTooltip")}:',
113+
enable_events=True,
113114
),
114115
],
115116
[
@@ -152,6 +153,7 @@ def __init__(
152153
key=self.gui_cam_resolution_x,
153154
size=(0, 20),
154155
tooltip=f'{lang._instance.get_string("general.xResolutionTooltip")}',
156+
enable_events=True,
155157
),
156158
# ],
157159
# [
@@ -164,6 +166,7 @@ def __init__(
164166
key=self.gui_cam_resolution_y,
165167
size=(0, 10),
166168
tooltip=f'{lang._instance.get_string("general.yResolutionTooltip")}',
169+
enable_events=True,
167170
),
168171
# ],
169172
# [
@@ -241,115 +244,96 @@ def render(self, window, event, values):
241244
# If anything has changed in our configuration settings, change/update those.
242245
changed = False
243246

244-
try:
245-
if self.config.gui_osc_port != int(values[self.gui_osc_port]):
246-
try:
247-
int(values[self.gui_osc_port])
248-
if len(values[self.gui_osc_port]) <= 5:
249-
self.config.gui_osc_port = int(values[self.gui_osc_port])
250-
changed = True
251-
else:
252-
print(
253-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscPort")}\033[0m'
254-
)
255-
except:
256-
print(
257-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscPort")}\033[0m'
258-
)
259-
except:
260-
print(
261-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscValue")}\033[0m'
262-
)
247+
# Check if the OSC port is a valid integer and update if necessary
248+
value = values[self.gui_osc_port]
249+
if value.isdigit() and len(value) <= 5:
250+
if self.config.gui_osc_port != int(value):
251+
self.config.gui_osc_port = int(value)
252+
changed = True
253+
else:
254+
if not is_valid_int_input(value):
255+
value = value[:-1]
256+
window[self.gui_osc_port].update(value)
257+
values[self.gui_osc_port] = value
263258

264-
try:
265-
if self.config.gui_osc_receiver_port != int(
266-
values[self.gui_osc_receiver_port]
267-
):
268-
try:
269-
int(values[self.gui_osc_receiver_port])
270-
if len(values[self.gui_osc_receiver_port]) <= 5:
271-
self.config.gui_osc_receiver_port = int(
272-
values[self.gui_osc_receiver_port]
273-
)
274-
changed = True
275-
else:
276-
print(
277-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscPort")}\033[0m'
278-
)
279-
except:
280-
print(
281-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscPort")}\033[0m'
282-
)
283-
except:
284-
print(
285-
f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("general.oscValue")}\033[0m'
286-
)
259+
# Check if the OSC receiver port is a valid integer and update if necessary
260+
value = values[self.gui_osc_receiver_port]
261+
if value.isdigit() and len(value) <= 5:
262+
if self.config.gui_osc_receiver_port != int(value):
263+
self.config.gui_osc_receiver_port = int(value)
264+
changed = True
265+
else:
266+
print(f'\033[91m[{lang._instance.get_string("log.error")}] {lang._instance.get_string("error.oscPort")}\033[0m')
267+
if not is_valid_int_input(value):
268+
value = value[:-1]
269+
window[self.gui_osc_receiver_port].update(value)
270+
values[self.gui_osc_receiver_port] = value
287271

272+
# Update OSC address if it has changed
288273
if self.config.gui_osc_address != values[self.gui_osc_address]:
289274
self.config.gui_osc_address = values[self.gui_osc_address]
290275
changed = True
291276

292-
if (
293-
self.config.gui_osc_recalibrate_address
294-
!= values[self.gui_osc_recalibrate_address]
295-
):
296-
self.config.gui_osc_recalibrate_address = values[
297-
self.gui_osc_recalibrate_address
298-
]
277+
# Update recalibrate address if it has changed
278+
if self.config.gui_osc_recalibrate_address != values[self.gui_osc_recalibrate_address]:
279+
self.config.gui_osc_recalibrate_address = values[self.gui_osc_recalibrate_address]
299280
changed = True
300281

282+
# Update check option
301283
if self.config.gui_update_check != values[self.gui_update_check]:
302284
self.config.gui_update_check = values[self.gui_update_check]
303285
changed = True
304286

287+
# Update ROSC option
305288
if self.config.gui_ROSC != values[self.gui_ROSC]:
306289
self.config.gui_ROSC = values[self.gui_ROSC]
307290
changed = True
308291

309-
if self.config.gui_osc_location != values[self.gui_osc_location]:
310-
self.config.gui_osc_location = values[self.gui_osc_location]
311-
changed = True
292+
# Update camera resolution X if it's a valid integer and different
293+
value = values[self.gui_cam_resolution_x]
294+
if value.isdigit():
295+
if str(self.config.gui_cam_resolution_x) != value:
296+
self.config.gui_cam_resolution_x = int(value)
297+
changed = True
298+
else:
299+
value = value[:-1]
300+
window[self.gui_cam_resolution_x].update(value)
301+
values[self.gui_cam_resolution_x] = value
312302

313-
if values[self.gui_cam_resolution_x] != "":
314-
if (
315-
str(self.config.gui_cam_resolution_x)
316-
!= values[self.gui_cam_resolution_x]
317-
):
318-
try:
319-
self.config.gui_cam_resolution_x = int(
320-
values[self.gui_cam_resolution_x]
321-
)
322-
changed = True
323-
except:
324-
print(lang._instance.get_string("general.notAnInt"))
325-
if values[self.gui_cam_resolution_y] != "":
326-
if (
327-
str(self.config.gui_cam_resolution_y)
328-
!= values[self.gui_cam_resolution_y]
329-
):
330-
try:
331-
self.config.gui_cam_resolution_y = int(
332-
values[self.gui_cam_resolution_y]
333-
)
334-
changed = True
335-
except:
336-
print(lang._instance.get_string("general.notAnInt"))
337-
if values[self.gui_cam_framerate] != "":
338-
if str(self.config.gui_cam_framerate) != values[self.gui_cam_framerate]:
339-
try:
340-
self.config.gui_cam_framerate = int(values[self.gui_cam_framerate])
341-
changed = True
342-
except:
343-
print(lang._instance.get_string("general.notAnInt"))
303+
# Update camera resolution Y if it's a valid integer and different
304+
value = values[self.gui_cam_resolution_y]
305+
if value.isdigit():
306+
if str(self.config.gui_cam_resolution_y) != value:
307+
self.config.gui_cam_resolution_y = int(value)
308+
changed = True
309+
else:
310+
value = value[:-1]
311+
window[self.gui_cam_resolution_y].update(value)
312+
values[self.gui_cam_resolution_y] = value
344313

314+
# Update camera framerate if it's a valid integer and different
315+
value = values[self.gui_cam_framerate]
316+
if value.isdigit():
317+
if str(self.config.gui_cam_framerate) != value:
318+
self.config.gui_cam_framerate = int(value)
319+
changed = True
320+
else:
321+
value = value[:-1]
322+
window[self.gui_cam_framerate].update(value)
323+
values[self.gui_cam_framerate] = value
324+
325+
# Update the use of the red channel
345326
if self.config.gui_use_red_channel != bool(values[self.gui_use_red_channel]):
346327
self.config.gui_use_red_channel = bool(values[self.gui_use_red_channel])
347328
changed = True
348329

330+
# Update language if it has changed
349331
if self.config.gui_language != values[self.gui_language]:
350332
self.config.gui_language = values[self.gui_language]
351333
changed = True
352334

335+
# Save the configuration if changes were made
353336
if changed:
354337
self.main_config.save()
338+
355339
self.osc_queue.put((Tab.SETTINGS))

0 commit comments

Comments
 (0)