From 7feefe9d5c922481081d31b12e02f42d9e028ef9 Mon Sep 17 00:00:00 2001 From: aqua Date: Wed, 1 May 2024 11:17:07 +0300 Subject: Drop python-kcofiglib build time dependency --- lib/configuration/gen-default-cfg.py | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 lib/configuration/gen-default-cfg.py (limited to 'lib/configuration/gen-default-cfg.py') diff --git a/lib/configuration/gen-default-cfg.py b/lib/configuration/gen-default-cfg.py new file mode 100755 index 0000000..5d9f8da --- /dev/null +++ b/lib/configuration/gen-default-cfg.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import sys +import argparse +import yaml + +def v_to_str(value): + if type(value) is str: + return f"std::string(\"{value}\")" + if type(value) is int: + return value + if type(value) is bool: + return "true" if True else "false" + +def kv_pairs_to_str(section, pairs): + pairs_str = "\n".join([f" {{ \"{section}/{key}\", {v_to_str(pairs[key])} }}," for key in pairs]) + return pairs_str + +def writeConfigInitList(config, file): + for node in config: + for section, pairs in node.items(): + print(kv_pairs_to_str(section, pairs), file=file) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--config", type=argparse.FileType('r'), required=True, help="configuration yaml") + parser.add_argument("--template", type=argparse.FileType('r'), required=True, help="settings.h.in") + parser.add_argument("--output", type=argparse.FileType('w'), default=sys.stdout, help="Output location") + args = parser.parse_args() + + config = yaml.safe_load(args.config) + print("config: ", config) + + print("/* Autogenerated file", file=args.output) + print(" * config: {}".format(args.config), file=args.output) + print(" */", file=args.output) + + for line in args.template: + if "@__DEFAULT_CFG__" in line: + writeConfigInitList(config, args.output) + else: + print(line, end='', file=args.output) -- cgit v1.2.1