Started experimenting with perl/python scripts to auto rename incl guards etc.
[aesalon.git] / newsource.py
bloba658a65e83bd6e78d26509c33a3076862009bca2
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 """
20 /** %(project)s, %(desc)s.
21 Copyright (C) %(devyears)s, %(authors)s.
23 %(project)s is distributed under the terms of the %(license)s. See
24 the included file LICENSE for more information.
26 @file %(path)s
28 """,
29 "cHeader":
30 """
31 #ifndef %(includeGuard)s
32 #define %(includeGuard)s
36 #endif
37 """,
39 "cSource":
40 """
41 #include "%(incPath)s"
44 """,
45 "cppHeader":
46 """
47 #ifndef %(includeGuard)s
48 #define %(includeGuard)s
50 """,
51 "openNamespace":
52 """
53 namespace %(namespace)s {
54 """,
56 "cppClassDef":
57 """
58 class %(className)s {
59 public:
60 %(className)s();
61 ~%(className)s();
63 """,
64 "closeNamespace":
65 """
66 } // namespace %(namespace)s
67 """,
68 "cppHeaderEnd":
69 """
70 #endif
72 """
75 class Generator:
76 def __init__(self):
77 self.__name = "Unknown name"
78 pass
80 def name(self):
81 return self.__name
82 def setName(self, name):
83 self.__name = name
85 def generate(self, name):
86 pass
88 class CHeader(Generator):
89 def __init__(self):
90 Generator.__init__(self)
91 self.setName("cheader")
93 def generate(self, name):
94 path = name
95 path += ".h"
97 fp = file(os.path.join(config["incBase"], path), "w")
99 includeGuard = config["guardBase"] + " " + name.replace("/", " ")
100 while True:
101 index = includeGuard.find(" ")
102 if index == -1: break
104 includeGuard = includeGuard[:index] + includeGuard[index+1].upper() + includeGuard[index+2:]
106 includeGuard += "_H"
108 fileConfig = config
109 fileConfig["path"] = path
110 fileConfig["includeGuard"] = includeGuard
112 fp.write(config["fileHeader"] % fileConfig)
113 fp.write(config["cHeader"] % fileConfig)
115 fp.close()
117 return path
119 class CSource(Generator):
120 def __init__(self):
121 Generator.__init__(self)
122 self.setName("csource")
124 def generate(self, name, incPath=""):
125 path = os.path.join(config["srcBase"], name)
126 path += ".c"
128 fp = file(path, "w")
130 fileConfig = config
131 fileConfig["path"] = path
132 fileConfig["incPath"] = incPath
134 fp.write(fileConfig["fileHeader"] % fileConfig)
135 if incPath != "":
136 fp.write(fileConfig["cSource"] % fileConfig)
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")
166 fp = sys.stdout
168 includeGuard = config["guardBase"] + " " + name.replace("/", "_")
169 includeGuard = includeGuard.replace("::", "_")
170 while True:
171 index = includeGuard.find(" ")
172 if index == -1: break
174 includeGuard = includeGuard[:index] + includeGuard[index+1].upper() + includeGuard[index+2:]
176 includeGuard += "_H"
178 fileConfig = config
179 fileConfig["path"] = filepath
180 fileConfig["includeGuard"] = includeGuard
182 fp.write(config["fileHeader"] % fileConfig)
183 fp.write(config["cppHeader"] % fileConfig)
185 namespaceList = name.split("::")
186 if len(namespaceList) != 1:
187 className = namespaceList[-1]
188 namespaceList = namespaceList[:-1]
189 for namespace in namespaceList:
190 fileConfig["namespace"] = namespace
191 fp.write(fileConfig["openNamespace"] % fileConfig)
193 fileConfig["className"] = className
194 fp.write(fileConfig["cppClassDef"] % fileConfig)
196 for namespace in namespaceList[::-1]:
197 fileConfig["namespace"] = namespace
198 fp.write(fileConfig["closeNamespace"] % fileConfig)
200 fp.write(config["cppHeaderEnd"] % fileConfig)
202 #fp.close()
204 return path
206 class CppSource(Generator):
207 def __init__(self):
208 Generator.__init__(self)
209 self.setName("cppsource")
211 def generate(self, name, incPath=""):
212 path = os.path.join(config["srcBase"], name)
213 path = path.replace("::", "/")
214 last = path.rfind("/")
215 if last != -1:
216 path = path[:last].lower() + path[last:]
217 path += ".cpp"
219 fp = file(path, "w")
221 fileConfig = config
222 fileConfig["path"] = path
223 fileConfig["incPath"] = incPath
225 fp.write(fileConfig["fileHeader"] % fileConfig)
226 if incPath != "":
227 fp.write(fileConfig["cSource"] % fileConfig)
229 fp.close()
231 class CppGenerator(Generator):
232 def __init__(self):
233 Generator.__init__(self)
234 self.setName("cpp")
236 def generate(self, name):
237 incPath = CppHeader().generate(name)
238 CppSource().generate(name, incPath)
242 def main():
243 generatorMap = dict()
244 def addGenerator(generator):
245 generatorMap[generator.name()] = generator
247 addGenerator(CHeader())
248 addGenerator(CSource())
249 addGenerator(CGenerator())
251 addGenerator(CppHeader())
252 addGenerator(CppSource())
253 addGenerator(CppGenerator())
255 if len(sys.argv) < 3:
256 print "Usage:", sys.argv[0], "generator name1 [name2 name3 . . .]"
257 print "Where generator is one of:"
258 for g in generatorMap.values():
259 print "\t", g.name()
260 return
261 generator = generatorMap[sys.argv[1]]
263 for name in sys.argv[2:]:
264 generator.generate(name)
266 main()