filepath_maker.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // filepath_maker: A family of related policies governing the translation
4 // of a filename into a fully-qualified filepath
5 //
6 // ======================================================================
7 
9 #include "cetlib/filesystem.h"
10 #include "cetlib/getenv.h"
11 #include "cetlib_except/exception.h"
12 
13 #include <iostream>
14 #include <map>
15 #include <vector>
16 
22 
23 // ----------------------------------------------------------------------
24 
26 filepath_maker::operator()(std::string const& filename)
27 {
28  return filename;
29 }
30 
31 // ----------------------------------------------------------------------
32 
34 
37 {
38  return paths.find_file(filename);
39 }
40 
41 // ----------------------------------------------------------------------
42 
44  : paths{move(paths)}
45 {}
46 
49 {
50  return cet::is_absolute_filepath(filename) ? filename :
51  paths.find_file(filename);
52 }
53 
54 // ----------------------------------------------------------------------
55 
57  : paths{move(paths)}
58 {}
59 
62 {
63  return after1 ? paths.find_file(filename) : (after1 = true, filename);
64 }
65 
66 void
68 {
69  after1 = false;
70 }
71 
72 // ----------------------------------------------------------------------
73 
76  : first_paths{std::string("./:") + paths}, after_paths{move(paths) + ':'}
77 {
78  if (after_paths.empty()) {
79  std::cerr << "search path empty (nonexistent environment variable"
80  << (paths.empty() ? "" : std::string(" ") + paths) << ")?\n"
81  << "Any included configurations will not be found by this lookup "
82  "policy.\n";
83  }
84 }
85 
88  std::string const& filename)
89 {
90  if (first) {
91  first = false;
92  return cet::is_absolute_filepath(filename) ?
93  filename :
94  first_paths.find_file(filename);
95  }
96  return after_paths.find_file(filename);
97 }
98 
99 void
101 {
102  first = true;
103 }
104 
105 // ======================================================================
106 
107 std::unique_ptr<filepath_maker>
109  std::string env_or_paths) const
110 {
111  if (policy == none()) {
112  return std::make_unique<filepath_maker>();
113  }
114 
115  if (policy == all()) {
116  return std::make_unique<filepath_lookup>(move(env_or_paths));
117  }
118 
119  if (policy == nonabsolute()) {
120  return std::make_unique<filepath_lookup_nonabsolute>(move(env_or_paths));
121  }
122 
123  if (policy == after1()) {
124  return std::make_unique<filepath_lookup_after1>(move(env_or_paths));
125  }
126 
127  if (policy == permissive()) {
128  auto search_paths = cet::getenv(env_or_paths);
129  if (empty(search_paths)) {
130  // 'env_or_paths' is not environment variable; assume it's a
131  // list of colon-delimited paths.
132  search_paths = env_or_paths;
133  }
134  return std::make_unique<filepath_first_absolute_or_lookup_with_dot>(
135  move(search_paths));
136  }
137 
138  throw cet::exception("Configuration")
139  << "An unsupported file-lookup policy was selected.";
140 }
141 
144 {
145  using namespace std::string_literals;
146  std::map<std::string, std::vector<std::string>> policies;
147  policies.emplace(none(), std::vector{"No lookup is done for any files."s});
148  policies.emplace(
149  all(),
150  std::vector{"Lookup is performed for all files presented to the filepath-"s,
151  "making object."s});
152  policies.emplace(
153  nonabsolute(),
154  std::vector{"Lookup is performed only for files that are not absolute--"s,
155  "i.e. file names not beginning with the '/' character."s});
156  policies.emplace(
157  after1(),
158  std::vector{"No lookup for the first file, and only lookup is performed"s,
159  "for all subsequent files."s});
160  policies.emplace(
161  permissive(),
162  std::vector{
163  "The first file can be an absolute path, a path relative to '.',"s,
164  "or a path that can be looked up; all subsequent files must be"s,
165  "looked up. This is the policy used by the art framework; it is"s,
166  "sometimes referred to as the first-absolute-or-lookup-with-dot"s,
167  "policy."s});
168  std::string result{"\nThe following file-lookup policies are supported:\n"};
169  for (auto const& [name, description] : policies) {
170  result += "\n '";
171  result += name;
172  result += "'\n";
173  for (auto const& line : description) {
174  result += " ";
175  result += line;
176  result += '\n';
177  }
178  }
179  result += '\n';
180  return result;
181 }
static QCString name
Definition: declinfo.cpp:673
static QCString result
std::string string
Definition: nybbler.cc:12
struct vector vector
std::string operator()(std::string const &filename) override
cet::search_path paths
string filename
Definition: train.py:213
filepath_lookup_nonabsolute(std::string paths)
std::unique_ptr< filepath_maker > select(std::string const &spec, std::string paths) const
std::string getenv(std::string const &name)
Definition: getenv.cc:15
def move(depos, offset)
Definition: depos.py:107
std::string operator()(std::string const &filename) override
filepath_lookup_after1(std::string paths)
bool is_absolute_filepath(std::string const &qualified_filename)
Definition: filesystem.cc:23
bool empty() const
Definition: search_path.cc:76
static QInternalList< QTextCodec > * all
Definition: qtextcodec.cpp:63
void line(double t, double *p, double &x, double &y, double &z)
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
std::string operator()(std::string const &filename) override
std::string operator()(std::string const &filename) override
filepath_lookup(std::string paths)
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
std::string help_message() const