Mantid
Loading...
Searching...
No Matches
MultipleFileProperty.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
12#include <boost/python/class.hpp>
13#include <boost/python/list.hpp>
14#include <boost/python/make_constructor.hpp>
15#include <boost/python/str.hpp>
16
22using namespace boost::python;
23
24namespace {
26using HeldType = std::vector<std::vector<std::string>>;
27
35boost::python::list valueAsPyObject(const MultipleFileProperty &self) {
36 const HeldType &propValue = self();
37
38 // Build a list of lists to mimic the behaviour of MultipleFileProperty
39 boost::python::list fileList;
40 for (const auto &filenames : propValue) {
41 if (filenames.size() == 1) {
42 fileList.append(filenames.front());
43 } else {
44 boost::python::list groupList;
45 for (const auto &filename : filenames) {
46 groupList.append(filename);
47 }
48 fileList.append(groupList);
49 }
50 }
51
52 return fileList;
53}
54
55MultipleFileProperty *createMultipleFilePropertyWithAction(const std::string &name, unsigned int action,
56 const object &extensions = object(),
57 const bool allowEmptyTokens = false) {
58 std::vector<std::string> extsAsVector;
59 if (!Mantid::PythonInterface::isNone(extensions)) {
60 extract<std::string> extractor(extensions);
61 if (extractor.check()) {
62 extsAsVector = std::vector<std::string>(1, extractor());
63 } else {
64 extsAsVector = PySequenceToVector<std::string>(extensions)();
65 }
66 }
67 return new MultipleFileProperty(name, action, extsAsVector, allowEmptyTokens);
68}
69
70MultipleFileProperty *createMultipleFileProperty(const std::string &name, const object &extensions = object()) {
71 return createMultipleFilePropertyWithAction(name, FileProperty::FileAction::Load, extensions);
72}
73} // namespace
74
76 using BaseClass = PropertyWithValue<HeldType>;
77 PropertyWithValueExporter<HeldType>::define("VectorVectorStringPropertyWithValue");
78
79 class_<MultipleFileProperty, bases<BaseClass>, boost::noncopyable>("MultipleFileProperty", no_init)
80 .def("__init__", make_constructor(&createMultipleFileProperty, default_call_policies(),
81 (arg("name"), arg("extensions") = object())))
82 .def("__init__",
83 make_constructor(&createMultipleFilePropertyWithAction, default_call_policies(),
84 (arg("name"), arg("action"), arg("extensions") = object(), arg("allow_empty") = false)))
85 // Override the base class one to do something more appropriate
86 .add_property("value", &valueAsPyObject);
87}
void export_MultipleFileProperty()
A specialized class for dealing with file properties.
Definition: FileProperty.h:42
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
A property to allow a user to specify multiple files to load.
The concrete, templated class for properties.
bool isNone(PyObject *ptr)
Definition: IsNone.h:26
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
A helper struct to export PropertyWithValue<> types to Python.
static void define(const char *pythonClassName)