Add Python debug runner

This commit is contained in:
shenw
2026-06-22 23:59:15 +08:00
parent efef7e6e18
commit e8678a7582
3 changed files with 129 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import re
import subprocess
from typing import Iterable
_FIELD_RE = re.compile(r"^\s*([A-Za-z][A-Za-z0-9 _-]*):\s*(.+?)\s*$")
@dataclass
class FemLiteResult:
args: list[str]
returncode: int
stdout: str
stderr: str
def check(self) -> "FemLiteResult":
if self.returncode != 0:
raise RuntimeError(
"FemLite command failed with code "
f"{self.returncode}: {' '.join(self.args)}\n{self.stdout}\n{self.stderr}"
)
return self
def fields(self) -> dict[str, str]:
values: dict[str, str] = {}
for line in self.stdout.splitlines():
match = _FIELD_RE.match(line)
if match:
key = match.group(1).strip().lower().replace(" ", "_")
values[key] = match.group(2).strip()
return values
def int_field(self, name: str) -> int:
return int(self.fields()[name])
def float_field(self, name: str) -> float:
return float(self.fields()[name])
class FemLiteRunner:
def __init__(self, executable: str | Path):
self.executable = Path(executable)
def inspect_model(self, inp_file: str | Path) -> FemLiteResult:
return self._run([str(inp_file)])
def run_explicit(self, inp_file: str | Path) -> FemLiteResult:
return self._run([str(inp_file), "--run"])
def solve_implicit_linear(self, inp_file: str | Path) -> FemLiteResult:
return self._run([str(inp_file), "--implicit-linear"])
def _run(self, args: Iterable[str]) -> FemLiteResult:
command = [str(self.executable)]
command.extend(args)
completed = subprocess.run(
command,
check=False,
text=True,
capture_output=True,
)
return FemLiteResult(
args=command,
returncode=completed.returncode,
stdout=completed.stdout,
stderr=completed.stderr,
)