Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
DataFileInterface.C
Go to the documentation of this file.
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 DataFileInterface::DataFileInterface(const ParallelParamObject & parent) : _parent(parent) {}
19 
20 std::string
21 DataFileInterface::getDataFileName(const std::string & param) const
22 {
23  _parent.mooseDeprecated("getDataFileName() is deprecated. The file path is now directly set "
24  "within the InputParameters.\nUse getParam<DataFileName>(\"",
25  param,
26  "\") instead.");
27  return _parent.getParam<DataFileName>(param);
28 }
29 
30 std::string
31 DataFileInterface::getDataFileNameByName(const std::string & relative_path) const
32 {
33  _parent.mooseDeprecated("getDataFileNameByName() is deprecated. Use getDataFilePath(\"",
34  relative_path,
35  "\") instead.");
36  return getDataFilePath(relative_path);
37 }
38 
39 std::string
40 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  if (std::filesystem::path(relative_path).is_absolute())
45  _parent.mooseWarning("While using getDataFilePath(\"",
46  relative_path,
47  "\"): This API should not be used for absolute paths.");
48 
49  // Throw on error so that if getPath() fails, we can throw an error
50  // with the context of _parent.mooseError()
51  const auto throw_on_error_before = Moose::_throw_on_error;
53  std::optional<std::string> error;
54 
55  // This will search the data paths for this relative path
56  Moose::DataFileUtils::Path found_path;
57  try
58  {
59  found_path = Moose::DataFileUtils::getPath(relative_path);
60  }
61  catch (std::exception & e)
62  {
63  error = e.what();
64  }
65 
66  Moose::_throw_on_error = throw_on_error_before;
67  if (error)
68  _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  "Using data file '" + found_path.path + "' from " + *found_path.data_name + " data";
76  _parent.mooseInfo(msg);
77 
78  return found_path.path;
79 }
std::string getDataFilePath(const std::string &relative_path) const
Returns the path of a data file for a given relative file path.
void mooseDeprecated(Args &&... args) const
Context context
Context for the file (where it came from)
Definition: DataFileUtils.h:48
void mooseInfo(Args &&... args) const
std::string getDataFileNameByName(const std::string &relative_path) const
Deprecated method.
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Representation of a data file path.
Definition: DataFileUtils.h:36
std::string getDataFileName(const std::string &param) const
Deprecated method.
Path getPath(std::string path, const std::optional< std::string > &base=std::optional< std::string >())
Get the data path for a given path, searching the registered data.
Definition: DataFileUtils.C:22
std::optional< std::string > data_name
The name of the data registry the file came from (with context == DATA)
Definition: DataFileUtils.h:50
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
Base class shared by both Action and MooseObject.
bool _throw_on_error
Variable to turn on exceptions during mooseError(), should only be used within MOOSE unit tests or wh...
Definition: Moose.C:756
const ParallelParamObject & _parent
DataFileInterface(const ParallelParamObject &parent)
Constructing the object.