Files
Technical-Assignment-CQG-Br…/config_loader.py
Martin Stajnko dc401be837 fix: reject a configuration entry with an empty value1
A line such as "=z" parsed into the pair ("", "z"). An empty value1
matches at every position and consumes nothing, so the scan in
replace_line never advanced and the program looped forever, printing
nothing and growing its output buffer until it was killed.

Reject the entry while parsing, reporting the offending line the same
way as a line with no "=" at all, and document the termination
invariant the loop depends on.
2026-08-26 14:35:32 +02:00

32 lines
1014 B
Python

"""Configuration file parsing."""
def load_config(filename: str) -> list[tuple[str, str]]:
"""Load value1=value2 pairs from the configuration file."""
pairs = []
with open(filename, "r", encoding="utf-8") as file:
for line_number, line in enumerate(file, start=1):
line = line.rstrip("\n\r")
if not line:
continue
if "=" not in line:
raise ValueError(
f"Invalid configuration at line {line_number}: {line!r}"
)
value1, value2 = line.split("=", 1)
# An empty value1 would match at every position without
# consuming anything, leaving replace_line unable to advance.
if not value1:
raise ValueError(
f"Invalid configuration at line {line_number}: {line!r} "
f"(the value to replace must not be empty)"
)
pairs.append((value1, value2))
return pairs