|
| 1 | +import re |
1 | 2 | import yaml
|
2 | 3 | import argparse
|
3 | 4 | from pathlib import Path
|
4 | 5 | from collections import OrderedDict
|
5 | 6 | from typing import Union, Dict, List
|
| 7 | +from yaml.representer import SafeRepresenter |
| 8 | +yaml.add_representer(OrderedDict, SafeRepresenter.represent_dict) |
6 | 9 |
|
7 | 10 |
|
8 | 11 | def parse_manifest_from_header(header_path: Path) -> Dict:
|
@@ -56,7 +59,10 @@ def _format_cpp_value(value: Union[dict, list, str, int, float, bool]) -> str:
|
56 | 59 | elif isinstance(value, bool):
|
57 | 60 | return "true" if value else "false"
|
58 | 61 | elif isinstance(value, str):
|
59 |
| - return f'"{value}"' |
| 62 | + if re.match(r"^[A-Za-z_][\w:<>\s,]*::[A-Za-z_][\w:]*$", value): |
| 63 | + return value |
| 64 | + else: |
| 65 | + return f'"{value}"' |
60 | 66 | else:
|
61 | 67 | return str(value)
|
62 | 68 |
|
@@ -93,10 +99,10 @@ def extract_constructor_args(modules: List[str], module_dir: Path, config_path:
|
93 | 99 | args_ordered.update(item)
|
94 | 100 |
|
95 | 101 | # Append as new module instance
|
96 |
| - output["modules"].append({ |
97 |
| - "name": mod, |
98 |
| - "constructor_args": dict(args_ordered) |
99 |
| - }) |
| 102 | + output["modules"].append(OrderedDict([ |
| 103 | + ("name", mod), |
| 104 | + ("constructor_args", args_ordered) |
| 105 | + ])) |
100 | 106 |
|
101 | 107 | print(f"[INFO] Writing configuration to {config_path}")
|
102 | 108 |
|
@@ -198,7 +204,11 @@ def main():
|
198 | 204 | print(f"[WARN] Configuration file not found: {config_path}")
|
199 | 205 | config_data = extract_constructor_args(args.modules, Path("Modules"), config_path)
|
200 | 206 | else:
|
201 |
| - config_data = extract_constructor_args(args.modules, Path("Modules"), Path("User/xrobot.yaml")) |
| 207 | + config_path = Path("User/xrobot.yaml") |
| 208 | + if config_path.exists(): |
| 209 | + config_data = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {} |
| 210 | + else: |
| 211 | + config_data = extract_constructor_args(args.modules, Path("Modules"), Path("User/xrobot.yaml")) |
202 | 212 |
|
203 | 213 | # Code generation
|
204 | 214 | output_code = generate_xrobot_main_code(args.hw, args.modules, config_data)
|
|
0 commit comments