Skip to content

Commit 9d9cd84

Browse files
committed
Refactor logic to improve readability
Move JSON accepted value ranges back to JSON array
1 parent 7a43666 commit 9d9cd84

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

tools/config/__init__.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,51 +1085,34 @@ def validate_config(self):
10851085
# Value is a hexadecimal or numerical string value
10861086
# Convert to a python integer and range check/compare to
10871087
# accepted list accordingly
1088-
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(value)):
1089-
value = int(value, 16)
1090-
elif value is not None:
1091-
value = int(value)
10921088

10931089
if min is not None or max is not None:
10941090
# Numerical range check
10951091
# Convert hex strings to integers for range checks
1096-
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(min)) if min is not None else False:
1097-
min = int(min, 16)
1098-
elif min is not None:
1099-
min = int(min)
11001092

1101-
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(max)) if max is not None else False:
1102-
max = int(max, 16)
1103-
elif max is not None:
1104-
max = int(max)
1093+
value = int(str(value), 0)
1094+
min = int(str(min), 0) if min is not None else None
1095+
max = int(str(max), 0) if max is not None else None
11051096

11061097
if (value < min or (value > max if max is not None else False)):
11071098
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
11081099
% (param,
11091100
min if min is not None else "-inf",
11101101
max if max is not None else "inf")
1111-
elif accepted is not None:
1112-
# Numerical accepted value check
1113-
integer_accepted_list = []
1114-
for acc in accepted.split(','):
1115-
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(acc.replace(' ', ''))):
1116-
integer_accepted_list.append(int(acc, 16))
1117-
else:
1118-
integer_accepted_list.append(int(acc))
1119-
if value not in integer_accepted_list:
1120-
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1121-
% (param, accepted)
1102+
1103+
# Numerical accepted value check
1104+
elif accepted is not None and value not in accepted:
1105+
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1106+
% (param, ", ".join(map(str, accepted)))
11221107
else:
11231108
if min is not None or max is not None:
11241109
err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\
11251110
"applicable to non-decimal/hexadecimal string values" % param
1111+
11261112
if accepted is not None:
1127-
# Generate list of stripped words from string to not allow a value of "test" to pass
1128-
# on a list of accepted words consisting of "test1, test2, test3..."
1129-
accepted_list = [w.replace(' ', '') for w in accepted.split(',')]
1130-
if value not in accepted_list:
1113+
if accepted is not None and value not in accepted:
11311114
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1132-
% (param, accepted)
1115+
% (param, ", ".join(accepted))
11331116

11341117
if (err_msg):
11351118
raise ConfigException(err_msg)

tools/test/config/range_limits/lib1/mbed_lib.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"config1": {
55
"help": "The default value should pass as it is in the list of accepted values",
66
"value": 5,
7-
"accepted_values": "0, 5, 10"
7+
"accepted_values": [0, 5, 10]
88
},
99
"config2": {
1010
"help": "The default value should pass as it is in the range of accepted values",
@@ -15,7 +15,7 @@
1515
"config3": {
1616
"help": "The default value should pass as it is in the range of accepted values",
1717
"value": "foo",
18-
"accepted_values": "foo, bar"
18+
"accepted_values": ["foo", "bar"]
1919
},
2020
"config4": {
2121
"help": "The default value should pass as it is in the range of accepted values",
@@ -32,7 +32,7 @@
3232
"config6": {
3333
"help": "The default value should pass as it is in the list of accepted values",
3434
"value": "0x8000",
35-
"accepted_values": "0x1000, 0x8000, 0x12000"
35+
"accepted_values": ["0x1000", "0x8000", "0x12000"]
3636
}
3737
}
3838
}

tools/test/config/range_limits_invalid/lib1/mbed_lib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"config1": {
55
"help": "The default value should fail as it is not in the range of accepted values",
66
"value": 99,
7-
"accepted_values": "0, 5, 10"
7+
"accepted_values": [0, 5, 10]
88
},
99
"config2": {
1010
"help": "The default value should fail as it is not in the range of accepted values",
@@ -26,7 +26,7 @@
2626
"help": "The default value should fail as it specified both a range and list of accepted values",
2727
"value": 103,
2828
"value_max": 104,
29-
"accepted_values": "103"
29+
"accepted_values": ["103"]
3030
},
3131
"config6": {
3232
"help": "The default value should fail as it is not in the range of accepted values",

0 commit comments

Comments
 (0)