-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassmaker.py
131 lines (90 loc) · 3.5 KB
/
classmaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
from optparse import OptionParser
import sys
import os
def parse_args(args):
parser = OptionParser()
parser.add_option("--root", default=".", help="Root path to create the class in")
parser.add_option("--filename", help="Path to the file, relative to root")
return parser.parse_args(args)
def pathSplitAll(path):
head, tail = os.path.split(path)
print("head: {0}, tail: {1}".format(head, tail))
if head:
head = pathSplitAll(head)
if tail:
head.append(tail)
print(head)
return head
else:
if tail:
return [tail]
else:
return []
def makeIncludeGuard(path_components):
pathcomponent = "__".join([x.upper() for x in path_components])
include_guard = "__{0}_H__".format(pathcomponent)
return include_guard
def makeInclude(path_components):
include = '#include "{0}"'.format(makeHeaderFilename(path_components))
return include
def makeBaseFilename(path_components):
return "/".join(path_components)
def makeHeaderFilename(path_components):
return "{0}.h".format(makeBaseFilename(path_components))
def makeCppFilename(path_components):
return "{0}.cpp".format(makeBaseFilename(path_components))
def makeNamespaces(path_components):
start = []
end = []
for elem in path_components:
start.append("namespace {0} {{".format(elem))
end.append("}} // namespace {0}".format(elem))
end.reverse()
start = "\n".join(start)
end = "\n".join(end)
return (start, end)
def makeClassDef(class_name):
return """/*
* \\brief Class {0}
*/
class {0} {{
public:
private:
}};""".format(class_name)
def main(args):
options, args = parse_args(args)
# remove comon prefix and sanitize
commonPath = os.path.commonpath((options.filename, options.root))
filename = options.filename[len(commonPath):]
filename = filename.strip(os.sep)
filename = filename.strip('/')
# Split all path components
path_components = pathSplitAll(filename)
filename = path_components[-1]
path_components = path_components[:-1]
# remove file extension
class_name, _ = os.path.splitext(filename)
# TODO: make class_name be CamelCase
print("Class name: {0}.Path: {1}".format(class_name, path_components))
namespaces_start, namespaces_end = makeNamespaces(path_components)
classDef = makeClassDef(class_name)
path_components.append(class_name)
include_guard = makeIncludeGuard(path_components)
include = makeInclude(path_components)
print("Include Guard: {0}".format(include_guard))
print("Include: {0}".format(include))
header_filename = os.path.join(options.root, makeHeaderFilename(path_components))
cpp_filename = os.path.join(options.root, makeCppFilename(path_components))
print("Header: {0}, CPP: {1}".format(header_filename, cpp_filename))
if (not os.path.exists(header_filename) and not os.path.exists(cpp_filename)):
print("Creating files")
with open(header_filename, 'w') as headerfile:
headerfile.write("""#ifndef {0}\n#define {0}\n\n{1}\n\n{3}\n\n{2}\n\n#endif // {0}\n""".format(include_guard, namespaces_start, namespaces_end, classDef))
with open(cpp_filename, 'w') as cppfile:
cppfile.write("""{0}\n\n{1}\n\n{2}\n""".format(include, namespaces_start, namespaces_end))
else:
print("At least one of the files already exists")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))