🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

node-red-contrib-python-function-ps

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-red-contrib-python-function-ps - npm Package Compare versions

Comparing version

to
0.0.9

9

CHANGELOG.md

@@ -39,1 +39,10 @@

- add 'circular reference error' prevention code for global context
<br>
## 0.0.9
- support 'print' function in function code.
- change editor to monaco(vscode).
- displays the corresponding values based on the selection of 'local' and 'global'.
- move temp file creation from initialize to message input.
- fix bugs.

135

lib/python-function-ps.js
const { PythonShell } = require("python-shell"),
strftime = require("strftime"),
path = require("path"), fs = require("fs");

@@ -10,5 +9,4 @@

# -*- coding: utf-8 -*-
import sys, json, traceback
import os, sys, json, traceback
class Node:

@@ -50,2 +48,3 @@ def __init__(self, message_id:str, node_globals:dict):

for line in sys.stdin:

@@ -58,3 +57,3 @@ req = json.loads(line)

result = python_function(req)
result["type"] = "result"
result["$type"] = "result"
result["node_globals"] = node.globals

@@ -81,45 +80,65 @@

var shell = new PythonShell(node.scriptFile, {
pythonPath: config.pythonPathType == "global" ? node.context().global[config.pythonPath] : config.pythonPath,
pythonPath: config.pythonPathType == "global" ? node.context().global[config.globalPythonName] : config.pythonPath,
pythonOptions: ["-u"], mode: "json"
});
shell.on("message", (message) => {
var messageType = message.type;
delete message.type;
var node_ctx = node.context()
for (var key in message.node_globals) {
node_ctx.global.set(key, message.node_globals[key]);
if (typeof message != "object" || !message.hasOwnProperty("$type")) {
console.log(`message of ${node.type}[${node.id}] : ${message}`);
}
delete message.node_globals
else {
var messageType = message["$type"];
delete message["$type"];
if (messageType == "result") {
// sendToNode(node, message._msgid, message);
var node_ctx = node.context()
for (var key in message.node_globals) {
node_ctx.global.set(key, message.node_globals[key]);
}
delete message.node_globals
// restore req, res if exists
if (messageCache.req != undefined) {
message.req = messageCache.req;
delete messageCache.req;
if (messageType == "result") {
// sendToNode(node, message._msgid, message);
// restore req, res if exists
if (messageCache.req != undefined) {
message.req = messageCache.req;
delete messageCache.req;
}
if (messageCache.res != undefined) {
message.res = messageCache.res;
delete messageCache.res;
}
node.send(message);
shell.kill();
delete node.shell;
if (fs.existsSync(node.scriptFile)) {
fs.unlinkSync(node.scriptFile);
}
node.status({ fill: "green", shape: "dot", text: "Finished" });
}
if (messageCache.res != undefined) {
message.res = messageCache.res;
delete messageCache.res;
else if (messageType == "log") {
node.log(...message.payload);
}
node.send(message);
node.status({ fill: "green", shape: "dot", text: "Finished" });
else if (messageType == "warn") {
node.warn(...message.payload);
node.status({ fill: "yellow", shape: "dot", text: "Warning, see debug panel" });
}
else if (messageType == "error") {
node.error(...message.payload);
node.status({ fill: "red", shape: "dot", text: "Stopped, see debug panel" });
shell.kill();
delete node.shell;
if (fs.existsSync(node.scriptFile)) {
fs.unlinkSync(node.scriptFile);
}
}
else if (messageType == "status") {
node.status(message.payload);
}
}
else if (messageType == "log") {
node.log(...message.payload);
}
else if (messageType == "warn") {
node.warn(...message.payload);
node.status({ fill: "yellow", shape: "dot", text: "Warning, see debug panel" });
}
else if (messageType == "error") {
node.error(...message.payload);
node.status({ fill: "red", shape: "dot", text: "Stopped, see debug panel" });
}
else if (messageType == "status") {
node.status(message.payload);
}
});

@@ -131,5 +150,2 @@ shell.on("stderr", (err) => {

// change state to ready
node.status({ fill: "blue", shape: "dot", text: "Ready" });
// map shell

@@ -142,20 +158,19 @@ node.shell = shell;

function fnNode(config) {
var node = this;
RED.nodes.createNode(node, config);
var node = this; RED.nodes.createNode(node, config);
// clear scriptDir if exists
if (fs.existsSync(scriptDir)) {
fs.rmSync(scriptDir, { recursive: true });
}
fs.mkdirSync(scriptDir);
// change state to ready
node.status({ fill: "blue", shape: "dot", text: "Ready" });
node.scriptFile = path.join(
scriptDir,
config.name == "" ? `${strftime("%Y-%m-%d-%H-%M-%S")}.py` : `${node.name}.py`
config.name == "" ? `${node.id}.py` : `${node.name}.py`
);
if (config.name != "" && fs.existsSync(node.scriptFile)) {
node.scriptFile = path.join(scriptDir, `${node.name}-${strftime("%Y-%m-%d-%H-%M-%S")}.py`);
node.scriptFile = path.join(scriptDir, `${node.name}-${node.id}.py`);
}
createShell(node, config);
// create scriptDir if not exists
if (!fs.existsSync(scriptDir)) {
fs.mkdirSync(scriptDir);
}

@@ -171,8 +186,9 @@ node.on("input", (message) => {

createShell(node, config);
// remove circular reference from global contexts
var globalMessageCache = [];
globalContextMsg = JSON.stringify(node.context().global, function(_, value) {
if (typeof value == 'object' && value != null) {
if (typeof value == "object" && value != null) {
if (globalMessageCache.indexOf(value) != -1) {
// Circular reference found, discard key
return;

@@ -192,5 +208,2 @@ }

});
if (fs.existsSync(node.scriptFile)) {
fs.unlinkSync(node.scriptFile);
}

@@ -200,8 +213,8 @@ node.status({ fill: "green", shape: "dot", text: "Running" });

node.on("close", function () {
node.shell.kill();
node.shell = null;
if (node.shell != undefined) {
node.shell.kill();
delete node.shell;
}
if (fs.existsSync(scriptFile)) {
fs.unlinkSync(scriptFile);
}
fs.rmSync(scriptDir, { recursive: true });
});

@@ -208,0 +221,0 @@ }

{
"name": "node-red-contrib-python-function-ps",
"version": "0.0.8",
"version": "0.0.9",
"description": "Function Node for execute python in Node-RED",
"node-red": {
"nodes": {
"python-function-ps": "lib/python-function-ps.js"
"python-function-ps": "lib/python-function-ps.js"
}

@@ -26,5 +26,4 @@ },

"dependencies": {
"python-shell": "^3.0.1",
"strftime": "^0.10.1"
"python-shell": "^3.0.1"
}
}

Sorry, the diff of this file is not supported yet