Mercurial > hg > orthanc-python
annotate CodeAnalysis/Class.mustache @ 69:07fdf388c12e
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 14:37:40 +0200 |
parents | 32de70a1e4c7 |
children | cbfc72a53970 |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
56
23f3099bed47
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
28
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
20 // Forward declaration of the autogenerated methods |
0 | 21 {{#methods}} |
22 static PyObject *sdk_{{class_name}}_{{c_function}}( | |
23 sdk_{{class_name}}_Object* self, PyObject *args); | |
24 {{/methods}} | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
25 // End of forward declarations |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
26 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
27 |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
28 // Forward declaration of the custom methods |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
29 {{#custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
30 extern PyObject *{{implementation}}( |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
31 sdk_{{class_name}}_Object* self, PyObject *args); |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
32 {{/custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
33 // End of forward declarations |
0 | 34 |
35 | |
36 static PyMethodDef sdk_{{class_name}}_Methods[] = { | |
37 {{#methods}} | |
38 { "{{short_name}}", | |
39 (PyCFunction) sdk_{{class_name}}_{{c_function}}, METH_VARARGS, | |
40 "Generated from C function {{c_function}}()" }, | |
41 {{/methods}} | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
42 {{#custom_methods}} |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
43 { "{{method_name}}", |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
44 (PyCFunction) {{implementation}}, METH_VARARGS, |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
45 "Generated from C function {{sdk_function}}()" }, |
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
46 {{/custom_methods}} |
0 | 47 { NULL } /* Sentinel */ |
48 }; | |
49 | |
50 | |
51 static int sdk_{{class_name}}_Constructor( | |
52 sdk_{{class_name}}_Object *self, PyObject *args, PyObject *kwds) | |
53 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
54 PythonLock::LogCall("Creating Python object of class {{class_name}}"); |
0 | 55 |
56 self->object_ = NULL; | |
57 self->borrowed_ = false; | |
58 | |
59 long long object = 0; | |
60 unsigned char borrowed = false; | |
61 | |
62 if (PyArg_ParseTuple(args, "Lb", &object, &borrowed)) | |
63 { | |
64 self->object_ = reinterpret_cast<{{class_name}}*>(static_cast<intptr_t>(object)); | |
65 self->borrowed_ = borrowed; | |
66 return 0; | |
67 } | |
68 else | |
69 { | |
70 PyErr_SetString(PyExc_ValueError, "Expected a pair (pointer, borrowed) in the constructor"); | |
71 return -1; | |
72 } | |
73 } | |
74 | |
75 | |
76 /** | |
77 * Static global structure => the fields that are beyond the last | |
78 * initialized field are set to zero. | |
79 * https://stackoverflow.com/a/11152199/881731 | |
80 **/ | |
81 static PyTypeObject sdk_{{class_name}}_Type = { | |
82 PyVarObject_HEAD_INIT(NULL, 0) | |
83 "orthanc.{{short_name}}", /* tp_name */ | |
84 sizeof(sdk_{{class_name}}_Object), /* tp_basicsize */ | |
85 }; | |
86 | |
87 | |
88 {{#destructor}} | |
89 static void sdk_{{class_name}}_Destructor(PyObject *self) | |
90 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
91 PythonLock::LogCall("Destroying Python object of class {{class_name}}"); |
0 | 92 |
93 sdk_{{class_name}}_Object& tmp = *((sdk_{{class_name}}_Object*) self); | |
94 | |
95 if (tmp.object_ != NULL && | |
96 !tmp.borrowed_) | |
97 { | |
98 {{destructor}}(OrthancPlugins::GetGlobalContext(), tmp.object_); | |
99 tmp.object_ = NULL; | |
100 } | |
101 | |
102 Py_TYPE(self)->tp_free((PyObject *)self); | |
103 } | |
104 {{/destructor}} | |
105 | |
106 | |
107 // Actual implementation of the methods | |
108 {{#methods}} | |
109 static PyObject *sdk_{{class_name}}_{{c_function}}( | |
110 sdk_{{class_name}}_Object* self, PyObject *args) | |
111 { | |
28
b2bbb516056e
The "Calling Python..." info logs are disabled if "PythonVerbose" is "false"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
112 PythonLock::LogCall("Calling method {{c_function}}() on object of class {{class_name}}"); |
0 | 113 |
114 if (self->object_ == NULL) | |
115 { | |
116 // TODO: RAISE | |
117 //PythonLock::RaiseException(module, OrthancPluginErrorCode_NullPointer); | |
118 PyErr_SetString(PyExc_ValueError, "Invalid object"); | |
119 return NULL; | |
120 } | |
121 | |
122 {{> function_body}} | |
123 } | |
124 | |
125 {{/methods}} | |
126 | |
127 | |
128 static void Register{{class_name}}Class(PyObject* module) | |
129 { | |
130 sdk_{{class_name}}_Type.tp_new = PyType_GenericNew; | |
131 sdk_{{class_name}}_Type.tp_flags = Py_TPFLAGS_DEFAULT; | |
132 sdk_{{class_name}}_Type.tp_doc = "Generated from Orthanc C class: {{class_name}}"; | |
133 sdk_{{class_name}}_Type.tp_methods = sdk_{{class_name}}_Methods; | |
134 sdk_{{class_name}}_Type.tp_init = (initproc) sdk_{{class_name}}_Constructor; | |
135 | |
136 {{#destructor}} | |
137 /** | |
138 * "tp_dealloc is called when the reference count of the object goes | |
139 * down to zero. This is where you destroy the object and its | |
140 * members. It should then free the memory occupied by the object by | |
141 * calling tp_free." | |
142 * https://stackoverflow.com/a/24863227/881731 | |
143 **/ | |
144 sdk_{{class_name}}_Type.tp_dealloc = sdk_{{class_name}}_Destructor; | |
145 {{/destructor}} | |
146 | |
147 if (PyType_Ready(&sdk_{{class_name}}_Type) < 0) | |
148 { | |
149 OrthancPlugins::LogError("Cannot register Python class: {{class_name}}"); | |
150 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
151 } | |
152 | |
153 Py_INCREF(&sdk_{{class_name}}_Type); | |
154 if (PyModule_AddObject(module, "{{short_name}}", (PyObject *)&sdk_{{class_name}}_Type) < 0) | |
155 { | |
156 OrthancPlugins::LogError("Cannot register Python class: {{class_name}}"); | |
157 Py_DECREF(&sdk_{{class_name}}_Type); | |
158 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
159 } | |
160 } | |
161 | |
162 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
163 PyTypeObject* Get{{class_name}}Type() |
0 | 164 { |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
165 return &sdk_{{class_name}}_Type; |
0 | 166 } |