|
| 1 | +from configparser import RawConfigParser |
| 2 | +from pathlib import Path |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import toml |
| 6 | + |
| 7 | + |
| 8 | +def cfg_to_dict(path: Path) -> dict[str, dict[str, str]]: |
| 9 | + config = RawConfigParser() |
| 10 | + config.read(path) |
| 11 | + |
| 12 | + dict_config: dict[str, dict[str, str]] = {} |
| 13 | + for section in config.sections(): |
| 14 | + dict_config[section] = {} |
| 15 | + for key, value in config.items(section): |
| 16 | + dict_config[section][key] = value |
| 17 | + |
| 18 | + return dict_config |
| 19 | + |
| 20 | + |
| 21 | +def write_to_toml(path: Path, config: dict[str, dict[str, Any]]): |
| 22 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 23 | + with open(path, "w") as f: |
| 24 | + toml.dump(config, f) |
| 25 | + |
| 26 | + |
| 27 | +class Loadout: |
| 28 | + def __init__(self, path: Path): |
| 29 | + self.cfg_dict = cfg_to_dict(path) |
| 30 | + |
| 31 | + def _convert_dict_section(self, old_name: str) -> dict[str, int]: |
| 32 | + return { |
| 33 | + key: int(value) |
| 34 | + for key, value in self.cfg_dict[old_name].items() |
| 35 | + if key not in {"primary_color_lookup", "secondary_color_lookup"} |
| 36 | + } |
| 37 | + |
| 38 | + def convert_to_toml(self) -> dict[str, dict[str, Any]]: |
| 39 | + toml_dict: dict[str, dict[str, Any]] = {} |
| 40 | + |
| 41 | + for section in self.cfg_dict.keys(): |
| 42 | + if section == "Bot Loadout": |
| 43 | + toml_dict["blue_loadout"] = self._convert_dict_section(section) |
| 44 | + elif section == "Bot Loadout Orange": |
| 45 | + toml_dict["orange_loadout"] = self._convert_dict_section(section) |
| 46 | + elif section == "Bot Paint Blue": |
| 47 | + toml_dict["blue_loadout"]["paint"] = self._convert_dict_section(section) # type: ignore |
| 48 | + elif section == "Bot Paint Orange": |
| 49 | + toml_dict["orange_loadout"]["paint"] = self._convert_dict_section( # type: ignore |
| 50 | + section |
| 51 | + ) |
| 52 | + |
| 53 | + return toml_dict |
| 54 | + |
| 55 | + def write_to_toml(self, path: Path): |
| 56 | + write_to_toml(path, self.convert_to_toml()) |
| 57 | + |
| 58 | + |
| 59 | +class Bot: |
| 60 | + def __init__(self, path: Path): |
| 61 | + self.parent_path = path.parent |
| 62 | + self.cfg_dict = cfg_to_dict(path) |
| 63 | + |
| 64 | + def _convert_settings(self) -> dict[str, str]: |
| 65 | + settings: dict[str, str] = {} |
| 66 | + |
| 67 | + use_virtual_environment = False |
| 68 | + python_file = "" |
| 69 | + |
| 70 | + for key, value in self.cfg_dict["Locations"].items(): |
| 71 | + if key == "looks_config": |
| 72 | + key = "loadout_file" |
| 73 | + value = value.replace(".cfg", ".toml") |
| 74 | + elif key == "use_virtual_environment": |
| 75 | + use_virtual_environment = True |
| 76 | + continue |
| 77 | + elif key == "maximum_tick_rate_preference": |
| 78 | + assert int(value) == 120, "Only 120 tick rate is supported" |
| 79 | + continue |
| 80 | + elif key == "python_file": |
| 81 | + python_file = value |
| 82 | + continue |
| 83 | + elif key in { |
| 84 | + "requirements_file", |
| 85 | + "supports_early_start", |
| 86 | + "supports_standalone", |
| 87 | + }: |
| 88 | + continue |
| 89 | + settings[key] = value |
| 90 | + |
| 91 | + if use_virtual_environment: |
| 92 | + settings["run_command"] = ".\\venv\\Scripts\\python " + python_file |
| 93 | + settings["run_command_linux"] = "./venv/bin/python " + python_file |
| 94 | + else: |
| 95 | + settings["run_command"] = "python " + python_file |
| 96 | + settings["run_command_linux"] = "python3 " + python_file |
| 97 | + |
| 98 | + return settings |
| 99 | + |
| 100 | + def _convert_details(self) -> dict[str, str | list[str]]: |
| 101 | + details: dict[str, str | list[str]] = {} |
| 102 | + |
| 103 | + for key, value in self.cfg_dict["Details"].items(): |
| 104 | + if key == "tags": |
| 105 | + details[key] = [tag.strip() for tag in value.split(",")] |
| 106 | + continue |
| 107 | + details[key] = value |
| 108 | + |
| 109 | + return details |
| 110 | + |
| 111 | + def convert_to_toml(self) -> dict[str, dict[str, Any]]: |
| 112 | + toml_dict: dict[str, dict[str, Any]] = {} |
| 113 | + |
| 114 | + toml_dict["settings"] = self._convert_settings() |
| 115 | + toml_dict["details"] = self._convert_details() |
| 116 | + toml_dict["settings"][ |
| 117 | + "agent_id" |
| 118 | + ] = f"{toml_dict["details"]["author"]}/{toml_dict["settings"]["name"]}" |
| 119 | + |
| 120 | + return toml_dict |
| 121 | + |
| 122 | + def write_to_toml(self, bot_path: Path): |
| 123 | + config = self.convert_to_toml() |
| 124 | + write_to_toml(bot_path, config) |
| 125 | + |
| 126 | + old_loadout = config["settings"]["loadout_file"] |
| 127 | + Loadout(self.parent_path / old_loadout.replace(".toml", ".cfg")).write_to_toml( |
| 128 | + bot_path.parent / old_loadout |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + import sys |
| 134 | + |
| 135 | + in_path = Path(sys.argv[1]) |
| 136 | + out_path = Path(sys.argv[2]) |
| 137 | + |
| 138 | + bot = Bot(in_path) |
| 139 | + bot.write_to_toml(out_path) |
| 140 | + |
| 141 | + print(f"Converted {in_path} to {out_path}") |
0 commit comments