Basic Function Change
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
"""This module contains the business logic of the function.
|
"""This module contains the business logic of the function.
|
||||||
|
|
||||||
Use the automation_context module to wrap your function in an Autamate context helper
|
Use the automation_context module to wrap your function in an Automate context helper
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
@@ -10,8 +10,6 @@ from speckle_automate import (
|
|||||||
execute_automate_function,
|
execute_automate_function,
|
||||||
)
|
)
|
||||||
|
|
||||||
from flatten import flatten_base
|
|
||||||
|
|
||||||
|
|
||||||
class FunctionInputs(AutomateBase):
|
class FunctionInputs(AutomateBase):
|
||||||
"""These are function author defined values.
|
"""These are function author defined values.
|
||||||
@@ -21,15 +19,6 @@ class FunctionInputs(AutomateBase):
|
|||||||
https://docs.pydantic.dev/latest/usage/models/
|
https://docs.pydantic.dev/latest/usage/models/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
forbidden_speckle_type: str = Field(
|
|
||||||
title="Forbidden speckle type",
|
|
||||||
description=(
|
|
||||||
"If a object has the following speckle_type,"
|
|
||||||
" it will be marked with an error."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def automate_function(
|
def automate_function(
|
||||||
automate_context: AutomationContext,
|
automate_context: AutomationContext,
|
||||||
function_inputs: FunctionInputs,
|
function_inputs: FunctionInputs,
|
||||||
@@ -40,45 +29,10 @@ def automate_function(
|
|||||||
automate_context: A context helper object, that carries relevant information
|
automate_context: A context helper object, that carries relevant information
|
||||||
about the runtime context of this function.
|
about the runtime context of this function.
|
||||||
It gives access to the Speckle project data, that triggered this run.
|
It gives access to the Speckle project data, that triggered this run.
|
||||||
It also has conveniece methods attach result data to the Speckle model.
|
It also has convenience methods attach result data to the Speckle model.
|
||||||
function_inputs: An instance object matching the defined schema.
|
function_inputs: An instance object matching the defined schema.
|
||||||
"""
|
"""
|
||||||
# the context provides a conveniet way, to receive the triggering version
|
automate_context.mark_run_success("Successfully Sent to RealtimeLCA™ by Gaiup.")
|
||||||
version_root_object = automate_context.receive_version()
|
|
||||||
|
|
||||||
objects_with_forbidden_speckle_type = [
|
|
||||||
b
|
|
||||||
for b in flatten_base(version_root_object)
|
|
||||||
if b.speckle_type == function_inputs.forbidden_speckle_type
|
|
||||||
]
|
|
||||||
count = len(objects_with_forbidden_speckle_type)
|
|
||||||
|
|
||||||
if count > 0:
|
|
||||||
# this is how a run is marked with a failure cause
|
|
||||||
automate_context.attach_error_to_objects(
|
|
||||||
category="Forbidden speckle_type"
|
|
||||||
" ({function_inputs.forbidden_speckle_type})",
|
|
||||||
object_ids=[o.id for o in objects_with_forbidden_speckle_type if o.id],
|
|
||||||
message="This project should not contain the type: "
|
|
||||||
f"{function_inputs.forbidden_speckle_type}",
|
|
||||||
)
|
|
||||||
automate_context.mark_run_failed(
|
|
||||||
"Automation failed: "
|
|
||||||
f"Found {count} object that have one of the forbidden speckle types: "
|
|
||||||
f"{function_inputs.forbidden_speckle_type}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# set the automation context view, to the original model / version view
|
|
||||||
# to show the offending objects
|
|
||||||
automate_context.set_context_view()
|
|
||||||
|
|
||||||
else:
|
|
||||||
automate_context.mark_run_success("No forbidden types found.")
|
|
||||||
|
|
||||||
# if the function generates file results, this is how it can be
|
|
||||||
# attached to the Speckle project / model
|
|
||||||
# automate_context.store_file_result("./report.pdf")
|
|
||||||
|
|
||||||
|
|
||||||
def automate_function_without_inputs(automate_context: AutomationContext) -> None:
|
def automate_function_without_inputs(automate_context: AutomationContext) -> None:
|
||||||
"""A function example without inputs.
|
"""A function example without inputs.
|
||||||
@@ -87,7 +41,7 @@ def automate_function_without_inputs(automate_context: AutomationContext) -> Non
|
|||||||
besides what the automation context provides,
|
besides what the automation context provides,
|
||||||
the inputs argument can be omitted.
|
the inputs argument can be omitted.
|
||||||
"""
|
"""
|
||||||
pass
|
automate_context.mark_run_success("Successfully Sent to Gaiup RealtimeLCA™.")
|
||||||
|
|
||||||
|
|
||||||
# make sure to call the function with the executor
|
# make sure to call the function with the executor
|
||||||
@@ -95,7 +49,4 @@ if __name__ == "__main__":
|
|||||||
# NOTE: always pass in the automate function by its reference, do not invoke it!
|
# NOTE: always pass in the automate function by its reference, do not invoke it!
|
||||||
|
|
||||||
# pass in the function reference with the inputs schema to the executor
|
# pass in the function reference with the inputs schema to the executor
|
||||||
execute_automate_function(automate_function, FunctionInputs)
|
execute_automate_function(automate_function_without_inputs)
|
||||||
|
|
||||||
# if the function has no arguments, the executor can handle it like so
|
|
||||||
# execute_automate_function(automate_function_without_inputs)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user