Skip to content
Snippets Groups Projects
Commit 492fa205 authored by Peterson, Peter's avatar Peterson, Peter
Browse files

Generalize method for finding filename

parent 9d76ea9a
No related merge requests found
......@@ -43,6 +43,9 @@ public:
void setPropertyValue(const std::string &name,
const std::string &value) override;
/// Free function to find first file property
static std::string findFilenamePropertyName(const IAlgorithm *loader);
protected:
Parallel::ExecutionMode getParallelExecutionMode(
const std::map<std::string, Parallel::StorageMode> &storageModes)
......
......@@ -214,26 +214,43 @@ API::IAlgorithm_sptr Load::getFileLoader(const std::string &filePath) {
return winningLoader;
}
void Load::findFilenameProperty(const API::IAlgorithm_sptr &loader) {
std::string Load::findFilenamePropertyName(const IAlgorithm *loader) {
std::string propertyName; // default is empty string
// Use the first file property as the main Filename
const auto &props = loader->getProperties();
for (auto prop : props) {
auto *fp = dynamic_cast<API::MultipleFileProperty *>(prop);
auto *fp2 = dynamic_cast<API::FileProperty *>(prop);
if (fp) {
m_filenamePropName = fp->name();
auto *multiprop = dynamic_cast<API::MultipleFileProperty *>(prop);
auto *singleprop = dynamic_cast<API::FileProperty *>(prop);
if (multiprop) {
propertyName = multiprop->name();
break;
}
if (fp2) {
m_filenamePropName = fp2->name();
if (singleprop) {
propertyName = singleprop->name();
break;
}
}
if (m_filenamePropName.empty()) {
// throw an exception if somehting nothing was found
if (propertyName.empty()) {
std::stringstream msg;
msg << "Cannot find FileProperty on \"" << loader->name() << "\" v"
<< loader->version() << " algorithm.";
throw std::runtime_error(msg.str());
}
return propertyName;
}
void Load::findFilenameProperty(const API::IAlgorithm_sptr &loader) {
try {
m_filenamePropName = findFilenamePropertyName(loader.get());
} catch (std::runtime_error &) {
// unset member variables
setPropertyValue("LoaderName", "");
setProperty("LoaderVersion", -1);
throw std::runtime_error("Cannot find FileProperty on " + loader->name() +
" algorithm.");
throw;
}
}
......
......@@ -5,13 +5,14 @@
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/NexusFileLoader.h"
#include "MantidDataHandling/Load.h"
namespace Mantid::DataHandling {
void NexusFileLoader::exec() {
// make sure the descriptor is initialized
if (!m_fileInfo) {
const std::string filename =
this->getPropertyValue("Filename"); // TODO be more generic
const std::string filePropName = Load::findFilenamePropertyName(this);
const std::string filename = this->getPropertyValue(filePropName);
m_fileInfo =
std::make_shared<Mantid::Kernel::NexusHDF5Descriptor>(filename);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment