annotate CodeAnalysis/GenerateOrthancSDK.py @ 220:7ecdfdcb49d5

NEWS
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Jul 2024 08:32:24 +0200
parents 3678a028f1f6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python3
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
219
3678a028f1f6 making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 206
diff changeset
3 # SPDX-FileCopyrightText: 2020-2023 Osimis S.A., 2024-2024 Orthanc Team SRL, 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain
3678a028f1f6 making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 206
diff changeset
4 # SPDX-License-Identifier: AGPL-3.0-or-later
3678a028f1f6 making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 206
diff changeset
5
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 ## Python plugin for Orthanc
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 ## Copyright (C) 2020-2023 Osimis S.A., Belgium
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 ## Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 ## Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 ## This program is free software: you can redistribute it and/or
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 ## modify it under the terms of the GNU Affero General Public License
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 ## as published by the Free Software Foundation, either version 3 of
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 ## the License, or (at your option) any later version.
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 ## This program is distributed in the hope that it will be useful, but
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 ## WITHOUT ANY WARRANTY; without even the implied warranty of
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 ## Affero General Public License for more details.
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 ## You should have received a copy of the GNU Affero General Public License
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import argparse
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 import json
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 import os
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 import sys
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 import pystache
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 ROOT = os.path.dirname(os.path.realpath(sys.argv[0]))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 ## Parse the command-line arguments
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 parser = argparse.ArgumentParser(description = 'Generate Python code to wrap the Orthanc SDK.')
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 parser.add_argument('--model',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 default = os.path.join(os.path.dirname(__file__),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 '../Resources/Orthanc/Sdk-1.10.0/CodeModel.json'),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 help = 'Input code model, as generated by the orthanc-java project')
194
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
45 parser.add_argument('--classes',
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
46 default = os.path.join(os.path.dirname(__file__),
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
47 '../Resources/Orthanc/Sdk-1.10.0/ClassDocumentation.json'),
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
48 help = 'Input description of classes, as defined in the orthanc-java project')
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 parser.add_argument('--target',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 default = os.path.join(os.path.dirname(__file__),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 '../Sources/Autogenerated'),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 help = 'Target folder')
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 args = parser.parse_args()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ## Configuration of the custom primitives that are manually
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 ## implemented (not autogenerated)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 ##
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
183
d31f216c9462 extracted custom functions/methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 182
diff changeset
63 TARGET = os.path.realpath(args.target)
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
183
d31f216c9462 extracted custom functions/methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 182
diff changeset
66 with open(os.path.join(ROOT, 'CustomMethods.json'), 'r') as f:
d31f216c9462 extracted custom functions/methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 182
diff changeset
67 CUSTOM_METHODS = json.loads(f.read())
182
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
68
183
d31f216c9462 extracted custom functions/methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 182
diff changeset
69 with open(os.path.join(ROOT, 'CustomFunctions.json'), 'r') as f:
d31f216c9462 extracted custom functions/methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 182
diff changeset
70 CUSTOM_FUNCTIONS = json.loads(f.read())
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 partials = {}
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 with open(os.path.join(ROOT, 'FunctionBody.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 partials['function_body'] = f.read()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
78 with open(os.path.join(ROOT, 'FunctionDocumentation.mustache'), 'r') as f:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
79 partials['function_documentation'] = f.read()
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
80
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 renderer = pystache.Renderer(
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 escape = lambda u: u, # No escaping
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 partials = partials,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 )
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 with open(args.model, 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 model = json.loads(f.read())
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
194
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
91 with open(args.classes, 'r') as f:
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
92 classes_description = json.loads(f.read())
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
93
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 def ToUpperCase(name):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 s = ''
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 for i in range(len(name)):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 if name[i].isupper():
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if len(s) == 0:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 s += name[i]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 elif name[i - 1].islower():
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 s += '_' + name[i]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 elif (i + 1 < len(name) and
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 name[i - 1].islower() and
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 name[i + 1].isupper()):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 s += '_' + name[i]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 s += name[i]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 s += name[i].upper()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 return s
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
114 def ToLowerCase(name):
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
115 s = ''
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
116 for i in range(len(name)):
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
117 if (name[i].isupper() and
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
118 len(s) != 0):
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
119 s += '_'
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
120 s += name[i].lower()
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
121 return s
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
122
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
123
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 def GetShortName(name):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if not name.startswith('OrthancPlugin'):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 raise Exception()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 return name[len('OrthancPlugin'):]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 ORTHANC_TO_PYTHON_NUMERIC_TYPES = {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 # https://docs.python.org/3/c-api/arg.html#numbers
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 # https://en.wikipedia.org/wiki/C_data_types
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 'uint8_t' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 'type' : 'unsigned char',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 'format' : 'b',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 },
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 'int32_t' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 'type' : 'long int',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 'format' : 'l',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 },
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 'uint16_t' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 'type' : 'unsigned short',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 'format' : 'H',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 },
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 'uint32_t' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 'type' : 'unsigned long',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 'format' : 'k',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 },
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 'uint64_t' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 'type' : 'unsigned long long',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 'format' : 'K',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 },
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 'float' : {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 'type' : 'float',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 'format' : 'f',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 }
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 }
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
162 def DocumentFunction(f):
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
163 documentation = {}
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
164 description = f['documentation'].get('description', [])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
165 if len(description) > 0:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
166 documentation['short_description'] = description[0].split('.') [0]
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
167 documentation['description'] = map(lambda x: { 'text' : x }, description)
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
168
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
169 args_declaration = []
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
170 args_documentation = []
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
171 for a in f['args']:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
172 arg_name = ToLowerCase(a['sdk_name'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
173 if a['sdk_type'] == 'const char *':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
174 arg_type = 'str'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
175 elif a['sdk_type'] == 'float':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
176 arg_type = 'float'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
177 elif a['sdk_type'] in [ 'const_void_pointer_with_size', 'const void *' ]:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
178 arg_type = 'bytes'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
179 elif a['sdk_type'] == 'enumeration':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
180 arg_type = GetShortName(a['sdk_enumeration'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
181 elif a['sdk_type'] == 'const_object':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
182 arg_type = GetShortName(a['sdk_class'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
183 elif a['sdk_type'] in [ 'int32_t', 'uint32_t', 'uint8_t', 'uint16_t', 'uint64_t' ]:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
184 arg_type = 'int'
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
185 elif a['sdk_type'] == 'Callable':
205
234681297600 documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
186 # This is only used to generate the documentation file "orthanc.pyi"
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
187 arg_type = a['callable_type']
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
188 else:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
189 raise Exception('Argument type not implemented: %s' % a['sdk_type'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
190 args_declaration.append('%s: %s' % (arg_name, arg_type))
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
191 args_documentation.append({
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
192 'name' : arg_name,
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
193 'type' : arg_type,
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
194 'text' : f['documentation']['args'] [a['sdk_name']],
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
195 })
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
196
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
197 documentation['args_declaration'] = ', '.join(args_declaration)
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
198 documentation['args'] = args_documentation
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
199 documentation['has_args'] = len(args_documentation) > 0
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
200 documentation['has_return'] = True
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
201
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
202 if f['return_sdk_type'] == 'enumeration':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
203 if f['return_sdk_enumeration'] == 'OrthancPluginErrorCode':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
204 documentation['has_return'] = False
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
205 documentation['return_type'] = 'None'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
206 else:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
207 documentation['return_type'] = GetShortName(f['return_sdk_enumeration'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
208 elif f['return_sdk_type'] == 'object':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
209 documentation['return_type'] = GetShortName(f['return_sdk_class'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
210 elif f['return_sdk_type'] == 'void':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
211 documentation['has_return'] = False
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
212 documentation['return_type'] = 'None'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
213 elif f['return_sdk_type'] == 'OrthancPluginMemoryBuffer *':
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
214 documentation['return_type'] = 'bytes'
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
215 elif f['return_sdk_type'] in [ 'char *', 'const char *' ]:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
216 documentation['return_type'] = 'str'
182
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
217 elif f['return_sdk_type'] in [ 'int32_t', 'uint32_t', 'uint16_t', 'int64_t' ]:
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
218 documentation['return_type'] = 'int'
205
234681297600 documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
219 elif f['return_sdk_type'] == 'Dictionary':
234681297600 documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
220 # This is only used to generate the documentation file "orthanc.pyi"
234681297600 documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
221 documentation['return_type'] = 'dict'
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
222 else:
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
223 raise Exception('Return type not implemented: %s' % f['return_sdk_type'])
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
224
182
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
225 if documentation['has_return']:
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
226 documentation['return_text'] = f['documentation']['return']
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
227
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
228 return documentation
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
229
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
230
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 def FormatFunction(f):
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 answer = {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 'c_function' : f['c_function'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 'short_name' : GetShortName(f['c_function']),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 'has_args' : len(f['args']) > 0,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 'count_args' : len(f['args']),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 tuple_format = ''
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 tuple_target = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 call_args = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 args = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 for arg in f['args']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 # https://docs.python.org/3/c-api/arg.html
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 if arg['sdk_type'] in [ 'const void *', 'const_void_pointer_with_size' ]:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 args.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 'name' : arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 'python_type' : 'Py_buffer',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 'release' : 'PyBuffer_Release(&%s);' % arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 tuple_format += 's*'
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 elif arg['sdk_type'] == 'const char *':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 args.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 'name' : arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 'python_type' : 'const char*',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 'initialization' : ' = NULL',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 tuple_format += 's'
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 elif arg['sdk_type'] == 'enumeration':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 args.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 'name' : arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 'python_type' : 'long int',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 'initialization' : ' = 0',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 tuple_format += 'l'
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 elif arg['sdk_type'] == 'const_object':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 args.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 'name' : arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 'python_type' : 'PyObject*',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 'initialization' : ' = NULL',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 'check_object_type' : arg['sdk_class'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 tuple_format += 'O'
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 elif arg['sdk_type'] in ORTHANC_TO_PYTHON_NUMERIC_TYPES:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 t = ORTHANC_TO_PYTHON_NUMERIC_TYPES[arg['sdk_type']]
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 args.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 'name' : arg['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 'python_type' : t['type'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 'initialization' : ' = 0',
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 tuple_format += t['format']
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 print('Ignoring function with unsupported argument type: %s(), type = %s' % (f['c_function'], arg['sdk_type']))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 return None
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 tuple_target.append('&' + arg['name'])
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 if arg['sdk_type'] == 'const void *':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 call_args.append(arg['name'] + '.buf')
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 elif arg['sdk_type'] == 'const_void_pointer_with_size':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 call_args.append(arg['name'] + '.buf')
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 call_args.append(arg['name'] + '.len')
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 elif arg['sdk_type'] == 'enumeration':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 call_args.append('static_cast<%s>(%s)' % (arg['sdk_enumeration'], arg['name']))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 elif arg['sdk_type'] == 'const_object':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 call_args.append('%s == Py_None ? NULL : reinterpret_cast<sdk_%s_Object*>(%s)->object_' % (
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 arg['name'], arg['sdk_class'], arg['name']))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 call_args.append(arg['name'])
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 answer['args'] = args
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 if f['return_sdk_type'] == 'void':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 answer['return_void'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 elif f['return_sdk_type'] in [ 'int32_t', 'uint32_t', 'int64_t' ]:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 answer['return_long'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 elif f['return_sdk_type'] == 'OrthancPluginMemoryBuffer *':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 answer['return_bytes'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 elif f['return_sdk_type'] == 'enumeration':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 if f['return_sdk_enumeration'] == 'OrthancPluginErrorCode':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 answer['return_error'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 answer['return_enumeration'] = f['return_sdk_enumeration']
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 elif f['return_sdk_type'] == 'char *':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 answer['return_dynamic_string'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 elif f['return_sdk_type'] == 'const char *':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 answer['return_static_string'] = True
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 elif f['return_sdk_type'] == 'object':
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 answer['return_object'] = f['return_sdk_class']
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 else:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 print('Ignoring function with unsupported return type: %s(), type = %s' % (f['c_function'], f['return_sdk_type']))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 return None
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
327 if 'documentation' in f:
181
faaa3fec799a refactoring using FunctionDocumentation.mustache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
328 answer['documentation'] = DocumentFunction(f)
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
329
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 if len(call_args) > 0:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 answer['call_args'] = ', ' + ', '.join(call_args)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 return answer
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 globalFunctions = []
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
338 customFunctions = []
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 for f in model['global_functions']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 g = FormatFunction(f)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 if g != None:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 globalFunctions.append(g)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 for f in CUSTOM_FUNCTIONS:
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
346 f['documentation'] = DocumentFunction(f)
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
347 customFunctions.append(f)
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 enumerations = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 with open(os.path.join(ROOT, 'Enumeration.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 ENUMERATION_TEMPLATE = f.read()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 for e in model['enumerations']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 values = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 for value in e['values']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 values.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 'key' : ToUpperCase(value['key']),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 'value' : value['value'],
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
361 'documentation' : value['documentation'],
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 enumerations.append({
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 'name' : e['name'],
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
366 'short_name' : GetShortName(e['name']),
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 'path' : 'sdk_%s.impl.h' % e['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 'values' : values,
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
369 'documentation' : e['documentation'],
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 path = 'sdk_%s.impl.h' % e['name']
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 with open(os.path.join(TARGET, path), 'w') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 f.write(pystache.render(ENUMERATION_TEMPLATE, {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 'name' : e['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 'short_name' : GetShortName(e['name']),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 'values' : values,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 }))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 classes = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383
204
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
384 countDestructors = 0
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
385
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 for c in model['classes']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 methods = []
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 for m in c['methods']:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 g = FormatFunction(m)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 if g != None:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 g['self'] = ', self->object_'
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 methods.append(g)
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394
182
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
395 custom_methods = []
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
396
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
397 if c['name'] in CUSTOM_METHODS:
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
398 for custom_method in CUSTOM_METHODS[c['name']]:
185
7e8085f80df5 simplification
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 184
diff changeset
399 custom_method['self'] = True # Indicates that this is a method
182
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
400 custom_method['documentation'] = DocumentFunction(custom_method)
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
401 custom_methods.append(custom_method)
20b004998fc2 documentation of custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 181
diff changeset
402
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 classes.append({
194
0c5da632f34d added description of classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
404 'description' : classes_description[c['name']],
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 'class_name' : c['name'],
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 'short_name' : GetShortName(c['name']),
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 'methods' : methods,
193
66cd20e970d8 simplification for custom methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
408 'custom_methods' : sorted(custom_methods, key = lambda x: x['short_name']),
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 })
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 if 'destructor' in c:
204
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
412 countDestructors += 1
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 classes[-1]['destructor'] = c['destructor']
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 with open(os.path.join(ROOT, 'Class.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 with open(os.path.join(ROOT, 'ClassMethods.mustache'), 'r') as g:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 classDefinition = f.read()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 classMethods = g.read()
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 for c in classes:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 with open(os.path.join(TARGET, 'sdk_%s.impl.h' % c['class_name']), 'w') as h:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 h.write(renderer.render(classDefinition, c))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 with open(os.path.join(TARGET, 'sdk_%s.methods.h' % c['class_name']), 'w') as h:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 h.write(renderer.render(classMethods, c))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 sortedClasses = sorted(classes, key = lambda x: x['class_name'])
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 sortedEnumerations = sorted(enumerations, key = lambda x: x['name'])
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 sortedGlobalFunctions = sorted(globalFunctions, key = lambda x: x['c_function'])
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
433 sortedCustomFunctions = sorted(customFunctions, key = lambda x: x['short_name'])
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 with open(os.path.join(ROOT, 'GlobalFunctions.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 with open(os.path.join(TARGET, 'sdk_GlobalFunctions.impl.h'), 'w') as h:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 h.write(renderer.render(f.read(), {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 'global_functions' : sortedGlobalFunctions,
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
439 'custom_functions' : sortedCustomFunctions,
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 }))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 with open(os.path.join(ROOT, 'sdk.cpp.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 with open(os.path.join(TARGET, 'sdk.cpp'), 'w') as h:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 h.write(renderer.render(f.read(), {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 'classes' : sortedClasses,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 'enumerations' : sortedEnumerations,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 'global_functions' : sortedGlobalFunctions,
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
448 'custom_functions' : sortedCustomFunctions,
172
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 }))
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 with open(os.path.join(TARGET, 'sdk.h'), 'w') as h:
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 h.write(renderer.render(f.read(), {
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 'classes' : sortedClasses,
8382c7dea471 created CodeAnalysis/GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 }))
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
456
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
457 with open(os.path.join(ROOT, 'PythonDocumentation.mustache'), 'r') as f:
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
458 with open(os.path.join(TARGET, 'orthanc.pyi'), 'w') as h:
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
459 h.write(renderer.render(f.read(), {
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
460 'classes' : sortedClasses,
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
461 'enumerations' : sortedEnumerations,
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
462 'global_functions' : sortedGlobalFunctions,
192
b0f096d2339e documentation of orthanc.RegisterRestCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 190
diff changeset
463 'custom_functions' : sortedCustomFunctions,
180
ddf3e987827f created Python documentation for the Orthanc interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
464 }))
190
0cb98433a382 print statistics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 185
diff changeset
465
0cb98433a382 print statistics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 185
diff changeset
466
204
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
467
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
468 ##
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
469 ## Print statistics
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
470 ##
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
471
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
472 countWrappedMethods = 0
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
473 countCustomMethods = 0
190
0cb98433a382 print statistics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 185
diff changeset
474 for c in sortedClasses:
204
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
475 countWrappedMethods += len(c['methods'])
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
476 countCustomMethods += len(c['custom_methods'])
190
0cb98433a382 print statistics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 185
diff changeset
477
204
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
478 print('\nNumber of automatically wrapped global functions: %d' % len(sortedGlobalFunctions))
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
479 print('Number of automatically wrapped methods: %d' % countWrappedMethods)
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
480 print('Number of automatically wrapped destructors: %d' % countDestructors)
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
481 print('Number of manually implemented (custom) global functions: %d' % len(sortedCustomFunctions))
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
482 print('Number of manually implemented (custom) methods: %d' % countCustomMethods)
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
483
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
484 totalWrapped = (len(sortedGlobalFunctions) + countWrappedMethods + countDestructors)
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
485 print('\nTotal number of automatically wrapped functions (including destructors): %d' % totalWrapped)
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
486 print('NB: This number must correspond to "ParseOrthancSDK.py" in "orthanc-java"')
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
487
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
488 total = totalWrapped + len(sortedCustomFunctions) + countCustomMethods
65ad095c25d8 report statistics in GenerateOrthancSDK.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 194
diff changeset
489 print('\n=> Total number of functions or methods in the Python SDK: %d\n' % total)