Skip to content

Commit ef1ee2c

Browse files
authored
Added python tool that generates json (#163)
* Added python tool to generate the json for robotpy and external_samples. * Generate json for server_python_scripts/blocks_base_classes python files. * Added support for built-in methods to mrc_call_python_function. Use mrc_call_python_function for the print block. * Make contents not optional in the Category class. * Make the print block have an empty string shadow argument.
1 parent 4f63d3f commit ef1ee2c

File tree

20 files changed

+1633
-188
lines changed

20 files changed

+1633
-188
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/build
1313

1414
# python stuff
15-
__pycache__
15+
__pycache__/
16+
venv/
1617
*.egg-info
1718

1819
# misc

external_samples/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# external samples

external_samples/spark_mini.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def __init__(self, ports : list[tuple[PortType, int]]):
3131
portType, port = ports[0]
3232
if portType != PortType.SMART_MOTOR_PORT:
3333
raise InvalidPortException
34-
self.spark_mini = wpilib.SparkMini(port)
34+
# TODO(lizlooney): When we upgrade to 2027 robotpy, change PWMSparkMax to SparkMini.
35+
self.spark_mini = wpilib.PWMSparkMax(port) # wpilib.SparkMini(port)
3536

3637
def get_manufacturer(self) -> str:
3738
return "REV Robotics"

python_tools/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Python Tools
2+
3+
## To generate JSON for the robotpy modules and classes:
4+
5+
The following instructions work on macOS Sonoma 14.6.1.
6+
7+
### Setup
8+
1. cd <your repo>/python_tools
9+
1. python3.12 -m venv ./venv
10+
1. source ./venv/bin/activate
11+
1. python3.12 -m pip install -r requirements.txt
12+
1. deactivate
13+
14+
### To Regenerate robotpy_data.json
15+
1. cd <your repo>/python_tools
16+
1. python3.12 -m venv ./venv
17+
1. source ./venv/bin/activate
18+
1. python3.12 generate_json.py --output_directory=../src/blocks/utils
19+
1. deactivate

python_tools/generate_json.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
__author__ = "[email protected] (Liz Looney)"
16+
17+
# Python Standard Library
18+
import pathlib
19+
import sys
20+
21+
# absl
22+
from absl import app
23+
from absl import flags
24+
from absl import logging
25+
26+
# robotpy
27+
import hal
28+
import hal.simulation
29+
import ntcore
30+
import pyfrc
31+
import wpilib
32+
import wpilib.counter
33+
import wpilib.drive
34+
import wpilib.event
35+
import wpilib.interfaces
36+
import wpilib.shuffleboard
37+
import wpilib.simulation
38+
import wpimath
39+
import wpimath.controller
40+
import wpimath.estimator
41+
import wpimath.filter
42+
import wpimath.geometry
43+
import wpimath.interpolation
44+
import wpimath.kinematics
45+
import wpimath.optimization
46+
import wpimath.path
47+
import wpimath.spline
48+
import wpimath.system
49+
import wpimath.system.plant
50+
import wpimath.trajectory
51+
import wpimath.trajectory.constraint
52+
import wpimath.units
53+
import wpinet
54+
import wpiutil
55+
56+
# Server python scripts
57+
sys.path.append("../server_python_scripts")
58+
import blocks_base_classes
59+
60+
# External samples
61+
sys.path.append("../external_samples")
62+
import color_range_sensor
63+
import component
64+
import rev_touch_sensor
65+
import servo
66+
import smart_motor
67+
import spark_mini
68+
import sparkfun_led_stick
69+
70+
# Local modules
71+
import json_util
72+
import python_util
73+
74+
75+
FLAGS = flags.FLAGS
76+
77+
flags.DEFINE_string('output_directory', None, 'The directory where output should be written.')
78+
79+
80+
def main(argv):
81+
del argv # Unused.
82+
83+
if not FLAGS.output_directory:
84+
logging.error(f'You must specify the --output_directory argument')
85+
return
86+
87+
pathlib.Path(f'{FLAGS.output_directory}/generated/').mkdir(parents=True, exist_ok=True)
88+
89+
robotpy_modules = [
90+
hal,
91+
hal.simulation,
92+
ntcore,
93+
wpilib,
94+
wpilib.counter,
95+
wpilib.drive,
96+
wpilib.event,
97+
wpilib.interfaces,
98+
wpilib.shuffleboard,
99+
wpilib.simulation,
100+
python_util.getModule('wpilib.sysid'),
101+
wpimath,
102+
wpimath.controller,
103+
wpimath.estimator,
104+
wpimath.filter,
105+
wpimath.geometry,
106+
wpimath.interpolation,
107+
wpimath.kinematics,
108+
wpimath.optimization,
109+
wpimath.path,
110+
wpimath.spline,
111+
wpimath.system,
112+
wpimath.system.plant,
113+
wpimath.trajectory,
114+
wpimath.trajectory.constraint,
115+
wpimath.units,
116+
wpinet,
117+
wpiutil,
118+
]
119+
json_generator = json_util.JsonGenerator(robotpy_modules)
120+
file_path = f'{FLAGS.output_directory}/generated/robotpy_data.json'
121+
json_generator.writeJsonFile(file_path)
122+
123+
server_python_scripts = [
124+
blocks_base_classes,
125+
]
126+
json_generator = json_util.JsonGenerator(server_python_scripts)
127+
file_path = f'{FLAGS.output_directory}/generated/server_python_scripts.json'
128+
json_generator.writeJsonFile(file_path)
129+
130+
external_samples_modules = [
131+
color_range_sensor,
132+
component,
133+
rev_touch_sensor,
134+
servo,
135+
smart_motor,
136+
spark_mini,
137+
sparkfun_led_stick,
138+
]
139+
json_generator = json_util.JsonGenerator(external_samples_modules)
140+
file_path = f'{FLAGS.output_directory}/generated/external_samples_data.json'
141+
json_generator.writeJsonFile(file_path)
142+
143+
144+
if __name__ == '__main__':
145+
app.run(main)

0 commit comments

Comments
 (0)