-
Notifications
You must be signed in to change notification settings - Fork 21
Allow starting REPL #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
laurensvalk
wants to merge
3
commits into
master
Choose a base branch
from
repl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Allow starting REPL #114
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,18 @@ | |
|
||
import argparse | ||
import asyncio | ||
import contextlib | ||
import logging | ||
import os | ||
import sys | ||
from abc import ABC, abstractmethod | ||
from os import PathLike, path | ||
from os import path | ||
from tempfile import NamedTemporaryFile | ||
from typing import ContextManager, TextIO | ||
|
||
import argcomplete | ||
from argcomplete.completers import FilesCompleter | ||
|
||
from pybricksdev import __name__ as MODULE_NAME | ||
from pybricksdev import __version__ as MODULE_VERSION | ||
from pybricksdev.ble.pybricks import UserProgramId | ||
|
||
PROG_NAME = ( | ||
f"{path.basename(sys.executable)} -m {MODULE_NAME}" | ||
|
@@ -50,44 +48,6 @@ async def run(self, args: argparse.Namespace): | |
pass | ||
|
||
|
||
def _get_script_path(file: TextIO) -> ContextManager[PathLike]: | ||
""" | ||
Gets the path to a script on the file system. | ||
|
||
If the file is ``sys.stdin``, the contents are copied to a temporary file | ||
and the path to the temporary file is returned. Otherwise, the file is closed | ||
and the path is returned. | ||
|
||
The context manager will delete the temporary file, if applicable. | ||
""" | ||
if file is sys.stdin: | ||
# Have to close the temp file so that mpy-cross can read it, so we | ||
# create our own context manager to delete the file when we are done | ||
# using it. | ||
|
||
@contextlib.contextmanager | ||
def temp_context(): | ||
try: | ||
with NamedTemporaryFile(suffix=".py", delete=False) as temp: | ||
temp.write(file.buffer.read()) | ||
|
||
yield temp.name | ||
finally: | ||
try: | ||
os.remove(temp.name) | ||
except NameError: | ||
# if NamedTemporaryFile() throws, temp is not defined | ||
pass | ||
except OSError: | ||
# file was already deleted or other strangeness | ||
pass | ||
|
||
return temp_context() | ||
|
||
file.close() | ||
return contextlib.nullcontext(file.name) | ||
|
||
|
||
class Compile(Tool): | ||
def add_parser(self, subparsers: argparse._SubParsersAction): | ||
parser = subparsers.add_parser( | ||
|
@@ -98,7 +58,7 @@ def add_parser(self, subparsers: argparse._SubParsersAction): | |
"file", | ||
metavar="<file>", | ||
help="path to a MicroPython script or `-` for stdin", | ||
type=argparse.FileType(), | ||
type=str, | ||
) | ||
parser.add_argument( | ||
"--abi", | ||
|
@@ -113,8 +73,12 @@ def add_parser(self, subparsers: argparse._SubParsersAction): | |
async def run(self, args: argparse.Namespace): | ||
from pybricksdev.compile import compile_multi_file, print_mpy | ||
|
||
with _get_script_path(args.file) as script_path: | ||
mpy = await compile_multi_file(script_path, args.abi) | ||
if args.file == "-": | ||
with NamedTemporaryFile(suffix=".py", delete=False) as temp: | ||
temp.write(sys.stdin.buffer.read()) | ||
args.file = temp.name | ||
|
||
mpy = await compile_multi_file(args.file, args.abi) | ||
print_mpy(mpy) | ||
|
||
|
||
|
@@ -134,8 +98,8 @@ def add_parser(self, subparsers: argparse._SubParsersAction): | |
parser.add_argument( | ||
"file", | ||
metavar="<file>", | ||
help="path to a MicroPython script or `-` for stdin", | ||
type=argparse.FileType(), | ||
help="path to a MicroPython script, `-` for stdin, or `repl` for interactive prompt", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well allow this to take a number to run programs in an existing slot as well. |
||
type=str, | ||
) | ||
parser.add_argument( | ||
"-n", | ||
|
@@ -213,11 +177,21 @@ def is_pybricks_usb(dev): | |
# Connect to the address and run the script | ||
await hub.connect() | ||
try: | ||
with _get_script_path(args.file) as script_path: | ||
# Handle builtin programs. | ||
if args.file == "repl": | ||
await hub.run(UserProgramId.REPL, args.wait) | ||
else: | ||
# If using stdin, save to temporary file first. | ||
if args.file == "-": | ||
with NamedTemporaryFile(suffix=".py", delete=False) as temp: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
temp.write(sys.stdin.buffer.read()) | ||
args.file = temp.name | ||
|
||
# Download program and optionally start it. | ||
if args.start: | ||
await hub.run(script_path, args.wait) | ||
await hub.run(args.file, args.wait) | ||
else: | ||
await hub.download(script_path) | ||
await hub.download(args.file) | ||
finally: | ||
await hub.disconnect() | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be best to eventually delete the temporary file. A
contextlib.ExitStack
would be the simplest way to do this.