Belle II Software  release-05-02-19
Configuration.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - 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 #pragma once
11 
12 #include <framework/utilities/EnvironmentVariables.h>
13 #include <framework/dataobjects/FileMetaData.h>
14 
15 #include <boost/python/list.hpp>
16 #include <boost/python/tuple.hpp>
17 
18 #include <variant>
19 #include <set>
20 
21 namespace Belle2::Conditions {
31  class CppOrPyList {
32  public:
34  boost::python::list& ensurePy();
36  std::vector<std::string>& ensureCpp();
38  void append(const std::string& element);
40  void prepend(const std::string& element);
43  void shallowCopy(const boost::python::object& source);
44  private:
46  std::variant<std::vector<std::string>, boost::python::list> m_value;
47  };
48 
62  class Configuration {
63  public:
65  Configuration();
67  static Configuration& getInstance();
69  void reset();
70 
76 
79  void appendGlobalTag(const std::string& globalTag) { ensureEditable(); m_globalTags.append(globalTag); }
81  void prependGlobalTag(const std::string& globalTag) { ensureEditable(); m_globalTags.prepend(globalTag); }
83  void setGlobalTags(const std::vector<std::string>& list) { ensureEditable(); m_globalTags.ensureCpp() = list; }
85  void setGlobalTagsPy(const boost::python::list& globalTags) { ensureEditable(); m_globalTags.shallowCopy(globalTags); }
87  std::vector<std::string> getGlobalTags() { return m_globalTags.ensureCpp(); }
89  boost::python::list getGlobalTagsPy() { return m_globalTags.ensurePy(); }
90 
92  std::vector<std::string> getDefaultGlobalTags() const;
94  boost::python::tuple getDefaultGlobalTagsPy() const;
95 
109  void setInputGlobaltags(const std::vector<std::string>& inputTags)
110  {
111  ensureEditable();
112  m_inputGlobaltags = inputTags;
113  }
114 
124  void setInputMetadata(const std::vector<FileMetaData>& inputMetadata);
125 
135  std::vector<std::string> getBaseTags() const;
136 
142  void overrideGlobalTagsPy(const boost::python::list& globalTags);
144  bool overrideEnabled() const { return m_overrideEnabled; }
145 
154  {
156  for (const auto& tag : getDefaultGlobalTags()) appendGlobalTag(tag);
157  }
158 
176  std::vector<std::string> getFinalListOfTags();
177 
179 
192 
195  void appendTestingPayloadLocation(const std::string& filename) { ensureEditable(); m_testingPayloadLocations.append(filename); }
197  void prependTestingPayloadLocation(const std::string& filename) { ensureEditable(); m_testingPayloadLocations.prepend(filename); }
199  void setTestingPayloadLocations(const std::vector<std::string>& list) { ensureEditable(); m_testingPayloadLocations.ensureCpp() = list;}
201  void setTestingPayloadLocationsPy(const boost::python::list& list) { ensureEditable(); m_testingPayloadLocations.shallowCopy(list); }
203  std::vector<std::string> getTestingPayloadLocations() { return m_testingPayloadLocations.ensureCpp(); }
206 
208 
220 
223  void appendMetadataProvider(const std::string& provider) { ensureEditable(); m_metadataProviders.append(provider); }
225  void prependMetadataProvider(const std::string& provider) { ensureEditable(); m_metadataProviders.prepend(provider); }
227  void setMetadataProviders(const std::vector<std::string>& list) { ensureEditable(); m_metadataProviders.ensureCpp() = list; }
229  void setMetadataProvidersPy(const boost::python::list& list) { ensureEditable(); m_metadataProviders.shallowCopy(list); }
231  std::vector<std::string> getMetadataProviders() { return m_metadataProviders.ensureCpp(); }
233  boost::python::list getMetadataProvidersPy() { return m_metadataProviders.ensurePy(); }
235 
244 
247  void appendPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.append(location); }
249  void prependPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.prepend(location); }
251  void setPayloadLocations(const std::vector<std::string>& list) { ensureEditable(); m_payloadLocations.ensureCpp() = list; }
253  void setPayloadLocationsPy(const boost::python::list& list) { ensureEditable(); m_payloadLocations.shallowCopy(list); }
255  std::vector<std::string> getPayloadLocations() { return m_payloadLocations.ensureCpp(); }
257  boost::python::list getPayloadLocationsPy() { return m_payloadLocations.ensurePy(); }
258 
260 
269 
272  void setNewPayloadLocation(const std::string& filename) { ensureEditable(); m_newPayloadFile = filename; }
274  std::string getNewPayloadLocation() const { return m_newPayloadFile; }
275 
278  void setDownloadCacheDirectory(const std::string& directory) { ensureEditable(); m_downloadCacheDirectory = directory; }
281  std::string getDownloadCacheDirectory() const { return m_downloadCacheDirectory; }
282 
284  void setDownloadLockTimeout(size_t timeout) { ensureEditable(); m_downloadLockTimeout = timeout; }
287 
290  void setUsableTagStates(const std::set<std::string>& states) { ensureEditable(); m_usableTagStates = states; }
292  const std::set<std::string>& getUsableTagStates() const { return m_usableTagStates; }
293 
297  void setGlobaltagCallbackPy(const boost::python::object& obj) { ensureEditable(); m_callback = obj; }
298 
300 
302  void setInitialized(bool value) { m_databaseInitialized = value; }
304  static void exposePythonAPI();
305  private:
307  template<class T> static void fillFromEnv(T& target, const std::string& envName, const std::string& defaultValue)
308  {
309  const auto values = EnvironmentVariables::getOrCreateList(envName, defaultValue);
310  for (const std::string& v : values) target.append(v);
311  }
313  void ensureEditable() const
314  {
316  throw std::runtime_error("Database already initialized, please reset before changing the configuration object");
317  }
319  bool m_overrideEnabled{false};
322  std::optional<std::vector<std::string>> m_inputGlobaltags;
324  std::vector<FileMetaData> m_inputMetadata;
326  CppOrPyList m_globalTags;
328  CppOrPyList m_testingPayloadLocations;
334  std::string m_newPayloadFile{"localdb/database.txt"};
336  std::string m_downloadCacheDirectory{""};
340  std::set<std::string> m_usableTagStates{"TESTING", "VALIDATED", "PUBLISHED", "RUNNING"};
342  std::optional<boost::python::object> m_callback;
345  bool m_databaseInitialized{false};
346  };
347 } // Belle2::Conditions namespace
Belle2::Conditions::Configuration::prependGlobalTag
void prependGlobalTag(const std::string &globalTag)
preprend a globaltag
Definition: Configuration.h:89
Belle2::Conditions::Configuration::m_databaseInitialized
bool m_databaseInitialized
bool indicating whether the database has been initialized, in which case any changes to the configura...
Definition: Configuration.h:353
Belle2::Conditions::Configuration::getPayloadLocationsPy
boost::python::list getPayloadLocationsPy()
Get the list og payload locations in python.
Definition: Configuration.h:265
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::Conditions::Configuration::overrideEnabled
bool overrideEnabled() const
Check if override is enabled by previous calls to overrideGlobalTags()
Definition: Configuration.h:152
Belle2::Conditions::Configuration::prependPayloadLocation
void prependPayloadLocation(const std::string &location)
Prepend a payload to the list of locations.
Definition: Configuration.h:257
Belle2::Conditions::CppOrPyList::prepend
void prepend(const std::string &element)
Prepend an element to whatever representation we currently have.
Belle2::Conditions::Configuration::getGlobalTagsPy
boost::python::list getGlobalTagsPy()
Get the list of user globaltags as python version.
Definition: Configuration.h:97
Belle2::Conditions::Configuration::fillFromEnv
static void fillFromEnv(T &target, const std::string &envName, const std::string &defaultValue)
Fill a target object from a list of environment variables.
Definition: Configuration.h:315
Belle2::Conditions::Configuration::overrideGlobalTags
void overrideGlobalTags()
Enable globaltag override: If this is called once than overrideEnabled() will return true and getFina...
Definition: Configuration.h:148
Belle2::Conditions::CppOrPyList::m_value
std::variant< std::vector< std::string >, boost::python::list > m_value
Store either a std::vector or a python list of strings.
Definition: Configuration.h:62
Belle2::Conditions::Configuration::m_overrideEnabled
bool m_overrideEnabled
is the globaltag override enabled?
Definition: Configuration.h:327
Belle2::Conditions::CppOrPyList
Wrapper class for a list of strings to either be held in a std::vector or in a python list.
Definition: Configuration.h:39
Belle2::Conditions::Configuration::getMetadataProviders
std::vector< std::string > getMetadataProviders()
Get the list of metadata providers.
Definition: Configuration.h:239
Belle2::Conditions::Configuration::setGlobaltagCallbackPy
void setGlobaltagCallbackPy(const boost::python::object &obj)
Set a callback function from python which will be called when processing starts and should return the...
Definition: Configuration.h:305
Belle2::Conditions::Configuration::m_usableTagStates
std::set< std::string > m_usableTagStates
the tag states accepted for processing
Definition: Configuration.h:348
Belle2::Conditions::Configuration::m_metadataProviders
CppOrPyList m_metadataProviders
the list with all the metadata providers
Definition: Configuration.h:338
Belle2::Conditions::Configuration::appendMetadataProvider
void appendMetadataProvider(const std::string &provider)
Append a metadata provider to the list.
Definition: Configuration.h:231
Belle2::Conditions::Configuration::m_downloadCacheDirectory
std::string m_downloadCacheDirectory
the directory to put newly downloaded payloads
Definition: Configuration.h:344
Belle2::Conditions::CppOrPyList::append
void append(const std::string &element)
Append an element to whatever representation we currently have.
Belle2::Conditions::Configuration::m_inputMetadata
std::vector< FileMetaData > m_inputMetadata
the file metadata of all input files if globaltag replay is requested by input module
Definition: Configuration.h:332
Belle2::Conditions::CppOrPyList::ensureCpp
std::vector< std::string > & ensureCpp()
Return the C++ vector version.
Belle2::Conditions::Configuration::setMetadataProvidersPy
void setMetadataProvidersPy(const boost::python::list &list)
Set the list of metadata providers in python.
Definition: Configuration.h:237
Belle2::Conditions::Configuration::setGlobalTags
void setGlobalTags(const std::vector< std::string > &list)
Set the list of globaltags.
Definition: Configuration.h:91
Belle2::Conditions::Configuration::m_inputGlobaltags
std::optional< std::vector< std::string > > m_inputGlobaltags
the list of globaltags from all the input files to be used in addition to the user globaltags
Definition: Configuration.h:330
Belle2::Conditions::Configuration::exposePythonAPI
static void exposePythonAPI()
expose this class to python
Belle2::Conditions::Configuration::getTestingPayloadLocationsPy
boost::python::list getTestingPayloadLocationsPy()
Get the list of text files containing test payloads in python.
Definition: Configuration.h:213
Belle2::Conditions::Configuration::setInputGlobaltags
void setInputGlobaltags(const std::vector< std::string > &inputTags)
To be called by input modules with the tags to be added from input files.
Definition: Configuration.h:117
Belle2::Conditions::Configuration::getMetadataProvidersPy
boost::python::list getMetadataProvidersPy()
Get the list of metadata providers in python.
Definition: Configuration.h:241
Belle2::Conditions::Configuration::setUsableTagStates
void setUsableTagStates(const std::set< std::string > &states)
Set the set of usable globaltag states to be allowed for processing.
Definition: Configuration.h:298
Belle2::Conditions::Configuration::getBaseTags
std::vector< std::string > getBaseTags() const
Get the base globaltags to be used in addition to user globaltags.
Belle2::Conditions::Configuration::getNewPayloadLocation
std::string getNewPayloadLocation() const
Get the filename where to save newly created payload information.
Definition: Configuration.h:282
Belle2::Conditions::Configuration::getPayloadLocations
std::vector< std::string > getPayloadLocations()
Get the list of payload locations.
Definition: Configuration.h:263
Belle2::Conditions::Configuration::m_callback
std::optional< boost::python::object > m_callback
the callback function to determine the final final list of globaltags
Definition: Configuration.h:350
Belle2::Conditions::Configuration::m_payloadLocations
CppOrPyList m_payloadLocations
the list with all the payload locations
Definition: Configuration.h:340
Belle2::Conditions::Configuration::getTestingPayloadLocations
std::vector< std::string > getTestingPayloadLocations()
Get the list of testing payload locations.
Definition: Configuration.h:211
Belle2::Conditions::Configuration::appendPayloadLocation
void appendPayloadLocation(const std::string &location)
Append a payload to the list of locations.
Definition: Configuration.h:255
Belle2::Conditions::Configuration::m_downloadLockTimeout
size_t m_downloadLockTimeout
the timeout when trying to lock files in the download directory
Definition: Configuration.h:346
Belle2::Conditions::Configuration::getInstance
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
Belle2::Conditions::Configuration::getFinalListOfTags
std::vector< std::string > getFinalListOfTags()
Get the final list of globaltags to be used for processing.
Belle2::Conditions::Configuration::setMetadataProviders
void setMetadataProviders(const std::vector< std::string > &list)
Set the list of metadata providers.
Definition: Configuration.h:235
Belle2::Conditions::Configuration
Class to enable configuration of the conditions database access in C++ and python.
Definition: Configuration.h:70
Belle2::Conditions::Configuration::setNewPayloadLocation
void setNewPayloadLocation(const std::string &filename)
Set the file where to save newly created payload information.
Definition: Configuration.h:280
Belle2::Conditions::Configuration::overrideGlobalTagsPy
void overrideGlobalTagsPy(const boost::python::list &globalTags)
Enable globaltag override and set the list of user globaltags in one go.
Belle2::Conditions::Configuration::m_newPayloadFile
std::string m_newPayloadFile
the file to put the newly created payload information
Definition: Configuration.h:342
Belle2::Conditions::Configuration::getDefaultGlobalTags
std::vector< std::string > getDefaultGlobalTags() const
Get the std::vector of default globaltags.
Belle2::Conditions::Configuration::getUsableTagStates
const std::set< std::string > & getUsableTagStates() const
Get the set of usable globaltag states allowed to be used for processing.
Definition: Configuration.h:300
Belle2::Conditions::Configuration::ensureEditable
void ensureEditable() const
Check whether the configuration object can be edited or if the database has been initialized already.
Definition: Configuration.h:321
Belle2::Conditions::Configuration::appendGlobalTag
void appendGlobalTag(const std::string &globalTag)
Append a globaltag.
Definition: Configuration.h:87
Belle2::Conditions::Configuration::setInputMetadata
void setInputMetadata(const std::vector< FileMetaData > &inputMetadata)
To be called by input modules with the list of all input FileMetaData.
Belle2::Conditions::Configuration::prependMetadataProvider
void prependMetadataProvider(const std::string &provider)
Prepend a metadata provider to the list.
Definition: Configuration.h:233
Belle2::Conditions::Configuration::appendTestingPayloadLocation
void appendTestingPayloadLocation(const std::string &filename)
Add a local text file with testing payloads.
Definition: Configuration.h:203
Belle2::Conditions::CppOrPyList::ensurePy
boost::python::list & ensurePy()
Return the python list version.
Belle2::Conditions::Configuration::Configuration
Configuration()
Initialize default values.
Belle2::Conditions::Configuration::setTestingPayloadLocationsPy
void setTestingPayloadLocationsPy(const boost::python::list &list)
Set the list of text files containing test payloads in python.
Definition: Configuration.h:209
Belle2::Conditions::Configuration::setGlobalTagsPy
void setGlobalTagsPy(const boost::python::list &globalTags)
Set the list of globaltags from python.
Definition: Configuration.h:93
Belle2::Conditions::Configuration::setPayloadLocationsPy
void setPayloadLocationsPy(const boost::python::list &list)
Set the list of payload locations in python.
Definition: Configuration.h:261
Belle2::Conditions::Configuration::setInitialized
void setInitialized(bool value)
Set by the Database singleton upon initialization and cleanup.
Definition: Configuration.h:310
Belle2::Conditions::Configuration::setPayloadLocations
void setPayloadLocations(const std::vector< std::string > &list)
Set the list of payload locations.
Definition: Configuration.h:259
Belle2::Conditions::Configuration::m_testingPayloadLocations
CppOrPyList m_testingPayloadLocations
the files with testing payloads to use during processing
Definition: Configuration.h:336
Belle2::Conditions::Configuration::prependTestingPayloadLocation
void prependTestingPayloadLocation(const std::string &filename)
Prepend a local text file with testing payloads to the list.
Definition: Configuration.h:205
Belle2::Conditions::Configuration::disableGlobalTagReplay
void disableGlobalTagReplay()
Disable global tag replay.
Definition: Configuration.h:161
Belle2::Conditions::Configuration::m_globalTags
CppOrPyList m_globalTags
the list with all user globaltags
Definition: Configuration.h:334
Belle2::Conditions::Configuration::getGlobalTags
std::vector< std::string > getGlobalTags()
Get the list of user globaltags.
Definition: Configuration.h:95
Belle2::Conditions::Configuration::reset
void reset()
Reset to default values.
Belle2::Conditions::Configuration::setTestingPayloadLocations
void setTestingPayloadLocations(const std::vector< std::string > &list)
Set the list of local text files to look for testing payloads.
Definition: Configuration.h:207
Belle2::Conditions::Configuration::getDownloadLockTimeout
size_t getDownloadLockTimeout() const
Get the timout we try to lock a file in the download cache directory for downloading.
Definition: Configuration.h:294
Belle2::Conditions::Configuration::setDownloadCacheDirectory
void setDownloadCacheDirectory(const std::string &directory)
Set the directory where to place downloaded payloads.
Definition: Configuration.h:286
Belle2::Conditions::CppOrPyList::shallowCopy
void shallowCopy(const boost::python::object &source)
shallow copy all elements of the source object into the python representation.
Belle2::Conditions::Configuration::setDownloadLockTimeout
void setDownloadLockTimeout(size_t timeout)
Set the timout we try to lock a file in the download cache directory for downloading.
Definition: Configuration.h:292
Belle2::Conditions::Configuration::getDefaultGlobalTagsPy
boost::python::tuple getDefaultGlobalTagsPy() const
Get the tuple of default globaltags as python version.
Belle2::Conditions::Configuration::getDownloadCacheDirectory
std::string getDownloadCacheDirectory() const
Get the directory where to place downloaded payloads.
Definition: Configuration.h:289