Kind-of worked on the R-Tree; not really enough time to do much.
[aesalon.git] / newsource.py
blob66b80966b9f7707d28c256b5667882a5c6283da7
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 import sys
5 import os
6 import string
8 config = {
9 "project": "Aesalon",
10 "desc": "a tool to visualize program behaviour in real time",
11 "authors": "Aesalon development team",
12 "license": "GNU GPLv3",
13 "devyears": "2009-2011",
14 "website": "http://aesalon.berlios.de/",
15 "srcBase": "src",
16 "incBase": "include",
17 "guardBase": "Aesalon",
18 "fileHeader":
19 """/** %(project)s, %(desc)s.
20 Copyright (C) %(devyears)s, %(authors)s.
22 %(project)s is distributed under the terms of the %(license)s. See
23 the included file LICENSE for more information.
25 @file %(path)s
27 """,
28 "cHeader":
29 """
30 #ifndef %(includeGuard)s
31 #define %(includeGuard)s
35 #endif
36 """,
38 "cSource":
39 """
40 #include "%(incPath)s"
43 """,
44 "cppHeader":
45 """
46 #ifndef %(includeGuard)s
47 #define %(includeGuard)s
49 """,
50 "openNamespace":
51 """
52 namespace %(namespace)s {""",
54 "cppClassDef":
55 """
57 class %(className)s {
58 public:
59 %(className)s();
60 ~%(className)s();
62 """,
63 "closeNamespace":
64 """
65 } // namespace %(namespace)s""",
66 "cppHeaderEnd":
67 """
69 #endif
70 """
73 class Generator:
74 def __init__(self):
75 self.__name = "Unknown name"
76 pass
78 def name(self):
79 return self.__name
80 def setName(self, name):
81 self.__name = name
83 def generate(self, name):
84 pass
86 class CHeader(Generator):
87 def __init__(self):
88 Generator.__init__(self)
89 self.setName("cheader")
91 def generate(self, name):
92 path = name
93 path += ".h"
95 fp = file(os.path.join(config["incBase"], path), "w")
97 includeGuard = config["guardBase"] + " " + name.replace("/", " ")
98 while True:
99 index = includeGuard.find(" ")
100 if index == -1: break
102 includeGuard = includeGuard[:index] + includeGuard[index+1].upper() + includeGuard[index+2:]
104 includeGuard += "_H"
106 fileConfig = config
107 fileConfig["path"] = path
108 fileConfig["includeGuard"] = includeGuard
110 fp.write(config["fileHeader"] % fileConfig)
111 fp.write(config["cHeader"] % fileConfig)
113 fp.close()
115 return path
117 class CSource(Generator):
118 def __init__(self):
119 Generator.__init__(self)
120 self.setName("csource")
122 def generate(self, name, incPath=""):
123 path = os.path.join(config["srcBase"], name)
124 path += ".c"
126 fp = file(path, "w")
128 fileConfig = config
129 fileConfig["path"] = path
130 fileConfig["incPath"] = incPath
132 fp.write(fileConfig["fileHeader"] % fileConfig)
133 if incPath != "":
134 fp.write(fileConfig["cSource"] % fileConfig)
136 fp.write("\n")
138 fp.close()
140 class CGenerator(Generator):
141 def __init__(self):
142 Generator.__init__(self)
143 self.setName("c")
145 def generate(self, name):
146 CHeader().generate(name)
147 CSource().generate(name, True)
151 class CppHeader(Generator):
152 def __init__(self):
153 Generator.__init__(self)
154 self.setName("cppheader")
156 def generate(self, name):
157 path = name
158 path = path.replace("::", "/")
159 last = path.rfind("/")
160 if last != -1:
161 path = path[:last].lower() + path[last:]
162 path += ".h"
163 filepath = os.path.join(config["incBase"], path)
165 fp = file(filepath, "w")
167 includeGuard = config["guardBase"] + " " + name.replace("/", "_")
168 includeGuard = includeGuard.replace("::", "_")
169 while True:
170 index = includeGuard.find(" ")
171 if index == -1: break
173 includeGuard = includeGuard[:index] + includeGuard[index+1].upper() + includeGuard[index+2:]
175 includeGuard += "_H"
177 fileConfig = config
178 fileConfig["path"] = filepath
179 fileConfig["includeGuard"] = includeGuard
181 fp.write(config["fileHeader"] % fileConfig)
182 fp.write(config["cppHeader"] % fileConfig)
184 namespaceList = name.split("::")
185 if len(namespaceList) != 1:
186 className = namespaceList[-1]
187 namespaceList = namespaceList[:-1]
188 for namespace in namespaceList:
189 fileConfig["namespace"] = namespace
190 fp.write(fileConfig["openNamespace"] % fileConfig)
192 fileConfig["className"] = className
193 fp.write(fileConfig["cppClassDef"] % fileConfig)
195 for namespace in namespaceList[::-1]:
196 fileConfig["namespace"] = namespace
197 fp.write(fileConfig["closeNamespace"] % fileConfig)
199 fp.write(config["cppHeaderEnd"] % fileConfig)
201 fp.close()
203 return path
205 class CppSource(Generator):
206 def __init__(self):
207 Generator.__init__(self)
208 self.setName("cppsource")
210 def generate(self, name, incPath=""):
211 path = os.path.join(config["srcBase"], name)
212 path = path.replace("::", "/")
213 last = path.rfind("/")
214 if last != -1:
215 path = path[:last].lower() + path[last:]
216 path += ".cpp"
218 fp = file(path, "w")
220 fileConfig = config
221 fileConfig["path"] = path
222 fileConfig["incPath"] = incPath
224 fp.write(fileConfig["fileHeader"] % fileConfig)
225 if incPath != "":
226 fp.write(fileConfig["cSource"] % fileConfig)
228 namespaceList = name.split("::")
229 if len(namespaceList) != 1:
230 className = namespaceList[-1]
231 namespaceList = namespaceList[:-1]
232 for namespace in namespaceList:
233 fileConfig["namespace"] = namespace
234 fp.write(fileConfig["openNamespace"] % fileConfig)
236 fp.write("\n\n")
238 for namespace in namespaceList[::-1]:
239 fileConfig["namespace"] = namespace
240 fp.write(fileConfig["closeNamespace"] % fileConfig)
242 fp.close()
244 class CppGenerator(Generator):
245 def __init__(self):
246 Generator.__init__(self)
247 self.setName("cpp")
249 def generate(self, name):
250 incPath = CppHeader().generate(name)
251 CppSource().generate(name, incPath)
255 def main():
256 generatorMap = dict()
257 def addGenerator(generator):
258 generatorMap[generator.name()] = generator
260 addGenerator(CHeader())
261 addGenerator(CSource())
262 addGenerator(CGenerator())
264 addGenerator(CppHeader())
265 addGenerator(CppSource())
266 addGenerator(CppGenerator())
268 if len(sys.argv) < 3:
269 print "Usage:", sys.argv[0], "generator name1 [name2 name3 . . .]"
270 print "Where generator is one of:"
271 for g in generatorMap.values():
272 print "\t", g.name()
273 return
274 generator = generatorMap[sys.argv[1]]
276 for name in sys.argv[2:]:
277 generator.generate(name)
279 main()