1
- from abc import ABC , abstractmethod
2
- import ctypes
3
1
import os
4
- from pathlib import Path
2
+ import pathlib
5
3
import subprocess
6
- from typing import Optional , Any
4
+ from abc import ABC , abstractmethod
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import Optional
8
+
9
+
10
+ @dataclass
11
+ class ReturnInformation :
12
+ """
13
+ Class to store information about a CADET run return status.
14
+
15
+ Parameters
16
+ ----------
17
+ return_code : int
18
+ An integer representing the return code. 0 indicates success, non-zero values indicate errors.
19
+ error_message : str
20
+ A string containing the error message if an error occurred. Empty if no error.
21
+ log : str
22
+ A string containing log information.
23
+ """
24
+ return_code : int
25
+ error_message : str
26
+ log : str
7
27
8
28
9
29
class CadetRunnerBase (ABC ):
@@ -12,13 +32,14 @@ class CadetRunnerBase(ABC):
12
32
13
33
Subclasses must implement the `run`, `clear`, and `load_results` methods.
14
34
"""
35
+ cadet_path : Optional [pathlib .Path ] = None
15
36
16
37
@abstractmethod
17
38
def run (
18
39
self ,
19
40
simulation : "Cadet" ,
20
41
timeout : Optional [int ] = None ,
21
- ) -> bool :
42
+ ) -> ReturnInformation :
22
43
"""
23
44
Run a CADET simulation.
24
45
@@ -31,8 +52,8 @@ def run(
31
52
32
53
Returns
33
54
-------
34
- bool
35
- True, if simulation ran successfully, False otherwise .
55
+ ReturnInformation
56
+ Information about the simulation run .
36
57
"""
37
58
pass
38
59
@@ -56,14 +77,14 @@ def load_results(self, sim: "Cadet") -> None:
56
77
pass
57
78
58
79
59
- class CadetFileRunner (CadetRunnerBase ):
80
+ class CadetCLIRunner (CadetRunnerBase ):
60
81
"""
61
82
File-based CADET runner.
62
83
63
84
This class runs CADET simulations using a command-line interface (CLI) executable.
64
85
"""
65
86
66
- def __init__ (self , cadet_path : str ) -> None :
87
+ def __init__ (self , cadet_path : str | os . PathLike ) -> None :
67
88
"""
68
89
Initialize the CadetFileRunner.
69
90
@@ -80,7 +101,7 @@ def run(
80
101
self ,
81
102
simulation : "Cadet" ,
82
103
timeout : Optional [int ] = None ,
83
- ) -> None :
104
+ ) -> ReturnInformation :
84
105
"""
85
106
Run a CADET simulation using the CLI executable.
86
107
@@ -95,6 +116,11 @@ def run(
95
116
------
96
117
RuntimeError
97
118
If the simulation process returns a non-zero exit code.
119
+
120
+ Returns
121
+ -------
122
+ ReturnInformation
123
+ Information about the simulation run.
98
124
"""
99
125
if simulation .filename is None :
100
126
raise ValueError ("Filename must be set before run can be used" )
@@ -105,12 +131,13 @@ def run(
105
131
capture_output = True
106
132
)
107
133
108
- if data .returncode != 0 :
109
- raise RuntimeError (
110
- f"Simulation failed with error: { data .stderr .decode ('utf-8' )} "
111
- )
134
+ return_info = ReturnInformation (
135
+ return_code = data .returncode ,
136
+ error_message = data .stderr .decode ('utf-8' ),
137
+ log = data .stdout .decode ('utf-8' )
138
+ )
112
139
113
- return
140
+ return return_info
114
141
115
142
def clear (self ) -> None :
116
143
"""
0 commit comments