-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_plugin.py
executable file
·138 lines (113 loc) · 3.38 KB
/
example_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import re
import sys
import typing
from dataclasses import dataclass
from arcaflow_plugin_sdk import annotations, plugin, schema, validation
@dataclass
class FullName:
"""A full name holds the first and last name of an individual."""
first_name: typing.Annotated[
str,
validation.min(1),
validation.pattern(re.compile("^[a-zA-Z]+$")),
schema.example("Arca"),
schema.name("First name"),
]
last_name: typing.Annotated[
str,
validation.min(1),
validation.pattern(re.compile("^[a-zA-Z]+$")),
schema.example("Lot"),
schema.name("Last name"),
]
def __str__(self) -> str:
"""
:return: the string representation of this name
"""
return self.first_name + " " + self.last_name
@dataclass
class Nickname:
"""A nickname is a simplified form of the name that only holds the
preferred name of an individual."""
nick: typing.Annotated[
str,
validation.min(1),
validation.pattern(re.compile("^[a-zA-Z]+$")),
schema.example("Arcalot"),
schema.name("Nickname"),
]
def __str__(self) -> str:
"""
:return: the string representation of this name
"""
return self.nick
@dataclass
class InputParams:
"""This is the data structure for the input parameters of the step defined
below."""
name: typing.Annotated[
typing.Union[
typing.Annotated[
FullName,
annotations.discriminator_value("fullname"),
schema.name("Full name"),
],
typing.Annotated[
Nickname,
annotations.discriminator_value("nickname"),
schema.name("Nick"),
],
],
schema.name("Name"),
schema.description("Who do we say hello to?"),
annotations.discriminator("_type"),
schema.example(
{
"_type": "fullname",
"first_name": "Arca",
"last_name": "Lot",
}
),
schema.example(
{
"_type": "nickname",
"nick": "Arcalot",
}
),
]
@dataclass
class SuccessOutput:
"""This is the output data structure for the success case."""
message: str
@dataclass
class ErrorOutput:
"""This is the output data structure in the error case."""
error: str
# The following is a decorator (starting with @). We add this in front of
# our function to define the metadata for our step.
@plugin.step(
id="hello-world",
name="Hello world!",
description="Says hello :)",
outputs={"success": SuccessOutput, "error": ErrorOutput},
)
def hello_world(
params: InputParams,
) -> typing.Tuple[str, typing.Union[SuccessOutput, ErrorOutput]]:
"""The function is the implementation for the step. It needs the decorator
above to make it into a step. The type hints for the params are required.
:param params:
:return: the string identifying which output it is, as well the output
structure
"""
return "success", SuccessOutput("Hello, {}!".format(params.name))
if __name__ == "__main__":
sys.exit(
plugin.run(
plugin.build_schema(
# List your step functions here:
hello_world,
)
)
)