|
| 1 | +import json |
| 2 | +import sys |
| 3 | +from typing import Any, Dict |
| 4 | + |
| 5 | +PLUGIN_INFO = { |
| 6 | + "id": "sportorg.example.python", |
| 7 | + "name": "Python Example Plugin", |
| 8 | + "version": "0.1.0", |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +class ExamplePlugin: |
| 13 | + def __init__(self) -> None: |
| 14 | + self.settings: Dict[str, Any] = { |
| 15 | + "auto_publish": False, |
| 16 | + "executions": 0, |
| 17 | + } |
| 18 | + self.race: Dict[str, Any] = {} |
| 19 | + self.update_counts = { |
| 20 | + "race": 0, |
| 21 | + "result": 0, |
| 22 | + "person": 0, |
| 23 | + "group": 0, |
| 24 | + "organization": 0, |
| 25 | + "course": 0, |
| 26 | + "entity": 0, |
| 27 | + } |
| 28 | + |
| 29 | + def handle_request(self, message: Dict[str, Any]) -> None: |
| 30 | + method = str(message.get("method", "")) |
| 31 | + request_id = message.get("id") |
| 32 | + params = message.get("params", {}) |
| 33 | + if not isinstance(params, dict): |
| 34 | + self.write_error(request_id, -32602, "Invalid params") |
| 35 | + return |
| 36 | + |
| 37 | + if method == "plugin.initialize": |
| 38 | + plugin_settings = ( |
| 39 | + params.get("settings", {}).get("plugin", {}) |
| 40 | + if isinstance(params.get("settings", {}), dict) |
| 41 | + else {} |
| 42 | + ) |
| 43 | + if isinstance(plugin_settings, dict): |
| 44 | + self.settings.update(plugin_settings) |
| 45 | + race = params.get("race", {}) |
| 46 | + if isinstance(race, dict): |
| 47 | + self.race = race |
| 48 | + self.write_result( |
| 49 | + request_id, |
| 50 | + { |
| 51 | + "plugin": PLUGIN_INFO, |
| 52 | + "capabilities": { |
| 53 | + "menu": True, |
| 54 | + "race_updates": True, |
| 55 | + "result_updates": True, |
| 56 | + "person_updates": True, |
| 57 | + "group_updates": True, |
| 58 | + "organization_updates": True, |
| 59 | + "course_updates": True, |
| 60 | + "settings": True, |
| 61 | + }, |
| 62 | + "settings": self.settings, |
| 63 | + }, |
| 64 | + ) |
| 65 | + return |
| 66 | + |
| 67 | + if method == "plugin.menu.get": |
| 68 | + self.write_result( |
| 69 | + request_id, |
| 70 | + { |
| 71 | + "items": [ |
| 72 | + { |
| 73 | + "id": "show_summary", |
| 74 | + "label": "Show plugin summary", |
| 75 | + "tooltip": "Show how many updates the example plugin received", |
| 76 | + "enabled": True, |
| 77 | + "visible": True, |
| 78 | + "group": "Example", |
| 79 | + "order": 100, |
| 80 | + }, |
| 81 | + { |
| 82 | + "id": "toggle_auto_publish", |
| 83 | + "label": self._toggle_label(), |
| 84 | + "enabled": True, |
| 85 | + "visible": True, |
| 86 | + "group": "Example", |
| 87 | + "order": 200, |
| 88 | + }, |
| 89 | + { |
| 90 | + "id": "update_race_description", |
| 91 | + "label": "Update race description", |
| 92 | + "enabled": bool(self.race), |
| 93 | + "visible": True, |
| 94 | + "group": "Example", |
| 95 | + "order": 300, |
| 96 | + }, |
| 97 | + ] |
| 98 | + }, |
| 99 | + ) |
| 100 | + return |
| 101 | + |
| 102 | + if method == "plugin.menu.execute": |
| 103 | + action_id = str(params.get("id", "")) |
| 104 | + if action_id == "show_summary": |
| 105 | + self.settings["executions"] = ( |
| 106 | + int(self.settings.get("executions", 0)) + 1 |
| 107 | + ) |
| 108 | + self.write_result( |
| 109 | + request_id, |
| 110 | + { |
| 111 | + "status": "ok", |
| 112 | + "message": self._summary_message(), |
| 113 | + "settings": self.settings, |
| 114 | + }, |
| 115 | + ) |
| 116 | + return |
| 117 | + |
| 118 | + if action_id == "toggle_auto_publish": |
| 119 | + self.settings["auto_publish"] = not bool( |
| 120 | + self.settings.get("auto_publish", False) |
| 121 | + ) |
| 122 | + self.write_result( |
| 123 | + request_id, |
| 124 | + { |
| 125 | + "status": "ok", |
| 126 | + "message": self._toggle_label(), |
| 127 | + "settings": self.settings, |
| 128 | + }, |
| 129 | + ) |
| 130 | + return |
| 131 | + |
| 132 | + if action_id == "update_race_description": |
| 133 | + self.send_race_description_update() |
| 134 | + self.write_result( |
| 135 | + request_id, |
| 136 | + { |
| 137 | + "status": "ok", |
| 138 | + "message": "Race update sent from example plugin", |
| 139 | + }, |
| 140 | + ) |
| 141 | + return |
| 142 | + |
| 143 | + self.write_error(request_id, -32601, "Unknown menu action") |
| 144 | + return |
| 145 | + |
| 146 | + self.write_error(request_id, -32601, "Method not found") |
| 147 | + |
| 148 | + def handle_notification(self, message: Dict[str, Any]) -> None: |
| 149 | + method = str(message.get("method", "")) |
| 150 | + if method == "plugin.shutdown": |
| 151 | + raise SystemExit(0) |
| 152 | + |
| 153 | + prefix = "sportorg." |
| 154 | + suffix = ".update" |
| 155 | + if method.startswith(prefix) and method.endswith(suffix): |
| 156 | + entity_name = method[len(prefix) : -len(suffix)] |
| 157 | + if entity_name in self.update_counts: |
| 158 | + self.update_counts[entity_name] += 1 |
| 159 | + |
| 160 | + def write_result(self, request_id: Any, result: Dict[str, Any]) -> None: |
| 161 | + self.write_message({"jsonrpc": "2.0", "id": request_id, "result": result}) |
| 162 | + |
| 163 | + def write_error(self, request_id: Any, code: int, message: str) -> None: |
| 164 | + self.write_message( |
| 165 | + { |
| 166 | + "jsonrpc": "2.0", |
| 167 | + "id": request_id, |
| 168 | + "error": { |
| 169 | + "code": code, |
| 170 | + "message": message, |
| 171 | + }, |
| 172 | + } |
| 173 | + ) |
| 174 | + |
| 175 | + def write_message(self, message: Dict[str, Any]) -> None: |
| 176 | + sys.stdout.write(json.dumps(message, ensure_ascii=False) + "\n") |
| 177 | + sys.stdout.flush() |
| 178 | + |
| 179 | + def send_race_description_update(self) -> None: |
| 180 | + if not self.race: |
| 181 | + return |
| 182 | + |
| 183 | + race_entity = dict(self.race) |
| 184 | + race_data = dict(race_entity.get("data", {})) |
| 185 | + race_data["description"] = "Updated by Python Example Plugin" |
| 186 | + race_entity["data"] = race_data |
| 187 | + self.write_message( |
| 188 | + { |
| 189 | + "jsonrpc": "2.0", |
| 190 | + "method": "sportorg.race.update", |
| 191 | + "params": { |
| 192 | + "operation": "updated", |
| 193 | + "race_id": race_entity.get("id", ""), |
| 194 | + "entity": race_entity, |
| 195 | + }, |
| 196 | + } |
| 197 | + ) |
| 198 | + |
| 199 | + def _summary_message(self) -> str: |
| 200 | + parts = [ |
| 201 | + "{}={}".format(key, value) |
| 202 | + for key, value in sorted(self.update_counts.items()) |
| 203 | + if value |
| 204 | + ] |
| 205 | + if not parts: |
| 206 | + return "Example plugin is connected; no updates received yet" |
| 207 | + return "Example plugin updates: {}".format(", ".join(parts)) |
| 208 | + |
| 209 | + def _toggle_label(self) -> str: |
| 210 | + if bool(self.settings.get("auto_publish", False)): |
| 211 | + return "Disable auto publish" |
| 212 | + return "Enable auto publish" |
| 213 | + |
| 214 | + |
| 215 | +def main() -> None: |
| 216 | + plugin = ExamplePlugin() |
| 217 | + for line in sys.stdin: |
| 218 | + line = line.strip() |
| 219 | + if not line: |
| 220 | + continue |
| 221 | + |
| 222 | + try: |
| 223 | + message = json.loads(line) |
| 224 | + except json.JSONDecodeError as exc: |
| 225 | + sys.stderr.write("Invalid JSON: {}\n".format(exc)) |
| 226 | + sys.stderr.flush() |
| 227 | + continue |
| 228 | + |
| 229 | + if not isinstance(message, dict): |
| 230 | + continue |
| 231 | + |
| 232 | + if "id" in message and "method" in message: |
| 233 | + plugin.handle_request(message) |
| 234 | + elif "method" in message: |
| 235 | + plugin.handle_notification(message) |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == "__main__": |
| 239 | + main() |
0 commit comments