"""Vendored third-party dependencies used by okstra runtime scripts.

This package isolates third-party Python libraries that okstra needs at
runtime so that end users do not need to `pip install` anything separate
from the npm-distributed runtime. The build system (`tools/build.mjs`)
copies this package into `runtime/python/okstra_vendor/` and the
installer (`src/install.mjs`) drops it under `~/.okstra/lib/python/`.

Currently vendored:
  - jinja2 3.1.6 (BSD-3-Clause) — final-report template rendering.
  - markupsafe 3.0.3 (BSD-3-Clause) — jinja2 dependency. C extension
    (`_speedups.c` / `_speedups.pyi`) is stripped; the pure-python
    `_native` fallback is used at import time.

Source provenance and licenses are preserved verbatim under each
sub-package. Do not edit vendored files in place — re-vendor from PyPI
sdists when upgrading.

Import alias rationale: jinja2 uses absolute imports (`from markupsafe
import ...`, and compiled templates `import jinja2`) internally. Without
re-routing, those would either fail (no system install) or resolve to
unrelated user-installed copies. We register the vendored copies in
`sys.modules` under their canonical top-level names so jinja2's internal
imports find them deterministically.
"""

import sys as _sys


def _alias_loaded_submodules(package: object, canonical_name: str) -> None:
    """Expose one vendored package tree under its canonical import name."""
    vendored_name = package.__name__
    _sys.modules[canonical_name] = package
    vendored_prefix = f"{vendored_name}."
    for module_name, module in tuple(_sys.modules.items()):
        if module is None or not module_name.startswith(vendored_prefix):
            continue
        suffix = module_name[len(vendored_name):]
        _sys.modules[f"{canonical_name}{suffix}"] = module


# Register markupsafe alias BEFORE importing jinja2 — jinja2's __init__
# evaluates `from .environment import ...`, which does `from markupsafe
# import Markup` at module-load time. Reversing the order would crash
# the import chain before the alias was registered.
from . import markupsafe as _markupsafe
_alias_loaded_submodules(_markupsafe, "markupsafe")

from . import jinja2 as _jinja2  # noqa: E402
_alias_loaded_submodules(_jinja2, "jinja2")
