Files
BEdit/src/bedit_gui/simulation_models.py

60 lines
2.2 KiB
Python

from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Any
from bedit_core.models import ComponentID
from bedit_gui.models import Simulation
@dataclass
class CompiledModel:
model_name: str
executable: str
working_directory: str
@classmethod
def from_data(cls, data: Mapping[str, Any]) -> CompiledModel:
return cls(model_name=str(data["model_name"]), executable=str(data["executable"]), working_directory=str(data["working_directory"]))
def to_data(self) -> dict[str, Any]:
return {"model_name": self.model_name, "executable": self.executable, "working_directory": self.working_directory}
@dataclass
class SimulationRoot:
format_version: int
source_document: str | None
source_document_id: str | None
component: ComponentID
component_path: str
settings_name: str | None
settings: Simulation
@classmethod
def from_data(cls, data: Mapping[str, Any]) -> SimulationRoot:
if data.get("root_type") != "simulation_root":
raise ValueError("file does not contain a simulation root")
return cls(
format_version=int(data.get("format_version", 1)),
source_document=str(data["source_document"]) if data.get("source_document") is not None else None,
source_document_id=str(data["source_document_id"]) if data.get("source_document_id") is not None else None,
component=ComponentID(str(data["component"])),
component_path=str(data.get("component_path", "")),
settings_name=str(data["settings_name"]) if data.get("settings_name") is not None else None,
settings=Simulation.from_data(data["settings"]),
)
def to_data(self) -> dict[str, Any]:
return {
"root_type": "simulation_root",
"format_version": self.format_version,
"source_document": self.source_document,
"source_document_id": self.source_document_id,
"component": str(self.component),
"component_path": self.component_path,
"settings_name": self.settings_name,
"settings": self.settings.to_data(),
}