oocana 0.16.9__tar.gz → 0.16.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {oocana-0.16.9 → oocana-0.16.10}/PKG-INFO +1 -1
- {oocana-0.16.9 → oocana-0.16.10}/oocana/context.py +127 -69
- {oocana-0.16.9 → oocana-0.16.10}/pyproject.toml +1 -1
- {oocana-0.16.9 → oocana-0.16.10}/oocana/__init__.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/data.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/extra.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/handle_data.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/mainframe.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/preview.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/schema.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/service.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/oocana/throtter.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/__init__.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_data.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_handle_data.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_json.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_mainframe.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_performance.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_schema.py +0 -0
- {oocana-0.16.9 → oocana-0.16.10}/tests/test_throtter.py +0 -0
@@ -36,7 +36,6 @@ class Context:
|
|
36
36
|
__block_info: BlockInfo
|
37
37
|
__outputs_def: Dict[str, HandleDef]
|
38
38
|
__store: Any
|
39
|
-
__is_done: bool = False
|
40
39
|
__keep_alive: OnlyEqualSelf = OnlyEqualSelf()
|
41
40
|
__session_dir: str
|
42
41
|
__tmp_dir: str
|
@@ -156,10 +155,6 @@ class Context:
|
|
156
155
|
"""
|
157
156
|
return os.getenv("OO_HOST_ENDPOINT", None)
|
158
157
|
|
159
|
-
@property
|
160
|
-
def is_done(self) -> bool:
|
161
|
-
return self.__is_done
|
162
|
-
|
163
158
|
def __store_ref(self, handle: str):
|
164
159
|
return StoreKey(
|
165
160
|
executor=EXECUTOR_NAME,
|
@@ -170,8 +165,67 @@ class Context:
|
|
170
165
|
|
171
166
|
def __is_basic_type(self, value: Any) -> bool:
|
172
167
|
return isinstance(value, (int, float, str, bool))
|
168
|
+
|
169
|
+
def __wrap_output_value(self, handle: str, value: Any):
|
170
|
+
"""
|
171
|
+
wrap the output value:
|
172
|
+
if the value is a var handle, store it in the store and return the reference.
|
173
|
+
if the value is a bin handle, store it in the store and return the reference.
|
174
|
+
if the handle is not defined in the block outputs schema, raise an ValueError.
|
175
|
+
otherwise, return the value.
|
176
|
+
:param handle: the handle of the output
|
177
|
+
:param value: the value of the output
|
178
|
+
:return: the wrapped value
|
179
|
+
"""
|
180
|
+
# __outputs_def should never be None
|
181
|
+
if self.__outputs_def is None:
|
182
|
+
return value
|
183
|
+
|
184
|
+
output_def = self.__outputs_def.get(handle)
|
185
|
+
if output_def is None:
|
186
|
+
raise ValueError(
|
187
|
+
f"Output handle key: [{handle}] is not defined in Block outputs schema."
|
188
|
+
)
|
189
|
+
|
190
|
+
if output_def.is_var_handle() and not self.__is_basic_type(value):
|
191
|
+
ref = self.__store_ref(handle)
|
192
|
+
self.__store[ref] = value
|
193
|
+
var: VarValueDict = {
|
194
|
+
"__OOMOL_TYPE__": "oomol/var",
|
195
|
+
"value": asdict(ref)
|
196
|
+
}
|
197
|
+
return var
|
198
|
+
|
199
|
+
if output_def.is_bin_handle():
|
200
|
+
if not isinstance(value, bytes):
|
201
|
+
self.send_warning(
|
202
|
+
f"Output handle key: [{handle}] is defined as binary, but the value is not bytes."
|
203
|
+
)
|
204
|
+
return value
|
205
|
+
|
206
|
+
bin_file = f"{self.session_dir}/binary/{self.session_id}/{self.job_id}/{handle}"
|
207
|
+
os.makedirs(os.path.dirname(bin_file), exist_ok=True)
|
208
|
+
try:
|
209
|
+
with open(bin_file, "wb") as f:
|
210
|
+
f.write(value)
|
211
|
+
except IOError as e:
|
212
|
+
raise IOError(
|
213
|
+
f"Output handle key: [{handle}] is defined as binary, but an error occurred while writing the file: {e}"
|
214
|
+
)
|
215
|
+
|
216
|
+
if os.path.exists(bin_file):
|
217
|
+
bin_value: BinValueDict = {
|
218
|
+
"__OOMOL_TYPE__": "oomol/bin",
|
219
|
+
"value": bin_file,
|
220
|
+
}
|
221
|
+
return bin_value
|
222
|
+
else:
|
223
|
+
raise IOError(
|
224
|
+
f"Output handle key: [{handle}] is defined as binary, but the file is not written."
|
225
|
+
)
|
226
|
+
return value
|
173
227
|
|
174
|
-
def output(self, key: str, value: Any
|
228
|
+
def output(self, key: str, value: Any):
|
175
229
|
"""
|
176
230
|
output the value to the next block
|
177
231
|
|
@@ -179,81 +233,85 @@ class Context:
|
|
179
233
|
value: Any, the value of the output
|
180
234
|
"""
|
181
235
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
output_def = self.__outputs_def.get(key)
|
186
|
-
if (
|
187
|
-
output_def is not None and output_def.is_var_handle() and not self.__is_basic_type(value) # 基础类型即使是变量也不放进 store,直接作为 json 内容传递
|
188
|
-
):
|
189
|
-
ref = self.__store_ref(key)
|
190
|
-
self.__store[ref] = value
|
191
|
-
d: VarValueDict = {
|
192
|
-
"__OOMOL_TYPE__": "oomol/var",
|
193
|
-
"value": asdict(ref)
|
194
|
-
}
|
195
|
-
v = d
|
196
|
-
elif output_def is not None and output_def.is_bin_handle():
|
197
|
-
if not isinstance(value, bytes):
|
198
|
-
self.send_warning(
|
199
|
-
f"Output handle key: [{key}] is defined as binary, but the value is not bytes."
|
200
|
-
)
|
201
|
-
return
|
202
|
-
|
203
|
-
bin_file = f"{self.session_dir}/binary/{self.session_id}/{self.job_id}/{key}"
|
204
|
-
os.makedirs(os.path.dirname(bin_file), exist_ok=True)
|
205
|
-
try:
|
206
|
-
with open(bin_file, "wb") as f:
|
207
|
-
f.write(value)
|
208
|
-
except IOError as e:
|
209
|
-
self.send_warning(
|
210
|
-
f"Output handle key: [{key}] is defined as binary, but an error occurred while writing the file: {e}"
|
211
|
-
)
|
212
|
-
return
|
213
|
-
|
214
|
-
if os.path.exists(bin_file):
|
215
|
-
bin_value: BinValueDict = {
|
216
|
-
"__OOMOL_TYPE__": "oomol/bin",
|
217
|
-
"value": bin_file,
|
218
|
-
}
|
219
|
-
v = bin_value
|
220
|
-
else:
|
221
|
-
self.send_warning(
|
222
|
-
f"Output handle key: [{key}] is defined as binary, but the file is not written."
|
223
|
-
)
|
224
|
-
return
|
225
|
-
|
226
|
-
# 如果传入 key 在输出定义中不存在,直接忽略,不发送数据。但是 done 仍然生效。
|
227
|
-
if self.__outputs_def is not None and self.__outputs_def.get(key) is None:
|
236
|
+
try:
|
237
|
+
wrap_value = self.__wrap_output_value(key, value)
|
238
|
+
except ValueError as e:
|
228
239
|
self.send_warning(
|
229
|
-
f"
|
240
|
+
f"{e}"
|
241
|
+
)
|
242
|
+
return
|
243
|
+
except IOError as e:
|
244
|
+
self.send_warning(
|
245
|
+
f"{e}"
|
230
246
|
)
|
231
|
-
if done:
|
232
|
-
self.done()
|
233
247
|
return
|
234
248
|
|
235
249
|
node_result = {
|
236
250
|
"type": "BlockOutput",
|
237
251
|
"handle": key,
|
238
|
-
"output":
|
239
|
-
"done": done,
|
252
|
+
"output": wrap_value,
|
240
253
|
}
|
241
254
|
self.__mainframe.send(self.job_info, node_result)
|
255
|
+
|
256
|
+
def outputs(self, outputs: Dict[str, Any]):
|
257
|
+
"""
|
258
|
+
output the value to the next block
|
242
259
|
|
243
|
-
|
244
|
-
|
260
|
+
map: Dict[str, Any], the key of the output, should be defined in the block schema output defs, the field name is handle
|
261
|
+
"""
|
245
262
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
263
|
+
values = {}
|
264
|
+
for key, value in outputs.items():
|
265
|
+
try:
|
266
|
+
wrap_value = self.__wrap_output_value(key, value)
|
267
|
+
values[key] = wrap_value
|
268
|
+
except ValueError as e:
|
269
|
+
self.send_warning(
|
270
|
+
f"{e}"
|
271
|
+
)
|
272
|
+
except IOError as e:
|
273
|
+
self.send_warning(
|
274
|
+
f"{e}"
|
275
|
+
)
|
276
|
+
self.__mainframe.send(self.job_info, {
|
277
|
+
"type": "BlockOutputs",
|
278
|
+
"outputs": values,
|
279
|
+
})
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def finish(self, *, result: Dict[str, Any] | None = None, error: str | None = None):
|
284
|
+
"""
|
285
|
+
finish the block, and send the result to oocana.
|
286
|
+
if error is not None, the block will be finished with error.
|
287
|
+
then if result is not None, the block will be finished with result.
|
288
|
+
lastly, if both error and result are None, the block will be finished without any result.
|
289
|
+
"""
|
290
|
+
|
291
|
+
if error is not None:
|
292
|
+
self.__mainframe.send(self.job_info, {"type": "BlockFinished", "error": error})
|
293
|
+
elif result is not None:
|
294
|
+
wrap_result = {}
|
295
|
+
if isinstance(result, dict):
|
296
|
+
for key, value in result.items():
|
297
|
+
try:
|
298
|
+
wrap_result[key] = self.__wrap_output_value(key, value)
|
299
|
+
except ValueError as e:
|
300
|
+
self.send_warning(
|
301
|
+
f"Output handle key: [{key}] is not defined in Block outputs schema. {e}"
|
302
|
+
)
|
303
|
+
except IOError as e:
|
304
|
+
self.send_warning(
|
305
|
+
f"Output handle key: [{key}] is not defined in Block outputs schema. {e}"
|
306
|
+
)
|
307
|
+
|
308
|
+
self.__mainframe.send(self.job_info, {"type": "BlockFinished", "result": wrap_result})
|
309
|
+
else:
|
310
|
+
raise ValueError(
|
311
|
+
f"result should be a dict, but got {type(result)}"
|
312
|
+
)
|
253
313
|
else:
|
254
|
-
self.__mainframe.send(
|
255
|
-
self.job_info, {"type": "BlockFinished", "error": error}
|
256
|
-
)
|
314
|
+
self.__mainframe.send(self.job_info, {"type": "BlockFinished"})
|
257
315
|
|
258
316
|
def send_message(self, payload):
|
259
317
|
self.__mainframe.report(
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|