# Copyright (C) 2008-2022 The Paparazzi Team # released under GNU GPLv2 or later. See COPYING file. from dataclasses import dataclass, field from typing import List, Dict import lxml.etree as ET import os import subprocess import utils MOD_DEP = os.path.join(utils.PAPARAZZI_SRC, "sw", "tools", "generators", "dump_modules_list.out") CONF = os.path.join(utils.PAPARAZZI_HOME, "conf", "conf.xml") class ConfError(Exception): ... @dataclass class Setting: name: str enabled: bool def __str__(self): if self.enabled: return self.name else: return f"[{self.name}]" @dataclass class Aircraft: name: str = "" ac_id: int = 0 airframe: str = "" radio: str = "" telemetry: str = "" flight_plan: str = "" gui_color: str = "red" settings: List[Setting] = field(default_factory=list) settings_modules: List[Setting] = field(default_factory=list) boards: Dict[str, str] = field(default_factory=dict, init=False) # {target: board} def get_color(self) -> str: if self.gui_color.startswith("#"): r = self.gui_color[1:3] g = self.gui_color[5:7] b = self.gui_color[9:11] color = "#{}{}{}".format(r, g, b) return color else: return self.gui_color def set_color(self, color: str): if color.startswith("#"): r = color[1:3] g = color[3:5] b = color[5:7] self.gui_color = "#{}00{}00{}00".format(r, g, b) else: self.gui_color = color def update(self): self.update_targets() self.update_settings() def update_settings(self): completed = subprocess.run([MOD_DEP, "-ac", self.name, "-af", self.airframe, "-fp", self.flight_plan], capture_output=True) if completed.returncode != 0: raise ConfError(completed.stderr.decode().strip()) def make_setting(m): setting = Setting(m, True) for s in self.settings_modules: if m == s.name and not s.enabled: setting.enabled = False return setting new_settings_modules = [] for module_path in completed.stdout.decode().strip().split(): module = utils.remove_prefix(module_path, utils.CONF_DIR) xml = ET.parse(module_path) for xml_setting in xml.getroot().findall("settings"): name = xml_setting.get("name") if name is None: txt = module else: txt = "{}~{}~".format(module, name) setting = make_setting(txt) new_settings_modules.append(setting) self.settings_modules = new_settings_modules def to_xml(self) -> ET.Element: xml = ET.Element("aircraft") xml.set("name", self.name) xml.set("ac_id", str(self.ac_id)) xml.set("airframe", self.airframe) xml.set("radio", self.radio) xml.set("telemetry", self.telemetry) xml.set("flight_plan", self.flight_plan) settings_modules = " ".join(str(setting) for setting in self.settings_modules) settings = " ".join(str(setting) for setting in self.settings) xml.set("settings", settings) xml.set("settings_modules", settings_modules) xml.set("gui_color", self.gui_color) return xml def to_string(self): xml = "