MakerSkillTree/python/create_skill_tree_svg.py
gromenauer e70562051b Create skill tree from yaml
Added a rough python script that reads from a yaml
file (see input.yml example) and creates a svg
skill tree.
2024-08-29 02:06:32 +02:00

67 lines
1.7 KiB
Python
Executable file

#!/bin/env python
import argparse
import yaml
def load_yaml(file_path):
with open(file_path) as file:
return yaml.safe_load(file)
def load_svg_template(file_path):
with open(file_path) as file:
return file.read()
def process_svg(template, data):
# Replace title and author
processed = template.replace("{{ title }}", data["title"])
processed = processed.replace("{{ author }}", data["author"])
displacement = (0, 9, 19, 29, 39, 49, 59, 69)
for i in range(10):
for j in range(7):
if i < 9:
box_number = i + displacement[j]
elif i == 9:
box_number = i + displacement[j + 1]
if j >= 5:
continue
value = data["row"][i][j]
placeholder = f"{{{{ box_{box_number:03d} }}}}"
processed = processed.replace(placeholder, str(value))
return processed
def save_processed_svg(content, output_path):
with open(output_path, "w") as file:
file.write(content)
def main():
svg_template_path = "skill_tree_template.svg.j2"
parser = argparse.ArgumentParser(description="Process SVG template with YAML data.")
parser.add_argument("input_yaml", help="Path to input YAML file")
parser.add_argument("output_svg", help="Path to output processed SVG file")
args = parser.parse_args()
# Load YAML data
yaml_data = load_yaml(args.input_yaml)
# Load SVG template
svg_template = load_svg_template(svg_template_path)
# Process SVG
processed_svg = process_svg(svg_template, yaml_data)
# Save processed SVG
save_processed_svg(processed_svg, args.output_svg)
print(f"Processed SVG saved to {args.output_svg}")
if __name__ == "__main__":
main()