From 426d95ae48348d5dd2ef444b7cc33e851e9b15d3 Mon Sep 17 00:00:00 2001 From: LTU Date: Mon, 11 Aug 2025 21:40:46 +0300 Subject: [PATCH] Improve config parser structure, error handling and style Signed-off-by: LTU --- scripts/read-config.py | 89 ++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/scripts/read-config.py b/scripts/read-config.py index 7b34f04..6a9034c 100644 --- a/scripts/read-config.py +++ b/scripts/read-config.py @@ -15,43 +15,66 @@ # limitations under the License. import argparse -import os +from pathlib import Path import re import sys -parser = argparse.ArgumentParser() -group = parser.add_mutually_exclusive_group(required=True) -group.add_argument("--sources", action="store_true") -group.add_argument("--family", action="store_true") -args = parser.parse_args() -with open(os.path.join("sources", "config.yaml")) as config: - data = config.read() - -if args.family: - m = re.search(r"(?m)^familyName: (.*)", data) - if m: - print(m[1]) - sys.exit(0) - else: - print("Could not determine family name from config file!") +def read_config_file(path: Path) -> str: + try: + return path.read_text() + except Exception as e: + print(f"Error reading config file {path}: {e}", file=sys.stderr) sys.exit(1) -toggle = False -sources = [] -for line in data.splitlines(): - if re.match("^sources:", line): - toggle = True - continue - if toggle: - m = re.match(r"^\s*-\s*(.*)", line) - if m: - sources.append("sources/" + m[1]) +def extract_family_name(data: str) -> str | None: + match = re.search(r"(?m)^familyName:\s*(.*)", data) + return match.group(1).strip() if match else None + +def extract_sources(data: str) -> list[str]: + sources = [] + lines = data.splitlines() + inside_sources = False + + for line in lines: + if re.match(r"^sources:\s*$", line): + inside_sources = True + continue + if inside_sources: + match = re.match(r"^\s*-\s*(.+)", line) + if match: + sources.append(match.group(1).strip()) + else: + break + return sources + +def main() -> None: + parser = argparse.ArgumentParser(description="Parse config.yaml for family name or sources.") + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument("--sources", action="store_true", help="Print sources list") + group.add_argument("--family", action="store_true", help="Print family name") + args = parser.parse_args() + + config_path = Path("sources") / "config.yaml" + data = read_config_file(config_path) + + if args.family: + family_name = extract_family_name(data) + if family_name: + print(family_name) + return 0 else: - toggle = False -if sources: - print(" ".join(sources)) - sys.exit(0) -else: - print("Could not determine sources from config file!") - sys.exit(1) + print("Could not determine family name from config file!", file=sys.stderr) + return 1 + + sources = extract_sources(data) + if sources: + sources_with_prefix = [str(Path("sources") / src) for src in sources] + print(" ".join(sources_with_prefix)) + return 0 + else: + print("Could not determine sources from config file!", file=sys.stderr) + return 1 + +if __name__ == "__main__": + sys.exit(main())