Mantid
Loading...
Searching...
No Matches
ConfigService.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
14#include <boost/python/class.hpp>
15#include <boost/python/copy_const_reference.hpp>
16#include <boost/python/def.hpp>
17#include <boost/python/overloads.hpp>
18#include <boost/python/reference_existing_object.hpp>
19
25
26using namespace boost::python;
27
28using ExtractStdString = boost::python::extract<std::string>;
29
31
32namespace {
34void setDataSearchDirs(ConfigServiceImpl &self, const object &paths) {
35 ExtractStdString singleString(paths);
36 if (singleString.check()) {
37 self.setDataSearchDirs(singleString());
38 } else {
40 }
41}
42
44std::string getStringUsingCache(ConfigServiceImpl const *const self, const std::string &key) {
45 return self->getString(key, true);
46}
47
48const InstrumentInfo &getInstrument(ConfigServiceImpl const *const self, const object &name = object()) {
49 if (name.is_none())
50 return self->getInstrument();
51 else
52 return self->getInstrument(ExtractStdString(name)());
53}
54
56std::string getStringUsingCacheElseDefault(ConfigServiceImpl const *const self, const std::string &key,
57 const std::string &defaultValue) {
58 if (self->hasProperty(key))
59 return self->getString(key, true);
60 else
61 return defaultValue;
62}
63
64GNU_DIAG_OFF("unused-local-typedef")
65// Ignore -Wconversion warnings coming from boost::python
66// Seen with GCC 7.1.1 and Boost 1.63.0
67GNU_DIAG_OFF("conversion")
68// Overload generator for getInstrument
69BOOST_PYTHON_FUNCTION_OVERLOADS(getInstrument_Overload, getInstrument, 1, 2)
70// Overload generator for getString
72
73GNU_DIAG_ON("conversion")
74GNU_DIAG_ON("unused-local-typedef")
75} // namespace
76
79
80 std_vector_exporter<FacilityInfo *>::wrap("std_vector_facilityinfo");
81
82 class_<ConfigServiceImpl, boost::noncopyable>("ConfigServiceImpl", no_init)
83 .def("reset", &ConfigServiceImpl::reset, arg("self"),
84 "Clears all user settings and removes the user properties file")
85 .def("getAppDataDirectory", &ConfigServiceImpl::getAppDataDir, arg("self"),
86 "Returns the path to Mantid's application directory")
87 .def("getLocalFilename", &ConfigServiceImpl::getLocalFilename, arg("self"),
88 "Returns the path to the system wide properties file.")
89 .def("getUserFilename", &ConfigServiceImpl::getUserFilename, arg("self"),
90 "Returns the path to the user properties file")
91 .def("getPropertiesDir", &ConfigServiceImpl::getPropertiesDir, arg("self"),
92 "Returns the directory containing the Mantid.properties file.")
93 .def("getUserPropertiesDir", &ConfigServiceImpl::getUserPropertiesDir, arg("self"),
94 "Returns the directory to use to write out Mantid information")
95 .def("getInstrumentDirectory", &ConfigServiceImpl::getInstrumentDirectory, arg("self"),
96 "Returns the directory used for the instrument definitions")
97 .def("getInstrumentDirectories", &ConfigServiceImpl::getInstrumentDirectories, arg("self"),
98 return_value_policy<reference_existing_object>(),
99 "Returns the list of directories searched for the instrument "
100 "definitions")
101 .def("getFacilityNames", &ConfigServiceImpl::getFacilityNames, arg("self"), "Returns the default facility")
102 .def("getFacilities", &ConfigServiceImpl::getFacilities, arg("self"), "Returns the default facility")
103 .def("getFacility", (const FacilityInfo &(ConfigServiceImpl::*)() const) & ConfigServiceImpl::getFacility,
104 arg("self"), return_value_policy<reference_existing_object>(), "Returns the default facility")
105 .def("getFacility",
106 (const FacilityInfo &(ConfigServiceImpl::*)(const std::string &) const) & ConfigServiceImpl::getFacility,
107 (arg("self"), arg("facilityName")), return_value_policy<reference_existing_object>(),
108 "Returns the named facility. Raises an RuntimeError if it does not "
109 "exist")
110 .def("setFacility", &ConfigServiceImpl::setFacility, (arg("self"), arg("facilityName")),
111 "Sets the current facility to the given name")
112 .def("updateFacilities", &ConfigServiceImpl::updateFacilities, (arg("self"), arg("fileName")),
113 "Loads facility information from a provided file")
114 .def("getInstrument", &getInstrument,
115 getInstrument_Overload(
116 "Returns the named instrument. If name = \"\" then the "
117 "default.instrument is returned",
118 (arg("self"),
119 arg("instrumentName") = boost::python::object()))[return_value_policy<copy_const_reference>()])
120 .def("getString", &ConfigServiceImpl::getString,
121 getString_Overload("Returns the named key's value. If use_cache = "
122 "true [default] then relative paths->absolute",
123 (arg("self"), arg("key"), arg("pathAbsolute") = true)))
124
125 .def("setString", &ConfigServiceImpl::setString, (arg("self"), arg("key"), arg("value")),
126 "Set the given property name. "
127 "If it does not exist it is added to the current configuration")
128 .def("hasProperty", &ConfigServiceImpl::hasProperty, (arg("self"), arg("rootName")))
129 .def("getDataSearchDirs", &ConfigServiceImpl::getDataSearchDirs, arg("self"),
130 return_value_policy<copy_const_reference>(), "Return the current list of data search paths")
131 .def("appendDataSearchDir", &ConfigServiceImpl::appendDataSearchDir, (arg("self"), arg("path")),
132 "Append a directory to the current list of data search paths")
133 .def("appendDataSearchSubDir", &ConfigServiceImpl::appendDataSearchSubDir, (arg("self"), arg("subdir")),
134 "Appends a sub-directory to each data search directory "
135 "and appends the new paths back to datasearch directories")
136 .def("setDataSearchDirs", &setDataSearchDirs, (arg("self"), arg("searchDirs")),
137 "Set the datasearch.directories property from a list of strings or "
138 "a single ';' separated string.")
139 .def("saveConfig", &ConfigServiceImpl::saveConfig, (arg("self"), arg("filename")),
140 "Saves the keys that have changed from their default to the given "
141 "filename")
142 .def("setLogLevel", &ConfigServiceImpl::setLogLevel, (arg("self"), arg("logLevel"), arg("quiet") = false),
143 "Sets the log level priority for all the log channels, logLevel "
144 "1 = Fatal, 6 = information, 7 = Debug")
145 .def("keys", &ConfigServiceImpl::keys, arg("self"))
146
147 // Treat this as a dictionary
148 .def("get", &getStringUsingCache, (arg("self"), arg("key")),
149 "get the string value of a property; return empty string value if the property "
150 "is not found in the configuration")
151 .def("get", &getStringUsingCacheElseDefault, (arg("self"), arg("key")), arg("default"),
152 "get the string value of a property; return a default string value if the property "
153 "is not found in the configuration")
154 .def("__getitem__", &getStringUsingCache, (arg("self"), arg("key")))
155 .def("__setitem__", &ConfigServiceImpl::setString, (arg("self"), arg("key"), arg("value")))
156 .def("__contains__", &ConfigServiceImpl::hasProperty, (arg("self"), arg("key")))
157 .def("Instance", &ConfigService::Instance, return_value_policy<reference_existing_object>(),
158 "Returns a reference to the ConfigService")
159 .staticmethod("Instance");
160}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
void export_ConfigService()
boost::python::extract< std::string > ExtractStdString
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2) void export_Property()
Definition: Property.cpp:102
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
The ConfigService class provides a simple facade to access the Configuration functionality of the Man...
Definition: ConfigService.h:63
const std::vector< std::string > & getInstrumentDirectories() const
Get instrument search directories.
const FacilityInfo & getFacility() const
Get the default facility.
void reset()
Reset to "factory" settings. Removes current user properties.
std::string getLocalFilename() const
Return the local properties filename.
std::string getAppDataDir()
Returns the system's appdata directory.
std::string getPropertiesDir() const
Returns the directory where the Mantid.properties file is found.
std::string getUserPropertiesDir() const
Returns a directory to use to write out Mantid information.
void saveConfig(const std::string &filename) const
Save the configuration to the user file.
const std::vector< std::string > & getDataSearchDirs() const
Get the list of search paths.
const InstrumentInfo & getInstrument(const std::string &instrumentName="") const
Look for an instrument.
void updateFacilities(const std::string &fName="")
Load facility information from instrumentDir/Facilities.xml file.
const std::vector< FacilityInfo * > getFacilities() const
Get the list of facilities.
void appendDataSearchDir(const std::string &path)
Adds the passed path to the end of the list of data search paths.
void setLogLevel(int logLevel, bool quiet=false)
Sets the log level priority for all log channels.
void setDataSearchDirs(const std::vector< std::string > &searchDirs)
Set a list of search paths via a vector.
std::string getString(const std::string &keyName, bool pathAbsolute=true) const
Searches for a configuration property.
const std::string getInstrumentDirectory() const
Get instrument search directory.
void setString(const std::string &key, const std::string &value)
Sets a configuration property.
std::string getUserFilename() const
Return the user properties filename.
void setFacility(const std::string &facilityName)
Set the default facility.
void appendDataSearchSubDir(const std::string &subdir)
Appends subdirectory to each of the specified data search directories.
std::vector< std::string > keys() const
Returns a list of all full keys in the config.
const std::vector< std::string > getFacilityNames() const
Get the list of facility names.
bool hasProperty(const std::string &rootName) const
Checks to see whether a key has a value assigned to it.
A class that holds information about a facility.
Definition: FacilityInfo.h:36
A class that holds information about an instrument.
Manage the lifetime of a class intended to be a singleton.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
A struct to help export std::vector types.