Refactor logic to improve readability

Move JSON accepted value ranges back to JSON array
pull/8673/head
kegilbert 2018-11-14 18:31:59 -06:00
parent 7a436660e9
commit 9d9cd84942
3 changed files with 16 additions and 33 deletions

View File

@ -1085,51 +1085,34 @@ class Config(object):
# Value is a hexadecimal or numerical string value # Value is a hexadecimal or numerical string value
# Convert to a python integer and range check/compare to # Convert to a python integer and range check/compare to
# accepted list accordingly # accepted list accordingly
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(value)):
value = int(value, 16)
elif value is not None:
value = int(value)
if min is not None or max is not None: if min is not None or max is not None:
# Numerical range check # Numerical range check
# Convert hex strings to integers for range checks # Convert hex strings to integers for range checks
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(min)) if min is not None else False:
min = int(min, 16)
elif min is not None:
min = int(min)
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(max)) if max is not None else False: value = int(str(value), 0)
max = int(max, 16) min = int(str(min), 0) if min is not None else None
elif max is not None: max = int(str(max), 0) if max is not None else None
max = int(max)
if (value < min or (value > max if max is not None else False)): if (value < min or (value > max if max is not None else False)):
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\ err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
% (param, % (param,
min if min is not None else "-inf", min if min is not None else "-inf",
max if max is not None else "inf") max if max is not None else "inf")
elif accepted is not None:
# Numerical accepted value check # Numerical accepted value check
integer_accepted_list = [] elif accepted is not None and value not in accepted:
for acc in accepted.split(','): err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
if re.match(r'^(0[xX])[A-Fa-f0-9]+$', str(acc.replace(' ', ''))): % (param, ", ".join(map(str, accepted)))
integer_accepted_list.append(int(acc, 16))
else:
integer_accepted_list.append(int(acc))
if value not in integer_accepted_list:
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
% (param, accepted)
else: else:
if min is not None or max is not None: if min is not None or max is not None:
err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\ err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\
"applicable to non-decimal/hexadecimal string values" % param "applicable to non-decimal/hexadecimal string values" % param
if accepted is not None: if accepted is not None:
# Generate list of stripped words from string to not allow a value of "test" to pass if accepted is not None and value not in accepted:
# on a list of accepted words consisting of "test1, test2, test3..."
accepted_list = [w.replace(' ', '') for w in accepted.split(',')]
if value not in accepted_list:
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\ err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
% (param, accepted) % (param, ", ".join(accepted))
if (err_msg): if (err_msg):
raise ConfigException(err_msg) raise ConfigException(err_msg)

View File

@ -4,7 +4,7 @@
"config1": { "config1": {
"help": "The default value should pass as it is in the list of accepted values", "help": "The default value should pass as it is in the list of accepted values",
"value": 5, "value": 5,
"accepted_values": "0, 5, 10" "accepted_values": [0, 5, 10]
}, },
"config2": { "config2": {
"help": "The default value should pass as it is in the range of accepted values", "help": "The default value should pass as it is in the range of accepted values",
@ -15,7 +15,7 @@
"config3": { "config3": {
"help": "The default value should pass as it is in the range of accepted values", "help": "The default value should pass as it is in the range of accepted values",
"value": "foo", "value": "foo",
"accepted_values": "foo, bar" "accepted_values": ["foo", "bar"]
}, },
"config4": { "config4": {
"help": "The default value should pass as it is in the range of accepted values", "help": "The default value should pass as it is in the range of accepted values",
@ -32,7 +32,7 @@
"config6": { "config6": {
"help": "The default value should pass as it is in the list of accepted values", "help": "The default value should pass as it is in the list of accepted values",
"value": "0x8000", "value": "0x8000",
"accepted_values": "0x1000, 0x8000, 0x12000" "accepted_values": ["0x1000", "0x8000", "0x12000"]
} }
} }
} }

View File

@ -4,7 +4,7 @@
"config1": { "config1": {
"help": "The default value should fail as it is not in the range of accepted values", "help": "The default value should fail as it is not in the range of accepted values",
"value": 99, "value": 99,
"accepted_values": "0, 5, 10" "accepted_values": [0, 5, 10]
}, },
"config2": { "config2": {
"help": "The default value should fail as it is not in the range of accepted values", "help": "The default value should fail as it is not in the range of accepted values",
@ -26,7 +26,7 @@
"help": "The default value should fail as it specified both a range and list of accepted values", "help": "The default value should fail as it specified both a range and list of accepted values",
"value": 103, "value": 103,
"value_max": 104, "value_max": 104,
"accepted_values": "103" "accepted_values": ["103"]
}, },
"config6": { "config6": {
"help": "The default value should fail as it is not in the range of accepted values", "help": "The default value should fail as it is not in the range of accepted values",