comparison src/bridge/bridge_constructor/bridge_contructor.py @ 271:0288f97334f2

bridge: constructor now generate files in a 'generated' subdirectory
author Goffi <goffi@goffi.org>
date Mon, 24 Jan 2011 21:48:09 +0100
parents bdcd535e179e
children 3b5e856b3a32
comparison
equal deleted inserted replaced
270:4005bf3ce35d 271:0288f97334f2
21 21
22 22
23 #consts 23 #consts
24 NAME = u"bridge_constructor" 24 NAME = u"bridge_constructor"
25 VERSION="0.1.0" 25 VERSION="0.1.0"
26 DEST_DIR="generated"
26 ABOUT = NAME+u""" v%s (c) Jérôme Poisson (aka Goffi) 2011 27 ABOUT = NAME+u""" v%s (c) Jérôme Poisson (aka Goffi) 2011
27 28
28 --- 29 ---
29 """+NAME+u""" Copyright (C) 2011 Jérôme Poisson (aka Goffi) 30 """+NAME+u""" Copyright (C) 2011 Jérôme Poisson (aka Goffi)
30 This program comes with ABSOLUTELY NO WARRANTY; 31 This program comes with ABSOLUTELY NO WARRANTY;
155 156
156 def generateCoreSide(self): 157 def generateCoreSide(self):
157 """create the constructor in SàT core side (backend)""" 158 """create the constructor in SàT core side (backend)"""
158 raise NotImplementedError 159 raise NotImplementedError
159 160
161 def generateFrontendSide(self):
162 """create the constructor in SàT frontend side"""
163 raise NotImplementedError
164
165 def finalWrite(self, filename, file_buf):
166 """Write the final generated file in DEST_DIR/filename
167 @param filename: name of the file to generate
168 @param file_buf: list of lines (stings) of the file"""
169 if os.path.exists(DEST_DIR) and not os.path.isdir(DEST_DIR):
170 print ("The destination dir [%s] can't be created: a file with this name already exists !")
171 sys.exit(1)
172 try:
173 if not os.path.exists(DEST_DIR):
174 os.mkdir(DEST_DIR)
175 full_path=os.path.join(DEST_DIR,filename)
176 if os.path.exists(full_path) and not self.options.force:
177 print ("The destination file [%s] already exists ! Use --force to overwrite it" % full_path)
178 try:
179 with open(full_path,'w') as dest_file:
180 dest_file.write('\n'.join(file_buf))
181 except IOError:
182 print ("Can't open destination file [%s]" % full_path)
183 except OSError:
184 print("It's not possible to generate the file, check your permissions")
185 exit(1)
186
160 class DbusConstructor(Constructor): 187 class DbusConstructor(Constructor):
161 188
162 def __init__(self, bridge_template, options): 189 def __init__(self, bridge_template, options):
163 Constructor.__init__(self, bridge_template, options) 190 Constructor.__init__(self, bridge_template, options)
164 self.core_template="dbus_core_template.py" 191 self.core_template="dbus_core_template.py"
224 except IOError: 251 except IOError:
225 print ("Can't open template file [%s]" % self.core_template) 252 print ("Can't open template file [%s]" % self.core_template)
226 sys.exit(1) 253 sys.exit(1)
227 254
228 #now we write to final file 255 #now we write to final file
229 if os.path.exists(self.core_dest) and not self.options.force: 256 self.finalWrite(self.core_dest, core_bridge)
230 print ("The destination file [%s] already exists ! Use --force to overwrite it" % self.core_dest)
231 try:
232 with open(self.core_dest,'w') as dest_file:
233 dest_file.write('\n'.join(core_bridge))
234 except IOError:
235 print ("Can't open destination file [%s]" % self.core_dest)
236 257
237 def generateFrontendSide(self): 258 def generateFrontendSide(self):
238 methods_part = [] 259 methods_part = []
239 sections = self.bridge_template.sections() 260 sections = self.bridge_template.sections()
240 sections.sort() 261 sections.sort()
272 except IOError: 293 except IOError:
273 print ("Can't open template file [%s]" % self.frontend_template) 294 print ("Can't open template file [%s]" % self.frontend_template)
274 sys.exit(1) 295 sys.exit(1)
275 296
276 #now we write to final file 297 #now we write to final file
277 if os.path.exists(self.frontend_dest) and not self.options.force: 298 self.finalWrite(self.frontend_dest, frontend_bridge)
278 print ("The destination file [%s] already exists ! Use --force to overwrite it" % self.frontend_dest)
279 try:
280 with open(self.frontend_dest,'w') as dest_file:
281 dest_file.write('\n'.join(frontend_bridge))
282 except IOError:
283 print ("Can't open destination file [%s]" % self.frontend_dest)
284 299
285 300
286 class ConstructorError(Exception): 301 class ConstructorError(Exception):
287 pass 302 pass
288 303