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.

cfg_aux.py 1.6KB

3 mesi fa
3 mesi fa
3 mesi fa
3 mesi fa
3 mesi fa
3 mesi fa
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from cld import *
  2. # Extract static sprite name from config section
  3. def cfg_aux_staticSpriteName(
  4. sectionName: str
  5. ) -> str:
  6. prefix = f"static \""
  7. postfix = f"\""
  8. start = cld_len(prefix)
  9. end = cld_len(postfix)
  10. return sectionName[start:-end]
  11. # Extract texture name from config section
  12. def cfg_aux_textureName(
  13. sectionName: str
  14. ) -> str:
  15. prefix = f"texture \""
  16. postfix = f"\""
  17. start = cld_len(prefix)
  18. end = cld_len(postfix)
  19. return sectionName[start:-end]
  20. # Convert config contents to tree: sections -> keys -> values
  21. def cfg_aux_tree(
  22. cfgContents: [str]
  23. ) -> dict[str, dict[str, str]]:
  24. tree: dict[str, dict[str, str]] = {}
  25. currentSection = None
  26. n = cld_len(cfgContents)
  27. for i in range(0, n):
  28. line = cfgContents[i]
  29. # Section.
  30. if (
  31. cld_startswith(line, "[")
  32. ):
  33. currentSection = cfg_aux_treeCreateSection(tree, line)
  34. # Key = Value.
  35. else:
  36. cfg_aux_treeSetKeyValue(tree, currentSection, line)
  37. return tree
  38. # Create new section in the tree
  39. def cfg_aux_treeCreateSection(
  40. tree: dict[str, dict[str, str]],
  41. line: str
  42. ) -> str:
  43. # Strip characters `[` and `]` at both ends.
  44. sectionName = line[1:-1]
  45. tree[sectionName] = {}
  46. return sectionName
  47. # Register key-value pair in the tree
  48. def cfg_aux_treeSetKeyValue(
  49. tree: dict[str, dict[str, str]],
  50. sectionName: str,
  51. line: str
  52. ):
  53. parts = line.split(" = ")
  54. # Ignore invalid key-value pairs.
  55. if (
  56. cld_len(parts) != 2
  57. ):
  58. return
  59. key = parts[0]
  60. value = parts[1]
  61. tree[sectionName][key] = value