https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
19
20std::string
21DataFileInterface::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
30std::string
31DataFileInterface::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
39std::string
40DataFileInterface::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 // This will search the data paths for this relative path
50 std::optional<std::string> error;
52 {
53 // Throw on error so that if getPath() fails, we can throw an error
54 // with the context of _parent.mooseError()
55 Moose::ScopedThrowOnError scoped_throw_on_error;
56
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
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.
DataFileInterface(const ParallelParamObject &parent)
Constructing the object.
const ParallelParamObject & _parent
std::string getDataFileNameByName(const std::string &relative_path) const
Deprecated method.
std::string getDataFileName(const std::string &param) const
Deprecated method.
void mooseDeprecated(Args &&... args) const
Emits a deprecation warning prefixed with the object name and type, and a stack trace.
Definition MooseBase.h:317
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
Definition MooseBase.h:299
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
void mooseInfo(Args &&... args) const
Definition MooseBase.h:334
Scoped helper for setting Moose::_throw_on_error during this scope.
Definition Moose.h:298
Base class shared by both Action and MooseObject.
@ DATA
From installed/in-tree data.
Path getPath(std::string path, const GetPathOptions &options={})
Get the data path for a given path, searching the registered data.
Representation of a data file path.
std::optional< std::string > data_name
The name of the data registry the file came from (with context == DATA)
Context context
Context for the file (where it came from)