113def CreateFiles(time):
114
115
116
117
118
119 cppBaseStartstring = "/*\n*This file was automatically created by XMLtoHeader.py\n* Created on: {creationTime}\n* Created with config: {xmlFile}\n*\n* Author: Ruth Poettgen\n* Contact: ruth.poettgen@cern.ch\n**Brief: Defines class for different CTP versions.\n*/\n\n\n#include <inttypes.h>\n#include <assert.h>\n#include <iostream>\n#include <stdlib.h>\n#include <map>\n#include <iomanip>\n#include <sstream>\n\n"
120 start = cppBaseStartstring.format(creationTime = time, xmlFile = inputXMLname)
121
122 baseNameRoot = os.path.basename(outputNames)
123 baseName = baseNameRoot + 'Version'
124 fileName = outputNames + 'Version'
125 baseHeader = open(fileName+'.h','w')
126 baseHeader.write(start)
127
128
129 baseHeader.write('#ifndef '+baseName.upper() + '_H\n')
130 baseHeader.write('#define '+baseName.upper() + '_H\n')
131 baseHeader.write('\nclass '+baseName + '{\n\n')
132
133 protectedString = "\nprotected:\n"
134 protectedString += " //---------------------------------------------------------------------------------------------------\n"
135 protectedString += " // For explanation of the variables and there values in different versions see config XML file.\n"
136 protectedString += " //---------------------------------------------------------------------------------------------------\n"
137
138 functionString = ''
139
140 publicString = "\npublic: \n"
141 publicString += '\n ' + baseName + '(unsigned int version) {\n'
142
143 dumpString = '\n std::string dump() const {\n';
144 dumpString += '\n std::ostringstream s;\n';
145 dumpString += '\n std::ostringstream tmp;\n\n';
146 dumpString += ' s << \"For a description of the parameters for different CTP version, see schema/L1CoreSpecifications.xml in L1CommonCore.\" << std::endl;\n\n';
147 dumpString += ' s << \"|-------------------------------------------------------------|\" << std::endl;\n';
148 dumpString += ' s << \"| Name | Value |\" << std::endl;\n';
149 dumpString += ' s << \"|-------------------------------------------------------------|\" << std::endl;\n';
150
151
152
153
154
155 changedValuesDict = {}
156 numberOfChanges = 0
157
158
159
160
161 pyStartstring = "#!/usr/bin/env python\n\n#This file was automatically created by XMLtoHeader.py\n# Created on: {creationTime}\n# Created with config: {xmlFile}\n# \n# Author: Ruth Poettgen\n# Contact: ruth.poettgen@cern.ch\n\n\n\nimport sys\n\nclass {classname}(object):\n def __init__(self):\n"
162
163
164
165
166
167
168
169 for v in versionDict:
170
171 if (v!='v0'):
172 changedValuesDict[v] = []
173
174 pyStartstring = pyStartstring.format(creationTime = time,version = versionDict[v], xmlFile = inputXMLname, classname=baseNameRoot)
175 headerName = outputNames + "_" + v + ".py"
176 pyHeader = open(headerName,'w')
177 pyHeader.write(pyStartstring)
178
179
180
181
182
183
184
185 childIndex=0
186 nameList=[]
187
188 for child in root:
189 hasChanged = False
190 childIndex=childIndex+1
191
192
193 if child.attrib['ns'] not in versionDict:
194
195 exit(1)
196
197
198 if child.find('name') is None:
199
200 exit(1)
201 if child.find('type') is None:
202
203 exit(1)
204 if child.find('value') is None:
205
206 exit(1)
207
208
209
210 if child.find('name').text[1:-1] in nameList:
211
212 continue
213
214
215
216
217
218
219
220
221 if list(versionDict).
index( child.attrib[
'ns'] )>list(versionDict).
index(v):
222
223 continue
224
225 tmp_v=v
226 while tmp_v not in nameVersionsDict[child.find('name').text[1:-1]]:
227
228 index = list(versionDict).
index(tmp_v)
229 tmp_v = list(versionDict)[index-1]
230
231
232 if tmp_v!='v0':
233 hasChanged=True
234
235 if child.attrib['ns']!=tmp_v:
236
237 continue
238
239
240
241
242
243
244 typeText = child.find('type').text
245 nameText = child.find('name').text[1:-1]
246 valueText = child.find('value').text
247
248
249 pyHeader.write(' self.'+nameText.ljust(30)+'= ')
250
251 if 'mult' in typeText:
252
253
254
255
256
257 pyHeader.write( ('[ ' + valueText + ' ]').ljust(50) )
258
259
260 listOfElements = valueText.split(',')
261 numberOfElements = len(listOfElements)
262
263 if v=='v0':
264
265 protectedString += ' '
266 protectedString += typeDictCpp[typeText[0:-5]] + ' '
267 protectedString += (nameText+ '['+ str(numberOfElements) + ']').ljust(20)+ '='
268 protectedString += '{ ' + valueText + '};'
269
270
271 functionString += '\n const '+typeDictCpp[typeText[0:-5]] + '* get'
272 functionString += nameText + '() const { return ' + nameText + '; }'
273
274
275 dumpString += '\n s << \"|\" << std::left << std::setw(40) << \" ' + nameText + '\" << \" | \";\n'
276 dumpString += ' tmp.clear(); tmp.str(\"\");\n tmp<< \"[\";\n'
277 dumpString += ' for (int i=0; i <' + str(numberOfElements) + '; i++) {\n'
278 dumpString += ' if (i) tmp<<\",\";\n'
279 dumpString += ' tmp << this->' + nameText + '[i];\n }\n tmp<<\"]\";\n'
280 dumpString += ' s << std::right << std::setw(17) << tmp.str() << \" | \" << std::endl;\n\n'
281
282
283
284
285
286
287
288
289 if hasChanged:
290 changedValuesDict[v].append([nameText,valueText])
291
292 else:
293
294
295
296
297 if (valueText)[0] not in ['0','1','2','3','4','5','6','7','8','9']:
298 val = valueText
299 pyHeader.write( ('self.' + val.replace('+',' + self.')).ljust(10))
300 else:
301 pyHeader.write(valueText.ljust(10))
302
303
304 if v=='v0':
305 functionString += '\n '+typeDictCpp[typeText] + ' get'
306 functionString += nameText + '() const { return ' + nameText + '; }'
307
308 protectedString += ' ' + typeDictCpp[typeText] + ' '
309 protectedString += nameText
310 protectedString += ' = ' + valueText + ';'
311
312 dumpString += ' s << \"|\" << std::left << std::setw(40) << \" ' + nameText + '\" << \" | \";\n'
313 dumpString += ' s << std::right << std::setw(17) << this->' + nameText + ' << \" | \" << std::endl;\n\n'
314
315
316
317
318
319
320
321 if hasChanged:
322 changedValuesDict[v].append([nameText,valueText])
323
324
325
326
327 if child.find('comment') is None:
328 pyHeader.write('\n')
329
330 else:
331 pyHeader.write( ' # ' + child.find('comment').text[1:-1] + '\n')
332 if v =='v0':
333 protectedString += '\n'
334
335 nameList.append(nameText)
336
337
338
339
340
341
342
343
344 pyHeader.write('\n\n' + baseNameRoot + '_' + v + ' = ' + baseNameRoot + '()'+'\n\n' + 'del ' + baseNameRoot)
345
346
347 dumpString += ' s << \"|-------------------------------------------------------------|\" << std::endl;\n\n';
348
349 dumpString += ' return s.str();\n\n }'
350
351 nVersions = len(versionDict)
352
353
354 selection= ' if (version>=' + str(nVersions)+') {\n';
355 selection+= ' std::cerr << \"ERROR: invalid CTP version requested (\" << version << \"). Should be in [0,'+ str(nVersions-1)+ '].Setting it to the latest version ('+str(nVersions-1)+'). \" << std::endl;\n'
356 selection += ' version='+str(nVersions-1)+';\n'
357 selection+= ' }\n\n'
358
359
360 versionInt = 1
361 for v in changedValuesDict:
362 selection += ' if (version==' + str(versionInt) + ' ) {\n'
363 for pair in changedValuesDict[v]:
364 if ',' in pair[1]:
365 listOfValues = pair[1].
split(
',')
366 numValues = len(listOfElements)
367 selection+='\n'
368 valIndex = 0
369 for val in listOfValues:
370 selection+= ' ' + pair[0] + '[' + str(valIndex) + '] = ' + val + ';\n'
371 valIndex+=1
372 else:
373 selection+=' ' + pair[0] + ' = ' + pair[1] + ';\n'
374
375 selection += ' }\n\n'
376 versionInt+=1
377 numberOfChanges = len(changedValuesDict)
378
379
380 baseHeader.write(protectedString+'\n\n')
381 baseHeader.write(publicString +'\n\n')
382 baseHeader.write(selection)
383 baseHeader.write(' };\n\n')
384 baseHeader.write(functionString+'\n\n')
385 baseHeader.write(dumpString+'\n\n')
386 baseHeader.write('\n};\n\n')
387 baseHeader.write('#endif //' + baseName.upper() + '_H')
388
389
std::vector< std::string > split(const std::string &s, const std::string &t=":")