14#include <boost/python/class.hpp>
15#include <boost/python/extract.hpp>
16#include <boost/python/list.hpp>
17#include <boost/python/str.hpp>
23namespace PythonInterface {
31 using PythonType = boost::python::class_<SvcType, boost::noncopyable>;
32 using WeakPtr = std::weak_ptr<typename SvcPtrType::element_type>;
56 "Adds the given object to the service with the given name. If "
57 "the name/object exists it will raise an error.")
59 "Adds the given object to the service with the given name. "
60 "If the name exists the object is replaced.")
61 .def(
"doesExist", &SvcType::doesExist, (arg(
"self"), arg(
"name")),
62 "Returns True if the object is found in the service.")
64 "Retrieve the named object. Raises an exception if the name "
68 "Removes all objects managed by the service.")
69 .def(
"size", &SvcType::size, arg(
"self"),
"Returns the number of objects within the service")
71 "Return the list of names currently known to the ADS")
74 .def(
"__len__", &SvcType::size, arg(
"self"))
77 .def(
"__contains__", &SvcType::doesExist, (arg(
"self"), arg(
"name")))
89 static void addItem(SvcType &self,
const std::string &
name,
const boost::python::object &item) {
122 if (self.size() > 0 && !silent) {
123 PyErr_Warn(PyExc_Warning,
"Running ADS.clear() also removes all hidden workspaces.\n"
124 "Mantid interfaces might still need some of these, for instance, MSlice.");
138 boost::python::extract<WeakPtr &> extractWeak(pyvalue);
139 if (extractWeak.check()) {
140 return extractWeak().lock();
142 boost::python::extract<SvcPtrType &> extractRefShared(pyvalue);
143 if (extractRefShared.check()) {
144 return extractRefShared();
146 throw std::invalid_argument(
"Cannot extract pointer from Python object argument. Incorrect type");
165 item = self->retrieve(
name);
168 std::string err =
"'" +
name +
"' does not exist.";
169 PyErr_SetString(PyExc_KeyError, err.c_str());
170 throw boost::python::error_already_set();
184 boost::python::list names;
187 for (
auto itr = keys.begin(); itr != keys.end(); ++itr) {
190 assert(names.attr(
"__len__")() == keys.size());
Exception for when an item is not found in a collection.
Defines a structure for releasing the Python GIL using the RAII pattern.
Helper class which provides the Collimation Length for SANS instruments.
A helper struct to export templated DataService<> types to Python.
boost::python::class_< SvcType, boost::noncopyable > PythonType
static void addOrReplaceItem(SvcType &self, const std::string &name, const boost::python::object &item)
Add or replace an item into the service, if it exists then an error is raised.
static PythonType define(const char *pythonClassName)
Define the necessary boost.python framework to expor the templated DataService type Note: This does n...
static WeakPtr retrieveOrKeyError(const SvcType *const self, const std::string &name)
Retrieves a shared_ptr from the ADS and raises a Python KeyError if it does not exist.
static void removeItem(SvcType &self, const std::string &name)
Remove an item from the service.
static boost::python::list getObjectNamesAsList(SvcType const *const self, const std::string &contain)
Return a Python list of object names from the ADS as this is far easier to work with than a set.
std::weak_ptr< typename SvcPtrType::element_type > WeakPtr
static void clearItems(SvcType &self, const bool silent)
Remove an item from the service.
static SvcPtrType extractCppValue(const boost::python::object &pyvalue)
Extract a SvcPtrType C++ value from the Python object.
static void addItem(SvcType &self, const std::string &name, const boost::python::object &item)
Add an item into the service, if it exists then an error is raised.