Implement phased modernization infrastructure for transitioning from multi-page legacy routing to SPA portal-shell architecture, plus post-delivery hardening fixes for policy loading, fallback consistency, and governance drift detection. Key changes: - Add route contract enrichment with scope/visibility/compatibility policies - Canonical 302 redirects from legacy direct-entry to /portal-shell/ routes - Asset readiness enforcement and runtime fallback retirement for in-scope routes - Shared feature-flag helpers (env > config > default) replacing duplicated _to_bool - Defensive copy for lru_cached policy payloads preventing mutation corruption - Unified retired-fallback response helper across app and blueprint routes - Frontend/backend route-contract cross-validation in governance gates - Shell CSS token fallback values for routes rendered outside shell scope - Local-safe .env.example defaults with production recommendation comments - Legacy contract fallback warning logging and single-hop redirect optimization Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Shared helpers for boolean parsing and feature-flag resolution."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any, Mapping
|
|
|
|
|
|
_TRUE_VALUES = {"1", "true", "yes", "on"}
|
|
_FALSE_VALUES = {"0", "false", "no", "off"}
|
|
|
|
|
|
def parse_bool(value: Any, default: bool = False) -> bool:
|
|
"""Parse bool-like values with explicit true/false token support."""
|
|
if value is None:
|
|
return default
|
|
if isinstance(value, bool):
|
|
return value
|
|
|
|
text = str(value).strip().lower()
|
|
if not text:
|
|
return default
|
|
if text in _TRUE_VALUES:
|
|
return True
|
|
if text in _FALSE_VALUES:
|
|
return False
|
|
return default
|
|
|
|
|
|
def resolve_bool_flag(
|
|
env_key: str,
|
|
*,
|
|
config: Mapping[str, Any] | None = None,
|
|
config_key: str | None = None,
|
|
default: bool = False,
|
|
environ: Mapping[str, str] | None = None,
|
|
) -> bool:
|
|
"""Resolve bool flag using precedence: environment > config > default."""
|
|
env = environ or os.environ
|
|
env_value = env.get(env_key)
|
|
if env_value is not None:
|
|
return parse_bool(env_value, default=default)
|
|
|
|
cfg = config or {}
|
|
key = config_key or env_key
|
|
if key in cfg:
|
|
return parse_bool(cfg.get(key), default=default)
|
|
return default
|