Coverage for pydelica/exception.py: 76%

1 import re

2

3

4 class BinaryNotFoundError(Exception):

5 def __init__(self, msg):

6 Exception.__init__(self, msg)

7

8

9 class OMParsingError(Exception):

10 def __init__(self, msg):

11 Exception.__init__(self, msg)

12

13

14 class OMExecutionError(Exception):

15 def __init__(self, msg):

16 Exception.__init__(self, msg)

17

18

19 class OMBuildError(Exception):

20 def __init__(self, msg):

21 Exception.__init__(self, msg)

22

23

24 class OMAssertionError(Exception):

25 def __init__(self, msg):

26 Exception.__init__(self, msg)

27

28

29 class NotImplementedError(Exception):

30 def __init__(self, msg):

31 Exception.__init__(self, msg)

32

33

34 class UnknownParameterError(Exception):

35 def __init__(self, param_name: str):

36 msg = f"Parameter '{param_name}' is not a recognised parameter name"

37 Exception.__init__(self, msg)

38

39

40 class UnknownModelError(Exception):

41 def __init__(self, model_name: str):

42 msg = f"Model '{model_name}' is not a recognised model name."

43 Exception.__init__(self, msg)

44

45

46 class ResultRetrievalError(Exception):

47 def __init__(self):

48 msg = "Failed to retrieve simulation results, could not read output files."

49 Exception.__init__(self, msg)

50

51

52 class UnknownOptionError(Exception):

53 def __init__(self, opt_name: str):

54 msg = f"Option '{opt_name}' is not a recognised simulation option"

55 Exception.__init__(self, msg)

56

57

58 class ModelicaFileGenerationError(Exception):

59 def __init__(self, msg):

60 Exception.__init__(self, msg)

61

62

63 class UnknownLibraryError(Exception):

64 def __init__(self, msg):

65 Exception.__init__(self, msg)

66

67

68 def parse_error_string_compiler(out_string: str, error_string: str):

69 if "Failed to parse file" in out_string:

70 print(out_string)

71 _error = [i for i in out_string.split("\n") if i and i[0] == "["]

72 raise OMParsingError(", ".join(_error))

73 elif "Execution failed!" in error_string:

74 raise OMExecutionError(f"Failed to execute compiled code:\n{out_string}")

75

76 # Check if "failed" is present within output, ignore print out of code

77 # assuming all lines containing the term end with ';'

78

79 _lines: list[str] = [

80 i for i in out_string.split("\n")

81 if "failed" in i.lower()

82 and not i.strip().endswith(";") # Do not include lines from code

83 ]

84

85 if "failed" in out_string and _lines:

86 raise OMBuildError(

87 ", ".join(_lines)

88 )

89

90

91 def parse_error_string_simulate(out_string: str, terminate_on_assert: str = "error"):

92 print(out_string)

93 if "division by zero" in out_string:

94 _line = [i for i in out_string.split("\n") if "division by zero" in i]

95 raise ZeroDivisionError(_line[0].split("|")[-1].strip())

96 elif "simulation terminated by an assertion" in out_string:

97 raise OMAssertionError(f"Simulation run failed:\n{out_string}")

98

99 _find_assert = re.compile(r"assert\s*\|\s*(\w+)\s*\|", re.IGNORECASE)

100 _asserts = _find_assert.findall(out_string)

101

102 _assertion_ranking = ("debug", "info", "warning", "error", "never")

103

104 if not _asserts:

105 return

106

107 _assertion_rank_pass = [

108 _assertion_ranking.index(i) >= _assertion_ranking.index(terminate_on_assert)

109 for i in _asserts

110 ]

111

112 if any(_assertion_rank_pass):

113 raise OMAssertionError(out_string)