Skip to content

Commit 21eaa9a

Browse files
ronald-jaepelschmoelder
authored andcommitted
Add ReturnInformation Class to CadetRunner
1 parent 165de2f commit 21eaa9a

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

cadet/runner.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
from abc import ABC, abstractmethod
2-
import ctypes
31
import os
4-
from pathlib import Path
2+
import pathlib
53
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
727

828

929
class CadetRunnerBase(ABC):
@@ -12,13 +32,14 @@ class CadetRunnerBase(ABC):
1232
1333
Subclasses must implement the `run`, `clear`, and `load_results` methods.
1434
"""
35+
cadet_path: Optional[pathlib.Path] = None
1536

1637
@abstractmethod
1738
def run(
1839
self,
1940
simulation: "Cadet",
2041
timeout: Optional[int] = None,
21-
) -> bool:
42+
) -> ReturnInformation:
2243
"""
2344
Run a CADET simulation.
2445
@@ -31,8 +52,8 @@ def run(
3152
3253
Returns
3354
-------
34-
bool
35-
True, if simulation ran successfully, False otherwise.
55+
ReturnInformation
56+
Information about the simulation run.
3657
"""
3758
pass
3859

@@ -56,14 +77,14 @@ def load_results(self, sim: "Cadet") -> None:
5677
pass
5778

5879

59-
class CadetFileRunner(CadetRunnerBase):
80+
class CadetCLIRunner(CadetRunnerBase):
6081
"""
6182
File-based CADET runner.
6283
6384
This class runs CADET simulations using a command-line interface (CLI) executable.
6485
"""
6586

66-
def __init__(self, cadet_path: str) -> None:
87+
def __init__(self, cadet_path: str | os.PathLike) -> None:
6788
"""
6889
Initialize the CadetFileRunner.
6990
@@ -80,7 +101,7 @@ def run(
80101
self,
81102
simulation: "Cadet",
82103
timeout: Optional[int] = None,
83-
) -> None:
104+
) -> ReturnInformation:
84105
"""
85106
Run a CADET simulation using the CLI executable.
86107
@@ -95,6 +116,11 @@ def run(
95116
------
96117
RuntimeError
97118
If the simulation process returns a non-zero exit code.
119+
120+
Returns
121+
-------
122+
ReturnInformation
123+
Information about the simulation run.
98124
"""
99125
if simulation.filename is None:
100126
raise ValueError("Filename must be set before run can be used")
@@ -105,12 +131,13 @@ def run(
105131
capture_output=True
106132
)
107133

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+
)
112139

113-
return
140+
return return_info
114141

115142
def clear(self) -> None:
116143
"""

0 commit comments

Comments
 (0)