Skip to content

Commit

Permalink
run_config_checks accepts special nones for bools
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Sep 19, 2024
1 parent 0e46a4f commit 45d886d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
34 changes: 24 additions & 10 deletions spinn_utilities/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,23 @@ def get_config_bool(section: str, option: str) -> bool:
return value


def get_config_bool_or_none(section, option) -> Optional[bool]:
def get_config_bool_or_none(section, option,
special_nones: Optional[List[str]] = None
) -> Optional[bool]:
"""
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.
:param special_nones: What special values to except as None
:return: The option value.
:rtype: bool
:raises ConfigException: if the Value would be None
"""
if __config is None:
return _pre_load_config().get_bool(section, option)
return _pre_load_config().get_bool(section, option, special_nones)
else:
return __config.get_bool(section, option)
return __config.get_bool(section, option, special_nones)


def set_config(section: str, option: str, value: Optional[str]):
Expand Down Expand Up @@ -330,7 +333,8 @@ def config_options(section: str) -> List[str]:

def _check_lines(py_path: str, line: str, lines: List[str], index: int,
method: Callable[[str, str], Any],
used_cfgs: Dict[str, Set[str]], start):
used_cfgs: Dict[str, Set[str]], start,
special_nones: Optional[List[str]] = None):
"""
Support for `_check_python_file`. Gets section and option name.
Expand All @@ -340,6 +344,7 @@ def _check_lines(py_path: str, line: str, lines: List[str], index: int,
:param method: Method to call to check cfg
:param dict(str), set(str) used_cfgs:
Dict of used cfg options to be added to
:param special_nones: What special values to except as None
:raises ConfigException: If an unexpected or uncovered `get_config` found
"""
while ")" not in line:
Expand All @@ -363,28 +368,34 @@ def _check_lines(py_path: str, line: str, lines: List[str], index: int,
print(line)
return
try:
method(section, option)
if special_nones:
method(section, option, special_nones)
else:
method(section, option)
except Exception as original:
raise ConfigException(
f"failed in line:{index} of file: {py_path} with "
f"section:{section} option:{option}") from original
used_cfgs[section].add(option)


def _check_python_file(py_path: str, used_cfgs: Dict[str, Set[str]]):
def _check_python_file(py_path: str, used_cfgs: Dict[str, Set[str]],
special_nones: Optional[List[str]] = None):
"""
A testing function to check that all the `get_config` calls work.
:param str py_path: path to file to be checked
:param used_cfgs: dict of cfg options found
:param special_nones: What special values to except as None
:raises ConfigException: If an unexpected or uncovered `get_config` found
"""
with open(py_path, 'r', encoding="utf-8") as py_file:
lines = list(py_file)
for index, line in enumerate(lines):
if ("skip_if_cfg" in line):
_check_lines(py_path, line, lines, index,
get_config_bool_or_none, used_cfgs, "skip_if_cfg")
get_config_bool_or_none, used_cfgs, "skip_if_cfg",
special_nones)
if ("configuration.get" in line):
_check_lines(py_path, line, lines, index,
get_config_bool_or_none, used_cfgs,
Expand All @@ -394,7 +405,8 @@ def _check_python_file(py_path: str, used_cfgs: Dict[str, Set[str]]):
if (("get_config_bool(" in line) or
("get_config_bool_or_none(" in line)):
_check_lines(py_path, line, lines, index,
get_config_bool_or_none, used_cfgs, "get_config")
get_config_bool_or_none, used_cfgs,
"get_config_bool", special_nones)
if (("get_config_float(" in line) or
("get_config_float_or_none(" in line)):
_check_lines(py_path, line, lines, index,
Expand Down Expand Up @@ -489,7 +501,8 @@ def _check_cfgs(path: str):
def run_config_checks(directories: Union[str, Collection[str]], *,
exceptions: Union[str, Collection[str]] = (),
repeaters: Optional[Collection[str]] = (),
check_all_used: bool = True):
check_all_used: bool = True,
special_nones: Optional[List[str]] = None):
"""
Master test.
Expand All @@ -503,6 +516,7 @@ def run_config_checks(directories: Union[str, Collection[str]], *,
:param exceptions:
:param repeaters:
:param bool check_all_used: Toggle for the used test.
:param special_nones: What special values to except as None
:raises ConfigException: If an incorrect directory passed in
"""
if isinstance(directories, str):
Expand Down Expand Up @@ -534,7 +548,7 @@ def run_config_checks(directories: Union[str, Collection[str]], *,
_check_cfg_file(config1, cfg_path)
elif file_name.endswith(".py"):
py_path = os.path.join(root, file_name)
_check_python_file(py_path, used_cfgs)
_check_python_file(py_path, used_cfgs, special_nones)

if not check_all_used:
return
Expand Down
6 changes: 5 additions & 1 deletion spinn_utilities/configs/camel_case_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ def get_float(self, section: str, option: str) -> Optional[float]:
return None
return float(value)

def get_bool(self, section: str, option: str) -> Optional[bool]:
def get_bool(self, section: str, option: str,
special_nones: Optional[List[str]]) -> Optional[bool]:
"""
Get the Boolean value of an option.
:param str section: What section to get the option from.
:param str option: What option to read.
:param special_nones: What special values to except as None
:return: The option value.
:rtype: bool
"""
Expand All @@ -132,4 +134,6 @@ def get_bool(self, section: str, option: str) -> Optional[bool]:
return False
if lower in NONES:
return None
if special_nones and lower in special_nones:
return None
raise ValueError(f"invalid truth value {value}")

0 comments on commit 45d886d

Please sign in to comment.