Skip to content

Commit

Permalink
Make the implementation use openstudio::path, keeping a std::string a…
Browse files Browse the repository at this point in the history
…ddSearchPath/setSearchPaths for convenience
  • Loading branch information
jmarrec committed Dec 19, 2024
1 parent 2957896 commit 4613368
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ namespace energyplus {
}

// Search Path
for (const std::string& searchPath : modelObject.searchPaths()) {
for (const openstudio::path& searchPath : modelObject.searchPaths()) {
IdfExtensibleGroup eg = idfObject.pushExtensibleGroup();
eg.setString(PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath);
eg.setString(PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath.generic_string());
}

return idfObject;
Expand Down
38 changes: 25 additions & 13 deletions src/model/PythonPluginSearchPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@

#include "../utilities/idf/WorkspaceExtensibleGroup.hpp"
#include "../utilities/core/Assert.hpp"
#include "../utilities/core/Path.hpp"

#include <utilities/idd/IddEnums.hxx>
#include <utilities/idd/OS_PythonPlugin_SearchPaths_FieldEnums.hxx>

#include <iterator>

namespace openstudio {
namespace model {

Expand Down Expand Up @@ -78,12 +81,12 @@ namespace model {
return getBooleanFieldValue(OS_PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath);
}

std::vector<std::string> PythonPluginSearchPaths_Impl::searchPaths() const {
std::vector<std::string> result;
std::vector<openstudio::path> PythonPluginSearchPaths_Impl::searchPaths() const {
std::vector<openstudio::path> result;
for (const auto& eg : extensibleGroups()) {
auto _s = eg.getString(OS_PythonPlugin_SearchPathsExtensibleFields::SearchPath);
OS_ASSERT(_s);
result.push_back(_s.get());
result.push_back(openstudio::toPath(_s.get()));
}
return result;
}
Expand All @@ -109,26 +112,24 @@ namespace model {
return result;
}

bool PythonPluginSearchPaths_Impl::addSearchPath(const std::string& searchPath) {
std::vector<std::string> existingSearchPaths = this->searchPaths();
if (std::find_if(existingSearchPaths.begin(), existingSearchPaths.end(),
[&searchPath](const std::string& s) { return openstudio::istringEqual(s, searchPath); })
!= existingSearchPaths.end()) {
bool PythonPluginSearchPaths_Impl::addSearchPath(const openstudio::path& searchPath) {
std::vector<openstudio::path> existingSearchPaths = this->searchPaths();
if (std::find(existingSearchPaths.begin(), existingSearchPaths.end(), searchPath) != existingSearchPaths.end()) {
LOG(Info, "Not adding search path '" << searchPath << "' to PythonPlugin:SearchPaths since it is already present");
// Return true anyways, it's a success
return true;
}

auto eg = getObject<ModelObject>().pushExtensibleGroup().cast<WorkspaceExtensibleGroup>();
bool result = eg.setString(OS_PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath);
bool result = eg.setString(OS_PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath.generic_string());
if (!result) {
getObject<ModelObject>().eraseExtensibleGroup(eg.groupIndex());
}

return result;
}

bool PythonPluginSearchPaths_Impl::setSearchPaths(const std::vector<std::string>& searchPaths) {
bool PythonPluginSearchPaths_Impl::setSearchPaths(const std::vector<openstudio::path>& searchPaths) {
bool result = true;

clearSearchPaths();
Expand Down Expand Up @@ -166,7 +167,7 @@ namespace model {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->addepinEnvironmentVariabletoSearchPath();
}

std::vector<std::string> PythonPluginSearchPaths::searchPaths() const {
std::vector<openstudio::path> PythonPluginSearchPaths::searchPaths() const {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->searchPaths();
}

Expand All @@ -182,14 +183,25 @@ namespace model {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->setAddepinEnvironmentVariabletoSearchPath(addepinEnvironmentVariabletoSearchPath);
}

bool PythonPluginSearchPaths::addSearchPath(const std::string& searchPath) {
bool PythonPluginSearchPaths::addSearchPath(const openstudio::path& searchPath) {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->addSearchPath(searchPath);
}

bool PythonPluginSearchPaths::setSearchPaths(const std::vector<std::string>& searchPaths) {
bool PythonPluginSearchPaths::setSearchPaths(const std::vector<openstudio::path>& searchPaths) {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->setSearchPaths(searchPaths);
}

bool PythonPluginSearchPaths::addSearchPath(const std::string& searchPath) {
return getImpl<detail::PythonPluginSearchPaths_Impl>()->addSearchPath(openstudio::toPath(searchPath));
}

bool PythonPluginSearchPaths::setSearchPaths(const std::vector<std::string>& searchPaths) {
std::vector<openstudio::path> paths;
paths.reserve(searchPaths.size());
std::transform(searchPaths.cbegin(), searchPaths.cend(), std::back_inserter(paths), [](const std::string& s) { return openstudio::toPath(s); });
return getImpl<detail::PythonPluginSearchPaths_Impl>()->setSearchPaths(paths);
}

void PythonPluginSearchPaths::clearSearchPaths() {
getImpl<detail::PythonPluginSearchPaths_Impl>()->clearSearchPaths();
}
Expand Down
9 changes: 7 additions & 2 deletions src/model/PythonPluginSearchPaths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <model/ModelAPI.hpp>
#include "ModelObject.hpp"

#include "../utilities/core/Filesystem.hpp"

namespace openstudio {
namespace model {

Expand Down Expand Up @@ -69,7 +71,7 @@ namespace model {

bool addepinEnvironmentVariabletoSearchPath() const;

std::vector<std::string> searchPaths() const;
std::vector<openstudio::path> searchPaths() const;

//@}
/** @name Setters */
Expand All @@ -81,8 +83,11 @@ namespace model {

bool setAddepinEnvironmentVariabletoSearchPath(bool addepinEnvironmentVariabletoSearchPath);

bool addSearchPath(const std::string& searchPath);
bool addSearchPath(const openstudio::path& searchPath);
bool setSearchPaths(const std::vector<openstudio::path>& searchPaths);

// Convenience, forwards to the openstudio::path equivalent
bool addSearchPath(const std::string& searchPath);
bool setSearchPaths(const std::vector<std::string>& searchPaths);

void clearSearchPaths();
Expand Down
7 changes: 4 additions & 3 deletions src/model/PythonPluginSearchPaths_Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <model/ModelAPI.hpp>
#include "ModelObject_Impl.hpp"
#include "../utilities/core/Filesystem.hpp"

namespace openstudio {
namespace model {
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace model {

bool addepinEnvironmentVariabletoSearchPath() const;

std::vector<std::string> searchPaths() const;
std::vector<openstudio::path> searchPaths() const;

//@}
/** @name Setters */
Expand All @@ -83,9 +84,9 @@ namespace model {

bool setAddepinEnvironmentVariabletoSearchPath(bool addepinEnvironmentVariabletoSearchPath);

bool addSearchPath(const std::string& searchPath);
bool addSearchPath(const openstudio::path& searchPath);

bool setSearchPaths(const std::vector<std::string>& searchPaths);
bool setSearchPaths(const std::vector<openstudio::path>& searchPaths);

void clearSearchPaths();

Expand Down

0 comments on commit 4613368

Please sign in to comment.