68 lines
1.6 KiB
Python
68 lines
1.6 KiB
Python
from cld import *
|
|
|
|
# Extract static sprite name from config section
|
|
def cfg_aux_staticSpriteName(
|
|
sectionName: str
|
|
) -> str:
|
|
prefix = f"static \""
|
|
postfix = f"\""
|
|
start = cld_len(prefix)
|
|
end = cld_len(postfix)
|
|
return sectionName[start:-end]
|
|
|
|
# Extract texture name from config section
|
|
def cfg_aux_textureName(
|
|
sectionName: str
|
|
) -> str:
|
|
prefix = f"texture \""
|
|
postfix = f"\""
|
|
start = cld_len(prefix)
|
|
end = cld_len(postfix)
|
|
return sectionName[start:-end]
|
|
|
|
# Convert config contents to tree: sections -> keys -> values
|
|
def cfg_aux_tree(
|
|
cfgContents: [str]
|
|
) -> dict[str, dict[str, str]]:
|
|
tree: dict[str, dict[str, str]] = {}
|
|
currentSection = None
|
|
n = cld_len(cfgContents)
|
|
for i in range(0, n):
|
|
line = cfgContents[i]
|
|
# Section.
|
|
if (
|
|
cld_startswith(line, "[")
|
|
):
|
|
currentSection = cfg_aux_treeCreateSection(tree, line)
|
|
# Key = Value.
|
|
else:
|
|
cfg_aux_treeSetKeyValue(tree, currentSection, line)
|
|
return tree
|
|
|
|
# Create new section in the tree
|
|
def cfg_aux_treeCreateSection(
|
|
tree: dict[str, dict[str, str]],
|
|
line: str
|
|
) -> str:
|
|
# Strip characters `[` and `]` at both ends.
|
|
sectionName = line[1:-1]
|
|
tree[sectionName] = {}
|
|
return sectionName
|
|
|
|
# Register key-value pair in the tree
|
|
def cfg_aux_treeSetKeyValue(
|
|
tree: dict[str, dict[str, str]],
|
|
sectionName: str,
|
|
line: str
|
|
):
|
|
parts = line.split(" = ")
|
|
# Ignore invalid key-value pairs.
|
|
if (
|
|
cld_len(parts) != 2
|
|
):
|
|
return
|
|
|
|
key = parts[0]
|
|
value = parts[1]
|
|
tree[sectionName][key] = value
|