Skip to content

Commit

Permalink
Work recursively as nesting length is not constant
Browse files Browse the repository at this point in the history
Refs #9067
  • Loading branch information
DanNixon authored and quantumsteve committed Apr 16, 2015
1 parent 8f4af44 commit b5ff8c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
Expand Up @@ -132,7 +132,9 @@ class MANTID_KERNEL_DLL ConfigServiceImpl {
bool use_cache = true) const;
/// Searches for a key in the configuration property
std::vector<std::string> getKeys(const std::string &keyName) const;
/// Returns a list of all keys in the configuration property
/// Returns a list of all keys under a given root key
std::vector<std::string> getKeysRecursive(const std::string &root = "") const;
/// Returns a list of all full keys in the config
std::vector<std::string> keys() const;
/// Removes the value from a selected keyName
void remove(const std::string &rootName) const;
Expand Down
42 changes: 31 additions & 11 deletions Code/Mantid/Framework/Kernel/src/ConfigService.cpp
Expand Up @@ -977,8 +977,7 @@ ConfigServiceImpl::getKeys(const std::string &keyName) const {
try {
m_pConf->keys(keyName, rawKeys);
// Work around a limitation of Poco < v1.4 which has no remove functionality
// so
// check those that have been marked with the correct flag
// so check those that have been marked with the correct flag
const size_t nraw = rawKeys.size();
for (size_t i = 0; i < nraw; ++i) {
const std::string key = rawKeys[i];
Expand All @@ -1000,27 +999,48 @@ ConfigServiceImpl::getKeys(const std::string &keyName) const {
}

/**
* Gets a list of all config options as property.key.
* Recursively gets a list of all config options from a given root node.
*
* @return Vector containing all config options
*/
std::vector<std::string> ConfigServiceImpl::keys() const {
std::vector<std::string> rootKeys;
m_pConf->keys(rootKeys);

std::vector<std::string> ConfigServiceImpl::getKeysRecursive(const std::string &root) const {
std::vector<std::string> allKeys;
std::vector<std::string> rootKeys = getKeys(root);

for (auto rkIt = rootKeys.begin(); rkIt != rootKeys.end(); ++rkIt) {
std::vector<std::string> subKeys;
m_pConf->keys(*rkIt, subKeys);
std::string searchString;
if (root.empty()) {
searchString = *rkIt;
} else {
searchString = root + "." + *rkIt;
}

std::vector<std::string> subKeys = getKeysRecursive(searchString);

for (auto skIt = subKeys.begin(); skIt != subKeys.end(); ++skIt) {
allKeys.push_back(*riIt + "." + *skIt);
if (subKeys.size() == 0) {
allKeys.push_back(*rkIt);
} else {
for (auto skIt = subKeys.begin(); skIt != subKeys.end(); ++skIt) {
allKeys.push_back(*rkIt + "." + *skIt);
}
}
}

return allKeys;
}

/**
* Recursively gets a list of all config options.
*
* This function is needed as Boost Python does not like calling function with
* default arguments.
*
* @return Vector containing all config options
*/
std::vector<std::string> ConfigServiceImpl::keys() const {
return getKeysRecursive();
}

/** Removes a key from the memory stored properties file and inserts the key
*into the
* changed key list so that when the program calls saveConfig the properties
Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h
Expand Up @@ -557,6 +557,17 @@ class ConfigServiceTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(keyVector.size(), 5);
}

void testGetAllKeys()
{
const std::string propfilePath = ConfigService::Instance().getDirectoryOfExecutable();
const std::string propfile = propfilePath + "MantidTest.properties";
ConfigService::Instance().updateConfig(propfile);

std::vector<std::string> keys = ConfigService::Instance().keys();

TS_ASSERT_EQUALS(keys.size(), 17);
}

void testRemovingProperty()
{
const std::string propfile = ConfigService::Instance().getDirectoryOfExecutable()
Expand Down

0 comments on commit b5ff8c3

Please sign in to comment.