Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "DataFileInterface.h" 11 : #include "MooseError.h" 12 : #include "ParallelParamObject.h" 13 : #include "DataFileUtils.h" 14 : #include "MooseUtils.h" 15 : 16 : #include <optional> 17 : 18 5675363 : DataFileInterface::DataFileInterface(const ParallelParamObject & parent) : _parent(parent) {} 19 : 20 : std::string 21 9 : DataFileInterface::getDataFileName(const std::string & param) const 22 : { 23 9 : _parent.mooseDeprecated("getDataFileName() is deprecated. The file path is now directly set " 24 : "within the InputParameters.\nUse getParam<DataFileName>(\"", 25 : param, 26 : "\") instead."); 27 9 : return _parent.getParam<DataFileName>(param); 28 : } 29 : 30 : std::string 31 9 : DataFileInterface::getDataFileNameByName(const std::string & relative_path) const 32 : { 33 9 : _parent.mooseDeprecated("getDataFileNameByName() is deprecated. Use getDataFilePath(\"", 34 : relative_path, 35 : "\") instead."); 36 9 : return getDataFilePath(relative_path); 37 : } 38 : 39 : std::string 40 24 : DataFileInterface::getDataFilePath(const std::string & relative_path) const 41 : { 42 : // This should only ever be used with relative paths. There is no point to 43 : // use this search path with an absolute path. 44 24 : if (std::filesystem::path(relative_path).is_absolute()) 45 3 : _parent.mooseWarning("While using getDataFilePath(\"", 46 : relative_path, 47 : "\"): This API should not be used for absolute paths."); 48 : 49 : // This will search the data paths for this relative path 50 21 : std::optional<std::string> error; 51 21 : Moose::DataFileUtils::Path found_path; 52 : { 53 : // Throw on error so that if getPath() fails, we can throw an error 54 : // with the context of _parent.mooseError() 55 21 : Moose::ScopedThrowOnError scoped_throw_on_error; 56 : 57 : try 58 : { 59 27 : found_path = Moose::DataFileUtils::getPath(relative_path); 60 : } 61 3 : catch (std::exception & e) 62 : { 63 3 : error = e.what(); 64 3 : } 65 21 : } 66 : 67 21 : if (error) 68 3 : _parent.mooseError(*error); 69 : 70 : mooseAssert(found_path.context == Moose::DataFileUtils::Context::DATA, 71 : "Should only ever obtain data"); 72 : mooseAssert(found_path.data_name, "Should be set"); 73 : 74 : const std::string msg = 75 18 : "Using data file '" + found_path.path + "' from " + *found_path.data_name + " data"; 76 18 : _parent.mooseInfo(msg); 77 : 78 36 : return found_path.path; 79 18 : }