From 79b6f4ace280d1386cc2d82f3b04dd03005fb05f Mon Sep 17 00:00:00 2001 From: Robert Whitley <robert.whitley@stfc.ac.uk> Date: Tue, 12 Jul 2011 08:23:48 +0000 Subject: [PATCH] Refs #3332. Added a method to ConfigService to retrieve subkeys from a configuration. --- .../Kernel/inc/MantidKernel/ConfigService.h | 2 ++ .../Framework/Kernel/src/ConfigService.cpp | 22 ++++++++++++++++ .../Framework/Kernel/test/ConfigServiceTest.h | 26 +++++++++++++++++++ .../Properties/MantidTest.properties | 6 +++++ 4 files changed, 56 insertions(+) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h index a770f125a36..a48818af4eb 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/ConfigService.h @@ -118,6 +118,8 @@ namespace Mantid void saveConfig(const std::string &filename) const; /// Searches for a configuration property std::string getString(const std::string& keyName, bool use_cache=true) const; + /// Searches for a key in the configuration property + std::vector<std::string> getKeys(const std::string& keyName) const; /// Sets a configuration property void setString(const std::string & keyName, const std::string & keyValue); // Searches for a configuration property and returns its value diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp index 413458fb289..f1cd69188b7 100644 --- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp +++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp @@ -814,6 +814,28 @@ std::string ConfigServiceImpl::getString(const std::string& keyName, bool use_ca return retVal; } +/** Searches for keys within the currently loaded configuaration values and + * returns them as strings in a vector. + * + * @param keyName :: The case sensitive name of the property that you need the key for. + * @returns The string value of each key within a vector, or an empty vector if there isn't + * a key or it couldn't be found. + */ +std::vector<std::string> ConfigServiceImpl::getKeys(const std::string& keyName) const +{ + std::vector<std::string> keyVector; + try + { + m_pConf->keys(keyName,keyVector); + } + catch (Poco::NotFoundException&) + { + g_log.debug() << "Unable to find " << keyName << " in the properties file" << std::endl; + keyVector.clear(); + } + return keyVector; +} + /** * Set a configuration property. An existing key will have its value updated. * @param key :: The key to refer to this property diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h index 5056f21de27..d48aec68fe6 100644 --- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h +++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h @@ -337,6 +337,32 @@ public: } + void testGetKeysWithValidInput() + { + // Returns all subkeys with the given root key + std::vector<std::string> keyVector = ConfigService::Instance().getKeys("workspace.sendto"); + TS_ASSERT_EQUALS(keyVector.size(), 4); + TS_ASSERT_EQUALS(keyVector[0], "1"); + TS_ASSERT_EQUALS(keyVector[1], "2"); + TS_ASSERT_EQUALS(keyVector[2], "3"); + TS_ASSERT_EQUALS(keyVector[3], "4"); + } + + void testGetKeysWithZeroSubKeys() + { + std::vector<std::string> keyVector = ConfigService::Instance().getKeys("mantid.legs"); + TS_ASSERT_EQUALS(keyVector.size(), 0); + std::vector<std::string> keyVector2 = ConfigService::Instance().getKeys("mantidlegs"); + TS_ASSERT_EQUALS(keyVector2.size(), 0); + } + + void testGetKeysWithEmptyPrefix() + { + //Returns all *root* keys, i.e. unique keys left of the first period + std::vector<std::string> keyVector = ConfigService::Instance().getKeys(""); + TS_ASSERT_EQUALS(keyVector.size(), 5); + } + protected: bool m_valueChangedSent; std::string m_key; diff --git a/Code/Mantid/Framework/Properties/MantidTest.properties b/Code/Mantid/Framework/Properties/MantidTest.properties index a0d8bb160c5..d95d88f40ea 100644 --- a/Code/Mantid/Framework/Properties/MantidTest.properties +++ b/Code/Mantid/Framework/Properties/MantidTest.properties @@ -16,3 +16,9 @@ logging.channels.fileChannel.formatter.pattern = %Y-%m-%d %H:%M:%S,%i [%I] %p %s # Test a relative path conversion. Using a property where the path doesn't have to exist for conversion. defaultsave.directory = ../../nonexistent +# Test keys +workspace.sendto.1 = python,SaveNexus +workspace.sendto.2 = gsas,SaveGSS +workspace.sendto.3 = SansView,SaveCansas1D +workspace.sendto.4.5 = SansView,SaveCansas1D + -- GitLab