comparison Resources/EmbedResources.py @ 1469:bf9b8bfea0e7

more generic EmbedResources.py
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 Jul 2015 14:12:12 +0200
parents 5068de14eef1
children 39ecd34fb1c7
comparison
equal deleted inserted replaced
1468:332af9bcabc8 1469:bf9b8bfea0e7
35 import os.path 35 import os.path
36 import pprint 36 import pprint
37 import re 37 import re
38 38
39 UPCASE_CHECK = True 39 UPCASE_CHECK = True
40 USE_SYSTEM_EXCEPTION = False
41 EXCEPTION_CLASS = 'OrthancException'
42 OUT_OF_RANGE_EXCEPTION = 'OrthancException(ErrorCode_ParameterOutOfRange)'
43 NAMESPACE = 'Orthanc'
44
40 ARGS = [] 45 ARGS = []
41 for i in range(len(sys.argv)): 46 for i in range(len(sys.argv)):
42 if not sys.argv[i].startswith('--'): 47 if not sys.argv[i].startswith('--'):
43 ARGS.append(sys.argv[i]) 48 ARGS.append(sys.argv[i])
44 elif sys.argv[i].lower() == '--no-upcase-check': 49 elif sys.argv[i].lower() == '--no-upcase-check':
45 UPCASE_CHECK = False 50 UPCASE_CHECK = False
51 elif sys.argv[i].lower() == '--system-exception':
52 USE_SYSTEM_EXCEPTION = True
53 EXCEPTION_CLASS = '::std::runtime_error'
54 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS
55 elif sys.argv[i].startswith('--namespace='):
56 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ]
46 57
47 if len(ARGS) < 2 or len(ARGS) % 2 != 0: 58 if len(ARGS) < 2 or len(ARGS) % 2 != 0:
48 print ('Usage:') 59 print ('Usage:')
49 print ('python %s [--no-upcase-check] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) 60 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
50 exit(-1) 61 exit(-1)
51 62
52 TARGET_BASE_FILENAME = ARGS[1] 63 TARGET_BASE_FILENAME = ARGS[1]
53 SOURCES = ARGS[2:] 64 SOURCES = ARGS[2:]
54 65
144 #pragma once 155 #pragma once
145 156
146 #include <string> 157 #include <string>
147 #include <list> 158 #include <list>
148 159
149 namespace Orthanc 160 namespace %s
150 { 161 {
151 namespace EmbeddedResources 162 namespace EmbeddedResources
152 { 163 {
153 enum FileResourceId 164 enum FileResourceId
154 { 165 {
155 """) 166 """ % NAMESPACE)
156 167
157 isFirst = True 168 isFirst = True
158 for name in resources: 169 for name in resources:
159 if resources[name]['Type'] == 'File': 170 if resources[name]['Type'] == 'File':
160 if isFirst: 171 if isFirst:
239 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) 250 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos))
240 251
241 252
242 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') 253 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w')
243 254
244 cpp.write(""" 255 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME))
245 #include "%s.h" 256
246 #include "%s/Core/OrthancException.h" 257 if USE_SYSTEM_EXCEPTION:
247 258 cpp.write('#include <stdexcept>')
259 else:
260 cpp.write('#include "%s/Core/OrthancException.h"' % os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
261
262 cpp.write("""
248 #include <stdint.h> 263 #include <stdint.h>
249 #include <string.h> 264 #include <string.h>
250 265
251 namespace Orthanc 266 namespace %s
252 { 267 {
253 namespace EmbeddedResources 268 namespace EmbeddedResources
254 { 269 {
255 """ % (os.path.basename(TARGET_BASE_FILENAME), 270 """ % NAMESPACE)
256 os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))))
257 271
258 272
259 for name in resources: 273 for name in resources:
260 if resources[name]['Type'] == 'File': 274 if resources[name]['Type'] == 'File':
261 WriteResource(cpp, resources[name]) 275 WriteResource(cpp, resources[name])
280 cpp.write(' case %s:\n' % name) 294 cpp.write(' case %s:\n' % name)
281 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) 295 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index'])
282 296
283 cpp.write(""" 297 cpp.write("""
284 default: 298 default:
285 throw OrthancException(ErrorCode_ParameterOutOfRange); 299 throw %s;
286 } 300 }
287 } 301 }
288 302
289 size_t GetFileResourceSize(FileResourceId id) 303 size_t GetFileResourceSize(FileResourceId id)
290 { 304 {
291 switch (id) 305 switch (id)
292 { 306 {
293 """) 307 """ % OUT_OF_RANGE_EXCEPTION)
294 308
295 for name in resources: 309 for name in resources:
296 if resources[name]['Type'] == 'File': 310 if resources[name]['Type'] == 'File':
297 cpp.write(' case %s:\n' % name) 311 cpp.write(' case %s:\n' % name)
298 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) 312 cpp.write(' return resource%dSize;\n' % resources[name]['Index'])
299 313
300 cpp.write(""" 314 cpp.write("""
301 default: 315 default:
302 throw OrthancException(ErrorCode_ParameterOutOfRange); 316 throw %s;
303 } 317 }
304 } 318 }
305 """) 319 """ % OUT_OF_RANGE_EXCEPTION)
306 320
307 321
308 322
309 ##################################################################### 323 #####################################################################
310 ## Write the accessors to the directory resources in .cpp 324 ## Write the accessors to the directory resources in .cpp
322 cpp.write(' case %s:\n' % name) 336 cpp.write(' case %s:\n' % name)
323 isFirst = True 337 isFirst = True
324 for path in resources[name]['Files']: 338 for path in resources[name]['Files']:
325 cpp.write(' if (!strcmp(path, "%s"))\n' % path) 339 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
326 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) 340 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index'])
327 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') 341 cpp.write(' throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS)
328 342
329 cpp.write(""" default: 343 cpp.write(""" default:
330 throw OrthancException(ErrorCode_ParameterOutOfRange); 344 throw %s;
331 } 345 }
332 } 346 }
333 347
334 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) 348 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path)
335 { 349 {
336 switch (id) 350 switch (id)
337 { 351 {
338 """) 352 """ % OUT_OF_RANGE_EXCEPTION)
339 353
340 for name in resources: 354 for name in resources:
341 if resources[name]['Type'] == 'Directory': 355 if resources[name]['Type'] == 'Directory':
342 cpp.write(' case %s:\n' % name) 356 cpp.write(' case %s:\n' % name)
343 isFirst = True 357 isFirst = True
344 for path in resources[name]['Files']: 358 for path in resources[name]['Files']:
345 cpp.write(' if (!strcmp(path, "%s"))\n' % path) 359 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
346 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) 360 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index'])
347 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') 361 cpp.write(' throw %s("Unknown path in a directory resource");\n\n' % EXCEPTION_CLASS)
348 362
349 cpp.write(""" default: 363 cpp.write(""" default:
350 throw OrthancException(ErrorCode_ParameterOutOfRange); 364 throw %s;
351 } 365 }
352 } 366 }
353 """) 367 """ % OUT_OF_RANGE_EXCEPTION)
354 368
355 369
356 370
357 371
358 ##################################################################### 372 #####################################################################
374 for path in sorted(resources[name]['Files']): 388 for path in sorted(resources[name]['Files']):
375 cpp.write(' result.push_back("%s");\n' % path) 389 cpp.write(' result.push_back("%s");\n' % path)
376 cpp.write(' break;\n\n') 390 cpp.write(' break;\n\n')
377 391
378 cpp.write(""" default: 392 cpp.write(""" default:
379 throw OrthancException(ErrorCode_ParameterOutOfRange); 393 throw %s;
380 } 394 }
381 } 395 }
382 """) 396 """ % OUT_OF_RANGE_EXCEPTION)
383 397
384 398
385 399
386 400
387 ##################################################################### 401 #####################################################################