Belle II Software light-2607-kasei
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
20namespace Belle2::Conditions {
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
62 public:
66 static Configuration& getInstance();
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 static std::vector<std::string> getDefaultGlobalTags();
93 boost::python::tuple getDefaultGlobalTagsPy() const;
94
108 void setInputGlobaltags(const std::vector<std::string>& inputTags)
109 {
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(); }
204 boost::python::list getTestingPayloadLocationsPy() { return m_testingPayloadLocations.ensurePy(); }
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(); }
234 // exposed to Python as a property: boost::python cannot wrap a function
235 // returning a reference without an explicit return_value_policy
236 // cppcheck-suppress returnByReference
239 // exposed to Python as a property: boost::python cannot wrap a function
240 // returning a reference without an explicit return_value_policy
241 // cppcheck-suppress returnByReference
245
247
257
259 void appendPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.append(location); }
261 void prependPayloadLocation(const std::string& location) { ensureEditable(); m_payloadLocations.prepend(location); }
263 void setPayloadLocations(const std::vector<std::string>& list) { ensureEditable(); m_payloadLocations.ensureCpp() = list; }
265 void setPayloadLocationsPy(const boost::python::list& list) { ensureEditable(); m_payloadLocations.shallowCopy(list); }
267 std::vector<std::string> getPayloadLocations() { return m_payloadLocations.ensureCpp(); }
269 boost::python::list getPayloadLocationsPy() { return m_payloadLocations.ensurePy(); }
270
272
282
284 void setNewPayloadLocation(const std::string& filename) { ensureEditable(); m_newPayloadFile = filename; }
286 const std::string& getNewPayloadLocation() const { return m_newPayloadFile; }
287
290 void setDownloadCacheDirectory(const std::string& directory) { ensureEditable(); m_downloadCacheDirectory = directory; }
293 const std::string& getDownloadCacheDirectory() const { return m_downloadCacheDirectory; }
294
296 void setDownloadLockTimeout(size_t timeout) { ensureEditable(); m_downloadLockTimeout = timeout; }
299
302 void setUsableTagStates(const std::set<std::string>& states) { ensureEditable(); m_usableTagStates = states; }
304 const std::set<std::string>& getUsableTagStates() const { return m_usableTagStates; }
305
309 void setGlobaltagCallbackPy(const boost::python::object& obj) { ensureEditable(); m_callback = obj; }
310
312
314 void setInitialized(bool value) { m_databaseInitialized = value; }
316 static void exposePythonAPI();
317 private:
319 template<class T> static void fillFromEnv(T& target, const std::string& envName, const std::string& defaultValue)
320 {
321 const auto values = EnvironmentVariables::getOrCreateList(envName, defaultValue);
322 for (const std::string& v : values) target.append(v);
323 }
324
325 void ensureEditable() const
326 {
328 throw std::runtime_error("Database already initialized, please reset before changing the configuration object");
329 }
330
331 bool m_overrideEnabled{false};
334 std::optional<std::vector<std::string>> m_inputGlobaltags;
336 std::vector<FileMetaData> m_inputMetadata;
346 std::string m_defaultLegacyRemoteMetadataProviderServer{"http://belle2db.sdcc.bnl.gov/b2s/rest/"};
348 std::string m_defaultHSFRemoteMetadataProviderServer{"http://blcdb.sdcc.bnl.gov/api/cdb_rest/"};
350 std::string m_defaultLocalMetadataProviderPath{"/cvmfs/belle.cern.ch/conditions"};
352 std::string m_newPayloadFile{"localdb/database.txt"};
358 std::set<std::string> m_usableTagStates{"TESTING", "VALIDATED", "PUBLISHED", "RUNNING",
359 "locked", "frozen"};
360
361 std::optional<boost::python::object> m_callback;
365 };
366} // Belle2::Conditions namespace
std::string getDefaultHSFRemoteMetadataProviderServer()
Get the default server URL for the HSF central metadata provider.
static std::vector< std::string > getDefaultGlobalTags()
Get the std::vector of default globaltags.
void appendMetadataProvider(const std::string &provider)
Append a metadata provider to the list.
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.
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)
prepend a globaltag
void setGlobalTags(const std::vector< std::string > &list)
Set the list of globaltags.
std::string m_defaultHSFRemoteMetadataProviderServer
default server URL for the HSF remote metadata provider
std::vector< std::string > getMetadataProviders()
Get the list of metadata providers.
void appendGlobalTag(const std::string &globalTag)
Append a globaltag.
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 > getTestingPayloadLocations()
Get the list of testing payload locations.
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.
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.
std::string m_defaultLocalMetadataProviderPath
default local path for the local metadata provider
std::vector< std::string > getFinalListOfTags()
Get the final list of globaltags to be used for processing.
boost::python::tuple getDefaultGlobalTagsPy() const
Get the tuple of default globaltags as python version.
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
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
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, including states from the HSF CDB
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.
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...
std::vector< std::string > getGlobalTags()
Get the list of user globaltags.
std::vector< std::string > getBaseTags() const
Get the base globaltags to be used in addition to user globaltags.
std::string m_defaultLegacyRemoteMetadataProviderServer
default server URL for the (legacy) remote metadata provider
void setTestingPayloadLocationsPy(const boost::python::list &list)
Set the list of text files containing test payloads in python.
static void exposePythonAPI()
expose this class to 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.
void setDownloadLockTimeout(size_t timeout)
Set the timeout we try to lock a file in the download cache directory for downloading.
CppOrPyList m_payloadLocations
the list with all the payload locations
const std::string & getDefaultLocalMetadataProviderPath()
Get the default local path for the local metadata provider.
const std::string & getNewPayloadLocation() const
Get the filename where to save newly created payload information.
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.
const std::set< std::string > & getUsableTagStates() const
Get the set of usable globaltag states allowed to be used for processing.
const std::string & getDownloadCacheDirectory() const
Get the directory where to place downloaded payloads.
std::vector< std::string > getPayloadLocations()
Get the list of payload locations.
std::string getDefaultRemoteMetadataProviderServer()
Get the default server URL for the remote metadata provider.
void setGlobalTagsPy(const boost::python::list &globalTags)
Set the list of globaltags from python.
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 timeout we try to lock a file in the download cache directory for downloading.
std::string m_downloadCacheDirectory
the directory to put newly downloaded payloads
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.
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.
boost::python::list & ensurePy()
Return the python list version.
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.
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...