Mantid
Loading...
Searching...
No Matches
FileFinder.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 +
9#include <boost/python/class.hpp>
10#include <boost/python/list.hpp>
11#include <boost/python/reference_existing_object.hpp>
12
15using namespace boost::python;
16
27std::vector<std::string> runFinderProxy(const FileFinderImpl &self, const std::string &hintstr, list exts_list,
28 const bool useExtsOnly) {
29 // Convert python list to c++ vector
30 std::vector<std::string> exts;
31 for (int i = 0; i < len(exts_list); ++i)
32 exts.emplace_back(extract<std::string>(exts_list[i]));
33
34 // Before calling the function we need to release the GIL,
35 // drop the Python threadstate and reset anything installed
36 // via PyEval_SetTrace while we execute the C++ code -
37 // ReleaseGlobalInterpreter does this for us
39 auto paths = self.findRuns(hintstr, exts, useExtsOnly);
40 std::vector<std::string> results;
41 results.reserve(paths.size());
42 std::transform(paths.begin(), paths.end(), std::back_inserter(results), [](const auto &p) { return p.string(); });
43 return results;
44}
45
46// Wrapper to convert std::filesystem::path to std::string for Boost.Python
47static std::string getFullPathProxy(const FileFinderImpl &self, const std::string &path,
48 const bool ignoreDirs = false) {
49 return self.getFullPath(path, ignoreDirs).string();
50}
51
53 class_<FileFinderImpl, boost::noncopyable>("FileFinderImpl", no_init)
54 .def("getFullPath", &getFullPathProxy, (arg("self"), arg("path"), arg("ignoreDirs") = false),
55 "Return a full path to the given file if it can be found within "
56 "datasearch.directories paths. Directories can be ignored with "
57 "ignoreDirs=True. An empty string is returned otherwise.")
58 .def("findRuns", &runFinderProxy,
59 (arg("self"), arg("hintstr"), arg("exts_list") = list(), arg("useExtsOnly") = false),
60 "Find a list of files file given a hint. "
61 "The hint can be a comma separated list of run numbers and can also "
62 "include ranges of runs, e.g. 123-135 or equivalently 123-35"
63 "If no instrument prefix is given then the current default is used."
64 "exts_list is an optional list containing strings of file "
65 "extensions to search."
66 "useExtsOnly is an optional bool. If it's true then don't use "
67 "facility exts.")
68 .def("getCaseSensitive", &FileFinderImpl::getCaseSensitive, (arg("self")),
69 "Option to get if file finder should be case sensitive.")
70 .def("setCaseSensitive", &FileFinderImpl::setCaseSensitive, (arg("self"), arg("cs")),
71 "Option to set if file finder should be case sensitive.")
72 .def("Instance", &FileFinder::Instance, return_value_policy<reference_existing_object>(),
73 "Returns a reference to the FileFinder singleton instance")
74 .staticmethod("Instance");
75}
void export_FileFinder()
std::vector< std::string > runFinderProxy(const FileFinderImpl &self, const std::string &hintstr, list exts_list, const bool useExtsOnly)
Runs FileFinder.findRuns after releasing the python GIL.
static std::string getFullPathProxy(const FileFinderImpl &self, const std::string &path, const bool ignoreDirs=false)
This class finds data files given an instrument name (optionally) and a run number.
Definition FileFinder.h:37
bool getCaseSensitive() const
Option to get if file finder should be case sensitive.
std::filesystem::path getFullPath(const std::string &filename, const bool ignoreDirs=false) const
Return the full path to the file given its name.
void setCaseSensitive(const bool cs)
Option to set if file finder should be case sensitive.
std::vector< std::filesystem::path > findRuns(const std::string &hintstr, const std::vector< std::string > &exts={}, const bool useExtsOnly=false) const
Find a list of files file given a hint.
Defines a structure for releasing the Python GIL using the RAII pattern.
Mantid::Kernel::SingletonHolder< FileFinderImpl > FileFinder
Definition FileFinder.h:93