File size: 5,982 Bytes
8628c98 | 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | """
ALLI Domain Executors: Structured JSON & CSV Data Processing.
Covers: json.parse, json.stringify, csv.parse, csv.stringify.
"""
import csv
import io
import json
from typing import Any, Dict, List
def _exec_json_parse(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
"""Execution handler for json.parse."""
raw_text = inputs.get("text")
if raw_text is None:
raw_text = params.get("text", "{}")
text_str = json.dumps(raw_text) if isinstance(raw_text, (dict, list)) else str(raw_text)
strict = bool(params.get("strict", True))
fallback_to_none = bool(params.get("fallback_to_none", False))
try:
data = json.loads(text_str, strict=strict)
return {
"then": True,
"data": data,
"result": data,
"val": data,
"out": data,
"is_valid": True,
"error": None,
}
except Exception as err:
if fallback_to_none:
return {
"then": True,
"data": None,
"result": None,
"val": None,
"out": None,
"is_valid": False,
"error": str(err),
}
raise ValueError(f"Invalid JSON string: {err}") from err
def _exec_json_stringify(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
"""Execution handler for json.stringify."""
data = inputs.get("data")
if data is None and "data" in params:
data = params.get("data")
if data is None:
data = {}
indent_param = params.get("indent", 2)
indent = None if str(indent_param) in ("0", "none", "None") else int(indent_param)
sort_keys = bool(params.get("sort_keys", False))
ensure_ascii = bool(params.get("ensure_ascii", False))
try:
serialized = json.dumps(data, indent=indent, sort_keys=sort_keys, ensure_ascii=ensure_ascii, default=str)
return {
"then": True,
"text": serialized,
"result": serialized,
"val": serialized,
"out": serialized,
"byte_size": len(serialized.encode("utf-8")),
"success": True,
}
except Exception as err:
raise ValueError(f"JSON serialization failed: {err}") from err
def _exec_csv_parse(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
"""Execution handler for csv.parse."""
raw_text = inputs.get("text")
if raw_text is None:
raw_text = params.get("text", "")
text_str = str(raw_text)
delimiter = str(params.get("delimiter", ","))
if delimiter == "\\t":
delimiter = "\t"
has_header = bool(params.get("has_header", True))
skip_empty_lines = bool(params.get("skip_empty_lines", True))
trim_whitespace = bool(params.get("trim_whitespace", True))
f = io.StringIO(text_str)
rows: List[Any] = []
headers: List[str] = []
if has_header:
reader = csv.DictReader(f, delimiter=delimiter)
headers = [h.strip() if trim_whitespace and isinstance(h, str) else h for h in (reader.fieldnames or [])]
for r in reader:
if skip_empty_lines and all(v is None or not str(v).strip() for v in r.values()):
continue
if trim_whitespace:
clean_r = {
(k.strip() if isinstance(k, str) else k): (v.strip() if isinstance(v, str) else v)
for k, v in r.items()
}
rows.append(clean_r)
else:
rows.append(dict(r))
else:
reader = csv.reader(f, delimiter=delimiter)
for r in reader:
if skip_empty_lines and (not r or all(not str(v).strip() for v in r)):
continue
rows.append([v.strip() if isinstance(v, str) else v for v in r] if trim_whitespace else list(r))
return {"then": True, "rows": rows, "headers": headers, "row_count": len(rows), "success": True}
def _exec_csv_stringify(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
"""Execution handler for csv.stringify."""
from language_core.nodes.loader import parse_safe_literal_or_json
rows = inputs.get("rows")
if rows is None:
rows = params.get("rows", [])
if isinstance(rows, str):
ok_r, parsed_r = parse_safe_literal_or_json(rows)
rows = parsed_r if ok_r else rows
if not isinstance(rows, (list, tuple)):
rows = [rows]
headers_in = inputs.get("headers") or params.get("headers")
if isinstance(headers_in, str):
ok_h, parsed_h = parse_safe_literal_or_json(headers_in)
if ok_h and isinstance(parsed_h, (list, tuple)):
headers_in = list(parsed_h)
delimiter = str(params.get("delimiter", ","))
if delimiter == "\\t":
delimiter = "\t"
include_header = bool(params.get("include_header", True))
line_terminator = str(params.get("line_terminator", "\n"))
f = io.StringIO()
if rows and isinstance(rows[0], dict):
fieldnames = [str(h) for h in headers_in] if (headers_in and isinstance(headers_in, list)) else list(rows[0].keys())
writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=delimiter, lineterminator=line_terminator, extrasaction="ignore")
if include_header:
writer.writeheader()
for r in rows:
if isinstance(r, dict):
writer.writerow(r)
else:
writer = csv.writer(f, delimiter=delimiter, lineterminator=line_terminator)
if headers_in and include_header and isinstance(headers_in, list):
writer.writerow(headers_in)
for r in rows:
writer.writerow(r if isinstance(r, (list, tuple)) else [r])
serialized = f.getvalue()
return {
"then": True,
"text": serialized,
"row_count": len(rows),
"byte_size": len(serialized.encode("utf-8")),
"success": True,
}
|