Mercurial > hg > orthanc-python
annotate Sources/IncomingInstanceFilter.cpp @ 112:b3a18e333f93
integration 4.0->mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 28 Mar 2023 17:28:16 +0200 |
parents | 87fdb1a108db |
children | 65ec5597ec70 |
rev | line source |
---|---|
97 | 1 /** |
2 * Python plugin for Orthanc | |
101
eb6ac5a801d1
update to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
100
diff
changeset
|
3 * Copyright (C) 2020-2022 Osimis S.A., Belgium |
eb6ac5a801d1
update to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
100
diff
changeset
|
4 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
97 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "IncomingInstanceFilter.h" | |
22 | |
23 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
24 #include "Autogenerated/sdk.h" | |
25 #include "ICallbackRegistration.h" | |
26 #include "PythonString.h" | |
27 | |
28 | |
100
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
29 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 10, 0) |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
30 |
97 | 31 static PyObject* incomingCStoreInstanceFilter_ = NULL; |
32 | |
33 | |
104
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
34 static int32_t IncomingCStoreInstanceFilter(uint16_t* dimseStatus, |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
35 const OrthancPluginDicomInstance *instance) |
97 | 36 { |
104
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
37 assert(dimseStatus != NULL && |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
38 instance != NULL); |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
39 |
97 | 40 try |
41 { | |
42 PythonLock lock; | |
43 | |
44 /** | |
45 * Construct an instance object of the "orthanc.DicomInstance" | |
46 * class. This is done by calling the constructor function | |
47 * "sdk_OrthancPluginDicomInstance_Type". | |
48 **/ | |
49 PythonObject args(lock, PyTuple_New(2)); | |
50 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) instance)); | |
51 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */)); | |
52 PyObject *pInst = PyObject_CallObject((PyObject*) GetOrthancPluginDicomInstanceType(), args.GetPyObject()); | |
53 | |
54 /** | |
55 * Construct the arguments tuple (instance) | |
56 **/ | |
57 PythonObject args2(lock, PyTuple_New(1)); | |
58 PyTuple_SetItem(args2.GetPyObject(), 0, pInst); | |
59 | |
60 PythonObject result(lock, PyObject_CallObject(incomingCStoreInstanceFilter_, args2.GetPyObject())); | |
61 | |
62 std::string traceback; | |
63 if (lock.HasErrorOccurred(traceback)) | |
64 { | |
65 OrthancPlugins::LogError("Error in the Python incoming-cstore-instance callback, " | |
66 "traceback:\n" + traceback); | |
67 return -1; | |
68 } | |
69 else | |
70 { | |
104
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
71 if (PyNumber_Check(result.GetPyObject())) |
97 | 72 { |
104
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
73 int32_t code = PyLong_AsLong(result.GetPyObject()); |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
74 |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
75 if (code < 0) |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
76 { |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
77 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has returned a negative value"); |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
78 return -1; |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
79 } |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
80 else if (code == 0) |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
81 { |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
82 return 1; // The instance is to be stored |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
83 } |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
84 else if (code <= 0xffff) |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
85 { |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
86 // The instance is to be discarded with a custom DIMSE status |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
87 *dimseStatus = static_cast<uint16_t>(code); |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
88 return 0; // Discard |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
89 } |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
90 else |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
91 { |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
92 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has returned an out-of-range DIMSE status: " + |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
93 boost::lexical_cast<std::string>(code)); |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
94 return -1; |
87fdb1a108db
updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
95 } |
97 | 96 } |
97 else | |
98 { | |
99 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has not returned an integer"); | |
100 return -1; | |
101 } | |
102 } | |
103 } | |
104 catch (OrthancPlugins::PluginException& e) | |
105 { | |
106 return e.GetErrorCode(); | |
107 } | |
108 } | |
109 | |
110 | |
111 PyObject* RegisterIncomingCStoreInstanceFilter(PyObject* module, PyObject* args) | |
112 { | |
113 // The GIL is locked at this point (no need to create "PythonLock") | |
114 | |
115 class Registration : public ICallbackRegistration | |
116 { | |
117 public: | |
118 virtual void Register() ORTHANC_OVERRIDE | |
119 { | |
120 OrthancPluginRegisterIncomingCStoreInstanceFilter( | |
121 OrthancPlugins::GetGlobalContext(), IncomingCStoreInstanceFilter); | |
122 } | |
123 }; | |
124 | |
125 Registration registration; | |
126 return ICallbackRegistration::Apply( | |
127 registration, args, incomingCStoreInstanceFilter_, "Python incoming CStore instance filter"); | |
128 } | |
129 | |
130 | |
131 void FinalizeIncomingCStoreInstanceFilter() | |
132 { | |
133 ICallbackRegistration::Unregister(incomingCStoreInstanceFilter_); | |
134 } | |
100
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
135 |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
136 #else |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
137 |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
138 #warning OrthancPluginRegisterIncomingCStoreInstanceFilter() is not supported |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
139 |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
140 PyObject* RegisterIncomingCStoreInstanceFilter(PyObject* module, PyObject* args) |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
141 { |
102
ed994991a20b
improved error handling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
142 PyErr_SetString(PyExc_RuntimeError, "The version of your Orthanc SDK doesn't provide OrthancPluginRegisterIncomingCStoreInstanceFilter()"); |
ed994991a20b
improved error handling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
143 return NULL; |
100
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
144 } |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
145 |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
146 void FinalizeIncomingCStoreInstanceFilter() |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
147 { |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
148 } |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
149 |
e2b2e1d4e1bb
fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
97
diff
changeset
|
150 #endif |