From a5c9c3096f25cdbbeedd23f65c320ea6f5083b76 Mon Sep 17 00:00:00 2001 From: Martyn Gigg <martyn.gigg@stfc.ac.uk> Date: Thu, 4 Jul 2013 12:41:15 +0100 Subject: [PATCH] Add canLoad method to FileLoaderRegistry. This better encapsulates the file checking so that other places don't have to worry about the desciptor stuff. Refs #7263 --- .../API/inc/MantidAPI/FileLoaderRegistry.h | 2 + .../Framework/API/src/FileLoaderRegistry.cpp | 53 +++++++++++++++---- .../src/EventNexusFileMemento.cpp | 13 ++--- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h index ed32ae13486..52b12b7a027 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h @@ -83,6 +83,8 @@ namespace Mantid /// Returns the name of an Algorithm that can load the given filename const boost::shared_ptr<IAlgorithm> chooseLoader(const std::string &filename) const; + /// Checks whether the given algorithm can load the file + bool canLoad(const std::string & algorithmName,const std::string & filename) const; private: /// Friend so that CreateUsingNew diff --git a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp index da1cb58965d..159d3dbff68 100644 --- a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp +++ b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp @@ -28,19 +28,20 @@ namespace Mantid ///endcond /** - * @param descriptor A descriptor object describing the file + * @param filename A string giving a filename * @param names The collection of names to search through * @param logger A reference to a Mantid Logger object * @return A string containing the name of an algorithm to load the file, or an empty string if nothing * was found */ template<typename DescriptorType, typename FileLoaderType> - const IAlgorithm_sptr searchForLoader(DescriptorType & descriptor,const std::multimap<std::string, int> & names, + const IAlgorithm_sptr searchForLoader(const std::string & filename,const std::multimap<std::string, int> & names, Kernel::Logger & logger) { const auto & factory = AlgorithmFactory::Instance(); IAlgorithm_sptr bestLoader; int maxConfidence(0); + DescriptorType descriptor(filename); DescriptorCallback<DescriptorType> callback; auto iend = names.end(); @@ -87,32 +88,62 @@ namespace Mantid { using Kernel::FileDescriptor; using Kernel::HDFDescriptor; - using Kernel::Logger; - if(m_log.is(Logger::Priority::PRIO_DEBUG)) m_log.debug() << "Trying to find loader for '" << filename << "'" << std::endl; + m_log.debug() << "Trying to find loader for '" << filename << "'" << std::endl; IAlgorithm_sptr bestLoader; if(HDFDescriptor::isHDF(filename)) { - if(m_log.is(Logger::Priority::PRIO_DEBUG)) m_log.debug() << filename << " looks like a HDF file. Checking registered HDF loaders\n"; - HDFDescriptor descriptor(filename); - bestLoader = searchForLoader<HDFDescriptor,IHDFFileLoader>(descriptor, m_names[HDF], m_log); + m_log.debug() << filename << " looks like a HDF file. Checking registered HDF loaders\n"; + bestLoader = searchForLoader<HDFDescriptor,IHDFFileLoader>(filename, m_names[HDF], m_log); } else { - if(m_log.is(Logger::Priority::PRIO_DEBUG)) m_log.debug() << "Checking registered non-HDF loaders\n"; - FileDescriptor descriptor(filename); - bestLoader = searchForLoader<FileDescriptor,IFileLoader>(descriptor, m_names[NonHDF], m_log); + m_log.debug() << "Checking registered non-HDF loaders\n"; + bestLoader = searchForLoader<FileDescriptor,IFileLoader>(filename, m_names[NonHDF], m_log); } if(!bestLoader) { throw Kernel::Exception::NotFoundError(filename, "Unable to find loader"); } - if(m_log.is(Logger::Priority::PRIO_DEBUG)) m_log.debug() << "Found loader " << bestLoader->name() << " for file '" << filename << "'" << std::endl; + m_log.debug() << "Found loader " << bestLoader->name() << " for file '" << filename << "'" << std::endl; return bestLoader; } + /** + * Perform a check that that the given algorithm can load the file + * @param algorithmName The name of the algorithm to check + * @param filename The name of file to check + * @returns True if the algorithm can load the file, false otherwise + * @throws std::invalid_argument if the loader does not exist + */ + bool FileLoaderRegistryImpl::canLoad(const std::string & algorithmName,const std::string & filename) const + { + using Kernel::FileDescriptor; + using Kernel::HDFDescriptor; + + // Check if it is in one of our lists + bool hdf(false),nonHDF(false); + if(m_names[HDF].find(algorithmName) != m_names[HDF].end()) hdf = true; + else if(m_names[NonHDF].find(algorithmName) != m_names[NonHDF].end()) nonHDF = true; + + if(!hdf && !nonHDF) throw std::invalid_argument("FileLoaderRegistryImpl::canLoad - Algorithm '" + algorithmName + "' is not registered as a loader."); + + std::multimap<std::string,int> names; + names.insert(std::make_pair(algorithmName, -1)); + IAlgorithm_sptr loader; + if(hdf && HDFDescriptor::isHDF(filename)) + { + loader = searchForLoader<HDFDescriptor,IHDFFileLoader>(filename, names, m_log); + } + else + { + loader = searchForLoader<FileDescriptor,IFileLoader>(filename, names, m_log); + } + if(loader) return true; + else return false; + } //---------------------------------------------------------------------------------------------- // Private members diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/EventNexusFileMemento.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/EventNexusFileMemento.cpp index cf0763bbb8d..11af894fdbe 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/EventNexusFileMemento.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/EventNexusFileMemento.cpp @@ -1,9 +1,10 @@ #include "MantidQtCustomInterfaces/EventNexusFileMemento.h" -#include "MantidAPI/LoadAlgorithmFactory.h" -#include "MantidKernel/Matrix.h" #include "MantidAPI/AlgorithmManager.h" +#include "MantidAPI/FileLoaderRegistry.h" #include "MantidAPI/IEventWorkspace.h" #include "MantidGeometry/Crystal/OrientedLattice.h" +#include "MantidKernel/HDFDescriptor.h" +#include "MantidKernel/Matrix.h" #include <iostream> #include <fstream> #include <boost/regex.hpp> @@ -35,9 +36,9 @@ namespace MantidQt throw std::invalid_argument("EventNexusFileMemento:: File doesn't exist"); } - //Detailed check of file structure. - IDataFileChecker_sptr alg = LoadAlgorithmFactory::Instance().create("LoadEventNexus"); - if(!alg->fileCheck(m_fileName)) + // Check that it can be loaded by LoadEventNexus + + if(!FileLoaderRegistry::Instance().canLoad("LoadEventNexus", fileName)) { throw std::invalid_argument("Expecting Event Nexus files. This file type is not recognised"); } @@ -96,7 +97,7 @@ namespace MantidQt { checkStillThere(); - IDataFileChecker_sptr alg = LoadAlgorithmFactory::Instance().create("LoadEventNexus"); + auto alg = AlgorithmManager::Instance().createUnmanaged("LoadEventNexus"); alg->initialize(); alg->setRethrows(true); alg->setProperty("Filename", m_fileName); -- GitLab