diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h index af816a2a07673e7763164d67667341d202438a7c..0d9696ddb00c1dc0fe5413a5b3c0bb4927c313ca 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h @@ -112,6 +112,8 @@ namespace Mantid std::string m_prev; ///< The previous value for the property }; + /// Reset to "factory" settings. Removes current user properties + void reset(); /// Wipe out the current configuration and load a new one void updateConfig(const std::string& filename, const bool append=false, const bool update_caches=true); /// Save the configuration to the user file diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp index d867be1ae751c465908a867dbefd7d0d0b34ff41..fb21d09ec9437f23538df20387a4847d3d4f0941 100644 --- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp +++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp @@ -696,6 +696,29 @@ std::string ConfigServiceImpl::defaultConfig() const // Public member functions //------------------------------- +/** + * Removes the user properties file & loads a fresh configuration + */ +void ConfigServiceImpl::reset() +{ + // Remove the current user properties file and write a fresh one + try + { + Poco::File userFile(getUserFilename()); + userFile.remove(); + } + catch(Poco::Exception &) + { + } + createUserPropertiesFile(); + + //Now load the original + const bool append = false; + const bool updateCaches = true; + updateConfig(getPropertiesDir() + m_properties_file_name, + append, updateCaches); +} + /** Updates and existing configuration and restarts the logging * @param filename :: The filename and optionally path of the file to load * @param append :: If false (default) then any previous configuration is discarded, diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h index 0c186d83bb185349df7c99d3119116a5efb4de2d..740efe4ab1df3835c85050f74414eb593132e372 100644 --- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h +++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h @@ -301,6 +301,40 @@ public: } + void testLoadChangeClearSavesOriginalPropsFile() + { + // Backup current user settings + ConfigServiceImpl & settings = ConfigService::Instance(); + const std::string userFileBackup = settings.getUserFilename() + ".unittest"; + try + { + Poco::File userFile(settings.getUserFilename()); + userFile.moveTo(userFileBackup); + } + catch(Poco::Exception&){} + + const std::string propfile = ConfigService::Instance().getDirectoryOfExecutable() + + "MantidTest.properties"; + settings.updateConfig(propfile); + settings.setString("mantid.legs", "15"); + + settings.reset(); + + const std::string contents = readFile(settings.getUserFilename()); + // No mention of mantid.legs but not empty + TS_ASSERT(!contents.empty()); + TS_ASSERT(contents.find("mantid.legs") == std::string::npos); + + + try + { + Poco::File backup(userFileBackup); + backup.moveTo(settings.getUserFilename()); + } + catch(Poco::Exception &) {} + + } + void testSaveConfigWithPropertyRemoved() { const std::string filename("user.settings.testSaveConfigWithPropertyRemoved"); diff --git a/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h b/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h index 9a377174f89b02020cfee55ba80c4b393ce52bbb..34b64fe4492faa17a16f5e4ab96c571ae2f6f0b7 100755 --- a/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h +++ b/Code/Mantid/Framework/PythonAPI/inc/MantidPythonAPI/kernel_exports.h @@ -126,6 +126,15 @@ namespace Mantid } //@} + /** + * Return the properties file directory + * @returns A string containing the filename of the user properties file + */ + std::string getPropertiesDir() const + { + return Mantid::Kernel::ConfigService::Instance().getPropertiesDir(); + } + /** * Return the user properties filename * @returns A string containing the filename of the user properties file @@ -135,6 +144,13 @@ namespace Mantid return Mantid::Kernel::ConfigService::Instance().getUserFilename(); } + /** Reset to factory defaults + */ + void reset() + { + Mantid::Kernel::ConfigService::Instance().reset(); + } + /** Saves and properties changed from the default to the given file * @param filename :: A filename to write the settings to */ diff --git a/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp b/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp index eb1fc885e74333ed554f64bec5450697437d66bf..9b28da02468ba2a2f1d81fe058c708ad8c7513c4 100644 --- a/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp +++ b/Code/Mantid/Framework/PythonAPI/src/kernel_exports.cpp @@ -379,6 +379,8 @@ namespace PythonAPI .def("setDataSearchDirs", (void (ConfigServiceWrapper::*)(const boost::python::list &))&ConfigServiceWrapper::setDataSearchDirs) .def("appendDataSearchDir", &ConfigServiceWrapper::appendDataSearchDir) .def("getInstrumentDirectory", &ConfigServiceWrapper::getInstrumentDirectory) + .def("reset", &ConfigServiceWrapper::reset) + .def("getPropertiesDir", &ConfigServiceWrapper::getPropertiesDir) .def("getUserFilename", &ConfigServiceWrapper::getUserFilename) .def("saveConfig", &ConfigServiceWrapper::saveConfig) ; diff --git a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp index e18ced63baabcc961c94f8d8e292d00cbb5c735c..5cb95eec4cb6b5a0d82f8c01b5e72bfc6c35128b 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/kernel/src/Exports/ConfigService.cpp @@ -38,6 +38,9 @@ namespace void export_ConfigService() { class_<ConfigServiceImpl, boost::noncopyable>("ConfigServiceImpl", no_init) + .def("reset", &ConfigServiceImpl::reset, + "Clears all user settings and removes the user properties file") + .def("getLocalFilename", &ConfigServiceImpl::getLocalFilename, "Returns the path to the system wide properties file.") .def("getUserFilename", &ConfigServiceImpl::getUserFilename, "Returns the path to the user properties file")