0
|
1 /**
|
|
2 * Python plugin for Orthanc
|
|
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
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
|
|
20 #include "OnStoredInstanceCallback.h"
|
|
21
|
|
22 #include "PythonObject.h"
|
|
23 #include "Autogenerated/sdk.h"
|
|
24
|
|
25 #include <OrthancPluginCppWrapper.h>
|
|
26
|
|
27
|
|
28 static PyObject* storedInstanceCallback_ = NULL;
|
|
29
|
|
30
|
|
31 static OrthancPluginErrorCode OnStoredInstanceCallback(OrthancPluginDicomInstance *instance,
|
|
32 const char *instanceId)
|
|
33 {
|
|
34 try
|
|
35 {
|
|
36 PythonLock lock;
|
|
37
|
|
38 /**
|
|
39 * Construct an instance object of the "orthanc.RestOutput"
|
|
40 * class. This is done by calling the constructor function
|
|
41 * "sdk_OrthancPluginRestOutput_Type".
|
|
42 **/
|
|
43 PythonObject args(lock, PyTuple_New(2));
|
|
44 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) instance));
|
|
45 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */));
|
|
46 PyObject *pInst = PyObject_CallObject(GetOrthancPluginDicomInstanceType(), args.GetPyObject());
|
|
47
|
|
48 /**
|
|
49 * Construct the arguments tuple (output, uri)
|
|
50 **/
|
|
51 PythonObject args2(lock, PyTuple_New(2));
|
|
52 PyTuple_SetItem(args2.GetPyObject(), 0, pInst);
|
|
53 PyTuple_SetItem(args2.GetPyObject(), 1, PyUnicode_FromString(instanceId));
|
|
54
|
|
55 PythonObject result(lock, PyObject_CallObject(storedInstanceCallback_, args2.GetPyObject()));
|
|
56 return OrthancPluginErrorCode_Success;
|
|
57 }
|
|
58 catch (OrthancPlugins::PluginException& e)
|
|
59 {
|
|
60 return e.GetErrorCode();
|
|
61 }
|
|
62 }
|
|
63
|
|
64
|
|
65 PyObject* RegisterOnStoredInstanceCallback(PyObject* module, PyObject* args)
|
|
66 {
|
|
67 // The GIL is locked at this point (no need to create "PythonLock")
|
|
68
|
|
69 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c
|
|
70 PyObject* callback = NULL;
|
|
71
|
|
72 if (!PyArg_ParseTuple(args, "O", &callback) ||
|
|
73 callback == NULL)
|
|
74 {
|
|
75 PyErr_SetString(PyExc_ValueError, "Expected a callback function");
|
|
76 return NULL;
|
|
77 }
|
|
78
|
|
79 if (storedInstanceCallback_ != NULL)
|
|
80 {
|
|
81 PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-stored-instance callback");
|
|
82 return NULL;
|
|
83 }
|
|
84
|
|
85 OrthancPlugins::LogInfo("Registering a Python on-stored-instance callback");
|
|
86
|
|
87 OrthancPluginRegisterOnStoredInstanceCallback(
|
|
88 OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback);
|
|
89
|
|
90 storedInstanceCallback_ = callback;
|
|
91 Py_XINCREF(storedInstanceCallback_);
|
|
92
|
|
93 Py_INCREF(Py_None);
|
|
94 return Py_None;
|
|
95 }
|
|
96
|
|
97
|
|
98 void FinalizeOnStoredInstanceCallback()
|
|
99 {
|
|
100 PythonLock lock;
|
|
101
|
|
102 if (storedInstanceCallback_ != NULL)
|
|
103 {
|
|
104 Py_XDECREF(storedInstanceCallback_);
|
|
105 }
|
|
106 }
|