選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cfg_aux.py 1.1KB

3ヶ月前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from cld import *
  2. # Convert config contents to tree: sections -> keys -> values
  3. def cfg_aux_tree(
  4. cfgContents: [str]
  5. ) -> dict[str, dict[str, str]]:
  6. tree: dict[str, dict[str, str]] = {}
  7. currentSection = None
  8. n = cld_len(cfgContents)
  9. for i in range(0, n):
  10. line = cfgContents[i]
  11. # Section.
  12. if (
  13. cld_startswith(line, "[")
  14. ):
  15. currentSection = cfg_aux_treeCreateSection(tree, line)
  16. # Key = Value.
  17. else:
  18. cfg_aux_treeSetKeyValue(tree, currentSection, line)
  19. return tree
  20. # Create new section in the tree
  21. def cfg_aux_treeCreateSection(
  22. tree: dict[str, dict[str, str]],
  23. line: str
  24. ) -> str:
  25. # Strip characters `[` and `]` at both ends.
  26. sectionName = line[1:-1]
  27. tree[sectionName] = {}
  28. return sectionName
  29. # Register key-value pair in the tree
  30. def cfg_aux_treeSetKeyValue(
  31. tree: dict[str, dict[str, str]],
  32. sectionName: str,
  33. line: str
  34. ):
  35. parts = line.split(" = ")
  36. # Ignore invalid key-value pairs.
  37. if (
  38. cld_len(parts) != 2
  39. ):
  40. return
  41. key = parts[0]
  42. value = parts[1]
  43. tree[sectionName][key] = value