Skip to content

Commit 657ec12

Browse files
committed
initial implementation (to_json / yaml , from_source and validation
Signed-off-by: Antonio Mendoza Pérez <antmendoza@gmail.com>
1 parent bfda9f3 commit 657ec12

34 files changed

+4168
-2
lines changed

Pipfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
jsonschema = "==4.4.0"
8+
pyyaml = "==6.0"
9+
10+
[dev-packages]
11+
pytest = "==6.2.5"
12+
pytest-runner = "==5.3.1"
13+
14+
[requires]
15+
python_version = "3.9"

Pipfile.lock

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,102 @@
1-
# sdk-python
2-
Serverless Workflow Python SDK
1+
# Serverless Workflow Specification - Typescript SDK
2+
3+
Provides the Python API/SPI for the [Serverless Workflow Specification](https://github.com/serverlessworkflow/specification)
4+
5+
> This is a WIP implementation
6+
7+
With the SDK you can:
8+
* **WIP** Programmatically build workflow definitions
9+
* Parse workflow JSON and YAML definitions
10+
* Validate workflow definitions
11+
12+
13+
## Install dependencies and run test
14+
15+
- `pipenv install`
16+
- `python setup.py pytest`
17+
18+
## **WIP** Programmatically build workflow definitions
19+
20+
```
21+
workflow = Workflow(id_="greeting",
22+
name="Greeting Workflow",
23+
description="Greet Someone",
24+
version='1.0',
25+
specVersion='0.8',
26+
start="Greet",
27+
states=[],
28+
functions=[]
29+
)
30+
31+
```
32+
33+
## Parse workflow JSON and YAML definitions
34+
35+
### Convert from JSON or YAML source
36+
37+
```
38+
swf_content = """id: greeting
39+
name: Greeting Workflow
40+
version: '1.0'
41+
description: Greet Someone
42+
specVersion: '0.8'
43+
start: Greet
44+
states:
45+
- name: Greet
46+
type: operation
47+
actions:
48+
- functionRef:
49+
refName: greetingFunction
50+
arguments:
51+
name: ${ .person.name }
52+
actionDataFilter:
53+
results: ${ .greeting }
54+
end: true
55+
functions:
56+
- name: greetingFunction
57+
operation: file://myapis/greetingapis.json#greeting
58+
"""
59+
workflow = Workflow.from_source(swf_content)
60+
```
61+
62+
You can see a full example in the [test_workflow.py](./tests/test_workflow.py) file
63+
64+
65+
### Parse workflow to JSON / YAML
66+
67+
```
68+
workflow = Workflow(id_="greeting",
69+
name="Greeting Workflow",
70+
description="Greet Someone",
71+
version='1.0',
72+
specVersion='0.8',
73+
start="Greet",
74+
states=[],
75+
functions=[]
76+
)
77+
78+
print(workflow.to_json())
79+
print(workflow.to_yaml())
80+
```
81+
82+
You can see a full example in the [test_workflow.py](./tests/test_workflow.py) file
83+
84+
85+
## Validate workflow definitions
86+
87+
```
88+
workflow = Workflow(id_="greeting",
89+
name="Greeting Workflow",
90+
description="Greet Someone",
91+
version='1.0',
92+
specVersion='0.8',
93+
start="Greet",
94+
states=[],
95+
functions=[]
96+
)
97+
WorkflowValidator(Workflow(workflow)).validate()
98+
99+
```
100+
The `validate` method will raise an exception if the provided workflow does not complaint specification.
101+
102+
You can see a full example in the [test_workflow_validator](./tests/test_workflow_validator.py) file

serverlessworkflow_sdk/__init__.py

Whitespace-only changes.

serverlessworkflow_sdk/action.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class FunctionRef:
2+
pass
3+
4+
5+
class ActionDataFilter:
6+
pass
7+
8+
9+
class Action:
10+
functionRef: FunctionRef
11+
actionDataFilter: ActionDataFilter
12+
13+
def __init__(self, functionRef: FunctionRef = None,
14+
actionDataFilter: ActionDataFilter = None,
15+
**kwargs):
16+
# duplicated
17+
for local in list(locals()):
18+
if local in ["self", "kwargs"]:
19+
continue
20+
value = locals().get(local)
21+
if not value:
22+
continue
23+
if value == "true":
24+
value = True
25+
# duplicated
26+
27+
28+
self.__setattr__(local.replace("_", ""), value)
29+
30+
# duplicated
31+
for k in kwargs.keys():
32+
value = kwargs[k]
33+
if value == "true":
34+
value = True
35+
36+
self.__setattr__(k.replace("_", ""), value)
37+
# duplicated
38+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from serverlessworkflow_sdk.state import State
2+
3+
4+
class InjectState(State):
5+
6+
data: dict
7+
8+
def __init__(self, data: dict = None,
9+
**kwargs):
10+
# duplicated
11+
for local in list(locals()):
12+
if local in ["self", "kwargs"]:
13+
continue
14+
value = locals().get(local)
15+
if not value:
16+
continue
17+
if value == "true":
18+
value = True
19+
# duplicated
20+
21+
self.__setattr__(local.replace("_", ""), value)
22+
23+
# duplicated
24+
for k in kwargs.keys():
25+
value = kwargs[k]
26+
if value == "true":
27+
value = True
28+
29+
self.__setattr__(k.replace("_", ""), value)
30+
# duplicated
31+
32+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy