new text mode editor
This commit is contained in:
@@ -152,6 +152,30 @@ class Connection:
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Parameter:
|
||||
id: str
|
||||
name: str
|
||||
type: str = "real"
|
||||
value: str = "0"
|
||||
|
||||
def to_dict(self) -> dict[str, str]:
|
||||
return {"id": self.id, "name": self.name, "type": self.type, "value": self.value}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, Any]) -> "Parameter":
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError("Each text component parameter must be an object")
|
||||
if "id" not in data:
|
||||
raise ValueError("Each text component parameter must have an ID")
|
||||
return cls(
|
||||
id=str(data["id"]),
|
||||
name=str(data.get("name", "")),
|
||||
type=str(data.get("type", "real")),
|
||||
value=str(data.get("value", "0")),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Annotation:
|
||||
id: str
|
||||
@@ -271,6 +295,19 @@ class Component:
|
||||
kind = str(implementation.get("kind", "graph"))
|
||||
if kind not in {"graph", "text"}:
|
||||
raise ValueError(f"Unknown component implementation kind: {kind}")
|
||||
source: dict[str, Any] = {}
|
||||
if kind == "text":
|
||||
raw_source = implementation.get("source", {})
|
||||
equations = raw_source.get("equations", "")
|
||||
parameters = raw_source.get("parameters", [])
|
||||
if not isinstance(equations, str):
|
||||
raise ValueError("Text component equations must be a string")
|
||||
if not isinstance(parameters, list):
|
||||
raise ValueError("Text component parameters must be a list")
|
||||
source = {
|
||||
"equations": equations,
|
||||
"parameters": [Parameter.from_dict(item).to_dict() for item in parameters],
|
||||
}
|
||||
return cls(
|
||||
id=str(data["id"]),
|
||||
name=str(data.get("name", "Unnamed")),
|
||||
@@ -284,7 +321,7 @@ class Component:
|
||||
show_subtree_in_library=bool(data.get("library", {}).get("showSubtree", True)),
|
||||
implementation_kind=kind,
|
||||
graph=Graph.from_dict(implementation.get("graph")) if kind == "graph" else Graph(),
|
||||
source=dict(implementation.get("source", {})) if kind == "text" else {},
|
||||
source=source,
|
||||
)
|
||||
|
||||
|
||||
@@ -348,6 +385,9 @@ class GraphDocument:
|
||||
|
||||
def validate(self) -> None:
|
||||
seen: set[str] = set()
|
||||
root_names = [component.name for component in self.roots.values()]
|
||||
if len(set(root_names)) != len(root_names):
|
||||
raise ValueError("Root component names must be unique")
|
||||
for component in self.all_components():
|
||||
if component.id in seen:
|
||||
raise ValueError(f"Duplicate component ID: {component.id}")
|
||||
@@ -358,6 +398,11 @@ class GraphDocument:
|
||||
|
||||
@staticmethod
|
||||
def _validate_graph(owner: Component) -> None:
|
||||
child_names = [component.name for component in owner.graph.blocks.values()]
|
||||
if len(set(child_names)) != len(child_names):
|
||||
raise ValueError(
|
||||
f"Component names inside {owner.name!r} must be unique"
|
||||
)
|
||||
input_ids = {port.id for port in owner.inputs}
|
||||
output_ids = {port.id for port in owner.outputs}
|
||||
if len(input_ids) != len(owner.inputs) or len(output_ids) != len(owner.outputs):
|
||||
|
||||
Reference in New Issue
Block a user