0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 ##
|
|
4 ## Python plugin for Orthanc
|
|
5 ## Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
6 ##
|
|
7 ## This program is free software: you can redistribute it and/or
|
|
8 ## modify it under the terms of the GNU Affero General Public License
|
|
9 ## as published by the Free Software Foundation, either version 3 of
|
|
10 ## the License, or (at your option) any later version.
|
|
11 ##
|
|
12 ## This program is distributed in the hope that it will be useful, but
|
|
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 ## Affero General Public License for more details.
|
|
16 ##
|
|
17 ## You should have received a copy of the GNU Affero General Public License
|
|
18 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 ##
|
|
20
|
|
21
|
|
22 import argparse
|
|
23 import clang.cindex
|
|
24 import os
|
|
25 import pprint
|
|
26 import pystache
|
|
27 import sys
|
|
28
|
|
29
|
|
30 ROOT = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
31
|
|
32
|
|
33 ##
|
|
34 ## Parse the command-line arguments
|
|
35 ##
|
|
36
|
|
37 parser = argparse.ArgumentParser(description = 'Parse the Orthanc SDK.')
|
|
38 parser.add_argument('--libclang',
|
|
39 default = 'libclang-4.0.so.1',
|
|
40 help = 'manually provides the path to the libclang shared library')
|
|
41 parser.add_argument('--source',
|
|
42 default = os.path.join(os.path.dirname(__file__),
|
|
43 '../Resources/Orthanc/Sdk-1.5.7/orthanc/OrthancCPlugin.h'),
|
|
44 help = 'Input C++ file')
|
|
45 parser.add_argument('--target',
|
|
46 default = os.path.join(os.path.dirname(__file__),
|
|
47 '../Sources/Autogenerated'),
|
|
48 help = 'Target folder')
|
|
49
|
|
50 args = parser.parse_args()
|
|
51
|
|
52
|
|
53
|
|
54 if len(args.libclang) != 0:
|
|
55 clang.cindex.Config.set_library_file(args.libclang)
|
|
56
|
|
57 index = clang.cindex.Index.create()
|
|
58
|
|
59 tu = index.parse(args.source, [ ])
|
|
60
|
|
61 TARGET = os.path.realpath(args.target)
|
|
62
|
|
63
|
|
64
|
|
65 def ToUpperCase(name):
|
|
66 s = ''
|
|
67 for i in range(len(name)):
|
|
68 if name[i].isupper():
|
|
69 if len(s) == 0:
|
|
70 s += name[i]
|
|
71 elif name[i - 1].islower():
|
|
72 s += '_' + name[i]
|
|
73 elif (i + 1 < len(name) and
|
|
74 name[i - 1].islower() and
|
|
75 name[i + 1].isupper()):
|
|
76 s += '_' + name[i]
|
|
77 else:
|
|
78 s += name[i]
|
|
79 else:
|
|
80 s += name[i].upper()
|
|
81 return s
|
|
82
|
|
83
|
|
84
|
|
85 with open(os.path.join(ROOT, 'Enumeration.mustache'), 'r') as f:
|
|
86 TEMPLATE = f.read()
|
|
87
|
|
88
|
|
89 classes = {}
|
|
90 enumerations = {}
|
|
91 globalFunctions = []
|
2
|
92 countAllFunctions = 0
|
|
93 countSupportedFunctions = 0
|
0
|
94
|
|
95 def IsSourceStringType(t):
|
|
96 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
97 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and
|
|
98 t.get_pointee().is_const_qualified())
|
|
99
|
|
100 def IsTargetStaticStringType(t):
|
|
101 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
102 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and
|
|
103 t.get_pointee().is_const_qualified())
|
|
104
|
|
105 def IsTargetDynamicStringType(t):
|
|
106 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
107 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and
|
|
108 not t.get_pointee().is_const_qualified())
|
|
109
|
|
110 def IsIntegerType(t):
|
|
111 return (t.kind == clang.cindex.TypeKind.INT or
|
|
112 t.spelling in [ 'int8_t', 'int16_t', 'int32_t', 'int64_t',
|
|
113 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t'])
|
|
114
|
|
115 def IsFloatType(t):
|
|
116 return t.kind == clang.cindex.TypeKind.FLOAT
|
|
117
|
|
118 def IsEnumerationType(t):
|
|
119 return (t.kind == clang.cindex.TypeKind.TYPEDEF and
|
|
120 t.spelling in enumerations)
|
|
121
|
|
122 def IsTargetMemoryBufferType(t):
|
|
123 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
124 not t.get_pointee().is_const_qualified() and
|
|
125 t.get_pointee().spelling == 'OrthancPluginMemoryBuffer')
|
|
126
|
|
127 def IsSourceMemoryBufferType(t):
|
|
128 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
129 t.get_pointee().kind == clang.cindex.TypeKind.VOID and
|
|
130 t.get_pointee().is_const_qualified())
|
|
131
|
|
132 def IsClassType(t):
|
|
133 return (t.kind == clang.cindex.TypeKind.POINTER and
|
|
134 ((t.get_pointee().is_const_qualified() and
|
|
135 t.get_pointee().spelling.startswith('const ') and
|
|
136 t.get_pointee().spelling[len('const '):] in classes) or
|
|
137 (not t.get_pointee().is_const_qualified() and
|
|
138 t.get_pointee().spelling in classes)))
|
|
139
|
|
140 def IsSimpleSourceType(t):
|
|
141 return (IsSourceStringType(t) or
|
|
142 IsFloatType(t) or
|
|
143 IsIntegerType(t) or
|
|
144 IsEnumerationType(t) or
|
|
145 IsSourceMemoryBufferType(t))
|
|
146
|
|
147 def IsVoidType(t):
|
|
148 return t.kind == clang.cindex.TypeKind.VOID
|
|
149
|
|
150 def IsSupportedTargetType(t):
|
|
151 return (IsVoidType(t) or
|
|
152 IsIntegerType(t) or
|
|
153 IsEnumerationType(t) or
|
|
154 # Constructor of a class
|
|
155 (t.kind == clang.cindex.TypeKind.POINTER and
|
|
156 not t.get_pointee().is_const_qualified() and
|
|
157 t.get_pointee().spelling in classes) or
|
|
158 # "const char*" or "char*" outputs
|
|
159 (t.kind == clang.cindex.TypeKind.POINTER and
|
|
160 #not t.get_pointee().is_const_qualified() and
|
|
161 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S))
|
|
162
|
|
163 def IsBytesArgument(args, index):
|
|
164 return (index + 1 < len(args) and
|
|
165 args[index].type.kind == clang.cindex.TypeKind.POINTER and
|
|
166 args[index].type.get_pointee().kind == clang.cindex.TypeKind.VOID and
|
|
167 args[index].type.get_pointee().is_const_qualified() and
|
|
168 args[index + 1].type.spelling == 'uint32_t')
|
|
169
|
|
170 def CheckOnlySupportedArguments(args):
|
|
171 j = 0
|
|
172 while j < len(args):
|
|
173 if IsBytesArgument(args, j):
|
|
174 j += 2
|
|
175 elif IsSimpleSourceType(args[j].type):
|
|
176 j += 1
|
|
177 else:
|
|
178 return False
|
|
179 return True
|
|
180
|
|
181
|
|
182 ORTHANC_TO_PYTHON_NUMERIC_TYPES = {
|
|
183 # https://docs.python.org/3/c-api/arg.html#numbers
|
|
184 'int' : {
|
|
185 'type' : 'int',
|
|
186 'format' : 'i',
|
|
187 },
|
|
188 'uint8_t' : {
|
|
189 'type' : 'unsigned char',
|
|
190 'format' : 'b',
|
|
191 },
|
|
192 'int32_t' : {
|
|
193 'type' : 'long int',
|
|
194 'format' : 'l',
|
|
195 },
|
|
196 'uint16_t' : {
|
|
197 'type' : 'unsigned short',
|
|
198 'format' : 'H',
|
|
199 },
|
|
200 'uint32_t' : {
|
|
201 'type' : 'unsigned long',
|
|
202 'format' : 'k',
|
|
203 },
|
|
204 'uint64_t' : {
|
|
205 'type' : 'unsigned long long',
|
|
206 'format' : 'K',
|
|
207 },
|
|
208 'float' : {
|
|
209 'type' : 'float',
|
|
210 'format' : 'f',
|
|
211 }
|
|
212 }
|
|
213
|
|
214
|
|
215 def GenerateFunctionBodyTemplate(cFunction, result_type, args):
|
|
216 if not cFunction.startswith('OrthancPlugin'):
|
|
217 raise Exception()
|
|
218
|
|
219 func = {
|
|
220 'c_function' : cFunction,
|
|
221 'short_name' : cFunction[len('OrthancPlugin'):],
|
|
222 'args' : [],
|
|
223 }
|
|
224
|
|
225 if IsIntegerType(result_type):
|
|
226 func['return_long'] = True
|
|
227 elif IsTargetDynamicStringType(result_type):
|
|
228 func['return_dynamic_string'] = True
|
|
229 elif IsTargetStaticStringType(result_type):
|
|
230 func['return_static_string'] = True
|
|
231 elif IsVoidType(result_type):
|
|
232 func['return_void'] = True
|
|
233 elif result_type.spelling == 'OrthancPluginErrorCode':
|
|
234 func['return_error'] = True
|
|
235 elif IsClassType(result_type):
|
|
236 func['return_object'] = result_type.get_pointee().spelling
|
|
237 elif IsTargetMemoryBufferType(result_type):
|
|
238 func['return_bytes'] = True
|
|
239 elif IsEnumerationType(result_type):
|
|
240 func['return_enumeration'] = result_type.spelling
|
|
241 else:
|
|
242 raise Exception('Not supported: %s' % result_type.spelling)
|
|
243
|
|
244 i = 0
|
|
245 while i < len(args):
|
|
246 a = {
|
|
247 'name' : 'arg%d' % i,
|
|
248 }
|
|
249
|
|
250 if (IsIntegerType(args[i].type) or
|
|
251 IsFloatType(args[i].type)):
|
|
252 t = ORTHANC_TO_PYTHON_NUMERIC_TYPES[args[i].type.spelling]
|
|
253 a['python_type'] = t['type']
|
|
254 a['python_format'] = t['format']
|
|
255 a['initialization'] = ' = 0'
|
|
256 a['orthanc_cast'] = 'arg%d' % i
|
|
257 func['args'].append(a)
|
|
258 elif IsSourceStringType(args[i].type):
|
|
259 a['python_type'] = 'const char*'
|
|
260 a['python_format'] = 's'
|
|
261 a['initialization'] = ' = NULL'
|
|
262 a['orthanc_cast'] = 'arg%d' % i
|
|
263 func['args'].append(a)
|
|
264 elif IsEnumerationType(args[i].type):
|
|
265 a['python_type'] = 'long int'
|
|
266 a['python_format'] = 'l'
|
|
267 a['initialization'] = ' = 0'
|
|
268 a['orthanc_cast'] = 'static_cast<%s>(arg%d)' % (args[i].type.spelling, i)
|
|
269 func['args'].append(a)
|
|
270 elif IsBytesArgument(args, i):
|
|
271 a['python_type'] = 'Py_buffer'
|
|
272 # In theory, one should use "y*" (this is the recommended
|
|
273 # way to accept binary data). However, this is not
|
|
274 # available in Python 2.7
|
|
275 a['python_format'] = 's*'
|
|
276 a['orthanc_cast'] = 'arg%d.buf, arg%d.len' % (i, i)
|
|
277 a['release'] = 'PyBuffer_Release(&arg%d);' % i
|
|
278 func['args'].append(a)
|
|
279 i += 1
|
|
280 elif IsSourceMemoryBufferType(args[i].type):
|
|
281 a['python_type'] = 'Py_buffer'
|
|
282 a['python_format'] = 's*'
|
|
283 a['orthanc_cast'] = 'arg%d.buf' % i
|
|
284 a['release'] = 'PyBuffer_Release(&arg%d);' % i
|
|
285 func['args'].append(a)
|
|
286 else:
|
|
287 raise Exception('Not supported: %s, %s' % (cFunction, args[i].spelling))
|
|
288
|
|
289 i += 1
|
|
290
|
|
291 func['tuple_format'] = '"%s", %s' % (
|
|
292 ''.join(map(lambda x: x['python_format'], func['args'])),
|
|
293 ', '.join(map(lambda x: '&' + x['name'], func['args'])))
|
|
294
|
|
295 if len(func['args']) > 0:
|
|
296 func['count_args'] = len(func['args'])
|
|
297 func['has_args'] = True
|
|
298 func['call_args'] = ', ' + ', '.join(map(lambda x: x['orthanc_cast'], func['args']))
|
|
299
|
|
300 return func
|
|
301
|
|
302
|
|
303 for node in tu.cursor.get_children():
|
|
304 if node.kind == clang.cindex.CursorKind.ENUM_DECL:
|
|
305 if node.type.spelling.startswith('OrthancPlugin'):
|
|
306 name = node.type.spelling
|
|
307
|
|
308 values = []
|
|
309 for item in node.get_children():
|
|
310 if (item.kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL and
|
|
311 item.spelling.startswith(name + '_')):
|
|
312 values.append({
|
|
313 'key' : ToUpperCase(item.spelling[len(name)+1:]),
|
|
314 'value' : item.enum_value
|
|
315 })
|
|
316
|
|
317 path = 'sdk_%s.impl.h' % name
|
|
318 shortName = name[len('OrthancPlugin'):]
|
|
319
|
|
320 with open(os.path.join(TARGET, path), 'w') as f:
|
|
321 f.write(pystache.render(TEMPLATE, {
|
|
322 'name' : name,
|
|
323 'short_name' : shortName,
|
|
324 'values' : values,
|
|
325 }))
|
|
326
|
|
327 enumerations[name] = {
|
|
328 'name' : name,
|
|
329 'path' : path,
|
|
330 }
|
|
331
|
|
332 elif node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
|
|
333 if node.spelling.startswith('OrthancPlugin'):
|
|
334 #if node.spelling != 'OrthancPluginWorklistGetDicomQuery':
|
|
335 # continue
|
|
336 shortName = node.spelling[len('OrthancPlugin'):]
|
|
337
|
|
338 # Check that the first argument is the Orthanc context
|
|
339 args = list(filter(lambda x: x.kind == clang.cindex.CursorKind.PARM_DECL,
|
|
340 node.get_children()))
|
|
341
|
|
342 if (len(args) == 0 or
|
|
343 args[0].type.kind != clang.cindex.TypeKind.POINTER or
|
|
344 args[0].type.get_pointee().spelling != 'OrthancPluginContext'):
|
|
345 print('Not in the Orthanc SDK: %s()' % node.spelling)
|
|
346 continue
|
|
347
|
|
348 # Discard the context from the arguments
|
2
|
349 countAllFunctions += 1
|
0
|
350 args = args[1:]
|
|
351
|
|
352 if not IsSupportedTargetType(node.result_type):
|
|
353 print('*** UNSUPPORTED OUTPUT: %s' % node.spelling)
|
|
354
|
|
355 elif (len(args) == 1 and
|
|
356 IsClassType(args[0].type) and
|
|
357 node.spelling.startswith('OrthancPluginFree')):
|
|
358 print('Destructor: %s' % node.spelling)
|
|
359 className = args[0].type.get_pointee().spelling
|
|
360 classes[className]['destructor'] = node.spelling
|
2
|
361 countSupportedFunctions += 1
|
0
|
362
|
|
363 elif CheckOnlySupportedArguments(args):
|
|
364 if IsClassType(node.result_type):
|
|
365 print('Constructor: %s' % node.spelling)
|
|
366 else:
|
|
367 print('Simple global function: %s => %s' % (node.spelling, node.result_type.spelling))
|
|
368
|
|
369 body = GenerateFunctionBodyTemplate(node.spelling, node.result_type, args)
|
|
370 globalFunctions.append(body)
|
2
|
371 countSupportedFunctions += 1
|
0
|
372
|
|
373 elif (len(args) >= 2 and
|
|
374 IsTargetMemoryBufferType(args[0].type) and
|
|
375 CheckOnlySupportedArguments(args[1:])):
|
|
376 print('Simple global function, returning bytes: %s' % node.spelling)
|
|
377
|
|
378 body = GenerateFunctionBodyTemplate(node.spelling, args[0].type, args[1:])
|
|
379 globalFunctions.append(body)
|
2
|
380 countSupportedFunctions += 1
|
0
|
381
|
|
382 elif (IsClassType(args[0].type) and
|
|
383 CheckOnlySupportedArguments(args[1:])):
|
|
384 className = args[0].type.get_pointee().spelling
|
|
385
|
|
386 if className.startswith('const '):
|
|
387 className = className[len('const '):]
|
|
388
|
|
389 print('Simple method of class %s: %s' % (className, node.spelling))
|
|
390
|
|
391 method = GenerateFunctionBodyTemplate(node.spelling, node.result_type, args[1:])
|
|
392 method['self'] = ', self->object_'
|
|
393 classes[className]['methods'].append(method)
|
2
|
394 countSupportedFunctions += 1
|
0
|
395
|
|
396 elif (len(args) >= 2 and
|
|
397 IsTargetMemoryBufferType(args[0].type) and
|
|
398 IsClassType(args[1].type) and
|
|
399 CheckOnlySupportedArguments(args[2:])):
|
|
400 print('Simple method of class %s, returning bytes: %s' % (
|
31
|
401 args[1].type.get_pointee().spelling,
|
0
|
402 node.spelling))
|
|
403
|
31
|
404 className = args[1].type.get_pointee().spelling
|
|
405
|
|
406 if className.startswith('const '):
|
|
407 className = className[len('const '):]
|
|
408
|
0
|
409 method = GenerateFunctionBodyTemplate(node.spelling, args[0].type, args[2:])
|
|
410 method['self'] = ', self->object_'
|
|
411 classes[className]['methods'].append(method)
|
2
|
412 countSupportedFunctions += 1
|
0
|
413
|
|
414 else:
|
|
415 print('*** UNSUPPORTED INPUT: %s' % node.spelling)
|
|
416
|
|
417
|
|
418
|
|
419 elif node.kind == clang.cindex.CursorKind.STRUCT_DECL:
|
|
420 if (node.spelling.startswith('_OrthancPlugin') and
|
|
421 node.spelling.endswith('_t') and
|
|
422 node.spelling != '_OrthancPluginContext_t'):
|
|
423 name = node.spelling[len('_') : -len('_t')]
|
|
424 classes[name] = {
|
|
425 'class_name' : name,
|
|
426 'short_name' : name[len('OrthancPlugin'):],
|
|
427 'methods' : [ ]
|
|
428 }
|
|
429
|
|
430
|
|
431
|
|
432
|
|
433 partials = {}
|
|
434
|
|
435 with open(os.path.join(ROOT, 'FunctionBody.mustache'), 'r') as f:
|
|
436 partials['function_body'] = f.read()
|
|
437
|
|
438 renderer = pystache.Renderer(
|
|
439 escape = lambda u: u, # No escaping
|
|
440 partials = partials,
|
|
441 )
|
|
442
|
|
443 with open(os.path.join(ROOT, 'Class.mustache'), 'r') as f:
|
|
444 template = f.read()
|
|
445
|
|
446 for (key, value) in classes.items():
|
|
447 with open(os.path.join(TARGET, 'sdk_%s.impl.h' % value['class_name']), 'w') as h:
|
|
448 h.write(renderer.render(template, value))
|
|
449
|
|
450
|
|
451 def FlattenDictionary(source):
|
|
452 result = []
|
|
453 for (key, value) in source.items():
|
|
454 result.append(value)
|
|
455 return result
|
|
456
|
|
457
|
|
458 with open(os.path.join(ROOT, 'GlobalFunctions.mustache'), 'r') as f:
|
|
459 with open(os.path.join(TARGET, 'sdk_GlobalFunctions.impl.h'), 'w') as h:
|
|
460 h.write(renderer.render(f.read(), {
|
|
461 'global_functions' : globalFunctions,
|
|
462 }))
|
|
463
|
|
464 with open(os.path.join(ROOT, 'sdk.cpp.mustache'), 'r') as f:
|
|
465 with open(os.path.join(TARGET, 'sdk.cpp'), 'w') as h:
|
|
466 h.write(renderer.render(f.read(), {
|
|
467 'classes' : FlattenDictionary(classes),
|
|
468 'enumerations' : FlattenDictionary(enumerations),
|
|
469 'global_functions' : globalFunctions,
|
|
470 }))
|
|
471
|
|
472 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f:
|
|
473 with open(os.path.join(TARGET, 'sdk.h'), 'w') as h:
|
|
474 h.write(renderer.render(f.read(), {
|
|
475 'classes' : FlattenDictionary(classes),
|
|
476 }))
|
2
|
477
|
|
478
|
|
479 print('')
|
|
480 print('Total functions in the SDK: %d' % countAllFunctions)
|
|
481 print('Total supported functions: %d' % countSupportedFunctions)
|
|
482 print('Coverage: %.0f%%' % (float(countSupportedFunctions) /
|
|
483 float(countAllFunctions) * 100.0))
|