Skip to content
Snippets Groups Projects
Commit 79b6f4ac authored by Robert Whitley's avatar Robert Whitley
Browse files

Refs #3332. Added a method to ConfigService to retrieve subkeys from a configuration.

parent 14bba0e9
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......
......@@ -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;
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment