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>
This commit is contained in:
committed by
Damien George
parent
6835743dcc
commit
1091021995
@@ -242,6 +242,11 @@ def do_soft_reset(state, _args=None):
|
|||||||
|
|
||||||
|
|
||||||
def do_rtc(state, args):
|
def do_rtc(state, args):
|
||||||
|
state.ensure_raw_repl()
|
||||||
|
state.did_action()
|
||||||
|
|
||||||
|
state.transport.exec("import machine")
|
||||||
|
|
||||||
if args.set:
|
if args.set:
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@@ -256,6 +261,6 @@ def do_rtc(state, args):
|
|||||||
now.second,
|
now.second,
|
||||||
now.microsecond,
|
now.microsecond,
|
||||||
)
|
)
|
||||||
_do_execbuffer(state, "import machine; machine.RTC().datetime({})".format(timetuple), True)
|
state.transport.exec("machine.RTC().datetime({})".format(timetuple))
|
||||||
else:
|
else:
|
||||||
_do_execbuffer(state, "import machine; print(machine.RTC().datetime())", True)
|
print(state.transport.eval("machine.RTC().datetime()"))
|
||||||
|
|||||||
@@ -150,10 +150,7 @@ def _install_package(transport, package, index, target, version, mpy):
|
|||||||
mpy_version = "py"
|
mpy_version = "py"
|
||||||
if mpy:
|
if mpy:
|
||||||
transport.exec("import sys")
|
transport.exec("import sys")
|
||||||
mpy_version = (
|
mpy_version = transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF") or "py"
|
||||||
int(transport.eval("getattr(sys.implementation, '_mpy', 0) & 0xFF").decode())
|
|
||||||
or "py"
|
|
||||||
)
|
|
||||||
|
|
||||||
package = f"{index}/package/{mpy_version}/{package}/{version}.json"
|
package = f"{index}/package/{mpy_version}/{package}/{version}.json"
|
||||||
|
|
||||||
@@ -178,11 +175,7 @@ def do_mip(state, args):
|
|||||||
|
|
||||||
if args.target is None:
|
if args.target is None:
|
||||||
state.transport.exec("import sys")
|
state.transport.exec("import sys")
|
||||||
lib_paths = (
|
lib_paths = [p for p in state.transport.eval("sys.path") if p.endswith("/lib")]
|
||||||
state.transport.eval("'|'.join(p for p in sys.path if p.endswith('/lib'))")
|
|
||||||
.decode()
|
|
||||||
.split("|")
|
|
||||||
)
|
|
||||||
if lib_paths and lib_paths[0]:
|
if lib_paths and lib_paths[0]:
|
||||||
args.target = lib_paths[0]
|
args.target = lib_paths[0]
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ class SerialTransport(Transport):
|
|||||||
self.exec_raw_no_follow(command)
|
self.exec_raw_no_follow(command)
|
||||||
return self.follow(timeout, data_consumer)
|
return self.follow(timeout, data_consumer)
|
||||||
|
|
||||||
def eval(self, expression, parse=False):
|
def eval(self, expression, parse=True):
|
||||||
if parse:
|
if parse:
|
||||||
ret = self.exec("print(repr({}))".format(expression))
|
ret = self.exec("print(repr({}))".format(expression))
|
||||||
ret = ret.strip()
|
ret = ret.strip()
|
||||||
@@ -331,7 +331,7 @@ class SerialTransport(Transport):
|
|||||||
def fs_stat(self, src):
|
def fs_stat(self, src):
|
||||||
try:
|
try:
|
||||||
self.exec("import os")
|
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:
|
except TransportError as e:
|
||||||
reraise_filesystem_error(e, src)
|
reraise_filesystem_error(e, src)
|
||||||
|
|
||||||
@@ -503,7 +503,7 @@ class SerialTransport(Transport):
|
|||||||
|
|
||||||
def mount_local(self, path, unsafe_links=False):
|
def mount_local(self, path, unsafe_links=False):
|
||||||
fout = self.serial
|
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(fs_hook_code)
|
||||||
self.exec("__mount()")
|
self.exec("__mount()")
|
||||||
self.mounted = True
|
self.mounted = True
|
||||||
|
|||||||
Reference in New Issue
Block a user