Skip to content

Commit

Permalink
feat(main): remove stub creation logic from micropy main state.
Browse files Browse the repository at this point in the history
Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
  • Loading branch information
BradenM committed Mar 27, 2023
1 parent 1f82d75 commit 1f8d9ba
Showing 1 changed file with 0 additions and 62 deletions.
62 changes: 0 additions & 62 deletions micropy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,65 +91,3 @@ def resolve_project(self, path, verbose=True):
self.log.success("Ready!")
return proj
return proj

def create_stubs(
self,
port,
verbose=False,
backend: Union[Literal["upydevice"], Literal["rshell"]] = "upydevice",
):
"""Create and add stubs from Pyboard.
Todo:
Extract and cleanup this mess.
Args:
port (str): Port of Pyboard
Returns:
Stub: generated stub
"""
self.log.title(f"Connecting to Pyboard @ $[{port}]")
pyb_log = Log.add_logger("Pyboard", "bright_white")
backend = UPyDeviceBackend if backend == "upydevice" else RShellPyDeviceBackend

def _get_desc(name: str, cfg: dict):
desc = f"{pyb_log.get_service()} {name}"
return name, cfg | dict(desc=desc)

message_handler = MessageHandlers(
on_message=lambda x: isinstance(x, str) and pyb_log.info(x.strip())
)
try:
pyb = PyDevice(
port,
auto_connect=True,
stream_consumer=ProgressStreamConsumer(on_description=_get_desc),
message_consumer=message_handler,
backend=backend,
)
except (SystemExit, PyDeviceError):
self.log.error(f"Failed to connect, are you sure $[{port}] is correct?")
return None
self.log.success("Connected!")
create_stubs = prepare_create_stubs()
self.log.info("Executing stubber on pyboard...")
try:
pyb.run_script(create_stubs.getvalue(), DevicePath("createstubs.py"))
except Exception as e:
# TODO: Handle more usage cases
self.log.error(f"Failed to execute script: {str(e)}", exception=e)
raise
self.log.success("Done!")
self.log.info("Copying stubs...")
with tempfile.TemporaryDirectory() as tmpdir:
pyb.copy_from(DevicePath("/stubs"), tmpdir)
out_dir = Path(tmpdir)
stub_path = next(out_dir.iterdir())
self.log.info(f"Copied Stubs: $[{stub_path.name}]")
stub_path = self.stubs.from_stubber(stub_path, out_dir)
stub = self.stubs.add(str(stub_path))
pyb.disconnect()
self.log.success(f"Added {stub.name} to stubs!")
return stub

0 comments on commit 1f8d9ba

Please sign in to comment.