Skip to content

Added logic to create a default robot.py from the arcade drive examle #145

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions robotpy_installer/cli_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import inspect
import logging
import pathlib
import urllib.request


from . import pyproject
from .utils import handle_cli_error
Expand All @@ -18,17 +20,29 @@ class Init:
def __init__(self, parser: argparse.ArgumentParser):
pass

def _default_main_file(self, main_file: pathlib.Path)-> None:
source = "https://raw.githubusercontent.com/robotpy/examples/refs/heads/main/ArcadeDrive/robot.py"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously, this isn't going to work offline. Probably better to do this download with the hatchling wheel build?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... lol, I just saw my comment below. I guess I could go either way.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha. Yea, target was for this to get something meaningful in robot.py during init - if there's internet we can yoink from the minimal example. If there isn't, fallback is to be same as today (presumably the first-time build will fail anyway unless they get some internet).

Perhaps a more useful fallback is to put the URL they go to for finding some initial robot code?

And/or, address the initial docs question a different way (like how it said, just put a hello world into the docs).

Then again, if we were to hardcode a hello world into the docs for python, why not just hardcode it here?

If I had to pick I think I'd want to keep as is (presuming most folks will have internet when they run robotpy init) and make the fallback just indicate the URL to get code from?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me.

The only challenge will be to remember to update the branch name if we have a different set of examples for 2027.

with open(main_file, "wb") as fp:
try:
with urllib.request.urlopen(source) as response:
fp.write(response.read())
logger.info("Created %s from example", main_file)
except Exception as e:
logger.error("Failed to download %s: %s", source, e)
self._default_main_file_fallback(main_file)

def _default_main_file_fallback(self, main_file: pathlib.Path)-> None:
with open(main_file, "w") as fp:
fp.write("# TODO: insert robot code here\n")
logger.info("Created empty %s", main_file)

@handle_cli_error
def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
project_path.mkdir(parents=True, exist_ok=True)

# Create robot.py if it doesn't already exist
# - TODO: it would be neat if it could download an example from github
if not main_file.exists():
with open(main_file, "w") as fp:
fp.write("# TODO: insert robot code here\n")

logger.info("Created empty %s", main_file)
self._default_main_file(main_file)

# Create pyproject.toml if it doesn't already exist
pyproject_path = pyproject.toml_path(project_path)
Expand Down
Loading