Skip to content

Commit 6819401

Browse files
authored
Merge pull request #227 from robotpy/robot-py-updates
Robot py updates
2 parents 413ed1b + 6e2bf70 commit 6819401

File tree

9 files changed

+138
-126
lines changed

9 files changed

+138
-126
lines changed

docs/conf.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
# Project must be built+installed to generate docs
1313
import pyfrc
14-
import pyfrc.config
15-
16-
pyfrc.config.config_obj["pyfrc"] = dict(game_specific_messages=[])
1714

1815
# -- RTD configuration ------------------------------------------------
1916

pyfrc/config.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

pyfrc/mains/cli_add_tests.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import inspect
21
import os
3-
from os.path import abspath, dirname, exists, join
2+
import pathlib
3+
import sys
44

55
builtin_tests = """'''
66
This test module imports tests that come with pyfrc, and can be used
@@ -12,36 +12,41 @@
1212

1313

1414
class PyFrcAddTests:
15+
"""
16+
Adds default pyfrc tests to your robot project directory
17+
"""
18+
1519
def __init__(self, parser=None):
1620
pass
1721

18-
def run(self, options, robot_class, **static_options):
19-
robot_file = abspath(inspect.getfile(robot_class))
20-
robot_path = dirname(robot_file)
22+
def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
23+
if not main_file.exists():
24+
print(
25+
f"ERROR: is this a robot project? {main_file} does not exist",
26+
file=sys.stderr,
27+
)
28+
return 1
2129

22-
try_dirs = [
23-
abspath(join(robot_path, "tests")),
24-
abspath(join(robot_path, "..", "tests")),
25-
]
30+
try_dirs = [project_path / "tests", project_path / ".." / "tests"]
2631

2732
test_directory = try_dirs[0]
2833

2934
for d in try_dirs:
30-
if exists(d):
35+
if d.exists():
3136
test_directory = d
3237
break
3338
else:
34-
os.makedirs(test_directory)
39+
test_directory.mkdir(parents=True)
3540

36-
print("Tests directory is %s" % test_directory)
41+
print(f"Tests directory is {test_directory}")
3742
print()
38-
builtin_tests_file = join(test_directory, "pyfrc_test.py")
39-
if exists(builtin_tests_file):
43+
builtin_tests_file = test_directory / "pyfrc_test.py"
44+
if builtin_tests_file.exists():
4045
print("- pyfrc_test.py already exists")
4146
else:
4247
with open(builtin_tests_file, "w") as fp:
4348
fp.write(builtin_tests)
4449
print("- builtin tests created at", builtin_tests_file)
4550

4651
print()
47-
print("Robot tests can be ran via 'python3 robot.py test'")
52+
print("Robot tests can be ran via 'python3 -m robotpy test'")

pyfrc/mains/cli_coverage.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import argparse
2-
import inspect
32
from os.path import dirname
3+
import pathlib
44
import subprocess
55
import sys
6+
import typing
67

78

89
class PyFrcCoverage:
910
"""
10-
Wraps other commands by running them via the coverage module. Requires
11-
the coverage module to be installed.
11+
Wraps other commands by running them via the coverage module.
12+
13+
Requires the coverage module to be installed.
1214
"""
1315

1416
def __init__(self, parser: argparse.ArgumentParser):
@@ -19,7 +21,13 @@ def __init__(self, parser: argparse.ArgumentParser):
1921
"args", nargs=argparse.REMAINDER, help="Arguments to pass to robot.py"
2022
)
2123

22-
def run(self, options, robot_class, **static_options):
24+
def run(
25+
self,
26+
main_file: pathlib.Path,
27+
project_path: pathlib.Path,
28+
parallel_mode: bool,
29+
args: typing.List[str],
30+
):
2331
try:
2432
import coverage
2533
except ImportError:
@@ -30,13 +38,11 @@ def run(self, options, robot_class, **static_options):
3038
)
3139
return 1
3240

33-
if len(options.args) == 0:
41+
if len(args) == 0:
3442
print("ERROR: Coverage command requires arguments to run other commands")
3543
return 1
3644

37-
file_location = inspect.getfile(robot_class)
38-
39-
option_args = list(options.args)
45+
option_args = args
4046
if option_args[0] == "test":
4147
option_args.insert(1, "--coverage-mode")
4248

@@ -47,19 +53,20 @@ def run(self, options, robot_class, **static_options):
4753
"coverage",
4854
"run",
4955
"--source",
50-
dirname(file_location),
56+
str(project_path),
5157
]
52-
if options.parallel_mode:
58+
if parallel_mode:
5359
args.append("--parallel-mode")
5460

55-
args.append(file_location)
61+
args += ["-m", "robotpy", "--main", main_file]
5662
args += option_args
5763

64+
print("+", *args, file=sys.stderr)
5865
retval = subprocess.call(args)
5966
if retval != 0:
6067
return retval
6168

62-
if options.parallel_mode:
69+
if parallel_mode:
6370
subprocess.call([sys.executable, "-m", "coverage", "combine"])
6471

6572
args = [sys.executable, "-m", "coverage", "report", "-m"]

pyfrc/mains/cli_create_physics.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import inspect
2-
import json
3-
from os import mkdir
4-
from os.path import abspath, dirname, exists, join
1+
import pathlib
2+
import sys
3+
54

65
physics_starter = '''
76
#
@@ -90,21 +89,32 @@ def update_sim(self, now: float, tm_diff: float) -> None:
9089

9190

9291
class PyFrcCreatePhysics:
92+
"""
93+
Create physics
94+
"""
95+
9396
def __init__(self, parser=None):
9497
pass
9598

96-
def run(self, options, robot_class, **static_options):
97-
robot_file = abspath(inspect.getfile(robot_class))
98-
robot_path = dirname(robot_file)
99-
sim_path = join(robot_path, "sim")
100-
101-
physics_file = join(robot_path, "physics.py")
102-
if exists(physics_file):
99+
def run(
100+
self,
101+
main_file: pathlib.Path,
102+
project_path: pathlib.Path,
103+
):
104+
if not main_file.exists():
105+
print(
106+
f"ERROR: is this a robot project? {main_file} does not exist",
107+
file=sys.stderr,
108+
)
109+
return 1
110+
111+
physics_file = project_path / "physics.py"
112+
if physics_file.exists():
103113
print("- physics.py already exists")
104114
else:
105115
with open(physics_file, "w") as fp:
106116
fp.write(physics_starter)
107117
print("- physics file created at", physics_file)
108118

109119
print()
110-
print("Robot simulation can be run via 'python3 robot.py sim'")
120+
print("Robot simulation can be run via 'python3 -m robotpy sim'")

pyfrc/mains/cli_profiler.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import argparse
22
import inspect
33
from os.path import abspath
4+
import pathlib
45
import subprocess
56
import sys
7+
import typing
68

79

810
class PyFrcProfiler:
911
"""
1012
Wraps other commands by running them via the built in cProfile module.
13+
1114
Use this to profile your program and figure out where you're spending
1215
a lot of time (note that cProfile only profiles the main thread)
1316
"""
@@ -17,17 +20,15 @@ def __init__(self, parser):
1720
"-o", "--outfile", default=None, help="Save stats to <outfile>"
1821
)
1922
parser.add_argument(
20-
"args", nargs=argparse.REMAINDER, help="Arguments to pass to robot.py"
23+
"args", nargs=argparse.REMAINDER, help="Arguments to pass to robotpy module"
2124
)
2225

23-
def run(self, options, robot_class, **static_options):
24-
print("profiling is not yet implemented for RobotPy 2020")
25-
return 1
26-
27-
from .. import config
28-
29-
config.mode = "profiler"
30-
26+
def run(
27+
self,
28+
main_file: pathlib.Path,
29+
outfile: typing.Optional[str],
30+
args: typing.List[str],
31+
):
3132
try:
3233
import cProfile
3334
except ImportError:
@@ -37,23 +38,22 @@ def run(self, options, robot_class, **static_options):
3738
)
3839
return 1
3940

40-
if len(options.args) == 0:
41+
if len(args) == 0:
4142
print("ERROR: Profiler command requires arguments to run other commands")
4243
return 1
4344

44-
file_location = abspath(inspect.getfile(robot_class))
45-
46-
if options.outfile:
47-
profile_args = ["-o", options.outfile]
45+
if outfile:
46+
profile_args = ["-o", outfile]
4847
else:
4948
profile_args = ["-s", "tottime"]
5049

5150
# construct the arguments to run the profiler
5251
args = (
5352
[sys.executable, "-m", "cProfile"]
5453
+ profile_args
55-
+ [file_location]
56-
+ options.args
54+
+ ["-m", "robotpy", "--main", str(main_file)]
55+
+ args
5756
)
5857

58+
print("+", *args, file=sys.stderr)
5959
return subprocess.call(args)

pyfrc/mains/cli_sim.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
2-
from os.path import abspath, dirname
32
import argparse
43
import importlib.metadata
5-
import inspect
64
import logging
75
import pathlib
86
import sys
7+
import typing
8+
9+
import wpilib
910

1011

1112
logger = logging.getLogger("pyfrc.sim")
@@ -56,12 +57,18 @@ def __init__(self, parser: argparse.ArgumentParser):
5657
help=cmd_help,
5758
)
5859

59-
def run(self, options, robot_class, **static_options):
60-
if not options.nogui:
60+
def run(
61+
self,
62+
options: argparse.Namespace,
63+
nogui: bool,
64+
project_path: pathlib.Path,
65+
robot_class: typing.Type[wpilib.RobotBase],
66+
):
67+
if not nogui:
6168
try:
6269
import halsim_gui
6370
except ImportError:
64-
print("robotpy-halsim-gui is not installed!")
71+
print("robotpy-halsim-gui is not installed!", file=sys.stderr)
6572
exit(1)
6673
else:
6774
halsim_gui.loadExtension()
@@ -74,19 +81,17 @@ def run(self, options, robot_class, **static_options):
7481
try:
7582
module.loadExtension()
7683
except:
77-
print(f"Error loading {name}!")
84+
print(f"Error loading {name}!", file=sys.stderr)
7885
raise
7986

8087
os.chdir(cwd)
8188

8289
# initialize physics, attach to the user robot class
8390
from ..physics.core import PhysicsInterface, PhysicsInitException
8491

85-
robot_file = pathlib.Path(inspect.getfile(robot_class)).absolute()
86-
8792
try:
8893
_, robot_class = PhysicsInterface._create_and_attach(
89-
robot_class, robot_file.parent
94+
robot_class, project_path
9095
)
9196

9297
# run the robot

0 commit comments

Comments
 (0)