comparison CodeAnalysis/GenerateOrthancSDK.py @ 181:faaa3fec799a java-code-model

refactoring using FunctionDocumentation.mustache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jul 2024 13:26:33 +0200
parents ddf3e987827f
children 20b004998fc2
comparison
equal deleted inserted replaced
180:ddf3e987827f 181:faaa3fec799a
79 'OrthancPluginFindQuery' : [ 79 'OrthancPluginFindQuery' : [
80 { 80 {
81 'method_name' : 'GetFindQueryTagGroup', 81 'method_name' : 'GetFindQueryTagGroup',
82 'implementation' : 'GetFindQueryTagGroup', 82 'implementation' : 'GetFindQueryTagGroup',
83 'sdk_function' : 'OrthancPluginGetFindQueryTag', 83 'sdk_function' : 'OrthancPluginGetFindQueryTag',
84 'documentation' : {
85 'description' : [ ],
86 },
87 'args' : [
88 ],
89 'return_sdk_type' : 'void',
84 }, 90 },
85 { 91 {
86 'method_name' : 'GetFindQueryTagElement', 92 'method_name' : 'GetFindQueryTagElement',
87 'implementation' : 'GetFindQueryTagElement', 93 'implementation' : 'GetFindQueryTagElement',
88 'sdk_function' : 'OrthancPluginGetFindQueryTag', 94 'sdk_function' : 'OrthancPluginGetFindQueryTag',
95 'documentation' : {
96 'description' : [ ],
97 },
98 'args' : [
99 ],
100 'return_sdk_type' : 'void',
89 }, 101 },
90 ], 102 ],
91 103
92 'OrthancPluginDicomInstance' : [ 104 'OrthancPluginDicomInstance' : [
93 { 105 {
94 'method_name' : 'GetInstanceData', 106 'method_name' : 'GetInstanceData',
95 'implementation' : 'GetInstanceData', 107 'implementation' : 'GetInstanceData',
96 'sdk_function' : 'OrthancPluginGetInstanceData', 108 'sdk_function' : 'OrthancPluginGetInstanceData',
109 'documentation' : {
110 'description' : [ ]
111 },
112 'args' : [
113 ],
114 'return_sdk_type' : 'OrthancPluginMemoryBuffer *',
97 }, 115 },
98 ], 116 ],
99 117
100 'OrthancPluginImage' : [ 118 'OrthancPluginImage' : [
101 { 119 {
102 'method_name' : 'GetImageBuffer', 120 'method_name' : 'GetImageBuffer',
103 'implementation' : 'GetImageBuffer', 121 'implementation' : 'GetImageBuffer',
104 'sdk_function' : 'OrthancPluginGetImageBuffer', 122 'sdk_function' : 'OrthancPluginGetImageBuffer',
123 'documentation' : {
124 'description' : [ ],
125 },
126 'args' : [
127 ],
128 'return_sdk_type' : 'void',
105 } 129 }
106 ], 130 ],
107 } 131 }
108 132
109 133
113 137
114 partials = {} 138 partials = {}
115 139
116 with open(os.path.join(ROOT, 'FunctionBody.mustache'), 'r') as f: 140 with open(os.path.join(ROOT, 'FunctionBody.mustache'), 'r') as f:
117 partials['function_body'] = f.read() 141 partials['function_body'] = f.read()
142
143 with open(os.path.join(ROOT, 'FunctionDocumentation.mustache'), 'r') as f:
144 partials['function_documentation'] = f.read()
118 145
119 renderer = pystache.Renderer( 146 renderer = pystache.Renderer(
120 escape = lambda u: u, # No escaping 147 escape = lambda u: u, # No escaping
121 partials = partials, 148 partials = partials,
122 ) 149 )
190 'float' : { 217 'float' : {
191 'type' : 'float', 218 'type' : 'float',
192 'format' : 'f', 219 'format' : 'f',
193 } 220 }
194 } 221 }
222
223
224 def DocumentFunction(f):
225 documentation = {}
226 description = f['documentation'].get('description', [])
227 if len(description) > 0:
228 documentation['short_description'] = description[0].split('.') [0]
229 documentation['description'] = map(lambda x: { 'text' : x }, description)
230
231 args_declaration = []
232 args_documentation = []
233 for a in f['args']:
234 arg_name = ToLowerCase(a['sdk_name'])
235 if a['sdk_type'] == 'const char *':
236 arg_type = 'str'
237 elif a['sdk_type'] == 'float':
238 arg_type = 'float'
239 elif a['sdk_type'] in [ 'const_void_pointer_with_size', 'const void *' ]:
240 arg_type = 'bytes'
241 elif a['sdk_type'] == 'enumeration':
242 arg_type = GetShortName(a['sdk_enumeration'])
243 elif a['sdk_type'] == 'const_object':
244 arg_type = GetShortName(a['sdk_class'])
245 elif a['sdk_type'] in [ 'int32_t', 'uint32_t', 'uint8_t', 'uint16_t', 'uint64_t' ]:
246 arg_type = 'int'
247 else:
248 raise Exception('Argument type not implemented: %s' % a['sdk_type'])
249 args_declaration.append('%s: %s' % (arg_name, arg_type))
250 args_documentation.append({
251 'name' : arg_name,
252 'type' : arg_type,
253 'text' : f['documentation']['args'] [a['sdk_name']],
254 })
255
256 documentation['args_declaration'] = ', '.join(args_declaration)
257 documentation['args'] = args_documentation
258 documentation['has_args'] = len(args_documentation) > 0
259 documentation['has_return'] = True
260 documentation['return_text'] = f['documentation'].get('return', None)
261
262 if f['return_sdk_type'] == 'enumeration':
263 if f['return_sdk_enumeration'] == 'OrthancPluginErrorCode':
264 documentation['has_return'] = False
265 documentation['return_type'] = 'None'
266 else:
267 documentation['return_type'] = GetShortName(f['return_sdk_enumeration'])
268 elif f['return_sdk_type'] == 'object':
269 documentation['return_type'] = GetShortName(f['return_sdk_class'])
270 elif f['return_sdk_type'] == 'void':
271 documentation['has_return'] = False
272 documentation['return_type'] = 'None'
273 elif f['return_sdk_type'] == 'OrthancPluginMemoryBuffer *':
274 documentation['return_type'] = 'bytes'
275 elif f['return_sdk_type'] in [ 'char *', 'const char *' ]:
276 documentation['return_type'] = 'str'
277 elif f['return_sdk_type'] in [ 'int32_t', 'uint32_t', 'int64_t' ]:
278 documentation['return_type'] = 'int'
279 else:
280 raise Exception('Return type not implemented: %s' % f['return_sdk_type'])
281
282 return documentation
195 283
196 284
197 def FormatFunction(f): 285 def FormatFunction(f):
198 answer = { 286 answer = {
199 'c_function' : f['c_function'], 287 'c_function' : f['c_function'],
296 384
297 answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target) 385 answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target)
298 answer['allow_threads'] = allow_threads 386 answer['allow_threads'] = allow_threads
299 387
300 if 'documentation' in f: 388 if 'documentation' in f:
301 documentation = {} 389 answer['documentation'] = DocumentFunction(f)
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 390
360 if len(call_args) > 0: 391 if len(call_args) > 0:
361 answer['call_args'] = ', ' + ', '.join(call_args) 392 answer['call_args'] = ', ' + ', '.join(call_args)
362 393
363 if not allow_threads: 394 if not allow_threads:
419 450
420 for m in c['methods']: 451 for m in c['methods']:
421 g = FormatFunction(m) 452 g = FormatFunction(m)
422 if g != None: 453 if g != None:
423 g['self'] = ', self->object_' 454 g['self'] = ', self->object_'
455 g['is_method'] = True
424 methods.append(g) 456 methods.append(g)
425 457
426 classes.append({ 458 classes.append({
427 'class_name' : c['name'], 459 'class_name' : c['name'],
428 'short_name' : GetShortName(c['name']), 460 'short_name' : GetShortName(c['name']),