Reset main.py
This commit is contained in:
@@ -1,16 +1,9 @@
|
|||||||
"""This module contains the business logic of the function.
|
import random
|
||||||
|
|
||||||
Use the automation_context module to wrap your function in an Autamate context helper
|
from pydantic import Field
|
||||||
"""
|
from speckle_automate import AutomationContext, AutomateBase, execute_automate_function
|
||||||
|
|
||||||
from pydantic import Field, SecretStr
|
from Utilities.helpers import flatten_base
|
||||||
from speckle_automate import (
|
|
||||||
AutomateBase,
|
|
||||||
AutomationContext,
|
|
||||||
execute_automate_function,
|
|
||||||
)
|
|
||||||
|
|
||||||
from flatten import flatten_base
|
|
||||||
|
|
||||||
|
|
||||||
class FunctionInputs(AutomateBase):
|
class FunctionInputs(AutomateBase):
|
||||||
@@ -21,14 +14,9 @@ class FunctionInputs(AutomateBase):
|
|||||||
https://docs.pydantic.dev/latest/usage/models/
|
https://docs.pydantic.dev/latest/usage/models/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# an example how to use secret values
|
comment_phrase: str = Field(
|
||||||
whisper_message: SecretStr = Field(title="This is a secret message")
|
title="Comment Phrase",
|
||||||
forbidden_speckle_type: str = Field(
|
description="This phrase will be added to a random model element.",
|
||||||
title="Forbidden speckle type",
|
|
||||||
description=(
|
|
||||||
"If a object has the following speckle_type,"
|
|
||||||
" it will be marked with an error."
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -42,54 +30,46 @@ 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
|
|
||||||
|
# the context provides a convenient way, to receive the triggering version
|
||||||
version_root_object = automate_context.receive_version()
|
version_root_object = automate_context.receive_version()
|
||||||
|
|
||||||
objects_with_forbidden_speckle_type = [
|
flat_list_of_objects = flatten_base(version_root_object)
|
||||||
b
|
|
||||||
for b in flatten_base(version_root_object)
|
# filter the list to only include objects that are displayable.
|
||||||
if b.speckle_type == function_inputs.forbidden_speckle_type
|
# this is a simple example, that checks if the object has a displayValue
|
||||||
|
displayable_objects = [
|
||||||
|
speckle_object
|
||||||
|
for speckle_object in flat_list_of_objects
|
||||||
|
if (
|
||||||
|
getattr(speckle_object, "displayValue", None)
|
||||||
|
or getattr(speckle_object, "@displayValue", None)
|
||||||
|
)
|
||||||
|
and getattr(speckle_object, "id", None) is not None
|
||||||
]
|
]
|
||||||
count = len(objects_with_forbidden_speckle_type)
|
|
||||||
|
|
||||||
if count > 0:
|
if len(displayable_objects) == 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(
|
automate_context.mark_run_failed(
|
||||||
"Automation failed: "
|
"Automation failed: No displayable objects found."
|
||||||
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:
|
else:
|
||||||
automate_context.mark_run_success("No forbidden types found.")
|
# select a random object from the list
|
||||||
|
random_object = random.choice(displayable_objects)
|
||||||
|
|
||||||
# if the function generates file results, this is how it can be
|
automate_context.attach_info_to_objects(
|
||||||
# attached to the Speckle project / model
|
category="Selected Object",
|
||||||
# automate_context.store_file_result("./report.pdf")
|
object_ids=[random_object.id],
|
||||||
|
message=function_inputs.comment_phrase,
|
||||||
|
)
|
||||||
|
|
||||||
|
automate_context.mark_run_success("Added a comment to a random object.")
|
||||||
|
|
||||||
def automate_function_without_inputs(automate_context: AutomationContext) -> None:
|
# set the automation context view, to the original model / version view
|
||||||
"""A function example without inputs.
|
automate_context.set_context_view()
|
||||||
|
|
||||||
If your function does not need any input variables,
|
|
||||||
besides what the automation context provides,
|
|
||||||
the inputs argument can be omitted.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# make sure to call the function with the executor
|
# make sure to call the function with the executor
|
||||||
@@ -98,6 +78,3 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# 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, FunctionInputs)
|
||||||
|
|
||||||
# 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