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