Skip to content

Commit

Permalink
Merge branch 'master' into spalloc-server-jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
rowleya committed Feb 3, 2023
2 parents f06aee2 + 388e3f3 commit 1ecef13
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 68 deletions.
5 changes: 3 additions & 2 deletions spinn_utilities/citation/citation_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def _handle_python_dependency(
list of names of dependencies already processed
:param str module_name:
the name of this module to consider as a dependency
:raises FileNotFoundError:
"""
# get modules citation file
citation_level_dir = os.path.abspath(imported_module.__file__)
Expand All @@ -225,8 +226,8 @@ def _handle_python_dependency(
last_citation_level_dir = citation_level_dir
citation_level_dir = os.path.dirname(citation_level_dir)
if citation_level_dir == last_citation_level_dir: # pragma: no cover
raise Exception("Folder for module {} not found".format(
module_name))
raise FileNotFoundError(
f"Folder for module {module_name} not found")

# get the reference data for the reference
reference_entry = self._process_reference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ def convert_month_name_to_number(version_month):
try:
return strptime(version_month, "%b").tm_mon
except ValueError: # pragma: no cover
raise Exception("Value {} not recognised as a month"
.format(version_month)) from original
raise ValueError(
f"Value {version_month} not recognised"
f" as a month") from original
else: # pragma: no cover
raise Exception("Value {} not recognised as a month".format(
version_month))
raise ValueError(
f"Value {version_month} not recognised as a month")
37 changes: 22 additions & 15 deletions spinn_utilities/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import spinn_utilities.conf_loader as conf_loader
from spinn_utilities.configs import CamelCaseConfigParser
from spinn_utilities.exceptions import ConfigException
from spinn_utilities.log import FormatAdapter

# pylint: disable=global-statement
Expand Down Expand Up @@ -73,21 +74,24 @@ def _pre_load_config():
"""
Loads configs due to early access to a config value
:raises ConfigException: Raise if called before setup
"""
# If you getthis error during a unittest then unittest_step was not called
if not __unittest_mode:
raise Exception(
raise ConfigException(
"Accessing config values before setup is not supported")
load_config()


def load_config():
"""
Reads in all the config files, resetting all values.
:raises ConfigException: If called before setting defaults
"""
global __config
if not __default_config_files:
raise Exception("No default configs set")
raise ConfigException("No default configs set")
if __config_file:
__config = conf_loader.load_config(
filename=__config_file, defaults=__default_config_files)
Expand Down Expand Up @@ -186,14 +190,15 @@ def set_config(section, option, value):
:param str section: What section to set the option in.
:param str option: What option to set.
:param object value: Value to set option to
:raises ConfigException: If called unexpectedly
"""
if __config is None:
if __unittest_mode:
load_config()
else:
# The actual error is that load_config should be called before
# set_config but this discourages the use outside of unittests
raise Exception(
raise ConfigException(
"set_config should only be called by unittests "
"which should have called unittest_setup")
__config.set(section, option, value)
Expand Down Expand Up @@ -232,7 +237,7 @@ def _check_lines(py_path, line, lines, index, method):
:param str line: Line with get_config call
:param list(str) lines: All lines in the file
:param int index: index of line with get_config call
:raise Exception: If an unexpected or uncovered get_config found
:raises ConfigException: If an unexpected or uncovered get_config found
"""
while ")" not in line:
index += 1
Expand All @@ -251,16 +256,17 @@ def _check_lines(py_path, line, lines, index, method):
try:
method(section, option)
except Exception as original:
raise Exception(f"failed in line:{index} of file: {py_path} with "
f"section:{section} option:{option}") from original
raise ConfigException(
f"failed in line:{index} of file: {py_path} with "
f"section:{section} option:{option}") from original


def _check_python_file(py_path):
"""
A testing function to check that all the get_config calls work
:param str py_path: path to file to be checked
:raise Exception: If an unexpected or uncovered get_config found
:raises ConfigException: If an unexpected or uncovered get_config found
"""
with open(py_path, 'r', encoding="utf-8") as py_file:
lines = py_file.readlines()
Expand Down Expand Up @@ -291,7 +297,7 @@ def _find_double_defaults(repeaters=None):
:param repeaters: List of options that are expected to be repeated.
:type repeaters: None or list(str)
:return:
:raises ConfigException: If two defaults config files set the same value
"""
config1 = CamelCaseConfigParser()
for default in __default_config_files[:-1]:
Expand All @@ -306,28 +312,28 @@ def _find_double_defaults(repeaters=None):
for option in config2.options(section):
if config1.has_option(section, option):
if option not in repeaters:
raise Exception(
raise ConfigException(
f"cfg:{__default_config_files[-1]} "
f"repeats [{section}]{option}")


def _check_cfg_file(config1, cfg_path):
"""
Supoort method for check_cfgs
Support method for check_cfgs
:param CamelCaseConfigParser config1:
:param str cfg_path:
:raise Exception: If an unexpected option is found
:raises ConfigException: If an unexpected option is found
"""
config2 = CamelCaseConfigParser()
config2.read(cfg_path)
for section in config2.sections():
if not config1.has_section(section):
raise Exception(
raise ConfigException(
f"cfg:{cfg_path} has unexpected section [{section}]")
for option in config2.options(section):
if not config1.has_option(section, option):
raise Exception(
raise ConfigException(
f"cfg:{cfg_path} "
f"has unexpected options [{section}]{option}")

Expand All @@ -341,7 +347,7 @@ def _check_cfgs(path):
type.
:param str path: Absolute path to the parent directory to search
:raise Exception: If an unexpected option is found
:raises ConfigException: If an unexpected option is found
"""
config1 = CamelCaseConfigParser()
for default in __default_config_files:
Expand All @@ -363,6 +369,7 @@ def run_config_checks(directories, *, exceptions=None, repeaters=None):
:param module:
:param repeaters:
:raises ConfigException: If an incorrect directory passed in
:return:
"""
if isinstance(directories, str):
Expand All @@ -380,7 +387,7 @@ def run_config_checks(directories, *, exceptions=None, repeaters=None):

for directory in directories:
if not os.path.isdir(directory):
raise Exception(f"Unable find {directory}")
raise ConfigException(f"Unable find {directory}")
for root, _, files in os.walk(directory):
for file_name in files:
if file_name in exceptions:
Expand Down
13 changes: 13 additions & 0 deletions spinn_utilities/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ class SimulatorNotRunException(SpiNNUtilsException):
"""
Raised when trying to reset or stop before starting
"""


class ConfigException(SpiNNUtilsException):
"""
Raise when reading or setting configs went wrong
"""


class UnexpectedCException(SpiNNUtilsException):
"""
Raise when the Converted which replaces log messages found a pattern
it did not expect and can not handle
"""
5 changes: 3 additions & 2 deletions spinn_utilities/make_tools/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def convert(src, dest, new_dict):

src_path = os.path.abspath(src)
if not os.path.exists(src_path):
raise Exception(f"Unable to locate source directory {src_path}")
raise FileNotFoundError(
f"Unable to locate source directory {src_path}")
dest_path = os.path.abspath(dest)
__convert_dir(src_path, dest_path)

Expand Down Expand Up @@ -65,7 +66,7 @@ def __mkdir(destination):
if not os.path.exists(destination):
os.mkdir(destination)
if not os.path.exists(destination):
raise Exception("mkdir failed {}".format(destination))
raise FileNotFoundError("mkdir failed {}".format(destination))


if __name__ == '__main__':
Expand Down
49 changes: 26 additions & 23 deletions spinn_utilities/make_tools/file_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import enum
import os
import re
from spinn_utilities.exceptions import UnexpectedCException
from .log_sqllite_database import LogSqlLiteDatabase

TOKEN = chr(30) # Record Separator
Expand Down Expand Up @@ -139,14 +140,14 @@ def _check_end_status(self):
if self._status == State.NORMAL_CODE:
return
if self._status == State.IN_LOG:
raise Exception(
raise UnexpectedCException(
f"Unclosed {self._log}{self._log_full} in {self._src}")
if self._status == State.IN_LOG_CLOSE_BRACKET:
raise Exception(
raise UnexpectedCException(
f"Semicolumn missing: "
f"{self._log}{self._log_full} in {self._src}")
if self._status == State.COMMENT:
raise Exception(
raise UnexpectedCException(
f"Unclosed block comment in {self._src}")
raise NotImplementedError(f"Unexpected status {self._status}")

Expand Down Expand Up @@ -342,6 +343,7 @@ def split_by_comma_plus(self, main, line_num):
:param str main:
:param int line_num:
:rtype: list(str)
:raises UnexpectedCException:
"""
try:
parts = main.split(",")
Expand Down Expand Up @@ -383,8 +385,8 @@ def split_by_comma_plus(self, main, line_num):
return parts

except Exception as e:
raise Exception("Unexpected line {} at {} in {}".format(
self._log_full, line_num, self._src)) from e
raise UnexpectedCException(f"Unexpected line {self._log_full} "
f"at {line_num} in {self._src}") from e

def _short_log(self, line_num):
""" shortens the log string message and adds the id
Expand All @@ -397,8 +399,9 @@ def _short_log(self, line_num):
match = LOG_END_REGEX.search(self._log_full)
main = self._log_full[:-len(match.group(0))]
except Exception as e:
raise Exception("Unexpected line {} at {} in {}".format(
self._log_full, line_num, self._src)) from e
raise UnexpectedCException(
f"Unexpected line {self._log_full} at "
f"{line_num} in {self._src}") from e
parts = self.split_by_comma_plus(main, line_num)
original = parts[0]

Expand All @@ -414,16 +417,16 @@ def _short_log(self, line_num):
matches = [x for x in FORMAT_EXP.findall(original)
if not x.startswith("%%")]
if len(matches) != count:
raise Exception(
"Unexpected formatString in {}".format(original))
raise UnexpectedCException(
f"Unexpected formatString in {original}")
if len(parts) < count + 1:
raise Exception(
"Too few parameters in line {} at {} in {}".format(
self._log_full, line_num, self._src))
raise UnexpectedCException(
f"Too few parameters in line {self._log_full} "
f"at {line_num} in {self._src}")
if len(parts) > count + 1:
raise Exception(
"Too many parameters in line {} at {} in {}".format(
self._log_full, line_num, self._src))
raise UnexpectedCException(
f"Too many parameters in line {self._log_full} "
f"at {line_num} in {self._src}")
for i, match in enumerate(matches):
front += TOKEN
if match.endswith("f"):
Expand Down Expand Up @@ -485,6 +488,7 @@ def _process_chars(self, dest_f, line_num, text):
:param dest_f: Open file like Object to write modified source to
:param int line_num: Line number in the source c file
:param str text: Text of that line including whitespace
:raises UnexpectedCException:
"""
pos = 0
write_flag = 0
Expand Down Expand Up @@ -525,15 +529,14 @@ def _process_chars(self, dest_f, line_num, text):
str_pos = pos + 1
while text[str_pos] != '"':
if text[str_pos] == "\n":
raise Exception(
"Unclosed string literal in {} at line: {}".
format(self._src, line_num))
raise UnexpectedCException(
f"Unclosed string literal in {self._src} "
f"at line: {line_num}")
elif text[str_pos] == "\\":
if text[str_pos+1] == "\n":
raise Exception(
"Unclosed string literal in {} at line: {}".
format(self._src, line_num))

raise UnexpectedCException(
f"Unclosed string literal in {self._src} "
f"at line: {line_num}")
else:
str_pos += 2 # ignore next char which may be a "
else:
Expand Down Expand Up @@ -616,7 +619,7 @@ def convert(src_dir, dest_dir, file_name):
"""
source = os.path.join(src_dir, file_name)
if not os.path.exists(source):
raise Exception(F"Unable to locate source {source}")
raise UnexpectedCException(f"Unable to locate source {source}")
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
destination = os.path.join(dest_dir, file_name)
Expand Down
6 changes: 3 additions & 3 deletions spinn_utilities/make_tools/log_sqllite_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, new_dict=False):
message += "This came from the environment variable "
message += "'C_LOGS_DICT "
message += "Please rebuild the c code."
raise Exception(message)
raise FileNotFoundError(message)

try:
self._db = sqlite3.connect(database_file)
Expand All @@ -86,7 +86,7 @@ def __init__(self, new_dict=False):
message += "Check this is a location with write access. "
else:
message += "Please rebuild the c code."
raise Exception(message) from ex
raise FileNotFoundError(message) from ex

def __del__(self):
self.close()
Expand Down Expand Up @@ -229,7 +229,7 @@ def check_original(self, original):
WHERE original = ?
""", ([original])):
if row["counts"] == 0:
raise Exception("Not found")
raise ValueError(f"{original} not found in database")

def get_max_log_id(self):
with self._db:
Expand Down
3 changes: 2 additions & 1 deletion spinn_utilities/package_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def load_modules(
if line_line:
print(" ", line_line.rstrip())
if errors:
raise Exception("Error when importing, starting at {}".format(prefix))
raise ImportError(
"Error when importing, starting at {}".format(prefix))


def load_module(
Expand Down

0 comments on commit 1ecef13

Please sign in to comment.