Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/ntfc/coreconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def __init__(self, cfg: Dict[str, Any]) -> None:
# load ELF
self._elf = ElfParser(elf_path)

@staticmethod
def _parse_config_value(val: str) -> Union[bool, str, int]:
"""Parse a single Kconfig option value."""
if val == "y":
return True
if val == "n":
return False
if val.startswith('"') and val.endswith('"'):
# quoted strings first: they may contain hex digits
return val[1:-1]
if val.startswith("0x"):
try:
return int(val, 16)
except ValueError:
return val
if val.isdigit():
return int(val)
return val

def _load_core_config(self) -> None:
"""Load core configuration."""
with open(self._config["conf_path"], "r", encoding="utf-8") as f:
Expand All @@ -59,22 +78,7 @@ def _load_core_config(self) -> None:
# no '=' found — skip malformed line
continue

# parse option value
val_parsed: Union[bool, str, int]
if val == "y":
val_parsed = True
elif val == "n":
val_parsed = False
elif "0x" in val:
val_parsed = int(val.rstrip(), 16)
elif val.isdigit():
val_parsed = int(val)
elif val.startswith('"') and val.endswith('"'):
val_parsed = val[1:-1]
else:
val_parsed = val

self._kv_values[name] = val_parsed
self._kv_values[name] = self._parse_config_value(val)

@property
def uptime(self) -> Any:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_coreconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def test_load_core_config_value_types(tmp_path):
"CONFIG_BOOL_N=n\n"
'CONFIG_QUOTED="hello"\n'
"CONFIG_UNQUOTED=plain_text\n"
'CONFIG_QUOTED_HEX="{0x40000000,0x100}"\n'
"CONFIG_BAD_HEX=0xZZ\n"
)
conf = {"name": "dummy", "conf_path": str(cfg_file)}
p = CoreConfig(conf)
Expand All @@ -89,6 +91,10 @@ def test_load_core_config_value_types(tmp_path):
assert p.kv_check("CONFIG_BOOL_N") is False
assert p.kv_check("CONFIG_QUOTED") == "hello"
assert p.kv_check("CONFIG_UNQUOTED") == "plain_text"
# quoted values containing hex digits must stay strings
assert p.kv_check("CONFIG_QUOTED_HEX") == "{0x40000000,0x100}"
# unparsable hex falls back to the raw string
assert p.kv_check("CONFIG_BAD_HEX") == "0xZZ"


def test_core_config_flash_only_property():
Expand Down