You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 975B

12345678910111213141516171819202122232425262728293031
  1. def parse_config(content: str) -> dict[str, str]:
  2. config: dict[str, str] = {}
  3. if not content:
  4. return config
  5. for line in content.splitlines():
  6. cleaned_line = line.strip()
  7. if not cleaned_line or cleaned_line.startswith(("#", "!")):
  8. continue
  9. separator_index = -1
  10. for i, c in enumerate(cleaned_line):
  11. if c in ("=", ":") and (i == 0 or cleaned_line[i - 1] != "\\"):
  12. separator_index = i
  13. break
  14. if separator_index == -1:
  15. continue
  16. key = cleaned_line[:separator_index].strip()
  17. raw_value = cleaned_line[separator_index + 1 :].strip()
  18. try:
  19. decoded_value = bytes(raw_value, "utf-8").decode("unicode_escape")
  20. decoded_value = decoded_value.replace(r"\=", "=").replace(r"\:", ":")
  21. except UnicodeDecodeError:
  22. decoded_value = raw_value
  23. config[key] = decoded_value
  24. return config