Browse Source

tools/mpremote: Make eval parse by default.

This is a step towards making the transport expose a Python API rather than
functions that mostly print to stdout.

Most use cases of `transport.eval()` are to get some state back from the
device, so have it return as a value directly by default.

Updates uses of `transport.eval()` to remove the parse argument where it
now isn't needed, make the `rtc` command use eval/exec, and update the
`mip` command to use eval's parsing.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
pull/11777/head
Jim Mussared 1 year ago
committed by Damien George
parent
commit
1091021995
  1. 9
      tools/mpremote/mpremote/commands.py
  2. 11
      tools/mpremote/mpremote/mip.py
  3. 6
      tools/mpremote/mpremote/transport_serial.py

9
tools/mpremote/mpremote/commands.py

@ -242,6 +242,11 @@ def do_soft_reset(state, _args=None):
def do_rtc(state, args):
state.ensure_raw_repl()
state.did_action()
state.transport.exec("import machine")
if args.set:
import datetime
@ -256,6 +261,6 @@ def do_rtc(state, args):
now.second,
now.microsecond,
)
_do_execbuffer(state, "import machine; machine.RTC().datetime({})".format(timetuple), True)
state.transport.exec("machine.RTC().datetime({})".format(timetuple))
else:
_do_execbuffer(state, "import machine; print(machine.RTC().datetime())", True)
print(state.transport.eval("machine.RTC().datetime()"))

11
tools/mpremote/mpremote/mip.py

@ -150,10 +150,7 @@ def _install_package(transport, package, index, target, version, mpy):
mpy_version = "py"
if mpy:
transport.exec("import sys")
mpy_version = (
int(transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF").decode())
or "py"
)
mpy_version = transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF") or "py"
package = f"{index}/package/{mpy_version}/{package}/{version}.json"
@ -178,11 +175,7 @@ def do_mip(state, args):
if args.target is None:
state.transport.exec("import sys")
lib_paths = (
state.transport.eval("'|'.join(p for p in sys.path if p.endswith('/lib'))")
.decode()
.split("|")
)
lib_paths = [p for p in state.transport.eval("sys.path") if p.endswith("/lib")]
if lib_paths and lib_paths[0]:
args.target = lib_paths[0]
else:

6
tools/mpremote/mpremote/transport_serial.py

@ -271,7 +271,7 @@ class SerialTransport(Transport):
self.exec_raw_no_follow(command)
return self.follow(timeout, data_consumer)
def eval(self, expression, parse=False):
def eval(self, expression, parse=True):
if parse:
ret = self.exec("print(repr({}))".format(expression))
ret = ret.strip()
@ -331,7 +331,7 @@ class SerialTransport(Transport):
def fs_stat(self, src):
try:
self.exec("import os")
return os.stat_result(self.eval("os.stat(%s)" % ("'%s'" % src), parse=True))
return os.stat_result(self.eval("os.stat(%s)" % ("'%s'" % src)))
except TransportError as e:
reraise_filesystem_error(e, src)
@ -503,7 +503,7 @@ class SerialTransport(Transport):
def mount_local(self, path, unsafe_links=False):
fout = self.serial
if self.eval('"RemoteFS" in globals()') == b"False":
if not self.eval('"RemoteFS" in globals()'):
self.exec(fs_hook_code)
self.exec("__mount()")
self.mounted = True

Loading…
Cancel
Save