This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsystem.py
104 lines (72 loc) · 2.46 KB
/
subsystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
robot_path = "src/main/java/frc/robot"
subsystem_class = """package frc.robot.subsystems;
import com.chopshop166.chopshoplib.commands.SmartSubsystemBase;
import frc.robot.maps.subsystems.${CLASSNAME}Map;
public class ${CLASSNAME} extends SmartSubsystemBase {
private ${CLASSNAME}Map map;
public ${CLASSNAME}(${CLASSNAME}Map map) {
this.map = map;
}
@Override
public void reset() {
// Nothing to reset here
}
@Override
public void safeState() {
}
@Override
public void periodic() {
// This method will be called once per scheduler run
// Use this for any background processing
}
}"""
subsystem_map = """package frc.robot.maps.subsystems;
public class ${CLASSNAME}Map {
public ${CLASSNAME}Map() {
}
}
"""
def insert_into(filename: str, keyword: str, line: str, before: bool = False):
with open(f"{robot_path}/{filename}", "r") as fp:
contents = fp.read()
if before:
to_replace = f"\n{line}\n{keyword}"
else:
to_replace = f"{keyword}\n{line}\n"
with open(f"{robot_path}/{filename}", "w") as fp:
fp.write(contents.replace(keyword, to_replace))
def save_template(filename, template, **kwargs):
new_template = template
for key, value in kwargs.items():
new_template = new_template.replace(f"${{{key}}}", value)
with open(f"{robot_path}/{filename}", "w") as fp:
fp.write(new_template)
class_name = input("Subsystem Class Name: ")
# Make sure that it is a proper class name
class_name = "".join((s[0].upper() + s[1:]) for s in class_name.split())
instance_name = class_name[0].lower() + class_name[1:]
save_template(f"subsystems/{class_name}.java", subsystem_class, CLASSNAME=class_name)
insert_into(
f"Robot.java",
"$Subsystems$",
f" {class_name} {instance_name} = new {class_name}(map.get{class_name}Map());",
)
insert_into(f"Robot.java", "$Imports$", f"import frc.robot.subsystems.{class_name};")
save_template(
f"maps/subsystems/{class_name}Map.java", subsystem_map, CLASSNAME=class_name
)
insert_into(
f"maps/RobotMap.java",
"$Maps$",
f" private {class_name}Map {instance_name}Map = new {class_name}Map();",
)
insert_into(
f"maps/RobotMap.java",
"$Getters$",
f" public {class_name}Map get{class_name}Map() {{\n return {instance_name}Map;\n }}",
)
insert_into(
f"maps/RobotMap.java",
"$Imports$",
f"import frc.robot.maps.subsystems.{class_name}Map;",
)