Mantid
Loading...
Searching...
No Matches
ConfigService.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
15#include <optional>
16
17#include <map>
18#include <set>
19#include <string>
20#include <vector>
21
22#include <Poco/Notification.h>
23#include <Poco/NotificationCenter.h>
24
25//----------------------------------------------------------------------
26// Forward declarations
27//----------------------------------------------------------------------
29namespace Poco {
30class AbstractObserver;
31class Channel;
32template <class C> class AutoPtr;
33namespace Util {
34class PropertyFileConfiguration;
35class SystemConfiguration;
36} // namespace Util
37} // namespace Poco
39
40namespace Mantid {
41
43MANTID_KERNEL_DLL std::string welcomeMessage();
44
45namespace Kernel {
46//----------------------------------------------------------------------
47// More forward declarations
48//----------------------------------------------------------------------
49class FacilityInfo;
50class InstrumentInfo;
51
63class MANTID_KERNEL_DLL ConfigServiceImpl final {
64public:
70 class ConfigServiceNotification : public Poco::Notification {
71 public:
73 ConfigServiceNotification() : Poco::Notification() {}
74 };
75
82 public:
88 ValueChanged(const std::string &name, const std::string &newvalue, const std::string &prevvalue)
89 : ConfigServiceNotification(), m_name(name), m_value(newvalue), m_prev(prevvalue) {}
92 const std::string &key() const { return this->m_name; }
94 const std::string &curValue() const { return this->m_value; }
96 const std::string &preValue() const { return this->m_prev; }
97 private:
98 std::string m_name;
99 std::string m_value;
100 std::string m_prev;
101 };
102
104 void setBaseDirectory();
106 void reset();
108 void updateConfig(const std::string &filename, const bool append = false, const bool update_caches = true);
110 void saveConfig(const std::string &filename) const;
112 std::string getString(const std::string &keyName, bool pathAbsolute = true) const;
114 std::vector<std::string> getKeys(const std::string &keyName) const;
116 std::vector<std::string> keys() const;
118 void remove(const std::string &rootName);
120 bool hasProperty(const std::string &rootName) const;
122 bool isExecutable(const std::string &target) const;
124 void launchProcess(const std::string &programFilePath, const std::vector<std::string> &programArguments) const;
126 void setString(const std::string &key, const std::string &value);
127 // Searches for a configuration property and returns its value
128 template <typename T> std::optional<T> getValue(const std::string &keyName);
130 std::string getLocalFilename() const;
132 std::string getUserFilename() const;
133
137 std::string getEnvironment(const std::string &keyName);
139 std::string getOSName();
141 std::string getComputerName();
143 std::string getOSArchitecture();
145 std::string getOSVersion();
147 std::string getOSVersionReadable();
149 std::string getUsername();
151 std::string getCurrentDir();
153 std::string getCurrentDir() const;
155 std::string getTempDir();
157 std::string getAppDataDir();
158 // Return the executable path
159 std::string getDirectoryOfExecutable() const;
160 // Return the full path to the executable
161 std::string getPathToExecutable() const;
162 // Check if the path is on a network drive
163 bool isNetworkDrive([[maybe_unused]] const std::string &path);
165
167 std::string getPropertiesDir() const;
170 std::string getUserPropertiesDir() const;
171
175 const std::vector<std::string> &getDataSearchDirs() const;
177 void setDataSearchDirs(const std::vector<std::string> &searchDirs);
179 void setDataSearchDirs(const std::string &searchDirs);
181 void appendDataSearchDir(const std::string &path);
183 void appendDataSearchSubDir(const std::string &subdir);
185 void setInstrumentDirectories(const std::vector<std::string> &directories);
187 const std::vector<std::string> &getInstrumentDirectories() const;
189 const std::string getInstrumentDirectory() const;
191 const std::string getVTPFileDirectory();
193
195 void updateFacilities(const std::string &fName = "");
197 const std::vector<FacilityInfo *> getFacilities() const;
199 const std::vector<std::string> getFacilityNames() const;
201 const FacilityInfo &getFacility() const;
203 const FacilityInfo &getFacility(const std::string &facilityName) const;
205 void setFacility(const std::string &facilityName);
207 void setLogLevel(int logLevel, bool quiet = false);
209 void setLogLevel(std::string const &logLevel, bool quiet = false);
210 // return the string name for the log level
211 std::string getLogLevel();
212
214 const InstrumentInfo &getInstrument(const std::string &instrumentName = "") const;
215
216 // Find the longest matching instrument prefix for the given hint.
217 const std::string findLongestInstrumentPrefix(const std::string &hint) const;
218
220 void addObserver(const Poco::AbstractObserver &observer) const;
221
223 void removeObserver(const Poco::AbstractObserver &observer) const;
224
225 // Starts up the logging
226 void configureLogging();
227
229 Kernel::ProxyInfo &getProxy(const std::string &url);
230
231 std::string getFullPath(const std::string &filename, const bool ignoreDirs, const int options) const;
232
233private:
236 mutable Poco::NotificationCenter m_notificationCenter;
237
238 // Private constructors and destructor for singleton class
242
243 virtual ~ConfigServiceImpl();
244
246 void loadConfig(const std::string &filename, const bool append = false);
248 bool readFile(const std::string &filename, std::string &contents) const;
249
251 void createUserPropertiesFile() const;
253 std::string makeAbsolute(const std::string &dir, const std::string &key) const;
255 void cacheDataSearchPaths();
257 void cacheInstrumentPaths();
259 bool isInDataSearchList(const std::string &path) const;
262 void clearFacilities();
264 const std::vector<std::string> getFacilityFilenames(const std::string &fName);
267 bool addDirectoryifExists(const std::string &directoryName, std::vector<std::string> &directoryList);
269 void getKeysRecursive(const std::string &root, std::vector<std::string> &allKeys) const;
270
272 Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> m_pConf;
274 Poco::AutoPtr<Poco::Util::SystemConfiguration> m_pSysConfig;
275
277 mutable std::set<std::string> m_changed_keys;
278
280 std::string m_strBaseDir;
282 std::string m_propertyString;
284 const std::string m_properties_file_name;
288 std::vector<std::string> m_dataSearchDirs;
290 std::vector<std::string> m_instrumentDirs;
291
293 std::vector<FacilityInfo *> m_facilities;
294
296 std::set<std::string> m_configPaths;
297
301 bool m_isProxySet = false;
302
303 // cache for longest matching instrument prefix
304 mutable std::vector<std::string> m_instrumentPrefixesCache;
305 mutable bool m_isInstrumentPrefixesCached = false;
306};
307
308EXTERN_MANTID_KERNEL template class MANTID_KERNEL_DLL Mantid::Kernel::SingletonHolder<ConfigServiceImpl>;
310
312using ConfigValChangeNotification_ptr = const Poco::AutoPtr<Mantid::Kernel::ConfigServiceImpl::ValueChanged> &;
313
314} // namespace Kernel
315} // namespace Mantid
const std::string & m_value
Definition Algorithm.cpp:71
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
This is the base class for POCO Notifications sent out from the Config Service.
ConfigServiceNotification()
Empty constructor for ConfigServiceNotification Base Class.
This is the class for the notification that is to be sent when a value has been changed in config ser...
std::string m_name
The name of the changed the property.
ValueChanged(const std::string &name, const std::string &newvalue, const std::string &prevvalue)
Creates the Notification object with the required values.
std::string m_prev
The previous value for the property.
std::string m_value
The new value for the property.
const std::string & curValue() const
The new value for the property.
const std::string & key() const
The name of the user property that has changed, as it appears in the user.properties file.
const std::string & preValue() const
The previous value for the property.
The ConfigService class provides a simple facade to access the Configuration functionality of the Man...
std::string m_propertyString
The configuration properties in string format.
const std::string m_properties_file_name
The filename of the Mantid properties file.
std::set< std::string > m_configPaths
List of config paths that may be relative.
std::string m_strBaseDir
The directory that is considered to be the base directory.
std::set< std::string > m_changed_keys
A set of property keys that have been changed.
std::vector< std::string > m_dataSearchDirs
Store a list of data search paths.
ConfigServiceImpl(const ConfigServiceImpl &)
Private copy constructor. Prevents singleton being copied.
std::vector< std::string > m_instrumentPrefixesCache
Poco::AutoPtr< Poco::Util::SystemConfiguration > m_pSysConfig
the POCO system Config Object
Poco::NotificationCenter m_notificationCenter
Handles distribution of Poco signals.
Kernel::ProxyInfo m_proxyInfo
local cache of proxy details
const std::string m_user_properties_file_name
The filename of the Mantid user properties file.
Poco::AutoPtr< Poco::Util::PropertyFileConfiguration > m_pConf
the POCO file config object
std::vector< FacilityInfo * > m_facilities
The list of available facilities.
std::vector< std::string > m_instrumentDirs
Store a list of instrument directory paths.
A class that holds information about a facility.
A class that holds information about an instrument.
ProxyInfo : Container for carrying around network proxy information.
Definition ProxyInfo.h:17
Manage the lifetime of a class intended to be a singleton.
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService
const Poco::AutoPtr< Mantid::Kernel::ConfigServiceImpl::ValueChanged > & ConfigValChangeNotification_ptr
Helper class which provides the Collimation Length for SANS instruments.
MANTID_KERNEL_DLL std::string welcomeMessage()
Returns the welcome message for Mantid.
Policy class controlling creation of the singleton Implementation classes should mark their default c...