Skip to content

Commit 43f2337

Browse files
committed
[DevToolKit] devToolKit -webrepl It open webrepl webUI in default browser (same as OTA file transfer in the background - micropython feature integration) in New module toolkit/WebRepl.py :)
1 parent cecef03 commit 43f2337

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

devToolKit.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
except Exception as e:
3636
print(f"Optional dependency missing in macroScript: {e}")
3737
macroScript = None
38+
try:
39+
from toolkit.WebRepl import open_webrepl
40+
except Exception as e:
41+
print(f"Optional dependency missing in WebRepl: {e}")
42+
open_webrepl = None
3843

3944

4045
def arg_parse():
@@ -70,6 +75,7 @@ def arg_parse():
7075
dev_group.add_argument("-gw", "--gateway", action="store_true", help="Start micrOS Gateway rest-api server, Env. vars: API_AUTH='username:password' (optional), GATEWAYIP needed for container deployment only.")
7176
dev_group.add_argument("-v", "--version", action="store_true", help="Get micrOS version - repo + connected device.")
7277
dev_group.add_argument("-lint", "--linter", action="store_true", help="Run micrOS system linter (pylint+)")
78+
dev_group.add_argument("-webrepl", "--open_webrepl", action="store_true", help="(beta) Open webrepl in default browser, micropython repl + file transfers (built-in)")
7379
dev_group.add_argument("--light", action="store_true", help="Skip optional dependency deployments (low level param: add this as first argument always)")
7480

7581
toolkit_group = parser.add_argument_group("Toolkit development")
@@ -293,6 +299,10 @@ def update_pip_package():
293299
if micrOSlint is not None:
294300
sys.exit(micrOSlint.main(verbose=False))
295301

302+
if cmd_args.open_webrepl:
303+
if open_webrepl is not None:
304+
open_webrepl()
305+
296306
if cmd_args.macro:
297307
if macroScript is None:
298308
print("macroScript not available :(")

toolkit/WebRepl.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import sys
2+
import os
3+
4+
MYDIR = os.path.dirname(__file__)
5+
print("Module [WebRepl] path: {} __package__: {} __name__: {} __file__: {}".format(
6+
sys.path[0], __package__, __name__, MYDIR))
7+
try:
8+
from .lib.TerminalColors import Colors
9+
from .lib.LocalMachine import FileHandler, CommandHandler, SimplePopPushd
10+
from .socketClient import ConnectionData
11+
except Exception as e:
12+
print("Import warning __name__:{}: {}".format(__name__, e))
13+
from lib.TerminalColors import Colors
14+
from lib.LocalMachine import FileHandler, CommandHandler, SimplePopPushd
15+
from socketClient import ConnectionData
16+
17+
WEBREPL_DIR = os.path.join(MYDIR, "workspace/webrepl")
18+
MAKE_HTML_WEBPAGE_PATH = os.path.join(WEBREPL_DIR, "make_html_js.py")
19+
WEBREPL_WEBPAGE_PATH = os.path.join(WEBREPL_DIR, "webrepl.html")
20+
21+
def init_webrepl_frontend():
22+
webrepldir_handler = SimplePopPushd()
23+
webrepldir_handler.pushd(WEBREPL_DIR)
24+
25+
is_exists, ftype = FileHandler.path_is_exists(MAKE_HTML_WEBPAGE_PATH)
26+
if is_exists and ftype == 'f':
27+
print("RUN webrepl/make_html_js.py")
28+
exitcode, _, stderr = CommandHandler.run_command(f"{sys.executable} {MAKE_HTML_WEBPAGE_PATH}", shell=True)
29+
webrepldir_handler.popd()
30+
if exitcode == 0:
31+
return True
32+
print(f"Cannot init {MAKE_HTML_WEBPAGE_PATH}: {stderr}")
33+
return False
34+
print(f"WEBREPL MAKE MISSING: {MAKE_HTML_WEBPAGE_PATH}")
35+
return False
36+
37+
def open_webrepl_webpage(address=None, port=8266):
38+
# file:///Users/usrname/micrOS/micrOS/toolkit/workspace/webrepl/webrepl.html#10.0.1.76:8266
39+
fuid = ''
40+
if address is None:
41+
try:
42+
address, _, fuid, _ = ConnectionData.select_device()
43+
except KeyboardInterrupt:
44+
address = None
45+
46+
device = "" if address is None else f"#{address}:{port}"
47+
webrepl_html = f"file://{WEBREPL_WEBPAGE_PATH}{device}"
48+
is_exists, ftype = FileHandler.path_is_exists(WEBREPL_WEBPAGE_PATH)
49+
if is_exists and ftype == "f":
50+
print(f"OPEN {fuid} webrepl {webrepl_html}")
51+
if sys.platform == "darwin":
52+
CommandHandler.run_command(f"open '{webrepl_html}'", shell=True)
53+
elif sys.platform == "win32":
54+
CommandHandler.run_command(f"start {webrepl_html}", shell=True)
55+
elif sys.platform == "linux":
56+
CommandHandler.run_command(f"xdg-open {webrepl_html}", shell=True)
57+
else:
58+
print("Unsupported OS")
59+
return False
60+
return True
61+
print(f"{WEBREPL_WEBPAGE_PATH} not exists.")
62+
return False
63+
64+
65+
def open_webrepl():
66+
state = init_webrepl_frontend()
67+
if state:
68+
open_webrepl_webpage()
69+
70+
71+
72+
if __name__ == "__main__":
73+
open_webrepl()

toolkit/socketClient.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
import socket
66
import os
7-
import select
87
import time
98
import json
109
import threading

0 commit comments

Comments
 (0)