generated from KBVE/nodepy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpynode.js
More file actions
64 lines (52 loc) · 1.39 KB
/
pynode.js
File metadata and controls
64 lines (52 loc) · 1.39 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
const { spawnSync } = require("child_process");
const { readFile } = require("fs/promises");
const { appendFile } = require("fs/promises");
const { join } = require("path");
class pyNode {
constructor(____file, ____json) {
this.loading = false;
this.file = ____file;
this.json = ____json;
this.error;
this.result;
this.process;
this._data;
this.data;
}
async _kbveInit() {
this.loading = true;
//console.log(await spawnSync("pip", ['freeze']));
this.process = await spawnSync("python3", [`${__dirname}/scripts/${this.file}.py`, this.json]);
this.error = this.process.stderr?.toString()?.trim();
this.result = this.process.stdout?.toString()?.trim();
const status = this.process.status;
if (status === 0) {
this._data = this.result;
} else {
this._data = this.error;
}
this.loading = false;
}
async initialize() {
if (!this.initializationPromise) {
this.initializationPromise = this._kbveInit();
}
return this.initializationPromise;
}
async _process() {
try {
const responseObject = {
data: this._data,
};
this.data = responseObject;
} catch (error) {
}
return this.data;
}
}
async function pyNodeManager(_file, _json) {
const _pyNode = new pyNode(_file, _json);
await _pyNode.initialize();
return _pyNode;
}
module.exports = { pyNodeManager };