Skip to content

Commit

Permalink
Merge pull request #244 from SpiNNakerManchester/is_config_none
Browse files Browse the repository at this point in the history
Is config none
  • Loading branch information
Christian-B authored Sep 11, 2023
2 parents de78c0b + 2023b6b commit 91f27c5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 13 deletions.
92 changes: 88 additions & 4 deletions spinn_utilities/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions spinn_utilities/socket_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
36 changes: 29 additions & 7 deletions unittests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,38 @@

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_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


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
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
try:
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

0 comments on commit 91f27c5

Please sign in to comment.