comparison CodeAnalysis/GenerateOrthancSDK.py @ 180:ddf3e987827f java-code-model

created Python documentation for the Orthanc interface
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jul 2024 12:30:16 +0200
parents f49864df6f1f
children faaa3fec799a
comparison
equal deleted inserted replaced
179:f49864df6f1f 180:ddf3e987827f
57 { 57 {
58 'c_function' : 'OrthancPluginCreateMemoryBuffer', 58 'c_function' : 'OrthancPluginCreateMemoryBuffer',
59 'args' : [ 59 'args' : [
60 { 60 {
61 'name' : 'arg0', 61 'name' : 'arg0',
62 'sdk_name' : 'size',
62 'sdk_type' : 'uint32_t', 63 'sdk_type' : 'uint32_t',
63 } 64 }
64 ], 65 ],
66 'documentation' : {
67 'args' : {
68 'size' : 'Size of the memory buffer to be created',
69 },
70 'return' : 'The newly allocated memory buffer',
71 'description' : [ 'Create a new memory buffer managed by the Orthanc core' ],
72 },
65 'return_sdk_type' : 'OrthancPluginMemoryBuffer *', 73 'return_sdk_type' : 'OrthancPluginMemoryBuffer *',
66 } 74 }
67 ] 75 ]
68 76
69 77
133 s += '_' + name[i] 141 s += '_' + name[i]
134 else: 142 else:
135 s += name[i] 143 s += name[i]
136 else: 144 else:
137 s += name[i].upper() 145 s += name[i].upper()
146 return s
147
148
149 def ToLowerCase(name):
150 s = ''
151 for i in range(len(name)):
152 if (name[i].isupper() and
153 len(s) != 0):
154 s += '_'
155 s += name[i].lower()
138 return s 156 return s
139 157
140 158
141 def GetShortName(name): 159 def GetShortName(name):
142 if not name.startswith('OrthancPlugin'): 160 if not name.startswith('OrthancPlugin'):
277 allow_threads = False # TODO 295 allow_threads = False # TODO
278 296
279 answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target) 297 answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target)
280 answer['allow_threads'] = allow_threads 298 answer['allow_threads'] = allow_threads
281 299
300 if 'documentation' in f:
301 documentation = {}
302 description = f['documentation'].get('description', [])
303 if len(description) > 0:
304 documentation['short_description'] = description[0].split('.') [0]
305 documentation['description'] = map(lambda x: { 'text' : x }, description)
306
307 args_declaration = []
308 args_documentation = []
309 for a in f['args']:
310 arg_name = ToLowerCase(a['sdk_name'])
311 if a['sdk_type'] == 'const char *':
312 arg_type = 'str'
313 elif a['sdk_type'] == 'float':
314 arg_type = 'float'
315 elif a['sdk_type'] in [ 'const_void_pointer_with_size', 'const void *' ]:
316 arg_type = 'bytes'
317 elif a['sdk_type'] == 'enumeration':
318 arg_type = GetShortName(a['sdk_enumeration'])
319 elif a['sdk_type'] == 'const_object':
320 arg_type = GetShortName(a['sdk_class'])
321 elif a['sdk_type'] in [ 'int32_t', 'uint32_t', 'uint8_t', 'uint16_t', 'uint64_t' ]:
322 arg_type = 'int'
323 else:
324 raise Exception('Argument type not implemented: %s' % a['sdk_type'])
325 args_declaration.append('%s: %s' % (arg_name, arg_type))
326 args_documentation.append({
327 'name' : arg_name,
328 'type' : arg_type,
329 'text' : f['documentation']['args'] [a['sdk_name']],
330 })
331
332 documentation['args_declaration'] = ', '.join(args_declaration)
333 documentation['args'] = args_documentation
334 documentation['has_args'] = len(args_documentation) > 0
335 documentation['has_return'] = True
336 documentation['return_text'] = f['documentation'].get('return', None)
337
338 if f['return_sdk_type'] == 'enumeration':
339 if f['return_sdk_enumeration'] == 'OrthancPluginErrorCode':
340 documentation['has_return'] = False
341 documentation['return_type'] = 'None'
342 else:
343 documentation['return_type'] = GetShortName(f['return_sdk_enumeration'])
344 elif f['return_sdk_type'] == 'object':
345 documentation['return_type'] = GetShortName(f['return_sdk_class'])
346 elif f['return_sdk_type'] == 'void':
347 documentation['has_return'] = False
348 documentation['return_type'] = 'None'
349 elif f['return_sdk_type'] == 'OrthancPluginMemoryBuffer *':
350 documentation['return_type'] = 'bytes'
351 elif f['return_sdk_type'] in [ 'char *', 'const char *' ]:
352 documentation['return_type'] = 'str'
353 elif f['return_sdk_type'] in [ 'int32_t', 'uint32_t', 'int64_t' ]:
354 documentation['return_type'] = 'int'
355 else:
356 raise Exception('Return type not implemented: %s' % f['return_sdk_type'])
357
358 answer['documentation'] = documentation
359
282 if len(call_args) > 0: 360 if len(call_args) > 0:
283 answer['call_args'] = ', ' + ', '.join(call_args) 361 answer['call_args'] = ', ' + ', '.join(call_args)
284 362
285 if not allow_threads: 363 if not allow_threads:
286 print('Threads are not allowed in function: %s()' % f['c_function']) 364 print('Threads are not allowed in function: %s()' % f['c_function'])
311 values = [] 389 values = []
312 for value in e['values']: 390 for value in e['values']:
313 values.append({ 391 values.append({
314 'key' : ToUpperCase(value['key']), 392 'key' : ToUpperCase(value['key']),
315 'value' : value['value'], 393 'value' : value['value'],
394 'documentation' : value['documentation'],
316 }) 395 })
317 396
318 enumerations.append({ 397 enumerations.append({
319 'name' : e['name'], 398 'name' : e['name'],
399 'short_name' : GetShortName(e['name']),
320 'path' : 'sdk_%s.impl.h' % e['name'], 400 'path' : 'sdk_%s.impl.h' % e['name'],
321 'values' : values, 401 'values' : values,
402 'documentation' : e['documentation'],
322 }) 403 })
323 404
324 path = 'sdk_%s.impl.h' % e['name'] 405 path = 'sdk_%s.impl.h' % e['name']
325 406
326 with open(os.path.join(TARGET, path), 'w') as f: 407 with open(os.path.join(TARGET, path), 'w') as f:
390 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f: 471 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f:
391 with open(os.path.join(TARGET, 'sdk.h'), 'w') as h: 472 with open(os.path.join(TARGET, 'sdk.h'), 'w') as h:
392 h.write(renderer.render(f.read(), { 473 h.write(renderer.render(f.read(), {
393 'classes' : sortedClasses, 474 'classes' : sortedClasses,
394 })) 475 }))
476
477 with open(os.path.join(ROOT, 'PythonDocumentation.mustache'), 'r') as f:
478 with open(os.path.join(TARGET, 'orthanc.pyi'), 'w') as h:
479 h.write(renderer.render(f.read(), {
480 'classes' : sortedClasses,
481 'enumerations' : sortedEnumerations,
482 'global_functions' : sortedGlobalFunctions,
483 }))