1
|
|
|
from __future__ import ( |
|
|
|
|
2
|
|
|
absolute_import, |
3
|
|
|
division, |
4
|
|
|
print_function |
5
|
|
|
) |
6
|
|
|
|
7
|
|
|
import logging |
8
|
|
|
|
9
|
|
|
from .step import Step |
10
|
|
|
from .activity import Activity |
11
|
|
|
from .schema import SchemaValidator |
12
|
|
|
|
13
|
|
|
_LOGGER = logging.getLogger(__name__) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Plan(object): |
17
|
|
|
"""Workflow plan. |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
_DATA_SCHEMA = { |
21
|
|
|
'$schema': 'http://json-schema.org/draft-04/schema#', |
22
|
|
|
'type': 'object', |
23
|
|
|
'properties': { |
24
|
|
|
'name': { |
25
|
|
|
'type': 'string', |
26
|
|
|
}, |
27
|
|
|
'version': { |
28
|
|
|
'type': 'string', |
29
|
|
|
}, |
30
|
|
|
'input_spec': { |
31
|
|
|
'oneOf': [ |
32
|
|
|
{'type': 'null'}, |
33
|
|
|
{'$ref': '#/definitions/input_spec'}, |
34
|
|
|
], |
35
|
|
|
}, |
36
|
|
|
'activities': { |
37
|
|
|
'type': 'array', |
38
|
|
|
'minItem': 1, |
39
|
|
|
'items': { |
40
|
|
|
'type': 'object' |
41
|
|
|
} |
42
|
|
|
}, |
43
|
|
|
'steps': { |
44
|
|
|
'type': 'array', |
45
|
|
|
'minItem': 1, |
46
|
|
|
'items': { |
47
|
|
|
'type': 'object' |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
}, |
51
|
|
|
'additionalProperties': False, |
52
|
|
|
'definitions': { |
53
|
|
|
'input_spec': { |
54
|
|
|
'$ref': 'http://json-schema.org/draft-04/schema#', |
55
|
|
|
}, |
56
|
|
|
}, |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
__slots__ = ('name', |
60
|
|
|
'version', |
61
|
|
|
'steps', |
62
|
|
|
'activities', |
63
|
|
|
'_input_validator', |
64
|
|
|
'__weakref__') |
65
|
|
|
|
66
|
|
|
def __init__(self, name, version, |
67
|
|
|
input_spec=None, steps=(), activities=()): |
68
|
|
|
|
69
|
|
|
self.name = name |
70
|
|
|
self.version = version |
71
|
|
|
self.steps = list(steps) |
72
|
|
|
self.activities = dict(activities) |
73
|
|
|
self._input_validator = SchemaValidator(input_spec=input_spec) |
74
|
|
|
|
75
|
|
|
def check_input(self, plan_input): |
|
|
|
|
76
|
|
|
return self._input_validator.validate(plan_input) |
77
|
|
|
|
78
|
|
|
@classmethod |
79
|
|
|
def from_data(cls, plan_data): |
80
|
|
|
"""Define a plan from a dictionary of attributes. |
81
|
|
|
""" |
82
|
|
|
validator = SchemaValidator(cls._DATA_SCHEMA) |
83
|
|
|
validator.validate(plan_data) |
84
|
|
|
|
85
|
|
|
activities = { |
86
|
|
|
activity_data['name']: Activity.from_data(activity_data) |
87
|
|
|
for activity_data in plan_data['activities'] |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
steps = [] |
91
|
|
|
for step_data in plan_data['steps']: |
92
|
|
|
step = Step.from_data(step_data, activities) |
93
|
|
|
steps.append(step) |
94
|
|
|
|
95
|
|
|
plan = cls( |
96
|
|
|
name=plan_data['name'], |
97
|
|
|
version=plan_data['version'], |
98
|
|
|
input_spec=plan_data.get('input_spec', None), |
99
|
|
|
steps=steps, |
100
|
|
|
activities=activities, |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
_LOGGER.debug('Loaded plan %s(steps:%d activities:%d)', |
104
|
|
|
plan, len(steps), len(activities)) |
105
|
|
|
|
106
|
|
|
return plan |
107
|
|
|
|
108
|
|
|
def __repr__(self): |
109
|
|
|
return 'Plan(name={name})'.format(name=self.name) |
110
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.