1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)
|