-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_loves_js.py
74 lines (65 loc) · 2.02 KB
/
py_loves_js.py
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
import json
import os
class Py:
"""
Creates a Json file with the given dictionar to be picked up
by the JavaScript JS object.
"""
def __init__(self) -> None:
self.NOTE = None
self.JSON_PATH = None
def send_note_to_js(self, note_object: dict, path: str):
try:
with open(path, 'x') as note:
json.dump(note_object, note)
note.close()
except FileNotFoundError as e:
print(repr(e))
except IOError as e:
print(repr(e))
def get_note_from_js(self) -> (dict | None):
try:
with open(self.JSON_PATH, "r") as note:
note_data = json.load(note)
note.close()
return note_data
except FileNotFoundError as e:
print(repr(e))
return None
except IOError as e:
print(repr(e))
return None
except TypeError as e:
print(repr(e))
return None
def check_for_note(self, path: str) -> (bool | None):
"""
Watches the folder specified by path for the existence of
a file specified by file by recursively checking for its
existence.
"""
try:
got_note = False
note_path = os.path.join(path, "note_from_js.json")
self.JSON_PATH = note_path
print(note_path)
while not got_note:
if os.path.exists(note_path):
note = self.get_note_from_js()
self.NOTE = note
got_note = True
else: print("Checking ...")
return True
except TypeError as e:
print(repr(e))
return False
py = Py()
# Listen for and get an object from JS.
# if py.check_for_note("."):
# print("Data available!")
# print(py.NOTE)
# Send an object to JS.
# py.send_note_to_js({
# "name": "python",
# "text": "Hello JavaScript!"
# }, "note_from_py.json")