-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
127 lines (99 loc) · 5.04 KB
/
Copy pathapp.py
File metadata and controls
127 lines (99 loc) · 5.04 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from argparse import ArgumentError, ArgumentParser
import json
from pathlib import Path
from typing import Optional, cast
from pathplanner.directory import get_pathplanner_dir
from pathplanner.pathplanner_json import PlannerAuto, PlannerPath
from pathplanner.pathplanner_settings import PPSettings
from util import inverter, name_conversions
def mirror_paths(path_folders: list[str], path_ifolder: str, path_ofolder: str, paths_dir: Path):
if(path_ifolder not in path_folders):
raise ValueError("Path folder " + path_ifolder + " does not exist!")
if(path_ofolder not in path_folders):
raise ValueError("Path folder " + path_ofolder + " does not exist!")
for input_path in paths_dir.rglob("*.path"):
path_name = input_path.name
pp_path: PlannerPath
with input_path.open("rt") as file:
pp_path = cast(PlannerPath, json.load(file))
if(pp_path["folder"] != path_ifolder): continue
pp_path["folder"] = path_ofolder
inverter.mirror_path(pp_path)
output_path = paths_dir / (name_conversions.flip_side_name(path_name) or (path_name + " Flipped"))
with output_path.open("wt") as file:
file.write(json.dumps(pp_path, indent=2))
print(f"{input_path.name} -> {output_path.name}")
def mirror_autos(auto_folders: list[str], auto_ifolder: str, auto_ofolder: str, autos_dir: Path):
if(auto_ifolder not in auto_folders):
raise ValueError("Path folder " + auto_ifolder + " does not exist!")
if(auto_ofolder not in auto_folders):
raise ValueError("Path folder " + auto_ofolder + " does not exist!")
for input_path in autos_dir.rglob("*.auto"):
auto_name = input_path.name
pp_auto: PlannerAuto
with input_path.open("rt") as file:
pp_auto = cast(PlannerAuto, json.load(file))
if(pp_auto["folder"] != auto_ifolder): continue
pp_auto["folder"] = auto_ofolder
inverter.mirror_auto(pp_auto)
output_path = autos_dir / (name_conversions.flip_side_name(auto_name) or (auto_name + " Flipped"))
with output_path.open("wt") as file:
file.write(json.dumps(pp_auto, indent=2))
print(f"{input_path.name} -> {output_path.name}")
def main():
parser = ArgumentParser(
description="Utility App for quickly mirroring PathPlanner paths and autos"
)
parser.add_argument(
"directory",
nargs="?",
metavar="DIRECTORY",
help="The directory to run the program in. If this is omitted, the program will use the current working directory"
)
parser.add_argument(
"-b", "--both",
nargs=2,
metavar=("INPUT_FOLDER", "OUTPUT_FOLDER"),
help="Shorthand to mirror paths first, then autos. This will assume the paths and autonomous routines use the same folder names. See -p or -a for more information"
)
parser.add_argument(
"-p", "--path",
nargs=2,
metavar=("INPUT_FOLDER", "OUTPUT_FOLDER"),
help="Mirror the paths within INPUT_FOLDER, and output them to OUTPUT_FOLDER. The folders refer to PathPlanner-defined folders, NOT directories"
)
parser.add_argument(
"-a", "--auto",
nargs=2,
metavar=("INPUT_FOLDER", "OUTPUT_FOLDER"),
help="Mirror the autos within INPUT_FOLDER, and output them to OUTPUT_FOLDER. The folders refer to PathPlanner-defined folders, NOT directories. " +
"This option will NOT automatically mirror the paths. If you want to mirror paths before mirroring the autos, you must explicitly call -p or use -b"
)
args = parser.parse_args()
#####################################################
path_args: Optional[tuple[str, str]] = args.path
auto_args: Optional[tuple[str, str]] = args.auto
both_args: Optional[tuple[str, str]] = args.both
if((path_args is None) and (auto_args is None) and (both_args is None)):
raise ArgumentError(None, "No folders provided. Please use -b/--both/-p/--path/-a/--auto")
ROOT_DIR = Path(args.directory) if (args.directory is not None) else Path.cwd()
if(not ROOT_DIR.exists()):
raise ValueError(ROOT_DIR.as_posix() + " does not exist")
print("Locating deploy/pathplanner in " + ROOT_DIR.as_posix())
PP_DIR = get_pathplanner_dir(ROOT_DIR)
if(PP_DIR is None):
raise ValueError("Could not find pathplanner directory")
print("deploy/pathplanner found!")
settingsPath = PP_DIR / "settings.json"
settingsFile = settingsPath.open("rt")
PP_SETTINGS: PPSettings = cast(PPSettings, json.load(settingsFile))
settingsFile.close()
if(path_args is not None):
mirror_paths(PP_SETTINGS["pathFolders"], *path_args, PP_DIR / "paths")
if(auto_args is not None):
mirror_autos(PP_SETTINGS["autoFolders"], *auto_args, PP_DIR / "autos")
if(both_args is not None):
mirror_paths(PP_SETTINGS["pathFolders"], *both_args, PP_DIR / "paths")
mirror_autos(PP_SETTINGS["autoFolders"], *both_args, PP_DIR / "autos")
if __name__ == "__main__":
main()