Skip to content

Scaffolder#

flet_pkg.core.scaffolder #

Jinja2-based project scaffolder.

Walks a template directory tree, resolves {{variable}} placeholders in file and directory names, and renders .jinja files through Jinja2 to produce a complete project skeleton.

Scaffolder #

Scaffolder(template_name: str, context: dict, output_dir: Path | None = None)

Render a project from a template directory.

Attributes:

Name Type Description
template_path

Resolved path to the template directory.

context

Variable mapping used for name resolution and rendering.

output_dir

Root directory where the project will be created.

env

Jinja2 environment configured with strict undefined.

Initialise the scaffolder.

Parameters:

Name Type Description Default
template_name str

Name of the template (service or ui_control).

required
context dict

Dictionary of variables for Jinja2 rendering.

required
output_dir Path | None

Target directory. Defaults to the current working directory.

None

Raises:

Type Description
FileNotFoundError

If the template directory does not exist.

Source code in src/flet_pkg/core/scaffolder.py
def __init__(self, template_name: str, context: dict, output_dir: Path | None = None):
    """Initialise the scaffolder.

    Args:
        template_name: Name of the template (``service`` or ``ui_control``).
        context: Dictionary of variables for Jinja2 rendering.
        output_dir: Target directory. Defaults to the current working directory.

    Raises:
        FileNotFoundError: If the template directory does not exist.
    """
    self.template_path = TEMPLATE_DIR / template_name
    if not self.template_path.is_dir():
        raise FileNotFoundError(f"Template '{template_name}' not found at {self.template_path}")

    self.context = context
    self.output_dir = output_dir or Path.cwd()

    self.env = Environment(
        loader=FileSystemLoader(str(self.template_path)),
        undefined=StrictUndefined,
        keep_trailing_newline=True,
    )

generate #

generate() -> Path

Generate the project directory from the template.

Returns:

Type Description
Path

Path to the created project directory.

Raises:

Type Description
FileExistsError

If the project directory already exists.

Source code in src/flet_pkg/core/scaffolder.py
def generate(self) -> Path:
    """Generate the project directory from the template.

    Returns:
        Path to the created project directory.

    Raises:
        FileExistsError: If the project directory already exists.
    """
    project_name = self.context.get("project_name", "output")
    project_dir = self.output_dir / project_name
    if project_dir.exists():
        raise FileExistsError(f"Directory already exists: {project_dir}")

    with console.status("[info]Generating project files...[/info]", spinner="dots"):
        self._walk_and_render(self.output_dir)

    return project_dir