Implemented messaging system.
[aesalon.git] / newsource.py
blob0008d75ca6161f0b6c2754932d86ad85498b9297
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 {
53 """,
55 "cppClassDef":
56 """
57 class %(className)s {
58 public:
59 %(className)s();
60 ~%(className)s();
62 """,
63 "closeNamespace":
64 """
65 } // namespace %(namespace)s
66 """,
67 "cppHeaderEnd":
68 """
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.close()
138 class CGenerator(Generator):
139 def __init__(self):
140 Generator.__init__(self)
141 self.setName("c")
143 def generate(self, name):
144 CHeader().generate(name)
145 CSource().generate(name, True)
149 class CppHeader(Generator):
150 def __init__(self):
151 Generator.__init__(self)
152 self.setName("cppheader")
154 def generate(self, name):
155 path = name
156 path = path.replace("::", "/")
157 last = path.rfind("/")
158 if last != -1:
159 path = path[:last].lower() + path[last:]
160 path += ".h"
161 filepath = os.path.join(config["incBase"], path)
163 fp = file(filepath, "w")
165 includeGuard = config["guardBase"] + " " + name.replace("/", "_")
166 includeGuard = includeGuard.replace("::", "_")
167 while True:
168 index = includeGuard.find(" ")
169 if index == -1: break
171 includeGuard = includeGuard[:index] + includeGuard[index+1].upper() + includeGuard[index+2:]
173 includeGuard += "_H"
175 fileConfig = config
176 fileConfig["path"] = filepath
177 fileConfig["includeGuard"] = includeGuard
179 fp.write(config["fileHeader"] % fileConfig)
180 fp.write(config["cppHeader"] % fileConfig)
182 namespaceList = name.split("::")
183 if len(namespaceList) != 1:
184 className = namespaceList[-1]
185 namespaceList = namespaceList[:-1]
186 for namespace in namespaceList:
187 fileConfig["namespace"] = namespace
188 fp.write(fileConfig["openNamespace"] % fileConfig)
190 fileConfig["className"] = className
191 fp.write(fileConfig["cppClassDef"] % fileConfig)
193 for namespace in namespaceList[::-1]:
194 fileConfig["namespace"] = namespace
195 fp.write(fileConfig["closeNamespace"] % fileConfig)
197 fp.write(config["cppHeaderEnd"] % fileConfig)
199 fp.close()
201 return path
203 class CppSource(Generator):
204 def __init__(self):
205 Generator.__init__(self)
206 self.setName("cppsource")
208 def generate(self, name, incPath=""):
209 path = os.path.join(config["srcBase"], name)
210 path = path.replace("::", "/")
211 last = path.rfind("/")
212 if last != -1:
213 path = path[:last].lower() + path[last:]
214 path += ".cpp"
216 fp = file(path, "w")
218 fileConfig = config
219 fileConfig["path"] = path
220 fileConfig["incPath"] = incPath
222 fp.write(fileConfig["fileHeader"] % fileConfig)
223 if incPath != "":
224 fp.write(fileConfig["cSource"] % fileConfig)
226 namespaceList = name.split("::")
227 if len(namespaceList) != 1:
228 className = namespaceList[-1]
229 namespaceList = namespaceList[:-1]
230 for namespace in namespaceList:
231 fileConfig["namespace"] = namespace
232 fp.write(fileConfig["openNamespace"] % fileConfig)
234 fp.write("\n\n")
236 for namespace in namespaceList[::-1]:
237 fileConfig["namespace"] = namespace
238 fp.write(fileConfig["closeNamespace"] % fileConfig)
240 fp.close()
242 class CppGenerator(Generator):
243 def __init__(self):
244 Generator.__init__(self)
245 self.setName("cpp")
247 def generate(self, name):
248 incPath = CppHeader().generate(name)
249 CppSource().generate(name, incPath)
253 def main():
254 generatorMap = dict()
255 def addGenerator(generator):
256 generatorMap[generator.name()] = generator
258 addGenerator(CHeader())
259 addGenerator(CSource())
260 addGenerator(CGenerator())
262 addGenerator(CppHeader())
263 addGenerator(CppSource())
264 addGenerator(CppGenerator())
266 if len(sys.argv) < 3:
267 print "Usage:", sys.argv[0], "generator name1 [name2 name3 . . .]"
268 print "Where generator is one of:"
269 for g in generatorMap.values():
270 print "\t", g.name()
271 return
272 generator = generatorMap[sys.argv[1]]
274 for name in sys.argv[2:]:
275 generator.generate(name)
277 main()