-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_loves_py.js
75 lines (63 loc) · 1.89 KB
/
js_loves_py.js
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
const fs = require('fs')
const path = require('path')
class JS {
/**
* Creates a Json file with the given object to be picked up
* by the Python Py object.
*/
constructor() {
this.NOTE = null
this.JSON_PATH = null
this.sendNoteToPy = (note_object, path) => {
try {
fs.writeFileSync(path, JSON.stringify(note_object))
}
catch (err) {
console.info(err.message)
}
}
this.getNoteFromPy = () => {
try {
let note_from_py = JSON.parse(fs.readFileSync(this.JSON_PATH))
return note_from_py
}
catch(err) {
console.info(err.message)
}
}
this.checkForNote = (path) => {
if (path) {
try {
let got_note = false
const note_path = path + '/' + "note_from_py.json"
this.JSON_PATH = note_path
while (!got_note) {
if (fs.existsSync(note_path)) {
let note = this.getNoteFromPy()
this.NOTE = note
got_note = true;
} else {
console.log('Checking ...')
}
}
return true
} catch (error) {
console.error(error)
return null
}
}
}
}
};
//Example usage
// Listen for and get an object from JS.
// const js = new JS()
// if (js.checkForNote(".")) {
// console.log("Data available!!!")
// console.log(js.NOTE)
// }
// Send an object to JS.
// js.sendNoteToPy({
// "name": "javascript",
// "text": "Hello Python!"
// }, "note_from_js.json")