-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (78 loc) · 3.9 KB
/
Copy pathmain.py
File metadata and controls
99 lines (78 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Coral-compatible CLI for the Python backend.
Presents the same command surface as the C++ ``coral`` binary so the DealiiX platform can drive this
backend by changing only the executable and plugin paths:
main.py -p <plugin> register # write the node registry (node_types.json) into the cwd
main.py -p <plugin> run <graph.json> # execute a workflow graph
For this Python backend ``-p/--plugin`` names the definition modules to load (comma-separated, e.g.
``"math,string"``); an empty value loads every available module. ``-p/--plugin`` must appear before the
subcommand. See the integration plan in issue #12.
"""
import argparse
from definitions import AVAILABLE_MODULES
from registry import save_registry_to_file
from executor import WorkflowExecutor
# Fixed filename the DealiiX platform probes for after `register`.
DEFAULT_REGISTRY_FILENAME = "node_types.json"
# Workflow used when `run` is given no graph argument (keeps the pre-refactor default reachable).
DEFAULT_WORKFLOW_FILE = "network-from-fe.json"
def main():
"""Parse coral-style CLI arguments and dispatch to the ``register`` or ``run`` subcommand.
The top-level parser owns the global ``-p/--plugin`` option (mirroring coral's plugin flag);
``register`` and ``run`` are subcommands. ``register`` writes the node registry to a JSON file
in the current working directory; ``run`` executes a workflow graph via ``WorkflowExecutor``.
"""
parser = argparse.ArgumentParser(
prog="main.py",
description="Coral-compatible CLI: generate the node registry or run a workflow graph.",
)
# Global option mirroring coral's plugin flag. For the Python backend it names the definition
# modules to load (comma-separated); an empty value means "load all available modules".
parser.add_argument(
"-p", "--plugin",
default="",
metavar="MODULES",
help="Comma-separated definition modules to load (e.g. 'math,string'); empty loads all. "
"Must precede the subcommand.",
)
subparsers = parser.add_subparsers(dest="command", required=True)
register_parser = subparsers.add_parser(
"register",
help="Generate the node registry and write it to a JSON file in the current directory.",
)
register_parser.add_argument(
"--output",
default=DEFAULT_REGISTRY_FILENAME,
help=f"Registry output filename, relative to the cwd (default: {DEFAULT_REGISTRY_FILENAME}).",
)
run_parser = subparsers.add_parser("run", help="Execute a workflow graph from a JSON file.")
run_parser.add_argument(
"graph",
nargs="?",
default=DEFAULT_WORKFLOW_FILE,
help=f"Path to the workflow JSON graph (default: {DEFAULT_WORKFLOW_FILE}).",
)
run_parser.add_argument(
"--touch-dir",
default=None,
help="Directory for per-node status files. Accepted for coral compatibility; not yet emitted.",
)
args = parser.parse_args()
modules = _resolve_modules(args.plugin)
if args.command == "register":
save_registry_to_file(args.output, modules=modules)
elif args.command == "run":
executor = WorkflowExecutor(args.graph, modules=modules)
results = executor.execute()
print(f"\nFinal results: {results}")
# ── Private helpers ──
def _resolve_modules(plugin_value):
"""Resolve the ``-p/--plugin`` value into an explicit list of module names.
Splits a comma-separated value into module names, ignoring blank entries. An empty or
whitespace-only value resolves to all available modules — this is passed explicitly (rather
than relying on ``None``) because ``save_registry_to_file``/``WorkflowExecutor`` default a
``None`` module list to ``['phiflow']`` only.
"""
modules = [m.strip() for m in plugin_value.split(",") if m.strip()]
return modules if modules else list(AVAILABLE_MODULES)
if __name__ == "__main__":
main()