From a0cb7289d18185e5b1bb32985b4156705aae3362 Mon Sep 17 00:00:00 2001 From: Chris Gerth Date: Thu, 19 Jun 2025 20:23:49 -0500 Subject: [PATCH] Added logic to create a default robot.py from the arcade drive example, rather than being blank. This should help ensure deployability. --- robotpy_installer/cli_init.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/robotpy_installer/cli_init.py b/robotpy_installer/cli_init.py index 55e1985..8ede200 100644 --- a/robotpy_installer/cli_init.py +++ b/robotpy_installer/cli_init.py @@ -2,6 +2,8 @@ import inspect import logging import pathlib +import urllib.request + from . import pyproject from .utils import handle_cli_error @@ -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" + 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)