Skip to content

Builder

BuilderLink

The sierra.core.builder module is responsible for building invoker scripts in the Sierra Dev. It provides functionality to assemble standalone scripts from invoker definitions, handling imports, dependencies, and entry points.

Creating a builder?
Python
import sierra

# Define an InvokerScript
invoker = sierra.InvokerScript(
    name="example",
    description="An example invoker script."
)

# Define the entry point
@invoker.entry_point
def run(param: sierra.Param[int, sierra.SierraOption(description="An integer parameter.")]) -> None:
    print(f"Running with parameter: {param}")

# Use SierraInvokerBuilder to build the script
builder = sierra.core.builder.SierraInvokerBuilder(client=sierra_client)
script = builder.build(invoker)

# Output the generated script
print(script)

sierra.core.builder Link

ClassesLink

SierraInvokerBuilder Link
Python
SierraInvokerBuilder(
    client: sierra_client.SierraDevelopmentClient,
)

Bases: sierra_core_base.SierraCoreObject

Builder for Sierra invoker scripts.

Generates commands, extracts and cleans imports, handles dependencies, and constructs a main entry-point with type-checked parameters.

PARAMETER DESCRIPTION
client

The client instance containing the environment and logger.

TYPE: SierraClient

ATTRIBUTE DESCRIPTION
client

The client instance provided during initialization.

TYPE: SierraClient

Initialize a SierraCoreObject.

PARAMETER DESCRIPTION
client

The Sierra development client.

TYPE: SierraDevelopmentClient

Notes

Logs each initialization step via the client's logger.

AttributesLink
client instance-attribute Link
Python
client = client
FunctionsLink
generate_command Link
Python
generate_command(
    invoker: sierra_invoker.InvokerScript,
) -> str

Construct the CLI command to invoke the given script in the virtual environment.

PARAMETER DESCRIPTION
invoker

The script to generate a command for.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
str

The constructed command.

extract_imports Link
Python
extract_imports(
    invoker: sierra_invoker.InvokerScript,
) -> list[str]

Extract all import statements from an invoker script.

PARAMETER DESCRIPTION
invoker

The invoker script from which to extract imports.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
list of str

A list of import statements found in the script.

Notes

This function reads the file associated with the provided invoker and parses it to extract all import statements, including standard imports and from-import statements.

Examples:

Python Console Session
1
2
3
>>> invoker = SierraInvokerScript("example.py")
>>> builder.extract_imports(invoker)
['import os', 'from sys import path']
get_filtered_imports Link
Python
get_filtered_imports(
    invoker: sierra_invoker.InvokerScript,
) -> list[str]

Retrieve all import statements excluding those from the sierra namespace.

PARAMETER DESCRIPTION
invoker

The invoker script from which to extract non-sierra imports.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
list of str

A list of import statements excluding sierra namespace imports.

Notes

This function utilizes the extract_imports method to gather all imports and filters out those that are related to the sierra namespace.

get_required_sierra_imports Link
Python
get_required_sierra_imports() -> list[str]

Get only the essential sierra imports needed for the standalone script.

remove_sierra_imports Link
Python
remove_sierra_imports(
    invoker: sierra_invoker.InvokerScript,
) -> str

Strip out all imports from the 'sierra' namespace in a script.

PARAMETER DESCRIPTION
invoker

The invoker script from which to remove sierra imports.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
str

The source code of the invoker with all sierra imports removed.

get_deps_source Link
Python
get_deps_source(
    invoker: sierra_invoker.InvokerScript,
) -> list[str]

Extract the source code for each dependency function and remove any decorators.

PARAMETER DESCRIPTION
invoker

The invoker script containing the dependencies.

TYPE: InvokerScript

RETURNS DESCRIPTION
list of str

A list of source code strings for each dependency function without decorators.

get_entry_point_source Link
Python
get_entry_point_source(
    invoker: sierra_invoker.InvokerScript,
) -> str

Extract and rebuild the entry point function with simple type annotations.

PARAMETER DESCRIPTION
invoker

The invoker script containing the entry point.

TYPE: InvokerScript

RETURNS DESCRIPTION
str

The source code of the entry point function with simple type annotations.

get_parameter_type_string Link
Python
get_parameter_type_string(
    param: sierra_abc_sierra.SierraInvokerParam,
) -> str

Get the proper type string for a parameter including Optional wrapper.

PARAMETER DESCRIPTION
param

The parameter to get the type string for.

TYPE: SierraInvokerParam

RETURNS DESCRIPTION
str

The type string for the parameter.

get_arg_type_checking Link
Python
get_arg_type_checking(
    param: sierra_abc_sierra.SierraInvokerParam,
) -> str

Generate runtime presence and type checks for a single parameter.

PARAMETER DESCRIPTION
param

The parameter to generate type checks for.

TYPE: SierraInvokerParam

RETURNS DESCRIPTION
str

The type checking code as a single string.

get_sys_args_parsing Link
Python
get_sys_args_parsing(
    invoker: sierra_invoker.InvokerScript,
) -> list[str]

Generate sys.argv parsing with proper type conversion and validation.

PARAMETER DESCRIPTION
invoker

The invoker script containing parameters for parsing.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
list of str

A list of source code lines for argument parsing.

create_type_safe_main Link
Python
create_type_safe_main(
    invoker: sierra_invoker.InvokerScript,
) -> str

Build a type-safe main guard with comprehensive error handling.

PARAMETER DESCRIPTION
invoker

The invoker script containing parameters for parsing.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
str

The type-safe main guard.

build Link
Python
build(invoker: sierra_invoker.InvokerScript) -> str

Assemble the full standalone script.

PARAMETER DESCRIPTION
invoker

The invoker script to generate a standalone script for.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
str

The full standalone script.

generate_file_header Link
Python
generate_file_header(
    invoker: sierra_invoker.InvokerScript,
) -> str

Generate file header with metadata.

PARAMETER DESCRIPTION
invoker

The invoker script to generate a standalone script for.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
str

The file header with metadata.

validate_script_syntax Link
Python
validate_script_syntax(script: str) -> bool

Check the syntax of a generated script.

PARAMETER DESCRIPTION
script

The script content to validate.

TYPE: str

RETURNS DESCRIPTION
bool

True if the script's syntax is valid, False if a syntax error occurs.

get_metadata Link
Python
get_metadata(
    invoker: sierra_invoker.InvokerScript,
) -> dict[str, typing.Any]

Retrieve metadata about the invoker.

PARAMETER DESCRIPTION
invoker

The invoker script instance.

TYPE: sierra_invoker.InvokerScript

RETURNS DESCRIPTION
dict of str to Any

Metadata dictionary containing invoker information.