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