Belle II Software  release-08-01-10
Configuration.h
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 #pragma once
9 
10 #include <framework/utilities/EnvironmentVariables.h>
11 #include <framework/dataobjects/FileMetaData.h>
12 
13 #include <boost/python/list.hpp>
14 #include <boost/python/tuple.hpp>
15 
16 #include <variant>
17 #include <set>
18 #include <optional>
19 
20 namespace Belle2::Conditions {
30  class CppOrPyList {
31  public:
33  boost::python::list& ensurePy();
35  std::vector<std::string>& ensureCpp();
37  void append(const std::string& element);
39  void prepend(const std::string& element);
42  void shallowCopy(const boost::python::object& source);
43  private:
45  std::variant<std::vector<std::string>, boost::python::list> m_value;
46  };
47 
61  class Configuration {
62  public:
68  void reset();
69 
76 
78  void appendGlobalTag(const std::string& globalTag) { ensureEditable(); m_globalTags.append(globalTag); }
80  void prependGlobalTag(const std::string& globalTag) { ensureEditable(); m_globalTags.prepend(globalTag); }
82  void setGlobalTags(const std::vector<std::string>& list) { ensureEditable(); m_globalTags.ensureCpp() = list; }
84  void setGlobalTagsPy(const boost::python::list& globalTags) { ensureEditable(); m_globalTags.shallowCopy(globalTags); }
86  std::vector<std::string> getGlobalTags() { return m_globalTags.ensureCpp(); }
88  boost::python::list getGlobalTagsPy() { return m_globalTags.ensurePy(); }
89 
91  std::vector<std::string> getDefaultGlobalTags() const;
93  boost::python::tuple getDefaultGlobalTagsPy() const;
94 
108  void setInputGlobaltags(const std::vector<std::string>& inputTags)
109  {
110  ensureEditable();
111  m_inputGlobaltags = inputTags;
112  }
113 
123  void setInputMetadata(const std::vector<FileMetaData>& inputMetadata);
124 
134  std::vector<std::string> getBaseTags() const;
135 
141  void overrideGlobalTagsPy(const boost::python::list& globalTags);
143  bool overrideEnabled() const { return m_overrideEnabled; }
144 
153  {
155  for (const auto& tag : getDefaultGlobalTags()) appendGlobalTag(tag);
156  }
157 
175  std::vector<std::string> getFinalListOfTags();
176 
178 
192 
194  void appendTestingPayloadLocation(const std::string& filename) { ensureEditable(); m_testingPayloadLocations.append(filename); }
196  void prependTestingPayloadLocation(const std::string& filename) { ensureEditable(); m_testingPayloadLocations.prepend(filename); }
198  void setTestingPayloadLocations(const std::vector<std::string>& list) { ensureEditable(); m_testingPayloadLocations.ensureCpp() = list;}
200  void setTestingPayloadLocationsPy(const boost::python::list& list) { ensureEditable(); m_testingPayloadLocations.shallowCopy(list); }
202  std::vector<std::string> getTestingPayloadLocations() { return m_testingPayloadLocations.ensureCpp(); }
205 
207 
220 
222  void appendMetadataProvider(const std::string& provider) { ensureEditable(); m_metadataProviders.append(provider); }
224  void prependMetadataProvider(const std::string& provider) { ensureEditable(); m_metadataProviders.prepend(provider); }
226  void setMetadataProviders(const std::vector<std::string>& list) { ensureEditable(); m_metadataProviders.ensureCpp() = list; }
228  void setMetadataProvidersPy(const boost::python::list& list) { ensureEditable(); m_metadataProviders.shallowCopy(list); }
230  std::vector<std::string> getMetadataProviders() { return m_metadataProviders.ensureCpp(); }
232  boost::python::list getMetadataProvidersPy() { return m_metadataProviders.ensurePy(); }
236 
246 
248  void appendPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.append(location); }
250  void prependPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.prepend(location); }
252  void setPayloadLocations(const std::vector<std::string>& list) { ensureEditable(); m_payloadLocations.ensureCpp() = list; }
254  void setPayloadLocationsPy(const boost::python::list& list) { ensureEditable(); m_payloadLocations.shallowCopy(list); }
256  std::vector<std::string> getPayloadLocations() { return m_payloadLocations.ensureCpp(); }
258  boost::python::list getPayloadLocationsPy() { return m_payloadLocations.ensurePy(); }
259 
261 
271 
273  void setNewPayloadLocation(const std::string& filename) { ensureEditable(); m_newPayloadFile = filename; }
275  std::string getNewPayloadLocation() const { return m_newPayloadFile; }
276 
279  void setDownloadCacheDirectory(const std::string& directory) { ensureEditable(); m_downloadCacheDirectory = directory; }
282  std::string getDownloadCacheDirectory() const { return m_downloadCacheDirectory; }
283 
285  void setDownloadLockTimeout(size_t timeout) { ensureEditable(); m_downloadLockTimeout = timeout; }
288 
291  void setUsableTagStates(const std::set<std::string>& states) { ensureEditable(); m_usableTagStates = states; }
293  const std::set<std::string>& getUsableTagStates() const { return m_usableTagStates; }
294 
298  void setGlobaltagCallbackPy(const boost::python::object& obj) { ensureEditable(); m_callback = obj; }
299 
301 
303  void setInitialized(bool value) { m_databaseInitialized = value; }
305  static void exposePythonAPI();
306  private:
308  template<class T> static void fillFromEnv(T& target, const std::string& envName, const std::string& defaultValue)
309  {
310  const auto values = EnvironmentVariables::getOrCreateList(envName, defaultValue);
311  for (const std::string& v : values) target.append(v);
312  }
314  void ensureEditable() const
315  {
317  throw std::runtime_error("Database already initialized, please reset before changing the configuration object");
318  }
320  bool m_overrideEnabled{false};
323  std::optional<std::vector<std::string>> m_inputGlobaltags;
325  std::vector<FileMetaData> m_inputMetadata;
335  std::string m_defaultMetadataProviderUrl{"http://belle2db.sdcc.bnl.gov/b2s/rest/"};
337  std::string m_newPayloadFile{"localdb/database.txt"};
339  std::string m_downloadCacheDirectory{""};
343  std::set<std::string> m_usableTagStates{"TESTING", "VALIDATED", "PUBLISHED", "RUNNING"};
345  std::optional<boost::python::object> m_callback;
349  };
350 } // Belle2::Conditions namespace
Class to enable configuration of the conditions database access in C++ and python.
Definition: Configuration.h:61
void appendMetadataProvider(const std::string &provider)
Append a metadata provider to the list.
std::vector< std::string > getGlobalTags()
Get the list of user globaltags.
Definition: Configuration.h:86
bool m_overrideEnabled
is the globaltag override enabled?
void setTestingPayloadLocations(const std::vector< std::string > &list)
Set the list of local text files to look for testing payloads.
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
CppOrPyList m_globalTags
the list with all user globaltags
void setPayloadLocations(const std::vector< std::string > &list)
Set the list of payload locations.
void prependGlobalTag(const std::string &globalTag)
preprend a globaltag
Definition: Configuration.h:80
boost::python::tuple getDefaultGlobalTagsPy() const
Get the tuple of default globaltags as python version.
void setGlobalTags(const std::vector< std::string > &list)
Set the list of globaltags.
Definition: Configuration.h:82
void appendGlobalTag(const std::string &globalTag)
Append a globaltag.
Definition: Configuration.h:78
Configuration()
Initialize default values.
void prependMetadataProvider(const std::string &provider)
Prepend a metadata provider to the list.
void setMetadataProviders(const std::vector< std::string > &list)
Set the list of metadata providers.
void setInitialized(bool value)
Set by the Database singleton upon initialization and cleanup.
void ensureEditable() const
Check whether the configuration object can be edited or if the database has been initialized already.
void setGlobaltagCallbackPy(const boost::python::object &obj)
Set a callback function from python which will be called when processing starts and should return the...
std::vector< std::string > getBaseTags() const
Get the base globaltags to be used in addition to user globaltags.
void disableGlobalTagReplay()
Disable global tag replay.
void prependPayloadLocation(const std::string &location)
Prepend a payload to the list of locations.
boost::python::list getGlobalTagsPy()
Get the list of user globaltags as python version.
Definition: Configuration.h:88
std::vector< std::string > getTestingPayloadLocations()
Get the list of testing payload locations.
void setMetadataProvidersPy(const boost::python::list &list)
Set the list of metadata providers in python.
boost::python::list getTestingPayloadLocationsPy()
Get the list of text files containing test payloads in python.
static void fillFromEnv(T &target, const std::string &envName, const std::string &defaultValue)
Fill a target object from a list of environment variables.
std::string m_newPayloadFile
the file to put the newly created payload information
std::vector< FileMetaData > m_inputMetadata
the file metadata of all input files if globaltag replay is requested by input module
std::vector< std::string > getFinalListOfTags()
Get the final list of globaltags to be used for processing.
void setDownloadCacheDirectory(const std::string &directory)
Set the directory where to place downloaded payloads.
void setPayloadLocationsPy(const boost::python::list &list)
Set the list of payload locations in python.
CppOrPyList m_metadataProviders
the list with all the metadata providers
void setInputMetadata(const std::vector< FileMetaData > &inputMetadata)
To be called by input modules with the list of all input FileMetaData.
std::set< std::string > m_usableTagStates
the tag states accepted for processing
void setInputGlobaltags(const std::vector< std::string > &inputTags)
To be called by input modules with the tags to be added from input files.
boost::python::list getMetadataProvidersPy()
Get the list of metadata providers in python.
std::string getNewPayloadLocation() const
Get the filename where to save newly created payload information.
CppOrPyList m_testingPayloadLocations
the files with testing payloads to use during processing
void overrideGlobalTags()
Enable globaltag override: If this is called once than overrideEnabled() will return true and getFina...
void setTestingPayloadLocationsPy(const boost::python::list &list)
Set the list of text files containing test payloads in python.
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
void prependTestingPayloadLocation(const std::string &filename)
Prepend a local text file with testing payloads to the list.
void setNewPayloadLocation(const std::string &filename)
Set the file where to save newly created payload information.
void appendPayloadLocation(const std::string &location)
Append a payload to the list of locations.
boost::python::list getPayloadLocationsPy()
Get the list og payload locations in python.
static void exposePythonAPI()
expose this class to python
std::string m_defaultMetadataProviderUrl
default URL where to look for the metadata provider
void setDownloadLockTimeout(size_t timeout)
Set the timout we try to lock a file in the download cache directory for downloading.
std::string getDefaultMetadataProviderUrl()
Get the default URL where to look for the metadata provider.
CppOrPyList m_payloadLocations
the list with all the payload locations
const std::set< std::string > & getUsableTagStates() const
Get the set of usable globaltag states allowed to be used for processing.
std::string getDownloadCacheDirectory() const
Get the directory where to place downloaded payloads.
void overrideGlobalTagsPy(const boost::python::list &globalTags)
Enable globaltag override and set the list of user globaltags in one go.
void reset()
Reset to default values.
std::vector< std::string > getMetadataProviders()
Get the list of metadata providers.
std::vector< std::string > getPayloadLocations()
Get the list of payload locations.
void setGlobalTagsPy(const boost::python::list &globalTags)
Set the list of globaltags from python.
Definition: Configuration.h:84
bool overrideEnabled() const
Check if override is enabled by previous calls to overrideGlobalTags()
std::optional< boost::python::object > m_callback
the callback function to determine the final final list of globaltags
size_t getDownloadLockTimeout() const
Get the timout we try to lock a file in the download cache directory for downloading.
std::string m_downloadCacheDirectory
the directory to put newly downloaded payloads
std::vector< std::string > getDefaultGlobalTags() const
Get the std::vector of default globaltags.
void appendTestingPayloadLocation(const std::string &filename)
Add a local text file with testing payloads.
size_t m_downloadLockTimeout
the timeout when trying to lock files in the download directory
bool m_databaseInitialized
bool indicating whether the database has been initialized, in which case any changes to the configura...
void setUsableTagStates(const std::set< std::string > &states)
Set the set of usable globaltag states to be allowed for processing.
Wrapper class for a list of strings to either be held in a std::vector or in a python list.
Definition: Configuration.h:30
boost::python::list & ensurePy()
Return the python list version.
void prepend(const std::string &element)
Prepend an element to whatever representation we currently have.
void shallowCopy(const boost::python::object &source)
shallow copy all elements of the source object into the python representation.
void append(const std::string &element)
Append an element to whatever representation we currently have.
std::vector< std::string > & ensureCpp()
Return the C++ vector version.
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:45
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...