Mercurial > hg > orthanc-python
annotate Sources/OnStoredInstanceCallback.cpp @ 16:777b677cc9fc OrthancPython-1.0
generation of windows resources
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 01 Apr 2020 10:43:11 +0200 |
parents | 26762eb9d704 |
children | fd58eb5749ed |
rev | line source |
---|---|
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())); | |
3
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
56 |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
57 std::string traceback; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
58 if (lock.HasErrorOccurred(traceback)) |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
59 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
60 OrthancPlugins::LogError("Error in the Python on-change callback, " |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
61 "traceback:\n" + traceback); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
62 return OrthancPluginErrorCode_Plugin; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
63 } |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
64 else |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
65 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
66 return OrthancPluginErrorCode_Success; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
67 } |
0 | 68 } |
69 catch (OrthancPlugins::PluginException& e) | |
70 { | |
71 return e.GetErrorCode(); | |
72 } | |
73 } | |
74 | |
75 | |
76 PyObject* RegisterOnStoredInstanceCallback(PyObject* module, PyObject* args) | |
77 { | |
78 // The GIL is locked at this point (no need to create "PythonLock") | |
79 | |
80 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
81 PyObject* callback = NULL; | |
82 | |
83 if (!PyArg_ParseTuple(args, "O", &callback) || | |
84 callback == NULL) | |
85 { | |
86 PyErr_SetString(PyExc_ValueError, "Expected a callback function"); | |
87 return NULL; | |
88 } | |
89 | |
90 if (storedInstanceCallback_ != NULL) | |
91 { | |
92 PyErr_SetString(PyExc_RuntimeError, "Can only register one Python on-stored-instance callback"); | |
93 return NULL; | |
94 } | |
95 | |
96 OrthancPlugins::LogInfo("Registering a Python on-stored-instance callback"); | |
97 | |
98 OrthancPluginRegisterOnStoredInstanceCallback( | |
99 OrthancPlugins::GetGlobalContext(), OnStoredInstanceCallback); | |
100 | |
101 storedInstanceCallback_ = callback; | |
102 Py_XINCREF(storedInstanceCallback_); | |
103 | |
104 Py_INCREF(Py_None); | |
105 return Py_None; | |
106 } | |
107 | |
108 | |
109 void FinalizeOnStoredInstanceCallback() | |
110 { | |
111 PythonLock lock; | |
112 | |
113 if (storedInstanceCallback_ != NULL) | |
114 { | |
115 Py_XDECREF(storedInstanceCallback_); | |
116 } | |
117 } |