From 9552178f9580426ab5e89c23d1d432479adde13b Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Fri, 25 Aug 2023 08:41:31 +0100 Subject: [PATCH 1/3] config methods which do and do not return none --- spinn_utilities/config_holder.py | 92 ++++++++++++++++++++++++++++++-- unittests/test_configs.py | 20 ++++--- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/spinn_utilities/config_holder.py b/spinn_utilities/config_holder.py index b77ef15d..f38b22e7 100644 --- a/spinn_utilities/config_holder.py +++ b/spinn_utilities/config_holder.py @@ -100,14 +100,44 @@ def load_config(): __config.read(default) +def is_config_none(section, option): + """ + Check if the value of a configuration option would be considered None + + :param str section: What section to get the option from. + :param str option: What option to read. + :return: True if and only if the value would be considered None + :rtype: bool + """ + value = get_config_str_or_none(section, option) + return value is None + + def get_config_str(section, option): """ Get the string value of a configuration option. + :param str section: What section to get the option from. + :param str option: What option to read. + :return: The option value + :rtype: str + :raises ConfigException: if the Value would be None + """ + value = get_config_str_or_none(section, option) + if value is None: + raise ConfigException(f"Unexpected None for {section=} {option=}") + return value + + +def get_config_str_or_none(section, option): + """ + Get the string value of a configuration option. + :param str section: What section to get the option from. :param str option: What option to read. :return: The option value :rtype: str or None + :raises ConfigException: if the Value would be None """ try: return __config.get_str(section, option) @@ -143,6 +173,23 @@ def get_config_int(section, option): :param str option: What option to read. :return: The option value :rtype: int + :raises ConfigException: if the Value would be None + """ + value = get_config_int_or_none(section, option) + if value is None: + raise ConfigException(f"Unexpected None for {section=} {option=}") + return value + + +def get_config_int_or_none(section, option): + """ + Get the integer value of a configuration option. + + :param str section: What section to get the option from. + :param str option: What option to read. + :return: The option value + :rtype: int or None + :raises ConfigException: if the Value would be None """ try: return __config.get_int(section, option) @@ -160,6 +207,22 @@ def get_config_float(section, option): :param str option: What option to read. :return: The option value. :rtype: float + :raises ConfigException: if the Value would be None + """ + value = get_config_float_or_none(section, option) + if value is None: + raise ConfigException(f"Unexpected None for {section=} {option=}") + return value + + +def get_config_float_or_none(section, option): + """ + Get the float value of a configuration option. + + :param str section: What section to get the option from. + :param str option: What option to read. + :return: The option value. + :rtype: float or None """ try: return __config.get_float(section, option) @@ -177,6 +240,23 @@ def get_config_bool(section, option): :param str option: What option to read. :return: The option value. :rtype: bool + :raises ConfigException: if the Value would be None + """ + value = get_config_bool_or_none(section, option) + if value is None: + raise ConfigException(f"Unexpected None for {section=} {option=}") + return value + + +def get_config_bool_or_none(section, option): + """ + Get the boolean value of a configuration option. + + :param str section: What section to get the option from. + :param str option: What option to read. + :return: The option value. + :rtype: bool + :raises ConfigException: if the Value would be None """ try: return __config.get_bool(section, option) @@ -279,13 +359,17 @@ def _check_python_file(py_path): lines = py_file.readlines() for index, line in enumerate(lines): if "get_config_bool(" in line: - _check_lines(py_path, line, lines, index, get_config_bool) + _check_lines( + py_path, line, lines, index, get_config_bool_or_none) if "get_config_float(" in line: - _check_lines(py_path, line, lines, index, get_config_float) + _check_lines( + py_path, line, lines, index, get_config_float_or_none) if "get_config_int(" in line: - _check_lines(py_path, line, lines, index, get_config_int) + _check_lines( + py_path, line, lines, index, get_config_int_or_none) if "get_config_str(" in line: - _check_lines(py_path, line, lines, index, get_config_str) + _check_lines( + py_path, line, lines, index, get_config_str_or_none) if "get_config_str_list(" in line: _check_lines(py_path, line, lines, index, get_config_str_list) diff --git a/unittests/test_configs.py b/unittests/test_configs.py index d3fc20de..b714d17c 100644 --- a/unittests/test_configs.py +++ b/unittests/test_configs.py @@ -14,16 +14,22 @@ from spinn_utilities.config_setup import unittest_setup from spinn_utilities.config_holder import ( - get_config_bool, get_config_float, get_config_int, get_config_str, - set_config) + get_config_bool, get_config_float_or_none, get_config_int_or_none, + get_config_str, get_config_str_or_none, set_config) +from spinn_utilities.exceptions import ConfigException, SpiNNUtilsException def test_configs_None(): unittest_setup() set_config("Mode", "Foo", "None") set_config("Mode", "Bar", "none") - assert get_config_str("Mode", "Foo") is None - assert get_config_str("Mode", "bar") is None - assert get_config_int("Mode", "Foo") is None - assert get_config_float("Mode", "Foo") is None - assert get_config_bool("Mode", "Foo") is None + assert get_config_str_or_none("Mode", "Foo") is None + assert get_config_str_or_none("Mode", "bar") is None + assert get_config_int_or_none("Mode", "Foo") is None + assert get_config_float_or_none("Mode", "Foo") is None + #assert get_config_bool("Mode", "Foo") is None + try: + assert get_config_str("Mode", "Foo") is None + raise SpiNNUtilsException("Expected ConfigException") + except ConfigException: + pass From a88206ee2ebff4679cdef72c710b75c35a59523c Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Fri, 25 Aug 2023 09:39:49 +0100 Subject: [PATCH 2/3] test all none exceptions --- unittests/test_configs.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/unittests/test_configs.py b/unittests/test_configs.py index b714d17c..2e0efda5 100644 --- a/unittests/test_configs.py +++ b/unittests/test_configs.py @@ -14,7 +14,8 @@ from spinn_utilities.config_setup import unittest_setup from spinn_utilities.config_holder import ( - get_config_bool, get_config_float_or_none, get_config_int_or_none, + get_config_bool, get_config_bool_or_none, get_config_float, + get_config_float_or_none, get_config_int, get_config_int_or_none, get_config_str, get_config_str_or_none, set_config) from spinn_utilities.exceptions import ConfigException, SpiNNUtilsException @@ -24,12 +25,27 @@ def test_configs_None(): set_config("Mode", "Foo", "None") set_config("Mode", "Bar", "none") assert get_config_str_or_none("Mode", "Foo") is None + try: + assert get_config_str("Mode", "Foo") is None + raise SpiNNUtilsException("Expected ConfigException") + except ConfigException: + pass assert get_config_str_or_none("Mode", "bar") is None assert get_config_int_or_none("Mode", "Foo") is None + try: + assert get_config_int("Mode", "Foo") is None + raise SpiNNUtilsException("Expected ConfigException") + except ConfigException: + pass assert get_config_float_or_none("Mode", "Foo") is None - #assert get_config_bool("Mode", "Foo") is None try: - assert get_config_str("Mode", "Foo") is None + assert get_config_float("Mode", "Foo") is None + raise SpiNNUtilsException("Expected ConfigException") + except ConfigException: + pass + assert get_config_bool_or_none("Mode", "Foo") is None + try: + assert get_config_bool("Mode", "Foo") is None raise SpiNNUtilsException("Expected ConfigException") except ConfigException: pass From 652652d2808184b3f66c00990961768f8f45e5d9 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Fri, 25 Aug 2023 10:02:23 +0100 Subject: [PATCH 3/3] another case that can be none --- spinn_utilities/socket_address.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spinn_utilities/socket_address.py b/spinn_utilities/socket_address.py index 5b6bc744..5d41e506 100644 --- a/spinn_utilities/socket_address.py +++ b/spinn_utilities/socket_address.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from spinn_utilities.config_holder import get_config_int, get_config_str +from spinn_utilities.config_holder import ( + get_config_int, get_config_int_or_none, get_config_str) class SocketAddress(object): @@ -50,7 +51,7 @@ def __init__(self, notify_host_name=None, notify_port_no=None, else: notify_host_name = str(notify_host_name) if listen_port is None: - listen_port = get_config_int("Database", "listen_port") + listen_port = get_config_int_or_none("Database", "listen_port") else: listen_port = int(listen_port) self._notify_host_name = notify_host_name