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