Skip to content

Commit 7a43666

Browse files
committed
Handle None and hex values in string format
1 parent b537387 commit 7a43666

File tree

6 files changed

+98
-24
lines changed

6 files changed

+98
-24
lines changed

tools/config/__init__.py

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import json
2222
import six
2323
import os
24+
import re
2425
from os.path import dirname, abspath, exists, join, isabs
2526
import sys
2627
from collections import namedtuple
@@ -1072,20 +1073,63 @@ def validate_config(self):
10721073
accepted = param.accepted_values
10731074
value = param.value
10741075

1075-
if (min is not None or max is not None) and (accepted is not None):
1076-
err_msg += "\n%s has both a range and list of accepted values specified. Please only "\
1077-
"specify either value_min and/or value_max, or accepted_values."\
1078-
% param
1079-
else:
1080-
if (value < min or (value > max if max is not None else False)):
1081-
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
1082-
% (param,
1083-
min if min is not None else "-inf",
1084-
max if max is not None else "inf")
1085-
1086-
if accepted and str(value) not in accepted:
1087-
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1088-
% (param, accepted)
1076+
# Config parameters that are only defined but do not have a default
1077+
# value should not be range limited
1078+
if value is not None:
1079+
if (min is not None or max is not None) and (accepted is not None):
1080+
err_msg += "\n%s has both a range and list of accepted values specified. Please only "\
1081+
"specify either value_min and/or value_max, or accepted_values"\
1082+
% param
1083+
else:
1084+
if re.match(r'^(0[xX])[A-Fa-f0-9]+$|^[0-9]+$', str(value)):
1085+
# Value is a hexadecimal or numerical string value
1086+
# Convert to a python integer and range check/compare to
1087+
# 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)
1092+
1093+
if min is not None or max is not None:
1094+
# Numerical range check
1095+
# 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)
1100+
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)
1105+
1106+
if (value < min or (value > max if max is not None else False)):
1107+
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
1108+
% (param,
1109+
min if min is not None else "-inf",
1110+
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)
1122+
else:
1123+
if min is not None or max is not None:
1124+
err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\
1125+
"applicable to non-decimal/hexadecimal string values" % param
1126+
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:
1131+
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1132+
% (param, accepted)
10891133

10901134
if (err_msg):
10911135
raise ConfigException(err_msg)

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@
77
"accepted_values": "0, 5, 10"
88
},
99
"config2": {
10-
"help": "The default value should pass as it is in the rage of accepted values",
10+
"help": "The default value should pass as it is in the range of accepted values",
1111
"value": 7,
1212
"value_min": 0,
1313
"value_max": 10
1414
},
1515
"config3": {
16-
"help": "The default value should pass as it is in the rage of accepted values",
16+
"help": "The default value should pass as it is in the range of accepted values",
1717
"value": "foo",
1818
"accepted_values": "foo, bar"
19+
},
20+
"config4": {
21+
"help": "The default value should pass as it is in the range of accepted values",
22+
"value": "0x1000",
23+
"value_min": "0x10",
24+
"value_max": "0x8000"
25+
},
26+
"config5": {
27+
"help": "The default value should pass as it is in the range of accepted values",
28+
"value": "0x2000",
29+
"value_min": 0,
30+
"value_max": "0x8000"
31+
},
32+
"config6": {
33+
"help": "The default value should pass as it is in the list of accepted values",
34+
"value": "0x8000",
35+
"accepted_values": "0x1000, 0x8000, 0x12000"
1936
}
2037
}
2138
}

tools/test/config/range_limits/test_data.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"test_target": {
33
"lib1.config1": 5,
44
"lib1.config2": 7,
5-
"lib1.config3": "foo"
5+
"lib1.config3": "foo",
6+
"lib1.config4": "0x1000",
7+
"lib1.config5": "0x2000",
8+
"lib1.config6": "0x8000"
69
}
710
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,41 @@
22
"name": "lib1",
33
"config": {
44
"config1": {
5-
"help": "The default value should fail as it is not in the rage of accepted values",
5+
"help": "The default value should fail as it is not in the range of accepted values",
66
"value": 99,
77
"accepted_values": "0, 5, 10"
88
},
99
"config2": {
10-
"help": "The default value should fail as it is not in the rage of accepted values",
10+
"help": "The default value should fail as it is not in the range of accepted values",
1111
"value": 100,
1212
"value_min": 0,
1313
"value_max": 10
1414
},
1515
"config3": {
16-
"help": "The default value should fail as it is not in the rage of accepted values",
16+
"help": "The default value should fail as it is not in the range of accepted values",
1717
"value": 101,
1818
"value_min": 102
1919
},
2020
"config4": {
21-
"help": "The default value should fail as it is not in the rage of accepted values",
21+
"help": "The default value should fail as it is not in the range of accepted values",
2222
"value": 102,
2323
"value_max": 101
2424
},
2525
"config5": {
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"
30+
},
31+
"config6": {
32+
"help": "The default value should fail as it is not in the range of accepted values",
33+
"value": "0x1000",
34+
"value_max": "0x500"
35+
},
36+
"config7": {
37+
"help": "The default value should fail as it is a non-decimal string with a max value",
38+
"value": "test",
39+
"value_max": "?"
3040
}
3141
}
3242
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"test_target": {
3-
"exception_msg": "\nInvalid config range for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: 0, 5, 10\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]\nlib1.config5 = 103 (macro name: \"MBED_CONF_LIB1_CONFIG5\") has both a range and list of accepted values specified. Please only specify either value_min and/or value_max, or accepted_values."
3+
"exception_msg": "\nInvalid config range for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: 0, 5, 10\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]\nlib1.config5 = 103 (macro name: \"MBED_CONF_LIB1_CONFIG5\") has both a range and list of accepted values specified. Please only specify either value_min and/or value_max, or accepted_values\nInvalid config range for lib1.config6 = 0x1000 (macro name: \"MBED_CONF_LIB1_CONFIG6\"), is not in the required range: [-inf:1280]\nInvalid config range settings for lib1.config7 = test (macro name: \"MBED_CONF_LIB1_CONFIG7\"). Range specifiers are not applicable to non-decimal/hexadecimal string values"
44
}
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"test_target": {
3-
"lib1.config1": 8,
3+
"lib1.config1": 12,
44
"exception_msg": "Invalid config range for lib1.config1 = 12 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not in the required range: [0:10]"
55
}
66
}

0 commit comments

Comments
 (0)