From 39ea299839e4b410d2ed4d59ab018e6466edeebe Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 26 Jan 2026 15:04:32 +0100 Subject: [PATCH] Replace aiorepl with source copy --- internal_filesystem/lib/README.md | 5 +- internal_filesystem/lib/aiorepl.mpy | Bin 3144 -> 0 bytes internal_filesystem/lib/aiorepl.py | 332 ++++++++++++++++++++++++++++ 3 files changed, 335 insertions(+), 2 deletions(-) delete mode 100644 internal_filesystem/lib/aiorepl.mpy create mode 100644 internal_filesystem/lib/aiorepl.py diff --git a/internal_filesystem/lib/README.md b/internal_filesystem/lib/README.md index a7f1b471..ed312a17 100644 --- a/internal_filesystem/lib/README.md +++ b/internal_filesystem/lib/README.md @@ -3,9 +3,10 @@ This /lib folder contains: - mip.install("aiohttp") # easy websockets - mip.install("collections") # used by aiohttp - mip.install("unittest") -- mip.install("aiorepl") -- https://github.com/micropython/micropython-lib/blob/master/python-stdlib/logging/logging.py version 0.6.2 # for About app +- https://github.com/micropython/micropython-lib/blob/master/micropython/aiorepl/aiorepl.py version 0.2.2 # for asyncio REPL, allowing await expressions + - https://github.com/micropython/micropython-lib/blob/master/python-stdlib/base64/base64.py version 3.3.6 # for nostr - https://github.com/micropython/micropython-lib/blob/master/python-stdlib/binascii/binascii.py version 2.4.1 # for base64.py +- https://github.com/micropython/micropython-lib/blob/master/python-stdlib/logging/logging.py version 0.6.2 # for About app - https://github.com/micropython/micropython-lib/blob/master/python-stdlib/shutil/shutil.py version 0.0.5 # for rmtree() diff --git a/internal_filesystem/lib/aiorepl.mpy b/internal_filesystem/lib/aiorepl.mpy deleted file mode 100644 index a8689549888e8af04a38bb682ca6252ad2b018ab..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3144 zcmeZeV~~&K5lPI3|t0!MtVjJe5n2Cgcm@uL`_mbiiWwS} z8x=D{JsZoLYkrM^8_W zfz=qCHWYI6Q&O!JoJ)#wbe$DS@)aNnTn8fJ0uo8f%*!m!;Ig%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zu*RnmQP-V!)JH~ca`xZ4<>^s~J^^zx#>h=Bv+K7ePYd|P6l3A#IYA)7L0Q>LL0MZt zT}@5N*+fGvRZCmRRa?nbHB~#wJyY9DNnK4ZQccfPnd^{%;$etMo4|z#lp|ni#uy24 z@DZVqEhdadw>I8%Ioz=M7)X8iHWS9Cn=WxeA=?^4O&D8mx&&`)3>C1t>EbD5vcZJ$ zu+zB)r%;5k5Ej@>r=qP*pezS2x4^l+A$e=_O&3=I#lucvTU!uv)}R#TzIn^j!%ko8 zgu*wQFgDzDff}mJ4UYnlvpuwhyf<$&VLHAYRKP%EStx8fI7A_q!@}3g2wa0?pYT)^ j3fOD{D>z`%LaZV-J3u 0x7E: + if c == 0x0A: + # LF + if paste: + sys.stdout.write(b) + cmd += b + continue + # If the previous character was also LF, and was less + # than 20 ms ago, this was likely due to CRLF->LFLF + # conversion, so ignore this linefeed. + if pc == 0x0A and time.ticks_diff(t, pt) < 20: + continue + if curs: + # move cursor to end of the line + sys.stdout.write("\x1b[{}C".format(curs)) + curs = 0 + sys.stdout.write("\n") + if cmd: + # Push current command. + hist[hist_i] = cmd + # Increase history length if possible, and rotate ring forward. + hist_n = min(_HISTORY_LIMIT - 1, hist_n + 1) + hist_i = (hist_i + 1) % _HISTORY_LIMIT + + result = await execute(cmd, g, s) + if result is not None: + sys.stdout.write(repr(result)) + sys.stdout.write("\n") + break + elif c == 0x08 or c == 0x7F: + # Backspace. + if cmd: + if curs: + cmd = "".join((cmd[: -curs - 1], cmd[-curs:])) + sys.stdout.write( + "\x08\x1b[K" + ) # move cursor back, erase to end of line + sys.stdout.write(cmd[-curs:]) # redraw line + sys.stdout.write("\x1b[{}D".format(curs)) # reset cursor location + else: + cmd = cmd[:-1] + sys.stdout.write("\x08 \x08") + elif c == CHAR_CTRL_A: + raw_repl(sys.stdin, g) + break + elif c == CHAR_CTRL_B: + continue + elif c == CHAR_CTRL_C: + if paste: + break + sys.stdout.write("\n") + break + elif c == CHAR_CTRL_D: + if paste: + result = await execute(cmd, g, s) + if result is not None: + sys.stdout.write(repr(result)) + sys.stdout.write("\n") + break + + sys.stdout.write("\n") + # Shutdown asyncio. + asyncio.new_event_loop() + return + elif c == CHAR_CTRL_E: + sys.stdout.write("paste mode; Ctrl-C to cancel, Ctrl-D to finish\n===\n") + paste = True + elif c == 0x1B: + # Start of escape sequence. + key = await s.read(2) + if key in ("[A", "[B"): # up, down + # Stash the current command. + hist[(hist_i - hist_b) % _HISTORY_LIMIT] = cmd + # Clear current command. + b = "\x08" * len(cmd) + sys.stdout.write(b) + sys.stdout.write(" " * len(cmd)) + sys.stdout.write(b) + # Go backwards or forwards in the history. + if key == "[A": + hist_b = min(hist_n, hist_b + 1) + else: + hist_b = max(0, hist_b - 1) + # Update current command. + cmd = hist[(hist_i - hist_b) % _HISTORY_LIMIT] + sys.stdout.write(cmd) + elif key == "[D": # left + if curs < len(cmd) - 1: + curs += 1 + sys.stdout.write("\x1b") + sys.stdout.write(key) + elif key == "[C": # right + if curs: + curs -= 1 + sys.stdout.write("\x1b") + sys.stdout.write(key) + elif key == "[H": # home + pcurs = curs + curs = len(cmd) + sys.stdout.write("\x1b[{}D".format(curs - pcurs)) # move cursor left + elif key == "[F": # end + pcurs = curs + curs = 0 + sys.stdout.write("\x1b[{}C".format(pcurs)) # move cursor right + else: + # sys.stdout.write("\\x") + # sys.stdout.write(hex(c)) + pass + else: + if curs: + # inserting into middle of line + cmd = "".join((cmd[:-curs], b, cmd[-curs:])) + sys.stdout.write(cmd[-curs - 1 :]) # redraw line to end + sys.stdout.write("\x1b[{}D".format(curs)) # reset cursor location + else: + sys.stdout.write(b) + cmd += b + finally: + micropython.kbd_intr(3) + + +def raw_paste(s, window=512): + sys.stdout.write("R\x01") # supported + sys.stdout.write(bytearray([window & 0xFF, window >> 8, 0x01]).decode()) + eof = False + idx = 0 + buff = bytearray(window) + file = b"" + while not eof: + for idx in range(window): + b = s.read(1) + c = ord(b) + if c == CHAR_CTRL_C or c == CHAR_CTRL_D: + # end of file + sys.stdout.write(chr(CHAR_CTRL_D)) + if c == CHAR_CTRL_C: + raise KeyboardInterrupt + file += buff[:idx] + eof = True + break + buff[idx] = c + + if not eof: + file += buff + sys.stdout.write("\x01") # indicate window available to host + + return file + + +def raw_repl(s, g: dict): + """ + This function is blocking to prevent other + async tasks from writing to the stdio stream and + breaking the raw repl session. + """ + heading = "raw REPL; CTRL-B to exit\n" + line = "" + sys.stdout.write(heading) + + while True: + line = "" + sys.stdout.write(">") + while True: + b = s.read(1) + c = ord(b) + if c == CHAR_CTRL_A: + rline = line + line = "" + + if len(rline) == 2 and ord(rline[0]) == CHAR_CTRL_E: + if rline[1] == "A": + line = raw_paste(s) + break + else: + # reset raw REPL + sys.stdout.write(heading) + sys.stdout.write(">") + continue + elif c == CHAR_CTRL_B: + # exit raw REPL + sys.stdout.write("\n") + return 0 + elif c == CHAR_CTRL_C: + # clear line + line = "" + elif c == CHAR_CTRL_D: + # entry finished + # indicate reception of command + sys.stdout.write("OK") + break + else: + # let through any other raw 8-bit value + line += b + + if len(line) == 0: + # Normally used to trigger soft-reset but stay in raw mode. + # Fake it for aiorepl / mpremote. + sys.stdout.write("Ignored: soft reboot\n") + sys.stdout.write(heading) + + try: + result = exec(line, g) + if result is not None: + sys.stdout.write(repr(result)) + sys.stdout.write(chr(CHAR_CTRL_D)) + except Exception as ex: + print(line) + sys.stdout.write(chr(CHAR_CTRL_D)) + sys.print_exception(ex, sys.stdout) + sys.stdout.write(chr(CHAR_CTRL_D))