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