From 3f293cb1de3d628bdf5f0af07ba22fbb6a37d5d0 Mon Sep 17 00:00:00 2001 From: Baili Deng Date: Fri, 15 Nov 2024 19:51:47 +0800 Subject: [PATCH] tool: convert sound_card_init yaml configs to xml BUG=b:377624799 TEST=`python3 sound_card_init_yaml_to_xml.py \ ${SRC}/overlays/overlay-brya/chromeos-base/chromeos-bsp-brya/files \ ../sound_card_init` Change-Id: Ic04371c6c051a572132daa9322a18132977b852b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/6020744 Reviewed-by: Yu-Hsuan Hsu Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com Commit-Queue: ChromeOS Auto Runner Commit-Queue: Baili Deng --- devtools/sound_card_init_yaml_to_xml.py | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 devtools/sound_card_init_yaml_to_xml.py diff --git a/devtools/sound_card_init_yaml_to_xml.py b/devtools/sound_card_init_yaml_to_xml.py new file mode 100644 index 000000000..2b03dc24d --- /dev/null +++ b/devtools/sound_card_init_yaml_to_xml.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# Copyright 2024 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import argparse +import os +import xml.etree.ElementTree as ET + +import yaml + + +def list_to_xml_element(data, root, key): + """Converts a array to XML elements.""" + for item in data: + if isinstance(item, dict): + sub_element = ET.SubElement(root, key) + dict_to_xml_element(item, sub_element) + elif isinstance(item, list): + sub_element = ET.SubElement(root, key) + list_to_xml_element(item, sub_element, key) + else: + ET.SubElement(root, key).text = str(item) + + +def dict_to_xml_element(data, root): + """Converts a dictionary to XML elements.""" + for key, value in data.items(): + if isinstance(value, dict): + sub_element = ET.SubElement(root, key) + dict_to_xml_element(value, sub_element) + elif isinstance(value, list): + sub_element = ET.SubElement(root, key + '_list') + list_to_xml_element(value, sub_element, key) + else: + ET.SubElement(root, key).text = str(value) + + +def dict_to_xml(data, output_file): + root = ET.Element("sound_card_init") + dict_to_xml_element(data, root) + # add the licence comment in the xml + comment = ET.Comment( + "Copyright (C) 2024 The Android Open Source Project\n\n" + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + "you may not use this file except in compliance with the License.\n" + "You may obtain a copy of the License at\n\n" + " http://www.apache.org/licenses/LICENSE-2.0\n\n" + "Unless required by applicable law or agreed to in writing, software \n" + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "See the License for the specific language governing permissions and\n" + "limitations under the License." + ) + root.insert(0, comment) # insert before the root's first child + tree = ET.ElementTree(root) + ET.indent(tree, space="\t", level=0) + + tree.write(output_file, encoding="utf-8", xml_declaration=True) + + print(f"XML written to {output_file}") + + +def parse_sound_card_init_yaml(config): + file = os.open(config, os.O_RDONLY) + with open(config) as file: + yaml_data = yaml.load(file, Loader=yaml.SafeLoader) + return yaml_data + + +def get_amp_name(yaml_file): + return yaml_file.split('.')[1] + + +def convert_sound_card_init_configs(base_dir, output_dir): + """Converts sound-card-init-config yaml files to AMP.xml.""" + + audio_sound_card_init_path = os.path.join("audio", "sound-card-init-config") + + for model_dir in os.listdir(base_dir): + model_path = os.path.join(base_dir, model_dir) + + if os.path.isdir(model_path) and os.path.isdir( + os.path.join(model_path, audio_sound_card_init_path) + ): + for yaml_file_name in os.listdir(os.path.join(model_path, audio_sound_card_init_path)): + config = os.path.join(model_path, audio_sound_card_init_path, yaml_file_name) + + if os.path.isfile(config): + amp_name = get_amp_name(yaml_file_name) + output_file = os.path.join(output_dir, model_dir, f"{amp_name}.xml") + config_data = parse_sound_card_init_yaml(config) + dict_to_xml(config_data, output_file) + + +if __name__ == "__main__": + # Argument parsing using argparse + parser = argparse.ArgumentParser( + description="Convert sound-card-init-config yaml files to AMP.xml" + ) + parser.add_argument( + "base_dir", + help="${CHROMIUMOS}/src/overlays/overlay-${BOARD}/chromeos-base/chromeos-bsp-${BOARD}/files", + ) + parser.add_argument( + "output_dir", help="Path to the output directory where converted files will be saved" + ) + + args = parser.parse_args() + + convert_sound_card_init_configs(args.base_dir, args.output_dir)