Mercurial > hg > orthanc-python
annotate CodeAnalysis/ParseOrthancSDK.py @ 95:c17cdaf687e3
use debian:buster-slim to compile Windows binaries
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 15 Sep 2021 11:52:30 +0200 |
parents | 6fc445793796 |
children | 627b8a19fb9f |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 ## | |
4 ## Python plugin for Orthanc | |
56
23f3099bed47
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
31
diff
changeset
|
5 ## Copyright (C) 2020-2021 Osimis S.A., Belgium |
0 | 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 ## | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
34 ## Configuration of the custom primitives that are manually |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
35 ## implemented (not autogenerated) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
36 ## |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
37 |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
38 CUSTOM_FUNCTIONS = set([ |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
39 'OrthancPluginCreateDicom', |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
40 'OrthancPluginCreateImageAccessor', # Replaced by "orthanc.CreateImageFromBuffer()" |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
41 'OrthancPluginFreeMemoryBuffer', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
42 'OrthancPluginFreeString', |
66
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
43 'OrthancPluginLookupDictionary', |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
44 'OrthancPluginRegisterFindCallback', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
45 'OrthancPluginRegisterIncomingHttpRequestFilter', # Implemented through v2 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
46 'OrthancPluginRegisterIncomingHttpRequestFilter2', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
47 'OrthancPluginRegisterMoveCallback', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
48 'OrthancPluginRegisterOnChangeCallback', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
49 'OrthancPluginRegisterOnStoredInstanceCallback', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
50 'OrthancPluginRegisterRestCallback', # Implemented using OrthancPlugins::RegisterRestCallback |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
51 'OrthancPluginRegisterRestCallbackNoLock', # Implemented using OrthancPlugins::RegisterRestCallback |
64
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
52 'OrthancPluginRegisterWorklistCallback', |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
53 ]) |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
54 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
55 CUSTOM_METHODS = [ |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
56 { |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
57 'class_name' : 'OrthancPluginFindQuery', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
58 'method_name' : 'GetFindQueryTagGroup', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
59 'implementation' : 'GetFindQueryTagGroup', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
60 'sdk_function' : 'OrthancPluginGetFindQueryTag', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
61 }, |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
62 { |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
63 'class_name' : 'OrthancPluginFindQuery', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
64 'method_name' : 'GetFindQueryTagElement', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
65 'implementation' : 'GetFindQueryTagElement', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
66 'sdk_function' : 'OrthancPluginGetFindQueryTag', |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
67 }, |
64
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
68 { |
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
69 'class_name' : 'OrthancPluginWorklistAnswers', |
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
70 'method_name' : 'WorklistAddAnswer', |
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
71 'implementation' : 'WorklistAddAnswer', |
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
72 'sdk_function' : 'OrthancPluginWorklistAddAnswer', |
091fb1903bfc
new wrapped function: orthanc.RegisterWorklistCallback() and orthanc.WorklistAnswers.WorklistAddAnswer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
73 }, |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
74 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
75 'class_name' : 'OrthancPluginDicomInstance', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
76 'method_name' : 'GetInstanceData', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
77 'implementation' : 'GetInstanceData', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
78 'sdk_function' : 'OrthancPluginGetInstanceData', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
79 }, |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
80 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
81 'class_name' : 'OrthancPluginImage', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
82 'method_name' : 'GetImageBuffer', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
83 'implementation' : 'GetImageBuffer', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
84 'sdk_function' : 'OrthancPluginGetImageBuffer', |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
85 }, |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
86 ] |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
87 |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
88 for method in CUSTOM_METHODS: |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
89 CUSTOM_FUNCTIONS.add(method['sdk_function']) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
90 |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
91 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
92 ## |
0 | 93 ## Parse the command-line arguments |
94 ## | |
95 | |
96 parser = argparse.ArgumentParser(description = 'Parse the Orthanc SDK.') | |
97 parser.add_argument('--libclang', | |
98 default = 'libclang-4.0.so.1', | |
99 help = 'manually provides the path to the libclang shared library') | |
100 parser.add_argument('--source', | |
101 default = os.path.join(os.path.dirname(__file__), | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
102 '../Resources/Orthanc/Sdk-1.8.1/orthanc/OrthancCPlugin.h'), |
0 | 103 help = 'Input C++ file') |
104 parser.add_argument('--target', | |
105 default = os.path.join(os.path.dirname(__file__), | |
106 '../Sources/Autogenerated'), | |
107 help = 'Target folder') | |
108 | |
109 args = parser.parse_args() | |
110 | |
111 | |
112 | |
113 if len(args.libclang) != 0: | |
114 clang.cindex.Config.set_library_file(args.libclang) | |
115 | |
116 index = clang.cindex.Index.create() | |
117 | |
118 tu = index.parse(args.source, [ ]) | |
119 | |
120 TARGET = os.path.realpath(args.target) | |
121 | |
122 | |
123 | |
124 def ToUpperCase(name): | |
125 s = '' | |
126 for i in range(len(name)): | |
127 if name[i].isupper(): | |
128 if len(s) == 0: | |
129 s += name[i] | |
130 elif name[i - 1].islower(): | |
131 s += '_' + name[i] | |
132 elif (i + 1 < len(name) and | |
133 name[i - 1].islower() and | |
134 name[i + 1].isupper()): | |
135 s += '_' + name[i] | |
136 else: | |
137 s += name[i] | |
138 else: | |
139 s += name[i].upper() | |
140 return s | |
141 | |
142 | |
143 | |
144 with open(os.path.join(ROOT, 'Enumeration.mustache'), 'r') as f: | |
145 TEMPLATE = f.read() | |
146 | |
147 | |
148 classes = {} | |
149 enumerations = {} | |
150 globalFunctions = [] | |
2 | 151 countAllFunctions = 0 |
152 countSupportedFunctions = 0 | |
0 | 153 |
154 def IsSourceStringType(t): | |
155 return (t.kind == clang.cindex.TypeKind.POINTER and | |
156 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and | |
157 t.get_pointee().is_const_qualified()) | |
158 | |
159 def IsTargetStaticStringType(t): | |
160 return (t.kind == clang.cindex.TypeKind.POINTER and | |
161 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and | |
162 t.get_pointee().is_const_qualified()) | |
163 | |
164 def IsTargetDynamicStringType(t): | |
165 return (t.kind == clang.cindex.TypeKind.POINTER and | |
166 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S and | |
167 not t.get_pointee().is_const_qualified()) | |
168 | |
169 def IsIntegerType(t): | |
170 return (t.kind == clang.cindex.TypeKind.INT or | |
171 t.spelling in [ 'int8_t', 'int16_t', 'int32_t', 'int64_t', | |
172 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t']) | |
173 | |
174 def IsFloatType(t): | |
175 return t.kind == clang.cindex.TypeKind.FLOAT | |
176 | |
177 def IsEnumerationType(t): | |
178 return (t.kind == clang.cindex.TypeKind.TYPEDEF and | |
179 t.spelling in enumerations) | |
180 | |
181 def IsTargetMemoryBufferType(t): | |
182 return (t.kind == clang.cindex.TypeKind.POINTER and | |
183 not t.get_pointee().is_const_qualified() and | |
184 t.get_pointee().spelling == 'OrthancPluginMemoryBuffer') | |
185 | |
186 def IsSourceMemoryBufferType(t): | |
187 return (t.kind == clang.cindex.TypeKind.POINTER and | |
188 t.get_pointee().kind == clang.cindex.TypeKind.VOID and | |
189 t.get_pointee().is_const_qualified()) | |
190 | |
191 def IsClassType(t): | |
192 return (t.kind == clang.cindex.TypeKind.POINTER and | |
193 ((t.get_pointee().is_const_qualified() and | |
194 t.get_pointee().spelling.startswith('const ') and | |
195 t.get_pointee().spelling[len('const '):] in classes) or | |
196 (not t.get_pointee().is_const_qualified() and | |
197 t.get_pointee().spelling in classes))) | |
198 | |
199 def IsSimpleSourceType(t): | |
200 return (IsSourceStringType(t) or | |
201 IsFloatType(t) or | |
202 IsIntegerType(t) or | |
203 IsEnumerationType(t) or | |
204 IsSourceMemoryBufferType(t)) | |
205 | |
206 def IsVoidType(t): | |
207 return t.kind == clang.cindex.TypeKind.VOID | |
208 | |
209 def IsSupportedTargetType(t): | |
210 return (IsVoidType(t) or | |
211 IsIntegerType(t) or | |
212 IsEnumerationType(t) or | |
213 # Constructor of a class | |
214 (t.kind == clang.cindex.TypeKind.POINTER and | |
215 not t.get_pointee().is_const_qualified() and | |
216 t.get_pointee().spelling in classes) or | |
217 # "const char*" or "char*" outputs | |
218 (t.kind == clang.cindex.TypeKind.POINTER and | |
219 #not t.get_pointee().is_const_qualified() and | |
220 t.get_pointee().kind == clang.cindex.TypeKind.CHAR_S)) | |
221 | |
222 def IsBytesArgument(args, index): | |
223 return (index + 1 < len(args) and | |
224 args[index].type.kind == clang.cindex.TypeKind.POINTER and | |
225 args[index].type.get_pointee().kind == clang.cindex.TypeKind.VOID and | |
226 args[index].type.get_pointee().is_const_qualified() and | |
227 args[index + 1].type.spelling == 'uint32_t') | |
228 | |
229 def CheckOnlySupportedArguments(args): | |
230 j = 0 | |
231 while j < len(args): | |
232 if IsBytesArgument(args, j): | |
233 j += 2 | |
234 elif IsSimpleSourceType(args[j].type): | |
235 j += 1 | |
236 else: | |
237 return False | |
238 return True | |
239 | |
240 | |
241 ORTHANC_TO_PYTHON_NUMERIC_TYPES = { | |
242 # https://docs.python.org/3/c-api/arg.html#numbers | |
243 'int' : { | |
244 'type' : 'int', | |
245 'format' : 'i', | |
246 }, | |
247 'uint8_t' : { | |
248 'type' : 'unsigned char', | |
249 'format' : 'b', | |
250 }, | |
251 'int32_t' : { | |
252 'type' : 'long int', | |
253 'format' : 'l', | |
254 }, | |
255 'uint16_t' : { | |
256 'type' : 'unsigned short', | |
257 'format' : 'H', | |
258 }, | |
259 'uint32_t' : { | |
260 'type' : 'unsigned long', | |
261 'format' : 'k', | |
262 }, | |
263 'uint64_t' : { | |
264 'type' : 'unsigned long long', | |
265 'format' : 'K', | |
266 }, | |
267 'float' : { | |
268 'type' : 'float', | |
269 'format' : 'f', | |
270 } | |
271 } | |
272 | |
273 | |
274 def GenerateFunctionBodyTemplate(cFunction, result_type, args): | |
275 if not cFunction.startswith('OrthancPlugin'): | |
276 raise Exception() | |
277 | |
278 func = { | |
279 'c_function' : cFunction, | |
280 'short_name' : cFunction[len('OrthancPlugin'):], | |
281 'args' : [], | |
282 } | |
283 | |
284 if IsIntegerType(result_type): | |
285 func['return_long'] = True | |
286 elif IsTargetDynamicStringType(result_type): | |
287 func['return_dynamic_string'] = True | |
288 elif IsTargetStaticStringType(result_type): | |
289 func['return_static_string'] = True | |
290 elif IsVoidType(result_type): | |
291 func['return_void'] = True | |
292 elif result_type.spelling == 'OrthancPluginErrorCode': | |
293 func['return_error'] = True | |
294 elif IsClassType(result_type): | |
295 func['return_object'] = result_type.get_pointee().spelling | |
296 elif IsTargetMemoryBufferType(result_type): | |
297 func['return_bytes'] = True | |
298 elif IsEnumerationType(result_type): | |
299 func['return_enumeration'] = result_type.spelling | |
300 else: | |
301 raise Exception('Not supported: %s' % result_type.spelling) | |
302 | |
303 i = 0 | |
304 while i < len(args): | |
305 a = { | |
306 'name' : 'arg%d' % i, | |
307 } | |
308 | |
309 if (IsIntegerType(args[i].type) or | |
310 IsFloatType(args[i].type)): | |
311 t = ORTHANC_TO_PYTHON_NUMERIC_TYPES[args[i].type.spelling] | |
312 a['python_type'] = t['type'] | |
313 a['python_format'] = t['format'] | |
314 a['initialization'] = ' = 0' | |
315 a['orthanc_cast'] = 'arg%d' % i | |
316 func['args'].append(a) | |
317 elif IsSourceStringType(args[i].type): | |
318 a['python_type'] = 'const char*' | |
319 a['python_format'] = 's' | |
320 a['initialization'] = ' = NULL' | |
321 a['orthanc_cast'] = 'arg%d' % i | |
322 func['args'].append(a) | |
323 elif IsEnumerationType(args[i].type): | |
324 a['python_type'] = 'long int' | |
325 a['python_format'] = 'l' | |
326 a['initialization'] = ' = 0' | |
327 a['orthanc_cast'] = 'static_cast<%s>(arg%d)' % (args[i].type.spelling, i) | |
328 func['args'].append(a) | |
329 elif IsBytesArgument(args, i): | |
330 a['python_type'] = 'Py_buffer' | |
331 # In theory, one should use "y*" (this is the recommended | |
332 # way to accept binary data). However, this is not | |
333 # available in Python 2.7 | |
334 a['python_format'] = 's*' | |
335 a['orthanc_cast'] = 'arg%d.buf, arg%d.len' % (i, i) | |
336 a['release'] = 'PyBuffer_Release(&arg%d);' % i | |
337 func['args'].append(a) | |
338 i += 1 | |
339 elif IsSourceMemoryBufferType(args[i].type): | |
340 a['python_type'] = 'Py_buffer' | |
341 a['python_format'] = 's*' | |
342 a['orthanc_cast'] = 'arg%d.buf' % i | |
343 a['release'] = 'PyBuffer_Release(&arg%d);' % i | |
344 func['args'].append(a) | |
345 else: | |
346 raise Exception('Not supported: %s, %s' % (cFunction, args[i].spelling)) | |
347 | |
348 i += 1 | |
349 | |
350 func['tuple_format'] = '"%s", %s' % ( | |
351 ''.join(map(lambda x: x['python_format'], func['args'])), | |
352 ', '.join(map(lambda x: '&' + x['name'], func['args']))) | |
353 | |
354 if len(func['args']) > 0: | |
355 func['count_args'] = len(func['args']) | |
356 func['has_args'] = True | |
357 func['call_args'] = ', ' + ', '.join(map(lambda x: x['orthanc_cast'], func['args'])) | |
358 | |
359 return func | |
360 | |
361 | |
362 for node in tu.cursor.get_children(): | |
363 if node.kind == clang.cindex.CursorKind.ENUM_DECL: | |
364 if node.type.spelling.startswith('OrthancPlugin'): | |
365 name = node.type.spelling | |
366 | |
367 values = [] | |
368 for item in node.get_children(): | |
369 if (item.kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL and | |
370 item.spelling.startswith(name + '_')): | |
371 values.append({ | |
372 'key' : ToUpperCase(item.spelling[len(name)+1:]), | |
373 'value' : item.enum_value | |
374 }) | |
375 | |
376 path = 'sdk_%s.impl.h' % name | |
377 shortName = name[len('OrthancPlugin'):] | |
378 | |
379 with open(os.path.join(TARGET, path), 'w') as f: | |
380 f.write(pystache.render(TEMPLATE, { | |
381 'name' : name, | |
382 'short_name' : shortName, | |
383 'values' : values, | |
384 })) | |
385 | |
386 enumerations[name] = { | |
387 'name' : name, | |
388 'path' : path, | |
389 } | |
390 | |
391 elif node.kind == clang.cindex.CursorKind.FUNCTION_DECL: | |
392 if node.spelling.startswith('OrthancPlugin'): | |
393 #if node.spelling != 'OrthancPluginWorklistGetDicomQuery': | |
394 # continue | |
395 shortName = node.spelling[len('OrthancPlugin'):] | |
396 | |
397 # Check that the first argument is the Orthanc context | |
398 args = list(filter(lambda x: x.kind == clang.cindex.CursorKind.PARM_DECL, | |
399 node.get_children())) | |
400 | |
401 if (len(args) == 0 or | |
402 args[0].type.kind != clang.cindex.TypeKind.POINTER or | |
403 args[0].type.get_pointee().spelling != 'OrthancPluginContext'): | |
404 print('Not in the Orthanc SDK: %s()' % node.spelling) | |
405 continue | |
406 | |
407 # Discard the context from the arguments | |
2 | 408 countAllFunctions += 1 |
0 | 409 args = args[1:] |
410 | |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
411 if node.spelling in CUSTOM_FUNCTIONS: |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
412 print('Ignoring custom function that is manually implemented: %s()' % node.spelling) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
413 countSupportedFunctions += 1 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
414 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
415 elif not IsSupportedTargetType(node.result_type): |
0 | 416 print('*** UNSUPPORTED OUTPUT: %s' % node.spelling) |
417 | |
418 elif (len(args) == 1 and | |
419 IsClassType(args[0].type) and | |
420 node.spelling.startswith('OrthancPluginFree')): | |
421 print('Destructor: %s' % node.spelling) | |
422 className = args[0].type.get_pointee().spelling | |
423 classes[className]['destructor'] = node.spelling | |
2 | 424 countSupportedFunctions += 1 |
0 | 425 |
426 elif CheckOnlySupportedArguments(args): | |
427 if IsClassType(node.result_type): | |
428 print('Constructor: %s' % node.spelling) | |
429 else: | |
430 print('Simple global function: %s => %s' % (node.spelling, node.result_type.spelling)) | |
431 | |
432 body = GenerateFunctionBodyTemplate(node.spelling, node.result_type, args) | |
433 globalFunctions.append(body) | |
2 | 434 countSupportedFunctions += 1 |
0 | 435 |
436 elif (len(args) >= 2 and | |
437 IsTargetMemoryBufferType(args[0].type) and | |
438 CheckOnlySupportedArguments(args[1:])): | |
439 print('Simple global function, returning bytes: %s' % node.spelling) | |
440 | |
441 body = GenerateFunctionBodyTemplate(node.spelling, args[0].type, args[1:]) | |
442 globalFunctions.append(body) | |
2 | 443 countSupportedFunctions += 1 |
0 | 444 |
445 elif (IsClassType(args[0].type) and | |
446 CheckOnlySupportedArguments(args[1:])): | |
447 className = args[0].type.get_pointee().spelling | |
448 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
449 print('Simple method of class %s: %s' % (className, node.spelling)) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
450 if node.spelling in CUSTOM_FUNCTIONS: |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
451 raise Exception('Cannot overwrite an autogenerated method: %s()' % node.spelling) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
452 |
0 | 453 if className.startswith('const '): |
454 className = className[len('const '):] | |
455 | |
456 method = GenerateFunctionBodyTemplate(node.spelling, node.result_type, args[1:]) | |
457 method['self'] = ', self->object_' | |
458 classes[className]['methods'].append(method) | |
2 | 459 countSupportedFunctions += 1 |
0 | 460 |
461 elif (len(args) >= 2 and | |
462 IsTargetMemoryBufferType(args[0].type) and | |
463 IsClassType(args[1].type) and | |
464 CheckOnlySupportedArguments(args[2:])): | |
31 | 465 className = args[1].type.get_pointee().spelling |
466 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
467 print('Simple method of class %s, returning bytes: %s' % (className, node.spelling)) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
468 if node.spelling in CUSTOM_FUNCTIONS: |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
469 raise Exception('Cannot overwrite an autogenerated method: %s()' % node.spelling) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
470 |
31 | 471 if className.startswith('const '): |
472 className = className[len('const '):] | |
473 | |
0 | 474 method = GenerateFunctionBodyTemplate(node.spelling, args[0].type, args[2:]) |
475 method['self'] = ', self->object_' | |
476 classes[className]['methods'].append(method) | |
2 | 477 countSupportedFunctions += 1 |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
478 |
0 | 479 else: |
480 print('*** UNSUPPORTED INPUT: %s' % node.spelling) | |
481 | |
482 | |
483 | |
484 elif node.kind == clang.cindex.CursorKind.STRUCT_DECL: | |
485 if (node.spelling.startswith('_OrthancPlugin') and | |
486 node.spelling.endswith('_t') and | |
487 node.spelling != '_OrthancPluginContext_t'): | |
488 name = node.spelling[len('_') : -len('_t')] | |
489 classes[name] = { | |
490 'class_name' : name, | |
491 'short_name' : name[len('OrthancPlugin'):], | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
492 'methods' : [ ], |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
493 'custom_methods' : [ ], |
0 | 494 } |
495 | |
496 | |
497 | |
498 | |
499 partials = {} | |
500 | |
501 with open(os.path.join(ROOT, 'FunctionBody.mustache'), 'r') as f: | |
502 partials['function_body'] = f.read() | |
503 | |
504 renderer = pystache.Renderer( | |
505 escape = lambda u: u, # No escaping | |
506 partials = partials, | |
507 ) | |
508 | |
509 with open(os.path.join(ROOT, 'Class.mustache'), 'r') as f: | |
510 template = f.read() | |
511 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
512 for method in CUSTOM_METHODS: |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
513 classes[method['class_name']]['custom_methods'].append(method) |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
514 |
0 | 515 for (key, value) in classes.items(): |
516 with open(os.path.join(TARGET, 'sdk_%s.impl.h' % value['class_name']), 'w') as h: | |
517 h.write(renderer.render(template, value)) | |
518 | |
519 | |
520 def FlattenDictionary(source): | |
521 result = [] | |
522 for (key, value) in source.items(): | |
523 result.append(value) | |
524 return result | |
525 | |
526 | |
527 with open(os.path.join(ROOT, 'GlobalFunctions.mustache'), 'r') as f: | |
528 with open(os.path.join(TARGET, 'sdk_GlobalFunctions.impl.h'), 'w') as h: | |
529 h.write(renderer.render(f.read(), { | |
530 'global_functions' : globalFunctions, | |
531 })) | |
532 | |
533 with open(os.path.join(ROOT, 'sdk.cpp.mustache'), 'r') as f: | |
534 with open(os.path.join(TARGET, 'sdk.cpp'), 'w') as h: | |
535 h.write(renderer.render(f.read(), { | |
536 'classes' : FlattenDictionary(classes), | |
537 'enumerations' : FlattenDictionary(enumerations), | |
538 'global_functions' : globalFunctions, | |
539 })) | |
540 | |
541 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f: | |
542 with open(os.path.join(TARGET, 'sdk.h'), 'w') as h: | |
543 h.write(renderer.render(f.read(), { | |
544 'classes' : FlattenDictionary(classes), | |
545 })) | |
2 | 546 |
547 | |
548 print('') | |
549 print('Total functions in the SDK: %d' % countAllFunctions) | |
550 print('Total supported functions: %d' % countSupportedFunctions) | |
551 print('Coverage: %.0f%%' % (float(countSupportedFunctions) / | |
552 float(countAllFunctions) * 100.0)) |