forked from longtao.wu/droboticflow
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
|
|
|
|
def run(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
source_path = helpers["resolve_source_path"](params, inputs)
|
|
tabular_suffixes = helpers["TABULAR_FILE_SUFFIXES"]
|
|
payload: dict[str, Any]
|
|
if source_path is not None and source_path.suffix.lower() in tabular_suffixes:
|
|
delimiter = tabular_suffixes[source_path.suffix.lower()]
|
|
columns, rows = helpers["load_tabular_rows"](source_path, delimiter)
|
|
payload = {
|
|
"sourcePath": str(source_path),
|
|
"columns": columns,
|
|
"items": rows,
|
|
"rowCount": len(rows),
|
|
}
|
|
else:
|
|
for artifact in inputs:
|
|
preview = helpers["artifact_preview_payload"](artifact) or {}
|
|
if preview.get("kind") == "table":
|
|
payload = {
|
|
"sourcePath": str(artifact.get("path", "")),
|
|
"columns": preview.get("columns", []),
|
|
"items": preview.get("rows", []),
|
|
"rowCount": preview.get("rowCount", len(preview.get("rows", []))),
|
|
"previewTruncated": preview.get("truncated", False),
|
|
}
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"table.json",
|
|
payload,
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
]
|
|
raise RuntimeError("CSV 转 JSON 需要 SOURCE_PATH 或上游 artifact 指向真实 csv/tsv 文件")
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
"table.json",
|
|
payload,
|
|
output=helpers["output_binding"](node_data),
|
|
)
|
|
]
|