Download node_validation_lab/fixtures/inventory_loader.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/node_validation_lab/fixtures/inventory_loader.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/node_validation_lab/fixtures/inventory_loader.py
-
curl -L -o inventory_loader.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/node_validation_lab/fixtures/inventory_loader.py
3.82 kB
| """Deterministic inventory loader for ALLI Node Validation Lab. | |
| Safely reads and parses `node_inventory_audit.json` without executing arbitrary package code. | |
| Enforces strict schema validation through Pydantic contracts. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import os | |
| from pathlib import Path | |
| from typing import Any, Dict, List, Optional, Tuple | |
| from ..schemas.models import ( | |
| InventorySummaryCounts, | |
| NodeValidationContract, | |
| ) | |
| def get_inventory_path(override_path: Optional[str] = None) -> Path: | |
| """Deterministically locate the canonical node_inventory_audit.json file.""" | |
| if override_path: | |
| p = Path(override_path) | |
| if p.is_file(): | |
| return p.resolve() | |
| raise FileNotFoundError(f"Specified inventory file not found: {override_path}") | |
| # Check environment variable | |
| env_path = os.environ.get("ALLI_NODE_INVENTORY_PATH") | |
| if env_path: | |
| p = Path(env_path) | |
| if p.is_file(): | |
| return p.resolve() | |
| # Potential relative paths | |
| candidates = [ | |
| # Relative to this loader file (node_validation_lab/fixtures/ -> alli_backend/) | |
| Path(__file__).resolve().parent.parent.parent / "node_inventory_audit.json", | |
| # Current working directory | |
| Path.cwd() / "node_inventory_audit.json", | |
| Path.cwd() / "alli_backend" / "node_inventory_audit.json", | |
| Path.cwd() / "alli_frontend" / "node_inventory_audit.json", | |
| # Monorepo root checks | |
| Path(__file__).resolve().parent.parent.parent.parent / "alli_backend" / "node_inventory_audit.json", | |
| Path(__file__).resolve().parent.parent.parent.parent / "alli_frontend" / "node_inventory_audit.json", | |
| ] | |
| for cand in candidates: | |
| if cand.is_file(): | |
| return cand.resolve() | |
| raise FileNotFoundError( | |
| "Could not find 'node_inventory_audit.json' in standard workspace locations." | |
| ) | |
| def load_raw_inventory(path: Optional[str] = None) -> Dict[str, Any]: | |
| """Deterministically load raw JSON data without executing arbitrary code.""" | |
| resolved_path = get_inventory_path(path) | |
| with open(resolved_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| if not isinstance(data, dict): | |
| raise ValueError("Invalid inventory format: Root must be a JSON object.") | |
| if "audit_metadata" not in data or "inventory" not in data: | |
| raise ValueError( | |
| "Invalid inventory schema: Missing 'audit_metadata' or 'inventory' key." | |
| ) | |
| if not isinstance(data["inventory"], list): | |
| raise ValueError("Invalid inventory schema: 'inventory' must be a list.") | |
| return data | |
| def load_inventory_contracts(path: Optional[str] = None) -> List[NodeValidationContract]: | |
| """Parse and validate inventory items into Pydantic NodeValidationContract models.""" | |
| data = load_raw_inventory(path) | |
| return [NodeValidationContract.model_validate(item) for item in data["inventory"]] | |
| def load_inventory_summary(path: Optional[str] = None) -> InventorySummaryCounts: | |
| """Extract and validate summary counts from audit metadata.""" | |
| data = load_raw_inventory(path) | |
| meta = data.get("audit_metadata", {}) | |
| summary_counts = meta.get("summary_counts", {}) | |
| return InventorySummaryCounts.model_validate(summary_counts) | |
| def load_complete_inventory_bundle( | |
| path: Optional[str] = None, | |
| ) -> Tuple[Dict[str, Any], InventorySummaryCounts, List[NodeValidationContract]]: | |
| """Return raw audit inventory dict, validated summary counts, and typed contracts.""" | |
| data = load_raw_inventory(path) | |
| meta = data.get("audit_metadata", {}) | |
| summary = InventorySummaryCounts.model_validate(meta.get("summary_counts", {})) | |
| contracts = [NodeValidationContract.model_validate(item) for item in data["inventory"]] | |
| return data, summary, contracts | |