Belle II Software light-2406-ragdoll
EnvironmentVariables.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/utilities/EnvironmentVariables.h>
9#include <boost/algorithm/string.hpp>
10#include <boost/python/import.hpp>
11#include <boost/python/extract.hpp>
12#include <cstdlib>
13
14namespace Belle2 {
19 bool EnvironmentVariables::isSet(const std::string& name)
20 {
21 char* envValue = std::getenv(name.c_str());
22 return envValue != nullptr;
23 }
24
25 std::string EnvironmentVariables::get(const std::string& name, const std::string& fallback)
26 {
27 char* envValue = std::getenv(name.c_str());
28 if (envValue != nullptr) {
29 std::string val(envValue);
30 boost::trim(val);
31 return envValue;
32 }
33 return fallback;
34 }
35
36 std::vector<std::string> EnvironmentVariables::getList(const std::string& name, const std::vector<std::string>& fallback,
37 const std::string& separators)
38 {
39 if (!isSet(name)) return fallback;
40 std::string value = get(name);
41 std::vector<std::string> items;
42 //Treat empty string as empty list
43 if (value.empty()) return items;
44 boost::split(items, value, boost::is_any_of(separators));
45 return items;
46 }
47
48 std::vector<std::string> EnvironmentVariables::getOrCreateList(const std::string& name, const std::string& fallback,
49 const std::string& separators)
50 {
51 //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
52 std::string value = get(name, fallback);
53 std::vector<std::string> items;
54 //Treat empty string as empty list
55 if (value.empty()) return items;
56 boost::split(items, value, boost::is_any_of(separators));
57 return items;
58 }
59
60 std::string EnvironmentVariables::expand(const std::string& text)
61 {
62 // we could implement this ourselves ... but that seems like a waste of
63 // time since there's a perfectly good implementation available.
64 namespace py = boost::python;
65 py::object path = py::import("os.path");
66 return py::extract<std::string>(path.attr("expandvars")(text));
67 }
69}
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.
static bool isSet(const std::string &name)
Check if a value is set in the database.
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...
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...
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...
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24