From 63c23f809c643e41a535be5aa9917d9e8fed52af Mon Sep 17 00:00:00 2001 From: Mathieu Choplain Date: Wed, 19 Jun 2024 15:19:16 +0200 Subject: [PATCH] scripts: genpinctrl: detect XML namespace automatically This commit modifies the genpinctrl script to detect the XML namespace of pin data files automatically. Signed-off-by: Mathieu Choplain --- scripts/genpinctrl/genpinctrl.py | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/scripts/genpinctrl/genpinctrl.py b/scripts/genpinctrl/genpinctrl.py index 9370ea9bf..e25387320 100644 --- a/scripts/genpinctrl/genpinctrl.py +++ b/scripts/genpinctrl/genpinctrl.py @@ -537,6 +537,48 @@ def get_mcu_signals(data_path, gpio_ip_afs): return results +def detect_xml_namespace(data_path: Path): + """ + Attempt to detect the XML namespace used in the pindata files automatically. + This removes the need to modify this file when using pin data from sources + other than the official ST repository, which may use a different xmlns. + """ + global NS + + mcus_path = data_path / "mcu" + try: + sampled_file = next(mcus_path.glob("STM32*.xml")) + except StopIteration: + # No STM32*.xml file found. Log a warning but continue script execution. + # If this really isn't a pindata folder, something else will panic later on. + logger.warn(f"No STM32*.xml found in {data_path!s} - XMLNS detection skipped") + return + + with open(sampled_file, "r") as fd: + line = "" + xmlns = None + while len(line) > 0: + line = fd.readline().removeprefix("<").removesuffix(">\n") + + # '' tag sets XML namespace + if line.startswith("Mcu"): + # Find the XML namespace in tag elements + for e in line.split(): + if e.startswith("xmlns="): + xmlns = e + break + break + + if xmlns is None: + logger.info(f"Could not determine XML namespace from {sampled_file}") + return + else: + xml_namespace_url = xmlns.removeprefix('xmlns="').removesuffix('"') + NS = "{" + xml_namespace_url + "}" + + logger.info(f"Using {NS} as XML namespace.") + + def main(data_path, output): """Entry point. @@ -561,6 +603,8 @@ def main(data_path, output): pinctrl_template = env.get_template(PINCTRL_TEMPLATE) readme_template = env.get_template(README_TEMPLATE) + detect_xml_namespace(data_path) + gpio_ip_afs = get_gpio_ip_afs(data_path) mcu_signals = get_mcu_signals(data_path, gpio_ip_afs)