little refactoring done
This commit is contained in:
@@ -2,12 +2,19 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from python_app.models.run_config_schema import ComboModel, RunConfigModel
|
||||
from python_app.models.run_config_validation import as_dict, load_ring_payload, load_switch_payload
|
||||
from python_app.models.run_config_validation import load_ring_payload, load_switch_payload
|
||||
|
||||
|
||||
def _as_dict(value: Any, context: str) -> dict[str, Any]:
|
||||
"""Validate payload node is object-like, treating missing values as empty object."""
|
||||
if value is None:
|
||||
return {}
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(f"{context} must be a JSON object")
|
||||
return value
|
||||
|
||||
|
||||
def run_config_from_dict(payload: dict[str, Any]) -> RunConfigModel:
|
||||
@@ -15,19 +22,19 @@ def run_config_from_dict(payload: dict[str, Any]) -> RunConfigModel:
|
||||
# Schema carries only minimal-safe fallbacks; operational defaults live in run_config.json.
|
||||
model = RunConfigModel()
|
||||
|
||||
radar_payload = as_dict(payload.get("radar"), "radar")
|
||||
sweep_payload = as_dict(radar_payload.get("sweep"), "radar.sweep")
|
||||
switches_payload = as_dict(payload.get("switches"), "switches")
|
||||
port1_payload = as_dict(switches_payload.get("port1"), "switches.port1")
|
||||
port2_payload = as_dict(switches_payload.get("port2"), "switches.port2")
|
||||
run_payload = as_dict(payload.get("run"), "run")
|
||||
preprocess_payload = as_dict(payload.get("preprocess"), "preprocess")
|
||||
rings_payload = as_dict(payload.get("rings"), "rings")
|
||||
raw_ring_payload = as_dict(rings_payload.get("raw"), "rings.raw")
|
||||
raw_tap_ring_payload = as_dict(rings_payload.get("raw_tap"), "rings.raw_tap")
|
||||
pre_ring_payload = as_dict(rings_payload.get("preprocessed"), "rings.preprocessed")
|
||||
pre_tap_ring_payload = as_dict(rings_payload.get("preprocessed_tap"), "rings.preprocessed_tap")
|
||||
result_ring_payload = as_dict(rings_payload.get("results"), "rings.results")
|
||||
radar_payload = _as_dict(payload.get("radar"), "radar")
|
||||
sweep_payload = _as_dict(radar_payload.get("sweep"), "radar.sweep")
|
||||
switches_payload = _as_dict(payload.get("switches"), "switches")
|
||||
port1_payload = _as_dict(switches_payload.get("port1"), "switches.port1")
|
||||
port2_payload = _as_dict(switches_payload.get("port2"), "switches.port2")
|
||||
run_payload = _as_dict(payload.get("run"), "run")
|
||||
preprocess_payload = _as_dict(payload.get("preprocess"), "preprocess")
|
||||
rings_payload = _as_dict(payload.get("rings"), "rings")
|
||||
raw_ring_payload = _as_dict(rings_payload.get("raw"), "rings.raw")
|
||||
raw_tap_ring_payload = _as_dict(rings_payload.get("raw_tap"), "rings.raw_tap")
|
||||
pre_ring_payload = _as_dict(rings_payload.get("preprocessed"), "rings.preprocessed")
|
||||
pre_tap_ring_payload = _as_dict(rings_payload.get("preprocessed_tap"), "rings.preprocessed_tap")
|
||||
result_ring_payload = _as_dict(rings_payload.get("results"), "rings.results")
|
||||
|
||||
model.radar.model = str(radar_payload.get("model", model.radar.model))
|
||||
model.radar.serial = str(radar_payload.get("serial", model.radar.serial))
|
||||
@@ -71,7 +78,7 @@ def run_config_from_dict(payload: dict[str, Any]) -> RunConfigModel:
|
||||
model.combos = []
|
||||
if isinstance(combos_payload, list):
|
||||
for combo in combos_payload:
|
||||
combo_payload = as_dict(combo, "run.combos[]")
|
||||
combo_payload = _as_dict(combo, "run.combos[]")
|
||||
model.combos.append(
|
||||
ComboModel(
|
||||
input=int(combo_payload.get("input", 0)),
|
||||
@@ -82,15 +89,6 @@ def run_config_from_dict(payload: dict[str, Any]) -> RunConfigModel:
|
||||
model.ensure_combos()
|
||||
return model
|
||||
|
||||
|
||||
def load_run_config(path: Path) -> RunConfigModel:
|
||||
"""Load JSON config from path and decode into :class:`RunConfigModel`."""
|
||||
payload = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError(f"Config root must be JSON object: {path}")
|
||||
return run_config_from_dict(payload)
|
||||
|
||||
|
||||
def run_config_to_dict(model: RunConfigModel) -> dict[str, Any]:
|
||||
"""Encode :class:`RunConfigModel` to C++ pipeline-compatible JSON structure."""
|
||||
model.ensure_combos()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Facade module for run configuration schema, codec, and validation helpers."""
|
||||
|
||||
from python_app.models.run_config_codec import load_run_config, run_config_from_dict, run_config_to_dict
|
||||
from python_app.models.run_config_codec import run_config_from_dict, run_config_to_dict
|
||||
from python_app.models.run_config_schema import (
|
||||
ComboModel,
|
||||
PreprocessModel,
|
||||
@@ -13,7 +13,6 @@ from python_app.models.run_config_schema import (
|
||||
SwitchModel,
|
||||
)
|
||||
from python_app.models.run_config_validation import (
|
||||
as_dict,
|
||||
load_ring_payload,
|
||||
load_switch_payload,
|
||||
parse_combos_from_text,
|
||||
@@ -29,9 +28,7 @@ __all__ = [
|
||||
"RunConfigModel",
|
||||
"RuntimeModel",
|
||||
"SwitchModel",
|
||||
"as_dict",
|
||||
"load_ring_payload",
|
||||
"load_run_config",
|
||||
"load_switch_payload",
|
||||
"parse_combos_from_text",
|
||||
"run_config_from_dict",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -131,9 +132,10 @@ class RunConfigModel:
|
||||
@classmethod
|
||||
def load_from_path(cls, path: Path) -> RunConfigModel:
|
||||
"""Load JSON file from disk and decode into model."""
|
||||
from python_app.models.run_config_codec import load_run_config
|
||||
|
||||
return load_run_config(path)
|
||||
payload = json.loads(path.read_text(encoding="utf-8"))
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError(f"Config root must be JSON object: {path}")
|
||||
return cls.from_dict(payload)
|
||||
|
||||
def clone(self) -> RunConfigModel:
|
||||
"""Create deep copy through codec round-trip."""
|
||||
|
||||
@@ -6,16 +6,6 @@ from typing import Any
|
||||
|
||||
from python_app.models.run_config_schema import ComboModel, RingEndpointModel, SwitchModel
|
||||
|
||||
|
||||
def as_dict(value: Any, context: str) -> dict[str, Any]:
|
||||
"""Validate that a payload node is a JSON object and return it."""
|
||||
if value is None:
|
||||
return {}
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(f"{context} must be a JSON object")
|
||||
return value
|
||||
|
||||
|
||||
def load_switch_payload(
|
||||
payload: dict[str, Any],
|
||||
target: SwitchModel,
|
||||
|
||||
Reference in New Issue
Block a user