1
|
|
|
from __future__ import ( |
|
|
|
|
2
|
|
|
absolute_import, |
3
|
|
|
division, |
4
|
|
|
print_function |
5
|
|
|
) |
6
|
|
|
|
7
|
|
|
import logging |
8
|
|
|
|
9
|
|
|
from .schema import SchemaValidator |
10
|
|
|
|
11
|
|
|
import yaql |
|
|
|
|
12
|
|
|
|
13
|
|
|
_LOGGER = logging.getLogger(__name__) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Activity(object): |
17
|
|
|
"""Activity abstraction. |
18
|
|
|
|
19
|
|
|
Attributes: |
20
|
|
|
name (str): The name of this `Activity`. |
21
|
|
|
version (str): The version of this `Activity`. |
22
|
|
|
task_list (str): Name of the SWF `task_list` to use when submitting |
23
|
|
|
activity tasks. |
24
|
|
|
heartbeat_timeout (str): SWF 'heartbeat_timeout' value. |
25
|
|
|
schedule_to_close_timeout (str): SWF 'schedule_to_close_timeout' value. |
26
|
|
|
schedule_to_start_timeout (str): SWF 'schedule_to_start_timeout' value. |
27
|
|
|
start_to_close_timeout (str): SWF 'start_to_close_timeout' value. |
28
|
|
|
|
29
|
|
|
Examples: |
30
|
|
|
>>> a = activity.Activity.from_data( |
31
|
|
|
... { |
32
|
|
|
... 'name': 'MyActivity', |
33
|
|
|
... 'version': '1.0', |
34
|
|
|
... 'task_list': 'TranscodeAsset', |
35
|
|
|
... 'heartbeat_timeout': '60', |
36
|
|
|
... 'schedule_to_close_timeout': '518400', |
37
|
|
|
... 'schedule_to_start_timeout': '43200', |
38
|
|
|
... 'start_to_close_timeout': '432000' |
39
|
|
|
... } |
40
|
|
|
... ) |
41
|
|
|
>>> a |
42
|
|
|
Activity(name='MyActivity') |
43
|
|
|
>>> a.name |
44
|
|
|
'MyActivity' |
45
|
|
|
|
46
|
|
|
""" |
47
|
|
|
|
48
|
|
|
_DATA_SCHEMA = { |
49
|
|
|
'$schema': 'http://json-schema.org/draft-04/schema#', |
50
|
|
|
'type': 'object', |
51
|
|
|
'properties': { |
52
|
|
|
'name': { |
53
|
|
|
'type': 'string', |
54
|
|
|
}, |
55
|
|
|
'version': { |
56
|
|
|
'type': 'string', |
57
|
|
|
}, |
58
|
|
|
'input_spec': { |
59
|
|
|
'oneOf': [ |
60
|
|
|
{'type': 'null'}, |
61
|
|
|
{'$ref': '#/definitions/input_spec'}, |
62
|
|
|
], |
63
|
|
|
}, |
64
|
|
|
'outputs_spec': { |
65
|
|
|
'oneOf': [ |
66
|
|
|
{'type': 'null'}, |
67
|
|
|
{'$ref': '#/definitions/outputs_spec'}, |
68
|
|
|
] |
69
|
|
|
}, |
70
|
|
|
'task_list': { |
71
|
|
|
'type': 'string', |
72
|
|
|
}, |
73
|
|
|
'heartbeat_timeout': { |
74
|
|
|
'type': 'string', |
75
|
|
|
}, |
76
|
|
|
'schedule_to_close_timeout': { |
77
|
|
|
'type': 'string', |
78
|
|
|
}, |
79
|
|
|
'schedule_to_start_timeout': { |
80
|
|
|
'type': 'string', |
81
|
|
|
}, |
82
|
|
|
'start_to_close_timeout': { |
83
|
|
|
'type': 'string', |
84
|
|
|
}, |
85
|
|
|
}, |
86
|
|
|
'additionalProperties': False, |
87
|
|
|
'definitions': { |
88
|
|
|
'input_spec': { |
89
|
|
|
'$ref': 'http://json-schema.org/draft-04/schema#', |
90
|
|
|
}, |
91
|
|
|
'outputs_spec': { |
92
|
|
|
'type': 'object', |
93
|
|
|
'patternProperties': { |
94
|
|
|
'^[a-zA-Z0-9]+$': {} |
95
|
|
|
}, |
96
|
|
|
'minProperties': 1, |
97
|
|
|
'additionalProperties': False, |
98
|
|
|
}, |
99
|
|
|
}, |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
__slots__ = ('name', 'version', |
103
|
|
|
'task_list', |
104
|
|
|
'heartbeat_timeout', |
105
|
|
|
'schedule_to_start_timeout', |
106
|
|
|
'schedule_to_close_timeout', |
107
|
|
|
'start_to_close_timeout', |
108
|
|
|
'_input_validator', '_outputs_spec') |
109
|
|
|
|
110
|
|
|
def __init__(self, name, version, |
111
|
|
|
input_spec=None, outputs_spec=None, |
112
|
|
|
task_list=None, |
113
|
|
|
heartbeat_timeout='60', |
114
|
|
|
schedule_to_close_timeout='518400', |
115
|
|
|
schedule_to_start_timeout='43200', |
116
|
|
|
start_to_close_timeout='432000'): |
117
|
|
|
|
118
|
|
|
self.name = name |
119
|
|
|
self.version = version |
120
|
|
|
|
121
|
|
|
if task_list is None: |
122
|
|
|
task_list = '{name}-{version}'.format( |
123
|
|
|
name=self.name, |
124
|
|
|
version=self.version |
125
|
|
|
) |
126
|
|
|
self.task_list = task_list |
127
|
|
|
|
128
|
|
|
self.heartbeat_timeout = heartbeat_timeout |
129
|
|
|
self.schedule_to_close_timeout = schedule_to_close_timeout |
130
|
|
|
self.schedule_to_start_timeout = schedule_to_start_timeout |
131
|
|
|
self.start_to_close_timeout = start_to_close_timeout |
132
|
|
|
|
133
|
|
|
self._input_validator = SchemaValidator(input_spec) |
134
|
|
|
if outputs_spec: |
135
|
|
|
try: |
136
|
|
|
self._outputs_spec = {key: yaql.parse(expr) |
137
|
|
|
for key, expr in outputs_spec.items()} |
138
|
|
|
|
139
|
|
|
except Exception as err: |
140
|
|
|
_LOGGER.critical('Invalid YAQL expression in Activity %r: %r', |
141
|
|
|
name, err) |
142
|
|
|
raise |
143
|
|
|
else: |
144
|
|
|
self._outputs_spec = {} |
145
|
|
|
|
146
|
|
|
def __repr__(self): |
147
|
|
|
return 'Activity(name={name!r})'.format(name=self.name) |
148
|
|
|
|
149
|
|
|
def render_outputs(self, output): |
150
|
|
|
"""Use the `Activity`'s `outputs_spec` to generate all the defined |
151
|
|
|
representation of this activity's output. |
152
|
|
|
""" |
153
|
|
|
return { |
154
|
|
|
key: expr.evaluate(output) |
155
|
|
|
for key, expr in self._outputs_spec.items() |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
def check_input(self, activity_input): |
|
|
|
|
159
|
|
|
return self._input_validator.validate(activity_input) |
160
|
|
|
|
161
|
|
|
@classmethod |
162
|
|
|
def from_data(cls, data): |
163
|
|
|
"""Define an `Activity` from a dictionary of attributes. |
164
|
|
|
""" |
165
|
|
|
validator = SchemaValidator(cls._DATA_SCHEMA) |
166
|
|
|
validator.validate(data) |
167
|
|
|
|
168
|
|
|
activity_data = { |
169
|
|
|
'name': data['name'], |
170
|
|
|
'version': data['version'], |
171
|
|
|
'input_spec': data.get('input_spec', None), |
172
|
|
|
'outputs_spec': data.get('outputs_spec', None), |
173
|
|
|
'task_list': data.get('task_list', None) |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
# Copy in all SWF activity options. |
177
|
|
|
for option in ('heartbeat_timeout', 'schedule_to_close_timeout', |
178
|
|
|
'schedule_to_start_timeout', 'start_to_close_timeout'): |
179
|
|
|
if option in data: |
180
|
|
|
activity_data[option] = data[option] |
181
|
|
|
|
|
|
|
|
182
|
|
|
return cls(**activity_data) |
|
|
|
|
183
|
|
|
|
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.