Skip to content

Commit 3516c5c

Browse files
committed
Add app loader as new default main.py
1 parent 56f4b42 commit 3516c5c

File tree

4 files changed

+76
-41
lines changed

4 files changed

+76
-41
lines changed

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ dist: $(BUILD)
155155
cp -r $(PY_BUILD)/nx-*/nx $(LIBDIR)
156156
cp $(OUTPUT).nro $(DIST_DIR)/
157157
cp $(OUTPUT).nacp $(DIST_DIR)/
158-
cp examples/hello.py $(DIST_DIR)/main.py
159158
cd $(BUILD) && zip -r $(TARGET)-$(APP_VERSION).zip $(TARGET)
160159

161160
#---------------------------------------------------------------------------------

examples/hello.py

-25
This file was deleted.

examples/tcp_repl.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import socket
22
import sys
33
import code
4-
import time
4+
55

66
DEBUG_PORT = 1337
77

8-
def remote_repl(local):
9-
sys.stdout.flush()
10-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11-
s.bind(("0.0.0.0", DEBUG_PORT))
12-
s.listen(1)
138

14-
print("Waiting for remote debug connection on port {}.".format(DEBUG_PORT))
9+
if __name__ == '__main__':
10+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11+
sock.bind(("0.0.0.0", DEBUG_PORT))
12+
sock.listen(1)
13+
print("Waiting for remote debug connection on port {}.".format(DEBUG_PORT), flush=True)
1514

16-
conn, addr = s.accept()
15+
conn, addr = sock.accept()
1716

1817
print("Accepted remote debug connection from '{}'.".format(addr[0]))
1918

@@ -25,15 +24,10 @@ def remote_repl(local):
2524
sys.stdin, sys.stdout, sys.stderr = rfile, wfile, wfile
2625

2726
try:
28-
code.interact(local=local)
27+
code.interact(local=locals())
2928
except SystemExit:
3029
pass
3130

3231
conn.close()
33-
s.close()
32+
sock.close()
3433
sys.stdin, sys.stdout, sys.stderr = stdin_bak, stdout_bak, stderr_bak
35-
print("Remote debug session closed.")
36-
37-
while 1:
38-
remote_repl(locals())
39-
time.sleep(1)

main.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import sys
2+
from glob import glob
3+
import os
4+
import runpy
5+
import time
6+
import _nx
7+
import nx
8+
from nx.utils import AnsiMenu
9+
10+
11+
sys.argv = [""] # workaround needed for runpy
12+
13+
TILED_DOUBLE = 1
14+
MAIN_PY = 'main.py'
15+
PYNX_DIR_PATH = os.getcwd()
16+
17+
18+
def run_python_module(path: str):
19+
runpy.run_path(path, run_name='__main__')
20+
_nx.gfx_set_mode(TILED_DOUBLE)
21+
print("{} exited.".format(selected), flush=True)
22+
23+
24+
def clear_screen():
25+
sys.stdout.buffer.write(b"\x1b[2J")
26+
sys.stdout.buffer.flush()
27+
28+
29+
def confirmation_prompt(program_name: str):
30+
name = program_name[:-3] if program_name.endswith('.py') else program_name
31+
name = name[:-1] if name.endswith(os.sep) else name
32+
print("Do you want to execute the Python program {}?\n"
33+
"Press A to confirm, B to cancel.".format(name), flush=True)
34+
while True:
35+
nx.refresh_inputs()
36+
if nx.p1.a_button.is_pressed:
37+
return True
38+
elif nx.p1.b_button.is_pressed:
39+
return False
40+
time.sleep(0.01)
41+
42+
43+
if __name__ == '__main__':
44+
while True:
45+
clear_screen()
46+
dirs = ["../"] + glob("*/")
47+
scripts = glob("*.py")
48+
if os.getcwd() == PYNX_DIR_PATH and MAIN_PY in scripts:
49+
scripts.remove(MAIN_PY)
50+
listing = dirs + scripts
51+
file_menu = AnsiMenu(listing)
52+
print("Listing for {}:".format(os.getcwd()), flush=True)
53+
selected = listing[file_menu.query()]
54+
clear_screen()
55+
if selected in dirs:
56+
os.chdir(selected)
57+
if os.path.isfile(MAIN_PY) and not selected == '../' and not os.getcwd() == PYNX_DIR_PATH:
58+
response = confirmation_prompt(selected)
59+
clear_screen()
60+
if response is True:
61+
run_python_module(MAIN_PY)
62+
os.chdir(os.pardir)
63+
else:
64+
response = confirmation_prompt(selected)
65+
clear_screen()
66+
if response is True:
67+
run_python_module(selected)

0 commit comments

Comments
 (0)