Belle II Software  release-05-02-19
EnvironmentVariables.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <framework/utilities/EnvironmentVariables.h>
11 #include <boost/algorithm/string.hpp>
12 #include <boost/python/import.hpp>
13 #include <boost/python/extract.hpp>
14 #include <cstdlib>
15 
16 namespace Belle2 {
21  bool EnvironmentVariables::isSet(const std::string& name)
22  {
23  char* envValue = std::getenv(name.c_str());
24  return envValue != nullptr;
25  }
26 
27  std::string EnvironmentVariables::get(const std::string& name, const std::string& fallback)
28  {
29  char* envValue = std::getenv(name.c_str());
30  if (envValue != nullptr) {
31  std::string val(envValue);
32  boost::trim(val);
33  return envValue;
34  }
35  return fallback;
36  }
37 
38  std::vector<std::string> EnvironmentVariables::getList(const std::string& name, const std::vector<std::string>& fallback,
39  const std::string& separators)
40  {
41  if (!isSet(name)) return fallback;
42  std::string value = get(name);
43  std::vector<std::string> items;
44  //Treat empty string as empty list
45  if (value.empty()) return items;
46  boost::split(items, value, boost::is_any_of(separators));
47  return items;
48  }
49 
50  std::vector<std::string> EnvironmentVariables::getOrCreateList(const std::string& name, const std::string& fallback,
51  const std::string& separators)
52  {
53  //almost the same as above but we don't need to check if its set, we just parse the value or fallback into a list
54  std::string value = get(name, fallback);
55  std::vector<std::string> items;
56  //Treat empty string as empty list
57  if (value.empty()) return items;
58  boost::split(items, value, boost::is_any_of(separators));
59  return items;
60  }
61 
62  std::string EnvironmentVariables::expand(const std::string& text)
63  {
64  // we could implement this ourselves ... but that seems like a waste of
65  // time since there's a perfectly good implementation available.
66  namespace py = boost::python;
67  py::object path = py::import("os.path");
68  return py::extract<std::string>(path.attr("expandvars")(text));
69  }
71 }
Belle2::EnvironmentVariables::getList
static std::vector< std::string > getList(const std::string &name, const std::vector< std::string > &fallback={}, const std::string &separators=" \t\n\r")
Get a list of values from an environment variable or the given fallback list if the variable is not s...
Definition: EnvironmentVariables.cc:46
Belle2::EnvironmentVariables::getOrCreateList
static std::vector< std::string > getOrCreateList(const std::string &name, const std::string &fallback, const std::string &separators=" \t\n\r")
Get a list of values from an environment variable or the given fallback string if the variable is not...
Definition: EnvironmentVariables.cc:58
Belle2::EnvironmentVariables::get
static std::string get(const std::string &name, const std::string &fallback="")
Get the value of an environment variable or the given fallback value if the variable is not set.
Definition: EnvironmentVariables.cc:35
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EnvironmentVariables::isSet
static bool isSet(const std::string &name)
Check if a value is set in the database.
Definition: EnvironmentVariables.cc:29
Belle2::EnvironmentVariables::expand
static std::string expand(const std::string &text)
Modify the given string and replace every occurence of $NAME or ${NAME} with the value of the environ...
Definition: EnvironmentVariables.cc:70