rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98 Download language_core/nodes/nodes_structured_data.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 5.98 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/nodes/nodes_structured_data.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/language_core/nodes/nodes_structured_data.py
-
curl -L -o nodes_structured_data.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/nodes/nodes_structured_data.py
5.98 kB
| """ | |
| 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, | |
| } | |