Skip to content

Commit 80f10ed

Browse files
committed
python: refactor: derive ini key from dict key in config parser
Factor the repetitive per-key parsing in collect_ini_file_simulation_configs() into a local set_first() helper that derives the ini key from the dict key name (sim_time_limit -> sim-time-limit) and centralizes the first-wins guard. Each key is now a single line, and the key can no longer drift from its regex or lose its guard by copy-paste. No behavior change.
1 parent 60415d3 commit 80f10ed

1 file changed

Lines changed: 17 additions & 24 deletions

File tree

python/inet/simulation/project.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@ def create_config_dict(config):
326326
# for duplicate keys within a section. The real defaults (False / "DONE") are
327327
# applied at the consumption site further down.
328328
return {"config": config, "abstract": None, "emulation": None, "expected_result": None, "user_interface": None, "description": None, "network": None, "extends": None, "sim_time_limit": None}
329+
def set_first(config_dict, line, key, value=r"(.*)", convert=lambda s: s, allow_comment=False):
330+
# Derive the ini key ("sim_time_limit" -> "sim-time-limit") from the dict key so
331+
# the two can never drift, and keep only the FIRST occurrence within a section
332+
# (first-wins, matching OMNeT++'s resolution). allow_comment=True also matches a
333+
# commented-out (#...) directive line.
334+
prefix = r"#? *" if allow_comment else r" *"
335+
match = re.match(prefix + key.replace("_", "-") + r" *= *" + value, line)
336+
if match and config_dict.get(key) is None:
337+
config_dict[key] = convert(match.group(1))
329338
num_runs_fast_regex = re.compile(r"(?m).*^\s*(include\s+.*\.ini|repeat\s*=\s*[0-9]+|.*\$\{.*\})")
330339
configuration_class_regex = re.compile(r"\s*configuration-class\s*=\s*(\w+)")
331340
simulation_configs = []
@@ -342,31 +351,15 @@ def create_config_dict(config):
342351
config = match.group(2) or match.group(3)
343352
config_dict = create_config_dict(config)
344353
config_dicts[config] = config_dict
345-
match = re.match(r"#? *abstract *= *(\w+)", line)
346-
if match and config_dict.get("abstract") is None:
347-
config_dict["abstract"] = bool(match.group(1))
348-
match = re.match(r"#? *emulation *= *(\w+)", line)
349-
if match and config_dict.get("emulation") is None:
350-
config_dict["emulation"] = bool(match.group(1))
351-
match = re.match(r"#? *expected-result *= *\"(\w+)\"", line)
352-
if match and config_dict.get("expected_result") is None:
353-
config_dict["expected_result"] = match.group(1)
354+
set_first(config_dict, line, "abstract", r"(\w+)", bool, allow_comment=True)
355+
set_first(config_dict, line, "emulation", r"(\w+)", bool, allow_comment=True)
356+
set_first(config_dict, line, "expected_result", r"\"(\w+)\"", allow_comment=True)
354357
line = re.sub(r"(.*)#.*", "//1", line).strip()
355-
match = re.match(r" *extends *= *(\w+)", line)
356-
if match and config_dict.get("extends") is None:
357-
config_dict["extends"] = match.group(1)
358-
match = re.match(r" *user-interface *= \"*(\w+)\"", line)
359-
if match and config_dict.get("user_interface") is None:
360-
config_dict["user_interface"] = match.group(1)
361-
match = re.match(r"description *= *\"(.*)\"", line)
362-
if match and config_dict.get("description") is None:
363-
config_dict["description"] = match.group(1)
364-
match = re.match(r"network *= *(.*)", line)
365-
if match and config_dict.get("network") is None:
366-
config_dict["network"] = match.group(1)
367-
match = re.match(r"sim-time-limit *= *(.*)", line)
368-
if match and config_dict.get("sim_time_limit") is None:
369-
config_dict["sim_time_limit"] = match.group(1)
358+
set_first(config_dict, line, "extends", r"(\w+)")
359+
set_first(config_dict, line, "user_interface", r"\"*(\w+)\"")
360+
set_first(config_dict, line, "description", r"\"(.*)\"")
361+
set_first(config_dict, line, "network")
362+
set_first(config_dict, line, "sim_time_limit")
370363
general_config_dict = config_dicts["General"]
371364
for config, config_dict in config_dicts.items():
372365
config = config_dict["config"]

0 commit comments

Comments
 (0)