Belle II Software development
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
9#include <framework/utilities/EnvironmentVariables.h>
10#include <boost/algorithm/string.hpp>
11#include <boost/python/import.hpp>
12#include <boost/python/extract.hpp>
13#include <algorithm>
14#include <cstdlib>
15
16namespace Belle2 {
21
22 bool EnvironmentVariables::isSet(const std::string& name)
23 {
24 char* envValue = std::getenv(name.c_str());
25 return envValue != nullptr;
26 }
27
28 std::string EnvironmentVariables::get(const std::string& name, const std::string& fallback)
29 {
30 char* envValue = std::getenv(name.c_str());
31 if (envValue != nullptr) {
32 std::string val(envValue);
33 boost::trim(val);
34 return envValue;
35 }
36 return fallback;
37 }
38
39 std::vector<std::string> EnvironmentVariables::getList(const std::string& name, const std::vector<std::string>& fallback,
40 const std::string& separators)
41 {
42 if (!isSet(name))
43 return fallback;
44 std::string value = get(name);
45 std::vector<std::string> items;
46 // Treat an empty string as empty list
47 if (value.empty())
48 return items;
49 // We have something to return: split the string, trim each element and remove empty ones before returning
50 boost::split(items, value, boost::is_any_of(separators));
51 std::for_each(items.begin(), items.end(), [](std::string & s) {boost::algorithm::trim(s);});
52 items.erase(std::remove_if(items.begin(), items.end(), [](const std::string & s) { return s.empty(); }), items.end());
53 return items;
54 }
55
56 std::vector<std::string> EnvironmentVariables::getOrCreateList(const std::string& name, const std::string& fallback,
57 const std::string& separators)
58 {
59 // 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
60 std::string value = get(name, fallback);
61 std::vector<std::string> items;
62 // Treat an empty string as empty list
63 if (value.empty())
64 return items;
65 // We have something to return: split the string, trim each element and remove empty ones before returning
66 boost::split(items, value, boost::is_any_of(separators));
67 std::for_each(items.begin(), items.end(), [](std::string & s) {boost::algorithm::trim(s);});
68 items.erase(std::remove_if(items.begin(), items.end(), [](const std::string & s) { return s.empty(); }), items.end());
69 return items;
70 }
71
72 std::string EnvironmentVariables::expand(const std::string& text)
73 {
74 // we could implement this ourselves ... but that seems like a waste of
75 // time since there's a perfectly good implementation available.
76 namespace py = boost::python;
77 py::object path = py::import("os.path");
78 return py::extract<std::string>(path.attr("expandvars")(text));
79 }
80
81}
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 occurrence of $NAME or ${NAME} with the value of the enviro...
Abstract base class for different kinds of events.