koode-taskgraph 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- koode_taskgraph-0.1.0.dist-info/METADATA +7 -0
- koode_taskgraph-0.1.0.dist-info/RECORD +23 -0
- koode_taskgraph-0.1.0.dist-info/WHEEL +5 -0
- koode_taskgraph-0.1.0.dist-info/entry_points.txt +3 -0
- koode_taskgraph-0.1.0.dist-info/top_level.txt +1 -0
- taskgraph/__init__.py +4 -0
- taskgraph/__main__.py +4 -0
- taskgraph/app.py +53 -0
- taskgraph/cli.py +123 -0
- taskgraph/core/__init__.py +2 -0
- taskgraph/core/executor.py +232 -0
- taskgraph/core/model.py +190 -0
- taskgraph/core/registry.py +90 -0
- taskgraph/core/serialization.py +35 -0
- taskgraph/nodes/__init__.py +14 -0
- taskgraph/nodes/builtin.py +152 -0
- taskgraph/ui/__init__.py +1 -0
- taskgraph/ui/items.py +403 -0
- taskgraph/ui/main_window.py +578 -0
- taskgraph/ui/plugins.py +208 -0
- taskgraph/ui/properties.py +165 -0
- taskgraph/ui/scene.py +426 -0
- taskgraph/ui/theme.py +30 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
taskgraph/__init__.py,sha256=DuNzJDlqEW5Nca11UH4UQKgh8R-GrwRhIZtp-jBTCDE,49
|
|
2
|
+
taskgraph/__main__.py,sha256=0A_W69tXd8298824yKSnOoQb4WEbNtjN3sxVWeeyEK8,58
|
|
3
|
+
taskgraph/app.py,sha256=Lf9HkG8ZUmtiU63gjlAcjq4-B58PAz8I0glCOchm2u8,1871
|
|
4
|
+
taskgraph/cli.py,sha256=EFM59xRcbJFQxEXfkMWTeby5aBQ0UmAJqamfyzCj1zM,3858
|
|
5
|
+
taskgraph/core/__init__.py,sha256=DC91c0iXgOcd9RSLxNzBFYhgU4Lszm6dXmVwqphcUy0,46
|
|
6
|
+
taskgraph/core/executor.py,sha256=n3Gh5Od1OEv9WBwElQV6aMZDpO0VQ2wKV5mDxK8Y7Mw,9112
|
|
7
|
+
taskgraph/core/model.py,sha256=L25MUoyB8nxyouG5DiWB3zxSVo32hg_Zb8hbyx6fHv8,6092
|
|
8
|
+
taskgraph/core/registry.py,sha256=X2JO2ZtQpjPn70bhyTh7HEJKMRpzp6GB9bT86mPooec,3041
|
|
9
|
+
taskgraph/core/serialization.py,sha256=myS2mm7TumPm8Eje5-OpiA622UN6oHfb7MzTlc7VCBk,1279
|
|
10
|
+
taskgraph/nodes/__init__.py,sha256=WVt7zW3yxOlB-_Y7s7yTy1InfYrVmTf43_bsfMEEYh4,445
|
|
11
|
+
taskgraph/nodes/builtin.py,sha256=qn8BtvR-iO45DXxIIb0dN8lgtXqOwFzdBoxGzhVJUXg,4958
|
|
12
|
+
taskgraph/ui/__init__.py,sha256=mzi5n3B0WyYi4y6JJ2DlIp9gVBgZeWJTv2NsS5bn6oI,39
|
|
13
|
+
taskgraph/ui/items.py,sha256=014g61eFBcXf5RVnrZHpYNxMBIh4W5UExoJf8cBbawI,15453
|
|
14
|
+
taskgraph/ui/main_window.py,sha256=yZvSNP7mHtMrt5LYGHdXgj101CVrbDG52TkbnKesaTw,22543
|
|
15
|
+
taskgraph/ui/plugins.py,sha256=yUwOGaXjTvzP0tZkePIVGjpMW0Fg6EzSMfewKMmmfac,6243
|
|
16
|
+
taskgraph/ui/properties.py,sha256=8yitHRJRPFQlTFQsdQH7slff1e-Tg9phKKd9Z1CSkpc,6415
|
|
17
|
+
taskgraph/ui/scene.py,sha256=A0lafajgoU77EeXHsmJ4VvtHCUAaNwMVRac2U3-ubD8,16667
|
|
18
|
+
taskgraph/ui/theme.py,sha256=aMoNCX-Yuhp-hIwbrLw7f750g8R3eStlZ2n4vN2JvxY,1384
|
|
19
|
+
koode_taskgraph-0.1.0.dist-info/METADATA,sha256=0rq28haUD3WOBMyEYwhA9WPYd1GxGEAP-cYoKxs6hps,206
|
|
20
|
+
koode_taskgraph-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
21
|
+
koode_taskgraph-0.1.0.dist-info/entry_points.txt,sha256=TBhTSPUy8BT9L3114B88vQoOcecNypF0APuQz0QtOMo,83
|
|
22
|
+
koode_taskgraph-0.1.0.dist-info/top_level.txt,sha256=3JNeYn_hNiNXC7DrdH_vcv-WYSE7QdgGjdvUYvSjVp0,10
|
|
23
|
+
koode_taskgraph-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
taskgraph
|
taskgraph/__init__.py
ADDED
taskgraph/__main__.py
ADDED
taskgraph/app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from qtpy.QtCore import QSettings
|
|
5
|
+
from qtpy.QtWidgets import QApplication
|
|
6
|
+
|
|
7
|
+
from taskgraph.core.registry import load_custom_node_directory
|
|
8
|
+
from taskgraph.nodes import load_builtin_nodes
|
|
9
|
+
from taskgraph.ui.main_window import MainWindow
|
|
10
|
+
from taskgraph.ui.plugins import load_gui_plugin_directory
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main() -> int:
|
|
14
|
+
app = QApplication.instance() or QApplication(sys.argv)
|
|
15
|
+
app.setApplicationName("TaskGraph")
|
|
16
|
+
app.setOrganizationName("TaskGraph")
|
|
17
|
+
load_builtin_nodes()
|
|
18
|
+
saved_locations = QSettings().value("nodes/custom_locations", [])
|
|
19
|
+
if isinstance(saved_locations, str):
|
|
20
|
+
saved_locations = [saved_locations]
|
|
21
|
+
environment_locations = [
|
|
22
|
+
path for path in os.environ.get("TASKGRAPH_NODE_PATH", "").split(os.pathsep)
|
|
23
|
+
if path
|
|
24
|
+
]
|
|
25
|
+
load_errors = []
|
|
26
|
+
for location in dict.fromkeys([*saved_locations, *environment_locations]):
|
|
27
|
+
try:
|
|
28
|
+
load_custom_node_directory(location)
|
|
29
|
+
except Exception as exc:
|
|
30
|
+
load_errors.append(f"{location}: {exc}")
|
|
31
|
+
window = MainWindow()
|
|
32
|
+
saved_plugin_locations = QSettings().value("plugins/custom_locations", [])
|
|
33
|
+
if isinstance(saved_plugin_locations, str):
|
|
34
|
+
saved_plugin_locations = [saved_plugin_locations]
|
|
35
|
+
environment_plugin_locations = [
|
|
36
|
+
path for path in os.environ.get("TASKGRAPH_PLUGIN_PATH", "").split(os.pathsep)
|
|
37
|
+
if path
|
|
38
|
+
]
|
|
39
|
+
for location in dict.fromkeys([
|
|
40
|
+
*saved_plugin_locations,
|
|
41
|
+
*environment_plugin_locations,
|
|
42
|
+
]):
|
|
43
|
+
try:
|
|
44
|
+
load_gui_plugin_directory(location, window)
|
|
45
|
+
except Exception as exc:
|
|
46
|
+
load_errors.append(f"{location}: {exc}")
|
|
47
|
+
window.palette.reload()
|
|
48
|
+
if load_errors:
|
|
49
|
+
window.console.appendPlainText(
|
|
50
|
+
"Custom loading errors:\n" + "\n".join(load_errors)
|
|
51
|
+
)
|
|
52
|
+
window.show()
|
|
53
|
+
return app.exec()
|
taskgraph/cli.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Headless command-line runner for TaskGraph files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
import signal
|
|
10
|
+
import sys
|
|
11
|
+
from threading import Event
|
|
12
|
+
|
|
13
|
+
from taskgraph.core.executor import (
|
|
14
|
+
VALID_WORKER_COUNTS,
|
|
15
|
+
GraphExecutionCancelled,
|
|
16
|
+
GraphExecutionError,
|
|
17
|
+
execute_graph,
|
|
18
|
+
)
|
|
19
|
+
from taskgraph.core.registry import load_custom_node_directory
|
|
20
|
+
from taskgraph.core.serialization import load_graph
|
|
21
|
+
from taskgraph.nodes import load_builtin_nodes
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def worker_count(value: str) -> int:
|
|
25
|
+
try:
|
|
26
|
+
count = int(value)
|
|
27
|
+
except ValueError as exc:
|
|
28
|
+
raise argparse.ArgumentTypeError("worker count must be an integer") from exc
|
|
29
|
+
if count not in VALID_WORKER_COUNTS:
|
|
30
|
+
raise argparse.ArgumentTypeError(
|
|
31
|
+
"worker count must be 1 or an even number from 2 through 32"
|
|
32
|
+
)
|
|
33
|
+
return count
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
37
|
+
parser = argparse.ArgumentParser(
|
|
38
|
+
prog="taskgraphcli",
|
|
39
|
+
description="Execute a TaskGraph file without starting the Qt UI.",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--file", "-f", required=True, type=Path,
|
|
43
|
+
help="path to a .taskgraph or compatible JSON graph file",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--workers", "-w", type=worker_count, default=4,
|
|
47
|
+
help="parallel workers: 1 or an even number from 2 through 32 (default: 4)",
|
|
48
|
+
)
|
|
49
|
+
parser.add_argument(
|
|
50
|
+
"--node-path", action="append", default=[], metavar="DIRECTORY",
|
|
51
|
+
help="custom node directory; may be passed more than once",
|
|
52
|
+
)
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
"--quiet", action="store_true",
|
|
55
|
+
help="suppress progress messages and print only the final status",
|
|
56
|
+
)
|
|
57
|
+
parser.add_argument(
|
|
58
|
+
"--json", action="store_true",
|
|
59
|
+
help="print the execution result as JSON",
|
|
60
|
+
)
|
|
61
|
+
return parser
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main(argv: list[str] | None = None) -> int:
|
|
65
|
+
args = build_parser().parse_args(argv)
|
|
66
|
+
graph_path = args.file.expanduser()
|
|
67
|
+
if not graph_path.is_file():
|
|
68
|
+
print(f"taskgraphcli: graph file not found: {graph_path}", file=sys.stderr)
|
|
69
|
+
return 2
|
|
70
|
+
|
|
71
|
+
try:
|
|
72
|
+
load_builtin_nodes()
|
|
73
|
+
environment_paths = [
|
|
74
|
+
path
|
|
75
|
+
for path in os.environ.get("TASKGRAPH_NODE_PATH", "").split(os.pathsep)
|
|
76
|
+
if path
|
|
77
|
+
]
|
|
78
|
+
for location in dict.fromkeys([*environment_paths, *args.node_path]):
|
|
79
|
+
load_custom_node_directory(location)
|
|
80
|
+
graph = load_graph(graph_path)
|
|
81
|
+
except Exception as exc:
|
|
82
|
+
print(f"taskgraphcli: could not load graph: {exc}", file=sys.stderr)
|
|
83
|
+
return 2
|
|
84
|
+
|
|
85
|
+
cancel_event = Event()
|
|
86
|
+
previous_handler = signal.getsignal(signal.SIGINT)
|
|
87
|
+
|
|
88
|
+
def request_cancel(_signum, _frame) -> None:
|
|
89
|
+
if not cancel_event.is_set():
|
|
90
|
+
print("Cancellation requested…", file=sys.stderr)
|
|
91
|
+
cancel_event.set()
|
|
92
|
+
|
|
93
|
+
signal.signal(signal.SIGINT, request_cancel)
|
|
94
|
+
event_stream = sys.stderr if args.json else sys.stdout
|
|
95
|
+
emit = None if args.quiet else lambda message: print(message, file=event_stream)
|
|
96
|
+
try:
|
|
97
|
+
result = execute_graph(
|
|
98
|
+
graph,
|
|
99
|
+
on_event=emit,
|
|
100
|
+
max_workers=args.workers,
|
|
101
|
+
cancel_event=cancel_event,
|
|
102
|
+
)
|
|
103
|
+
except GraphExecutionCancelled as exc:
|
|
104
|
+
print(f"taskgraphcli: {exc}", file=sys.stderr)
|
|
105
|
+
return 130
|
|
106
|
+
except GraphExecutionError as exc:
|
|
107
|
+
print(f"taskgraphcli: execution failed:\n{exc}", file=sys.stderr)
|
|
108
|
+
return 1
|
|
109
|
+
finally:
|
|
110
|
+
signal.signal(signal.SIGINT, previous_handler)
|
|
111
|
+
|
|
112
|
+
if args.json:
|
|
113
|
+
print(json.dumps({
|
|
114
|
+
"order": result.order,
|
|
115
|
+
"outputs": result.outputs,
|
|
116
|
+
}, indent=2, default=repr))
|
|
117
|
+
else:
|
|
118
|
+
print(f"Completed {len(result.order)} node(s).")
|
|
119
|
+
return 0
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict, deque
|
|
4
|
+
from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from threading import Event
|
|
7
|
+
from typing import Any, Callable
|
|
8
|
+
|
|
9
|
+
from taskgraph.core.model import Graph, NodeCancelled
|
|
10
|
+
|
|
11
|
+
VALID_WORKER_COUNTS = (1, *range(2, 33, 2))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GraphExecutionError(RuntimeError):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class GraphExecutionCancelled(GraphExecutionError):
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class ExecutionResult:
|
|
24
|
+
order: list[str]
|
|
25
|
+
outputs: dict[str, dict[str, Any]]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def execute_graph(
|
|
29
|
+
graph: Graph,
|
|
30
|
+
on_event: Callable[[str], None] | None = None,
|
|
31
|
+
max_workers: int = 4,
|
|
32
|
+
on_node_state: Callable[[str, str], None] | None = None,
|
|
33
|
+
cancel_event: Event | None = None,
|
|
34
|
+
) -> ExecutionResult:
|
|
35
|
+
if max_workers not in VALID_WORKER_COUNTS:
|
|
36
|
+
raise ValueError(
|
|
37
|
+
"max_workers must be 1 or an even number between 2 and 32"
|
|
38
|
+
)
|
|
39
|
+
emit = on_event or (lambda _message: None)
|
|
40
|
+
set_state = on_node_state or (lambda _node_id, _state: None)
|
|
41
|
+
cancel_event = cancel_event or Event()
|
|
42
|
+
indegree = {node_id: 0 for node_id in graph.nodes}
|
|
43
|
+
downstream: dict[str, list[str]] = defaultdict(list)
|
|
44
|
+
incoming = defaultdict(list)
|
|
45
|
+
|
|
46
|
+
attribute_edges = []
|
|
47
|
+
for edge in graph.connections:
|
|
48
|
+
if edge.source_node not in graph.nodes or edge.target_node not in graph.nodes:
|
|
49
|
+
raise GraphExecutionError("A connection references a missing node")
|
|
50
|
+
if edge.kind == "dependency":
|
|
51
|
+
indegree[edge.target_node] += 1
|
|
52
|
+
downstream[edge.source_node].append(edge.target_node)
|
|
53
|
+
else:
|
|
54
|
+
attribute_edges.append(edge)
|
|
55
|
+
incoming[edge.target_node].append(edge)
|
|
56
|
+
|
|
57
|
+
def has_dependency_path(source: str, target: str) -> bool:
|
|
58
|
+
pending = [source]
|
|
59
|
+
visited = set()
|
|
60
|
+
while pending:
|
|
61
|
+
current = pending.pop()
|
|
62
|
+
if current == target:
|
|
63
|
+
return True
|
|
64
|
+
if current not in visited:
|
|
65
|
+
visited.add(current)
|
|
66
|
+
pending.extend(downstream[current])
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
for edge in attribute_edges:
|
|
70
|
+
if not has_dependency_path(edge.source_node, edge.target_node):
|
|
71
|
+
source = graph.nodes[edge.source_node]
|
|
72
|
+
target = graph.nodes[edge.target_node]
|
|
73
|
+
raise GraphExecutionError(
|
|
74
|
+
f"Attribute {source.display_name}.{edge.source_port} → "
|
|
75
|
+
f"{target.display_name}.{edge.target_port} requires a dependency path"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
queue = deque(node_id for node_id, degree in indegree.items() if degree == 0)
|
|
79
|
+
order: list[str] = []
|
|
80
|
+
outputs: dict[str, dict[str, Any]] = {}
|
|
81
|
+
terminal: set[str] = set()
|
|
82
|
+
failures: list[str] = []
|
|
83
|
+
|
|
84
|
+
def inputs_for(node_id: str) -> dict[str, Any]:
|
|
85
|
+
node = graph.nodes[node_id]
|
|
86
|
+
node_inputs: dict[str, Any] = {}
|
|
87
|
+
for edge in incoming[node_id]:
|
|
88
|
+
source_outputs = outputs.get(edge.source_node, {})
|
|
89
|
+
if edge.source_port not in source_outputs:
|
|
90
|
+
source = graph.nodes[edge.source_node]
|
|
91
|
+
raise GraphExecutionError(
|
|
92
|
+
f"{node.display_name}: input '{edge.target_port}' is unavailable "
|
|
93
|
+
f"because {source.display_name} did not produce '{edge.source_port}'"
|
|
94
|
+
)
|
|
95
|
+
value = source_outputs[edge.source_port]
|
|
96
|
+
port = next(spec for spec in node.inputs if spec.name == edge.target_port)
|
|
97
|
+
if port.multiple:
|
|
98
|
+
node_inputs.setdefault(edge.target_port, []).append(value)
|
|
99
|
+
else:
|
|
100
|
+
node_inputs[edge.target_port] = value
|
|
101
|
+
|
|
102
|
+
missing = [port.name for port in node.inputs if port.required and port.name not in node_inputs]
|
|
103
|
+
if missing:
|
|
104
|
+
raise GraphExecutionError(
|
|
105
|
+
f"{node.display_name}: missing required input(s): {', '.join(missing)}"
|
|
106
|
+
)
|
|
107
|
+
return node_inputs
|
|
108
|
+
|
|
109
|
+
def complete(node_id: str, result: dict[str, Any]) -> None:
|
|
110
|
+
outputs[node_id] = result
|
|
111
|
+
order.append(node_id)
|
|
112
|
+
terminal.add(node_id)
|
|
113
|
+
for target in downstream[node_id]:
|
|
114
|
+
indegree[target] -= 1
|
|
115
|
+
if indegree[target] == 0:
|
|
116
|
+
queue.append(target)
|
|
117
|
+
|
|
118
|
+
def fail_branch(node_id: str, message: str) -> None:
|
|
119
|
+
"""Fail one node and block only nodes that depend on its output."""
|
|
120
|
+
if node_id in terminal:
|
|
121
|
+
return
|
|
122
|
+
terminal.add(node_id)
|
|
123
|
+
failures.append(message)
|
|
124
|
+
set_state(node_id, "failed")
|
|
125
|
+
emit(f"Failure: {message}")
|
|
126
|
+
pending = deque(downstream[node_id])
|
|
127
|
+
while pending:
|
|
128
|
+
blocked_id = pending.popleft()
|
|
129
|
+
if blocked_id in terminal:
|
|
130
|
+
continue
|
|
131
|
+
terminal.add(blocked_id)
|
|
132
|
+
set_state(blocked_id, "blocked")
|
|
133
|
+
emit(
|
|
134
|
+
f"Blocked {graph.nodes[blocked_id].display_name}: "
|
|
135
|
+
"an upstream dependency failed"
|
|
136
|
+
)
|
|
137
|
+
pending.extend(downstream[blocked_id])
|
|
138
|
+
|
|
139
|
+
running = {}
|
|
140
|
+
cancellation_applied = False
|
|
141
|
+
with ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix="taskgraph") as pool:
|
|
142
|
+
while queue or running:
|
|
143
|
+
if cancel_event.is_set() and not cancellation_applied:
|
|
144
|
+
cancellation_applied = True
|
|
145
|
+
emit("Cancellation requested…")
|
|
146
|
+
running_ids = set(running.values())
|
|
147
|
+
for future in running:
|
|
148
|
+
future.cancel()
|
|
149
|
+
for node_id in graph.nodes:
|
|
150
|
+
if node_id not in terminal and node_id not in running_ids:
|
|
151
|
+
terminal.add(node_id)
|
|
152
|
+
set_state(node_id, "cancelled")
|
|
153
|
+
queue.clear()
|
|
154
|
+
|
|
155
|
+
while queue and len(running) < max_workers:
|
|
156
|
+
node_id = queue.popleft()
|
|
157
|
+
if node_id in terminal:
|
|
158
|
+
continue
|
|
159
|
+
node = graph.nodes[node_id]
|
|
160
|
+
if node.disabled:
|
|
161
|
+
emit(f"Skipped disabled node: {node.display_name}")
|
|
162
|
+
set_state(node_id, "skipped")
|
|
163
|
+
complete(node_id, {})
|
|
164
|
+
continue
|
|
165
|
+
try:
|
|
166
|
+
node_inputs = inputs_for(node_id)
|
|
167
|
+
except GraphExecutionError as exc:
|
|
168
|
+
fail_branch(node_id, str(exc))
|
|
169
|
+
continue
|
|
170
|
+
emit(f"Running {node.display_name}")
|
|
171
|
+
set_state(node_id, "running")
|
|
172
|
+
node._cancel_event = cancel_event
|
|
173
|
+
running[pool.submit(node.process, node_inputs)] = node_id
|
|
174
|
+
|
|
175
|
+
if not running:
|
|
176
|
+
continue
|
|
177
|
+
|
|
178
|
+
finished, _pending = wait(running, return_when=FIRST_COMPLETED)
|
|
179
|
+
for future in finished:
|
|
180
|
+
node_id = running.pop(future)
|
|
181
|
+
node = graph.nodes[node_id]
|
|
182
|
+
if cancel_event.is_set():
|
|
183
|
+
try:
|
|
184
|
+
future.result()
|
|
185
|
+
except Exception:
|
|
186
|
+
pass
|
|
187
|
+
terminal.add(node_id)
|
|
188
|
+
set_state(node_id, "cancelled")
|
|
189
|
+
emit(f"Cancelled {node.display_name}")
|
|
190
|
+
node._cancel_event = None
|
|
191
|
+
continue
|
|
192
|
+
try:
|
|
193
|
+
result = future.result()
|
|
194
|
+
except NodeCancelled:
|
|
195
|
+
terminal.add(node_id)
|
|
196
|
+
set_state(node_id, "cancelled")
|
|
197
|
+
emit(f"Cancelled {node.display_name}")
|
|
198
|
+
node._cancel_event = None
|
|
199
|
+
continue
|
|
200
|
+
except Exception as exc:
|
|
201
|
+
fail_branch(node_id, f"{node.display_name} failed: {exc}")
|
|
202
|
+
node._cancel_event = None
|
|
203
|
+
continue
|
|
204
|
+
if not isinstance(result, dict):
|
|
205
|
+
fail_branch(
|
|
206
|
+
node_id,
|
|
207
|
+
f"{node.display_name} must return a dictionary",
|
|
208
|
+
)
|
|
209
|
+
node._cancel_event = None
|
|
210
|
+
continue
|
|
211
|
+
complete(node_id, result)
|
|
212
|
+
set_state(node_id, "finished")
|
|
213
|
+
emit(f"Finished {node.display_name}: {result}")
|
|
214
|
+
node._cancel_event = None
|
|
215
|
+
|
|
216
|
+
if cancel_event.is_set():
|
|
217
|
+
# A running cooperative node may notice cancellation and finish before
|
|
218
|
+
# the scheduler reaches the next loop iteration. Ensure descendants
|
|
219
|
+
# that never entered the ready queue still receive a cancelled state.
|
|
220
|
+
for node_id in graph.nodes:
|
|
221
|
+
if node_id not in terminal:
|
|
222
|
+
terminal.add(node_id)
|
|
223
|
+
set_state(node_id, "cancelled")
|
|
224
|
+
raise GraphExecutionCancelled("Graph execution cancelled")
|
|
225
|
+
unresolved = set(graph.nodes) - terminal
|
|
226
|
+
if unresolved:
|
|
227
|
+
failures.append("The graph contains a cycle and cannot be executed")
|
|
228
|
+
for node_id in unresolved:
|
|
229
|
+
set_state(node_id, "blocked")
|
|
230
|
+
if failures:
|
|
231
|
+
raise GraphExecutionError("\n".join(failures))
|
|
232
|
+
return ExecutionResult(order, outputs)
|
taskgraph/core/model.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Any, ClassVar
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NodeCancelled(RuntimeError):
|
|
9
|
+
"""Raised by a process node after honoring an execution cancellation."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class PortSpec:
|
|
14
|
+
name: str
|
|
15
|
+
data_type: str = "any"
|
|
16
|
+
required: bool = False
|
|
17
|
+
multiple: bool = False
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class NodeProperty:
|
|
22
|
+
name: str
|
|
23
|
+
label: str
|
|
24
|
+
kind: str
|
|
25
|
+
default: Any = None
|
|
26
|
+
choices: tuple[Any, ...] = ()
|
|
27
|
+
minimum: float | None = None
|
|
28
|
+
maximum: float | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ProcessNode:
|
|
32
|
+
type_id: ClassVar[str] = "core.process"
|
|
33
|
+
title: ClassVar[str] = "Process"
|
|
34
|
+
category: ClassVar[str] = "General"
|
|
35
|
+
color: ClassVar[str] = "#256b82"
|
|
36
|
+
dependency_input: ClassVar[PortSpec] = PortSpec(
|
|
37
|
+
"dependency", "dependency", multiple=True
|
|
38
|
+
)
|
|
39
|
+
dependency_output: ClassVar[PortSpec] = PortSpec(
|
|
40
|
+
"dependency", "dependency", multiple=True
|
|
41
|
+
)
|
|
42
|
+
inputs: ClassVar[tuple[PortSpec, ...]] = ()
|
|
43
|
+
outputs: ClassVar[tuple[PortSpec, ...]] = ()
|
|
44
|
+
properties: ClassVar[tuple[NodeProperty, ...]] = ()
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
node_id: str | None = None,
|
|
49
|
+
values: dict[str, Any] | None = None,
|
|
50
|
+
disabled: bool = False,
|
|
51
|
+
name: str | None = None,
|
|
52
|
+
):
|
|
53
|
+
self.id = node_id or uuid4().hex
|
|
54
|
+
self.disabled = disabled
|
|
55
|
+
self.name = name
|
|
56
|
+
self._cancel_event = None
|
|
57
|
+
values = values or {}
|
|
58
|
+
self.values = {spec.name: values.get(spec.name, spec.default) for spec in self.properties}
|
|
59
|
+
|
|
60
|
+
def __getattr__(self, name: str) -> Any:
|
|
61
|
+
values = self.__dict__.get("values", {})
|
|
62
|
+
if name in values:
|
|
63
|
+
return values[name]
|
|
64
|
+
raise AttributeError(name)
|
|
65
|
+
|
|
66
|
+
def process(self, inputs: dict[str, Any]) -> dict[str, Any]:
|
|
67
|
+
raise NotImplementedError
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def display_name(self) -> str:
|
|
71
|
+
return self.name or self.title
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def cancellation_requested(self) -> bool:
|
|
75
|
+
return bool(self._cancel_event and self._cancel_event.is_set())
|
|
76
|
+
|
|
77
|
+
def to_dict(self) -> dict[str, Any]:
|
|
78
|
+
return {
|
|
79
|
+
"id": self.id,
|
|
80
|
+
"type": self.type_id,
|
|
81
|
+
"values": self.values,
|
|
82
|
+
"disabled": self.disabled,
|
|
83
|
+
"name": self.name,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(frozen=True)
|
|
88
|
+
class Connection:
|
|
89
|
+
source_node: str
|
|
90
|
+
source_port: str
|
|
91
|
+
target_node: str
|
|
92
|
+
target_port: str
|
|
93
|
+
kind: str = "attribute"
|
|
94
|
+
|
|
95
|
+
def to_dict(self) -> dict[str, str]:
|
|
96
|
+
return {
|
|
97
|
+
"source_node": self.source_node,
|
|
98
|
+
"source_port": self.source_port,
|
|
99
|
+
"target_node": self.target_node,
|
|
100
|
+
"target_port": self.target_port,
|
|
101
|
+
"kind": self.kind,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class Backdrop:
|
|
107
|
+
title: str = "Notes"
|
|
108
|
+
note: str = ""
|
|
109
|
+
color: str = "#168c9c"
|
|
110
|
+
position: tuple[float, float] = (0, 0)
|
|
111
|
+
size: tuple[float, float] = (480, 300)
|
|
112
|
+
id: str = field(default_factory=lambda: uuid4().hex)
|
|
113
|
+
|
|
114
|
+
def to_dict(self) -> dict[str, Any]:
|
|
115
|
+
return {
|
|
116
|
+
"id": self.id,
|
|
117
|
+
"title": self.title,
|
|
118
|
+
"note": self.note,
|
|
119
|
+
"color": self.color,
|
|
120
|
+
"position": list(self.position),
|
|
121
|
+
"size": list(self.size),
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass
|
|
126
|
+
class Graph:
|
|
127
|
+
nodes: dict[str, ProcessNode] = field(default_factory=dict)
|
|
128
|
+
positions: dict[str, tuple[float, float]] = field(default_factory=dict)
|
|
129
|
+
connections: list[Connection] = field(default_factory=list)
|
|
130
|
+
backdrops: dict[str, Backdrop] = field(default_factory=dict)
|
|
131
|
+
|
|
132
|
+
def add_node(self, node: ProcessNode, position: tuple[float, float] = (0, 0)) -> None:
|
|
133
|
+
self.nodes[node.id] = node
|
|
134
|
+
self.positions[node.id] = position
|
|
135
|
+
|
|
136
|
+
def remove_node(self, node_id: str) -> None:
|
|
137
|
+
self.nodes.pop(node_id, None)
|
|
138
|
+
self.positions.pop(node_id, None)
|
|
139
|
+
self.connections = [
|
|
140
|
+
edge for edge in self.connections
|
|
141
|
+
if edge.source_node != node_id and edge.target_node != node_id
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
def add_backdrop(self, backdrop: Backdrop) -> None:
|
|
145
|
+
self.backdrops[backdrop.id] = backdrop
|
|
146
|
+
|
|
147
|
+
def remove_backdrop(self, backdrop_id: str) -> None:
|
|
148
|
+
self.backdrops.pop(backdrop_id, None)
|
|
149
|
+
|
|
150
|
+
def connect(self, connection: Connection) -> None:
|
|
151
|
+
if connection.kind not in {"attribute", "dependency"}:
|
|
152
|
+
raise ValueError(f"Unknown connection kind: {connection.kind}")
|
|
153
|
+
if connection.kind == "attribute":
|
|
154
|
+
target = self.nodes[connection.target_node]
|
|
155
|
+
target_spec = next(port for port in target.inputs if port.name == connection.target_port)
|
|
156
|
+
else:
|
|
157
|
+
source = self.nodes[connection.source_node]
|
|
158
|
+
target = self.nodes[connection.target_node]
|
|
159
|
+
if connection.source_port != source.dependency_output.name:
|
|
160
|
+
raise ValueError(
|
|
161
|
+
f"Unknown dependency output: {connection.source_port}"
|
|
162
|
+
)
|
|
163
|
+
if connection.target_port != target.dependency_input.name:
|
|
164
|
+
raise ValueError(
|
|
165
|
+
f"Unknown dependency input: {connection.target_port}"
|
|
166
|
+
)
|
|
167
|
+
target_spec = target.dependency_input
|
|
168
|
+
if not target_spec.multiple:
|
|
169
|
+
self.connections = [
|
|
170
|
+
edge for edge in self.connections
|
|
171
|
+
if not (
|
|
172
|
+
edge.target_node == connection.target_node
|
|
173
|
+
and edge.target_port == connection.target_port
|
|
174
|
+
)
|
|
175
|
+
]
|
|
176
|
+
if connection not in self.connections:
|
|
177
|
+
self.connections.append(connection)
|
|
178
|
+
|
|
179
|
+
def to_dict(self) -> dict[str, Any]:
|
|
180
|
+
return {
|
|
181
|
+
"version": 1,
|
|
182
|
+
"nodes": [
|
|
183
|
+
{**node.to_dict(), "position": list(self.positions.get(node.id, (0, 0)))}
|
|
184
|
+
for node in self.nodes.values()
|
|
185
|
+
],
|
|
186
|
+
"connections": [edge.to_dict() for edge in self.connections],
|
|
187
|
+
"backdrops": [
|
|
188
|
+
backdrop.to_dict() for backdrop in self.backdrops.values()
|
|
189
|
+
],
|
|
190
|
+
}
|