comparison Sources/IncomingInstanceFilter.cpp @ 97:e37ed36541be

added missing files
author Alain Mazy <am@osimis.io>
date Fri, 15 Oct 2021 18:40:10 +0200
parents
children e2b2e1d4e1bb
comparison
equal deleted inserted replaced
96:627b8a19fb9f 97:e37ed36541be
1 /**
2 * Python plugin for Orthanc
3 * Copyright (C) 2020-2021 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 "IncomingInstanceFilter.h"
21
22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
23 #include "Autogenerated/sdk.h"
24 #include "ICallbackRegistration.h"
25 #include "PythonString.h"
26
27
28 static PyObject* incomingCStoreInstanceFilter_ = NULL;
29
30
31 static int32_t IncomingCStoreInstanceFilter(const OrthancPluginDicomInstance *instance)
32 {
33 try
34 {
35 PythonLock lock;
36
37 /**
38 * Construct an instance object of the "orthanc.DicomInstance"
39 * class. This is done by calling the constructor function
40 * "sdk_OrthancPluginDicomInstance_Type".
41 **/
42 PythonObject args(lock, PyTuple_New(2));
43 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) instance));
44 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */));
45 PyObject *pInst = PyObject_CallObject((PyObject*) GetOrthancPluginDicomInstanceType(), args.GetPyObject());
46
47 /**
48 * Construct the arguments tuple (instance)
49 **/
50 PythonObject args2(lock, PyTuple_New(1));
51 PyTuple_SetItem(args2.GetPyObject(), 0, pInst);
52
53 PythonObject result(lock, PyObject_CallObject(incomingCStoreInstanceFilter_, args2.GetPyObject()));
54
55 std::string traceback;
56 if (lock.HasErrorOccurred(traceback))
57 {
58 OrthancPlugins::LogError("Error in the Python incoming-cstore-instance callback, "
59 "traceback:\n" + traceback);
60 return -1;
61 }
62 else
63 {
64 if (PyLong_Check(result.GetPyObject()))
65 {
66 return static_cast<int32_t>(PyLong_AsLong(result.GetPyObject()));
67 }
68 else
69 {
70 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has not returned an integer");
71 return -1;
72 }
73 }
74 }
75 catch (OrthancPlugins::PluginException& e)
76 {
77 return e.GetErrorCode();
78 }
79 }
80
81
82 PyObject* RegisterIncomingCStoreInstanceFilter(PyObject* module, PyObject* args)
83 {
84 // The GIL is locked at this point (no need to create "PythonLock")
85
86 class Registration : public ICallbackRegistration
87 {
88 public:
89 virtual void Register() ORTHANC_OVERRIDE
90 {
91 OrthancPluginRegisterIncomingCStoreInstanceFilter(
92 OrthancPlugins::GetGlobalContext(), IncomingCStoreInstanceFilter);
93 }
94 };
95
96 Registration registration;
97 return ICallbackRegistration::Apply(
98 registration, args, incomingCStoreInstanceFilter_, "Python incoming CStore instance filter");
99 }
100
101
102 void FinalizeIncomingCStoreInstanceFilter()
103 {
104 ICallbackRegistration::Unregister(incomingCStoreInstanceFilter_);
105 }