-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudf_python.mjs
More file actions
110 lines (92 loc) · 3.17 KB
/
udf_python.mjs
File metadata and controls
110 lines (92 loc) · 3.17 KB
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
/**
* Node-BLUE PythonFunctionNode for writing Node-RED user-defined functions in Python.
*
* Copyright (c) 2023, The Panodata developers and contributors.
*
* Distributed under the terms of the Apache-2.0 license, see LICENSE.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
import { red } from "./blue.mjs"
import { PythonCodeBox } from "../droste/nodejs/python_api.mjs"
/**
* Running Python user-defined functions in Node-RED.
* The improved variant. Can do kwargs.
*
* @param config
* @constructor
*/
export function PythonFunctionNode(config) {
const realfunc = async function async(config) {
red.nodes.createNode(this, config)
const node = this
console.log("node:", node.id)
// Derive function name from node identifier.
const funcname = id_to_funcname(node.id)
// v1
// let pyfun = await mkpyfun(config.func)
// v2
const box = await PythonCodeBox.init({verbose: true, pretty: true})
await box.register(funcname, config.func)
node.on("input", async function(msg, send, done) {
// v1
// let result = await pyfun({msg: msg, send: send, done: done})
// console.log("pyfun-result:", result)
// v2
let retval = await box.invoke(funcname, {msg: msg, send: send, done: done})
if (retval === undefined || retval === null) {
done()
} else {
send(await retval.valueOf())
done()
}
})
}
realfunc.apply(this, [config])
}
/**
* Running Python user-defined functions in Node-RED.
* The basic variant. Can't do kwargs.
*
* @param config
* @constructor
*/
export function PythonFunctionNodeBasic(config) {
const realfunc = async function async(config) {
red.nodes.createNode(this, config)
const node = this
let pyfun = await mkpyfun(config.func)
node.on("input", async function(msg, send, done) {
let result = await pyfun({msg: msg, send: send, done: done})
console.log("pyfun-result:", result)
// let out = {topic: msg2.topic, payload: msg2.payload}
// let out = await (await res.msg).valueOf()
})
}
realfunc.apply(this, [config])
}
/**
* Derive function name from node identifier.
*
* TODO: Some sort of `slugify` function will be appropriate.
* The current implementation is a bit thin.
* There are more characters which are not valid within function names.
*
* @param node_id {String}
* @return {String}
*/
function id_to_funcname(node_id) {
node_id = node_id.replace(".", "_")
node_id = `f_${node_id}`
return node_id
}