Ir para o conteúdo

tempestweb.transpile

O compilador do Modo C: lê a camada de app em Python tipado e emite JavaScript nativo. TranspileError traz file:line quando o código sai do subconjunto suportado — é o erro que você vai encontrar antes de qualquer outro ao portar um app para bundle estático.

Guia com exemplos: Modo C — transpile.

tempestweb.transpile

Mode C transpiler — typed Python app layer → native JavaScript.

Transcribes a tempestweb app module (state dataclasses + view + handlers) into an ES module that runs on the native JS runtime (client/transpile/runtime.js), with zero Python at execution time. See docs/modo-c-transpile.md.

TranspileError

Bases: Exception

A Python construct fell outside the transpilable subset.

Carries the source location so the message reads like a compiler diagnostic (file:line: <what> is not supported), in the spirit of mypy --strict.

Source code in tempestweb/transpile/errors.py
class TranspileError(Exception):
    """A Python construct fell outside the transpilable subset.

    Carries the source location so the message reads like a compiler diagnostic
    (``file:line: <what> is not supported``), in the spirit of ``mypy --strict``.
    """

    def __init__(
        self, message: str, node: ast.AST | None, filename: str = "<source>"
    ) -> None:
        """Initialize the error.

        Args:
            message: What is unsupported and, ideally, why.
            node: The offending AST node (its ``lineno`` locates the
                diagnostic), or ``None`` when the diagnostic is about the module
                as a whole and no node carries the blame — the line then reads
                ``0``, which is still better than losing the message.
            filename: The source file name, for the ``file:line`` prefix.
        """
        line: int = getattr(node, "lineno", 0)
        self.filename: str = filename
        self.lineno: int = line
        super().__init__(f"{filename}:{line}: {message}")

transpile_source

transpile_source(source: str, filename: str = '<source>', *, banner: str | None = None) -> str

Transpile Python module source into native-JS module source.

Parameters:

Name Type Description Default
source str

The Python source to transpile.

required
filename str

Source name for diagnostics and the default banner.

'<source>'
banner str | None

Optional leading comment line (see :func:generate).

None

Returns:

Type Description
str

The generated JavaScript module source.

Raises:

Type Description
TranspileError

If the module uses a construct outside the subset.

Source code in tempestweb/transpile/__init__.py
def transpile_source(
    source: str, filename: str = "<source>", *, banner: str | None = None
) -> str:
    """Transpile Python module source into native-JS module source.

    Args:
        source: The Python source to transpile.
        filename: Source name for diagnostics and the default banner.
        banner: Optional leading comment line (see :func:`generate`).

    Returns:
        The generated JavaScript module source.

    Raises:
        TranspileError: If the module uses a construct outside the subset.
    """
    return generate(source, filename, banner=banner)

transpile_file

transpile_file(path: str | Path, *, banner: str | None = None) -> str

Transpile a Python source file into native-JS module source.

Parameters:

Name Type Description Default
path str | Path

Path to the .py module to transpile.

required
banner str | None

Optional leading comment line (see :func:generate).

None

Returns:

Type Description
str

The generated JavaScript module source.

Raises:

Type Description
TranspileError

If the module uses a construct outside the subset.

Source code in tempestweb/transpile/__init__.py
def transpile_file(path: str | Path, *, banner: str | None = None) -> str:
    """Transpile a Python source file into native-JS module source.

    Args:
        path: Path to the `.py` module to transpile.
        banner: Optional leading comment line (see :func:`generate`).

    Returns:
        The generated JavaScript module source.

    Raises:
        TranspileError: If the module uses a construct outside the subset.
    """
    file_path = Path(path)
    source = file_path.read_text(encoding="utf-8")
    return generate(source, file_path.name, banner=banner)