Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn Compiler

From Leeroopedia
Knowledge Sources
Domains Template_Engine, Compilation
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete tool for compiling Jinja2 templates with SAYN-specific globals (parameters, run arguments, src/out/config macros) and deferred compilation support provided by the SAYN framework.

Description

The Compiler class wraps Jinja2 Environment instances with SAYN-specific configuration:

  • Two environments: env (dev parameters) and prod_env (prod parameters)
  • Globals: full_load, start_dt, end_dt (formatted as 'YYYY-MM-DD' strings), project parameters
  • StrictUndefined to catch undefined variables
  • FileSystemLoader rooted at the project directory
  • cache_size=0 to disable template caching (ensures fresh reads)

The Prepared class enables deferred compilation: a template is parsed once but can be rendered multiple times with different globals (used for from_prod recompilation).

The TaskCompiler subclass adds task-scoped globals (task.name, task.group) for use in templates.

Usage

Used internally by App.set_settings() to create the global compiler, and by App.get_task_compiler() to create per-task compilers. The update_globals method is called by tasks to set src, out, and config macros.

Code Reference

Source Location

  • Repository: SAYN
  • File: sayn/utils/compiler.py
  • Lines: L11-19 (TaskJinjaEnv), L31-107 (Compiler), L113-124 (Prepared), L127-135 (TaskCompiler)

Signature

class Compiler(BaseCompiler):
    def __init__(self, run_arguments: RunArguments, parameters: dict, prod_parameters: dict)

    def compile(self, obj: Union[Template, Path, str], **kwargs) -> str
    def compile_prod(self, obj: Union[Path, str], **kwargs) -> str
    def prepare(self, obj) -> Prepared
    def update_globals(self, **params) -> None
    def add_global(self, name: str, obj) -> None
    def get_task_compiler(self, group: str, name: str) -> TaskCompiler

class Prepared:
    def compile(self, **kwargs) -> str
    def compile_prod(self, **kwargs) -> str

class TaskCompiler(Compiler):
    # Adds task.name and task.group to env globals
    pass

Import

from sayn.utils.compiler import Compiler, Prepared, TaskCompiler

I/O Contract

Inputs

Name Type Required Description
run_arguments RunArguments Yes Provides full_load, start_dt, end_dt
parameters dict Yes Project parameters (dev)
prod_parameters dict Yes Project parameters (prod)

Outputs

Name Type Description
compile returns str Rendered template string
prepare returns Prepared Deferred compilation object

Usage Examples

Creating the Compiler

from sayn.utils.compiler import Compiler

# Created by App.set_settings()
compiler = Compiler(
    run_arguments=run_arguments,
    parameters=project_parameters,
    prod_project_parameters=prod_project_parameters,
)

# Globals available in templates:
# - start_dt: '2026-10-16' (yesterday as quoted string)
# - end_dt: '2026-10-16'
# - full_load: False
# - All project parameters

Compiling SQL Files

# Compile a SQL file
sql = compiler.compile(Path("sql/user_summary.sql"))

# Compile a string template
sql = compiler.compile("SELECT * FROM {{ src_table }}", src_table="raw.users")

# Prepare for deferred compilation (recompilable with different globals)
prepared = compiler.prepare(Path("sql/user_summary.sql"))
sql = prepared.compile()  # First compilation

# Update globals and recompile
compiler.update_globals(src=real_src_func, out=real_out_func)
sql = prepared.compile()  # Second compilation with new globals

Task-Scoped Compiler

# Each task gets its own compiler with task.name and task.group
task_compiler = compiler.get_task_compiler(group="models", name="user_summary")
# In templates: task.name = "user_summary", task.group = "models"

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment