Private
Public Access
TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 10: refactor(gui_2): migrate L264 _resolve_font_path to Result[T] (Phase 10 site 2)
Extracted _resolve_font_path_result(font_path, assets_dir) -> Result[str] helper above the legacy wrapper. ANTI-SLIMING: full Result[T] propagation (NO narrowing+logging). The helper returns Result(data=resolved_path) on success or Result(data=fallback, errors=[ErrorInfo]) on exception at Path.is_relative_to (logging NOT a drain per the user's principle 2026-06-17). The legacy _resolve_font_path() wrapper preserves its signature and delegates to the helper. The call site in App._load_fonts invokes the result helper directly and drains errors to self._startup_timeline_errors. Tests: 2 new tests verify both paths (relative-under-assets success and is_relative_to raising ValueError on cross-drive paths). Audit: L264 reclassified from INTERNAL_SILENT_SWALLOW (11 sites remaining, was 12). New helper L243 is INTERNAL_COMPLIANT.
This commit is contained in:
+43
-21
@@ -240,6 +240,40 @@ def _detect_refresh_rate_win32() -> float:
|
||||
result = _detect_refresh_rate_win32_result()
|
||||
return result.data
|
||||
|
||||
def _resolve_font_path_result(font_path: str, assets_dir: Path) -> Result[str]:
|
||||
"""Drain-aware variant of _resolve_font_path (L264 INTERNAL_SILENT_SWALLOW).
|
||||
|
||||
Extracts the multi-step normalization logic into a Result-returning helper.
|
||||
The narrow except (ValueError, AttributeError) at the is_relative_to check
|
||||
is converted to a full Result conversion (logging NOT a drain per the user's
|
||||
principle 2026-06-17). On success, returns Result(data=resolved_path). On
|
||||
exception at is_relative_to, returns Result(data="fonts/Inter-Regular.ttf",
|
||||
errors=[ErrorInfo]) so the legacy wrapper can fall back to the bundled
|
||||
Inter font (preserving the original behavior).
|
||||
|
||||
[C: src/gui_2.py:_resolve_font_path (L264 legacy wrapper)]
|
||||
"""
|
||||
p = Path(font_path)
|
||||
if not p.is_absolute():
|
||||
return Result(data=font_path)
|
||||
try:
|
||||
if p.is_relative_to(assets_dir):
|
||||
return Result(data=str(p.relative_to(assets_dir)).replace("\\", "/"))
|
||||
except Exception as e:
|
||||
return Result(data="fonts/Inter-Regular.ttf", errors=[ErrorInfo(
|
||||
kind=ErrorKind.INTERNAL,
|
||||
message=f"Path.is_relative_to failed during font path resolution: {e}",
|
||||
source="gui_2._resolve_font_path_result",
|
||||
original=e,
|
||||
)])
|
||||
if p.exists():
|
||||
return Result(data=font_path)
|
||||
name = p.name
|
||||
for rel in (f"fonts/{name}", name):
|
||||
if (assets_dir / rel).exists():
|
||||
return Result(data=rel)
|
||||
return Result(data="fonts/Inter-Regular.ttf")
|
||||
|
||||
def _resolve_font_path(font_path: str, assets_dir: Path) -> str:
|
||||
"""Normalize a configured font path to something hello_imgui can load.
|
||||
|
||||
@@ -249,28 +283,16 @@ def _resolve_font_path(font_path: str, assets_dir: Path) -> str:
|
||||
moved to C:/projects/sloppy). In that case the absolute file does not exist
|
||||
and the load fails. This recovers by:
|
||||
|
||||
1. If the absolute path lives under the current assets folder -> relativize.
|
||||
2. If the absolute path exists on disk as-is -> keep it.
|
||||
3. Otherwise recover the basename under assets/fonts or assets.
|
||||
4. Final fallback: the bundled default Inter font.
|
||||
1. If the absolute path lives under the current assets folder -> relativize.
|
||||
2. If the absolute path exists on disk as-is -> keep it.
|
||||
3. Otherwise recover the basename under assets/fonts or assets.
|
||||
4. Final fallback: the bundled default Inter font.
|
||||
|
||||
Legacy wrapper: delegates to _resolve_font_path_result. Preserves the
|
||||
original signature (returns str). The call site in App._load_fonts invokes
|
||||
the result helper directly and drains errors to self._startup_timeline_errors.
|
||||
"""
|
||||
p = Path(font_path)
|
||||
if not p.is_absolute():
|
||||
return font_path # already relative; hello_imgui searches the assets folder
|
||||
#Note(Ed): Exception(Thirdparty)
|
||||
try:
|
||||
if p.is_relative_to(assets_dir):
|
||||
return str(p.relative_to(assets_dir)).replace("\\", "/")
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
if p.exists():
|
||||
return font_path # absolute path to a real file elsewhere — load directly
|
||||
# Stale absolute path: recover by basename relative to the assets folder.
|
||||
name = p.name
|
||||
for rel in (f"fonts/{name}", name):
|
||||
if (assets_dir / rel).exists():
|
||||
return rel
|
||||
return "fonts/Inter-Regular.ttf"
|
||||
return _resolve_font_path_result(font_path, assets_dir).data
|
||||
|
||||
def _apply_runtime_caps_override(app: "App", caps: "VendorCapabilities") -> "VendorCapabilities":
|
||||
from dataclasses import replace
|
||||
|
||||
Reference in New Issue
Block a user