Python example: mrpt_config_example.py
Reads and writes INI-like configuration files and YAML with mrpt.config.
Modules: mrpt.config
1#!/usr/bin/env python3
2"""
3Reads and writes INI-like configuration files and YAML with mrpt.config.
4
5Demonstrates:
6 - CConfigFileMemory: in-memory INI-style configuration (no file I/O)
7 - CConfigFile: file-backed configuration (written to a temp file)
8 - read_double / read_int / read_string / read_bool: typed getters
9 - write: typed setters
10 - sections / keys: enumeration
11 - setContentFromYAML / getContentAsYAML: YAML round-trip
12 - config_parser: pre-processing of config text
13"""
14
15import os
16import tempfile
17from mrpt.config import CConfigFile, CConfigFileMemory, config_parser
18
19# ── CConfigFileMemory — no file I/O ─────────────────────────────────────────
20print("── CConfigFileMemory ───────────────────────")
21ini = """
22[General]
23AppName = MRPTDemo
24Version = 3.0
25Debug = true
26
27[Camera]
28FocalLength = 1200.5
29Width = 640
30Height = 480
31"""
32cfg = CConfigFileMemory(ini)
33
34app = cfg.read_string("General", "AppName", "")
35ver = cfg.read_double("General", "Version", 0.0)
36dbg = cfg.read_bool ("General", "Debug", False)
37fl = cfg.read_double("Camera", "FocalLength", 0.0)
38w = cfg.read_int ("Camera", "Width", 0)
39
40print(f"AppName = {app!r}")
41print(f"Version = {ver}")
42print(f"Debug = {dbg}")
43print(f"FocalLength = {fl}")
44print(f"Width = {w}")
45
46assert app == "MRPTDemo"
47assert abs(ver - 3.0) < 1e-9
48assert dbg is True
49assert abs(fl - 1200.5) < 1e-6
50assert w == 640
51print(" typed reads ✓")
52
53# Section / key enumeration
54sections = cfg.getAllSections()
55print(f"\nSections: {sections}")
56assert "General" in sections and "Camera" in sections
57
58keys_gen = cfg.getAllKeys("General")
59print(f"Keys in [General]: {keys_gen}")
60assert "AppName" in keys_gen
61
62assert cfg.sectionExists("Camera")
63assert cfg.keyExists("General", "Version")
64assert not cfg.sectionExists("Lidar")
65print(" sectionExists / keyExists ✓")
66
67# Modify a value and read it back
68cfg.write("Camera", "Width", 1280)
69w2 = cfg.read_int("Camera", "Width", 0)
70print(f"\nAfter write, Width = {w2}")
71assert w2 == 1280
72print(" write ✓")
73
74# YAML round-trip
75yaml_str = cfg.getContentAsYAML()
76print(f"\ngetContentAsYAML() (excerpt):\n{yaml_str[:200]}")
77
78cfg2 = CConfigFileMemory()
79cfg2.setContentFromYAML(yaml_str)
80assert cfg2.read_string("General", "AppName", "") == "MRPTDemo"
81print(" YAML round-trip ✓")
82
83# clear()
84cfg.clear()
85assert not cfg.sectionExists("General")
86print(" clear() ✓")
87
88# ── CConfigFile — file-backed ────────────────────────────────────────────────
89print("\n── CConfigFile (temp file) ─────────────────")
90tmp_fd, tmp_path = tempfile.mkstemp(suffix=".ini", prefix="mrpt_cfg_test_")
91os.close(tmp_fd)
92try:
93 fc = CConfigFile(tmp_path)
94 fc.write("Sensor", "Type", "Lidar")
95 fc.write("Sensor", "Range", 100.0)
96 fc.writeNow() # flush to disk immediately
97
98 # Re-open and read back
99 fc2 = CConfigFile(tmp_path)
100 stype = fc2.read_string("Sensor", "Type", "")
101 srange = fc2.read_double("Sensor", "Range", 0.0)
102 print(f"Read back: Type={stype!r}, Range={srange}")
103 assert stype == "Lidar"
104 assert abs(srange - 100.0) < 1e-9
105 print(" CConfigFile write + read ✓")
106 print(f" (temp file: {tmp_path})")
107finally:
108 os.unlink(tmp_path)
109
110# ── config_parser ────────────────────────────────────────────────────────────
111print("\n── config_parser ───────────────────────────")
112# config_parser substitutes ${VAR} with values defined via @define.
113raw = "@define MY_VAR 42\n[Section]\nvalue = ${MY_VAR}\n"
114parsed = config_parser(raw)
115print(f"config_parser result:\n{parsed}")
116assert "42" in parsed
117print(" config_parser ran without error ✓")
118
119print("\nAll checks ✓")