Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
create_class.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# This file is part of the Rendering Engine project.
3# Author: Alexander Obzherin <alexanderobzherin@gmail.com>
4# Copyright (c) 2026 Alexander Obzherin
5# Distributed under the terms of the zlib License. See LICENSE.md for details.
6##
7
8import os
9import sys
10import re
11
12SUPPORTED_BASES = {
13 "Actor": "actors_based",
14 "Actor2D": "actor_2d_based",
15 "Scene": "scene_based"
16}
17
18
20 result = ""
21 for i, c in enumerate(name):
22 if c.isupper() and i != 0 and not name[i-1].isupper():
23 result += "_"
24 result += c
25 return result.lower()
26
27
28def main():
29 if len(sys.argv) != 3:
30 print("Usage: create_class.py <Actor|Actor2D|Scene> <ClassName>")
31 sys.exit(1)
32
33 base_type = sys.argv[1]
34 class_name = sys.argv[2]
35
36 if base_type not in SUPPORTED_BASES:
37 print(f"Error: Unsupported base type '{base_type}'")
38 print("Supported types: Actor, Actor2D, Scene")
39 sys.exit(1)
40
41 # Resolve paths independent of execution directory
42 script_dir = os.path.dirname(os.path.abspath(__file__))
43 project_root = os.path.abspath(os.path.join(script_dir, ".."))
44 template_dir = os.path.join(script_dir, "Templates")
45 include_dir = os.path.join(project_root, "Include")
46 source_dir = os.path.join(project_root, "Source")
47
48 if not os.path.isdir(include_dir) or not os.path.isdir(source_dir):
49 print("Error: Include/ or Source/ directory not found.")
50 print("Make sure you are inside a valid RenderingEngine project.")
51 sys.exit(1)
52
53 template_prefix = SUPPORTED_BASES[base_type]
54
55 header_template = os.path.join(template_dir, template_prefix + ".hpp.in")
56 source_template = os.path.join(template_dir, template_prefix + ".cpp.in")
57
58 if not os.path.isfile(header_template) or not os.path.isfile(source_template):
59 print("Error: Template files not found.")
60 sys.exit(1)
61
62 class_file_name = pascal_to_snake(class_name)
63
64 header_output = os.path.join(include_dir, class_file_name + ".hpp")
65 source_output = os.path.join(source_dir, class_file_name + ".cpp")
66
67 if os.path.exists(header_output) or os.path.exists(source_output):
68 print("Error: Class files already exist.")
69 sys.exit(1)
70
71 # Process header template
72 with open(header_template, "r") as f:
73 header_content = f.read()
74
75 header_content = header_content.replace("@CLASS_NAME@", class_name)
76 header_content = header_content.replace("@CLASS_FILE_NAME@", class_file_name)
77
78 with open(header_output, "w") as f:
79 f.write(header_content)
80
81 # Process source template
82 with open(source_template, "r") as f:
83 source_content = f.read()
84
85 source_content = source_content.replace("@CLASS_NAME@", class_name)
86 source_content = source_content.replace("@CLASS_FILE_NAME@", class_file_name)
87
88 with open(source_output, "w") as f:
89 f.write(source_content)
90
91 print(f"Successfully created class '{class_name}'")
92 print(f" Header: {header_output}")
93 print(f" Source: {source_output}")
94
95
96if __name__ == "__main__":
97 main()
def pascal_to_snake(name)
Definition: create_class.py:19