Skip to content

Commit ed29ca8

Browse files
committed
Support enum in params.
1 parent 171f28d commit ed29ca8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "xrobot"
7-
version = "0.1.2"
7+
version = "0.1.3"
88
description = "A lightweight application framework for robot module management, lifecycle control, and hardware abstraction."
99
requires-python = ">=3.8"
1010
authors = [

src/xrobot/GenerateMain.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import re
12
import yaml
23
import argparse
34
from pathlib import Path
45
from collections import OrderedDict
56
from typing import Union, Dict, List
7+
from yaml.representer import SafeRepresenter
8+
yaml.add_representer(OrderedDict, SafeRepresenter.represent_dict)
69

710

811
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:
5659
elif isinstance(value, bool):
5760
return "true" if value else "false"
5861
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}"'
6066
else:
6167
return str(value)
6268

@@ -93,10 +99,10 @@ def extract_constructor_args(modules: List[str], module_dir: Path, config_path:
9399
args_ordered.update(item)
94100

95101
# 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+
]))
100106

101107
print(f"[INFO] Writing configuration to {config_path}")
102108

@@ -198,7 +204,11 @@ def main():
198204
print(f"[WARN] Configuration file not found: {config_path}")
199205
config_data = extract_constructor_args(args.modules, Path("Modules"), config_path)
200206
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"))
202212

203213
# Code generation
204214
output_code = generate_xrobot_main_code(args.hw, args.modules, config_data)

0 commit comments

Comments
 (0)