cmvr-visual/backend/app/runtime_policy.py

37 lines
1.6 KiB
Python

"""Hot-read, operator-owned policy. Client job data never selects this file."""
import json
from pathlib import Path
def read_runtime_policy(settings=None):
result = {"batch_window_enabled": True, "workflow_max_shots": 120,
"configuration_error": None}
path = getattr(settings, "runtime_policy_file", None)
if path is None:
return result
try:
with Path(path).open("rb") as stream:
raw = stream.read(65537)
except FileNotFoundError:
return result
except OSError:
return {**result, "configuration_error": "runtime_policy_unavailable"}
try:
if len(raw) > 65536:
raise ValueError("Policy too large")
value = json.loads(raw.decode("utf-8"))
if not isinstance(value, dict) or set(value) - {
"schema_version", "batch_window_enabled", "workflow_max_shots"}:
raise ValueError("Unknown policy fields")
if type(value.get("schema_version")) is not int or value["schema_version"] != 1:
raise ValueError("Unsupported schema")
if type(value.get("batch_window_enabled")) is not bool:
raise ValueError("Expected boolean switch")
max_shots = value.get("workflow_max_shots", 120)
if type(max_shots) is not int or not 1 <= max_shots <= 1000:
raise ValueError("Invalid shot limit")
except (ValueError, TypeError, UnicodeError):
return {**result, "configuration_error": "runtime_policy_invalid"}
return {"batch_window_enabled": value["batch_window_enabled"],
"workflow_max_shots": max_shots, "configuration_error": None}