From 4613368223f368bff8fc9be839d01fbd6a45a782 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 19 Dec 2024 13:00:25 +0100 Subject: [PATCH] Make the implementation use openstudio::path, keeping a std::string addSearchPath/setSearchPaths for convenience --- ...orwardTranslatePythonPluginSearchPaths.cpp | 4 +- src/model/PythonPluginSearchPaths.cpp | 38 ++++++++++++------- src/model/PythonPluginSearchPaths.hpp | 9 ++++- src/model/PythonPluginSearchPaths_Impl.hpp | 7 ++-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/energyplus/ForwardTranslator/ForwardTranslatePythonPluginSearchPaths.cpp b/src/energyplus/ForwardTranslator/ForwardTranslatePythonPluginSearchPaths.cpp index 6e1be37bb4..dce66cea4f 100644 --- a/src/energyplus/ForwardTranslator/ForwardTranslatePythonPluginSearchPaths.cpp +++ b/src/energyplus/ForwardTranslator/ForwardTranslatePythonPluginSearchPaths.cpp @@ -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; diff --git a/src/model/PythonPluginSearchPaths.cpp b/src/model/PythonPluginSearchPaths.cpp index 4b13b08ed6..d88f5763ee 100644 --- a/src/model/PythonPluginSearchPaths.cpp +++ b/src/model/PythonPluginSearchPaths.cpp @@ -32,10 +32,13 @@ #include "../utilities/idf/WorkspaceExtensibleGroup.hpp" #include "../utilities/core/Assert.hpp" +#include "../utilities/core/Path.hpp" #include #include +#include + namespace openstudio { namespace model { @@ -78,12 +81,12 @@ namespace model { return getBooleanFieldValue(OS_PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath); } - std::vector PythonPluginSearchPaths_Impl::searchPaths() const { - std::vector result; + std::vector PythonPluginSearchPaths_Impl::searchPaths() const { + std::vector 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; } @@ -109,18 +112,16 @@ namespace model { return result; } - bool PythonPluginSearchPaths_Impl::addSearchPath(const std::string& searchPath) { - std::vector 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 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().pushExtensibleGroup().cast(); - bool result = eg.setString(OS_PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath); + bool result = eg.setString(OS_PythonPlugin_SearchPathsExtensibleFields::SearchPath, searchPath.generic_string()); if (!result) { getObject().eraseExtensibleGroup(eg.groupIndex()); } @@ -128,7 +129,7 @@ namespace model { return result; } - bool PythonPluginSearchPaths_Impl::setSearchPaths(const std::vector& searchPaths) { + bool PythonPluginSearchPaths_Impl::setSearchPaths(const std::vector& searchPaths) { bool result = true; clearSearchPaths(); @@ -166,7 +167,7 @@ namespace model { return getImpl()->addepinEnvironmentVariabletoSearchPath(); } - std::vector PythonPluginSearchPaths::searchPaths() const { + std::vector PythonPluginSearchPaths::searchPaths() const { return getImpl()->searchPaths(); } @@ -182,14 +183,25 @@ namespace model { return getImpl()->setAddepinEnvironmentVariabletoSearchPath(addepinEnvironmentVariabletoSearchPath); } - bool PythonPluginSearchPaths::addSearchPath(const std::string& searchPath) { + bool PythonPluginSearchPaths::addSearchPath(const openstudio::path& searchPath) { return getImpl()->addSearchPath(searchPath); } - bool PythonPluginSearchPaths::setSearchPaths(const std::vector& searchPaths) { + bool PythonPluginSearchPaths::setSearchPaths(const std::vector& searchPaths) { return getImpl()->setSearchPaths(searchPaths); } + bool PythonPluginSearchPaths::addSearchPath(const std::string& searchPath) { + return getImpl()->addSearchPath(openstudio::toPath(searchPath)); + } + + bool PythonPluginSearchPaths::setSearchPaths(const std::vector& searchPaths) { + std::vector 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()->setSearchPaths(paths); + } + void PythonPluginSearchPaths::clearSearchPaths() { getImpl()->clearSearchPaths(); } diff --git a/src/model/PythonPluginSearchPaths.hpp b/src/model/PythonPluginSearchPaths.hpp index 0e43f616c4..3edb0275f3 100644 --- a/src/model/PythonPluginSearchPaths.hpp +++ b/src/model/PythonPluginSearchPaths.hpp @@ -33,6 +33,8 @@ #include #include "ModelObject.hpp" +#include "../utilities/core/Filesystem.hpp" + namespace openstudio { namespace model { @@ -69,7 +71,7 @@ namespace model { bool addepinEnvironmentVariabletoSearchPath() const; - std::vector searchPaths() const; + std::vector searchPaths() const; //@} /** @name Setters */ @@ -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& searchPaths); + // Convenience, forwards to the openstudio::path equivalent + bool addSearchPath(const std::string& searchPath); bool setSearchPaths(const std::vector& searchPaths); void clearSearchPaths(); diff --git a/src/model/PythonPluginSearchPaths_Impl.hpp b/src/model/PythonPluginSearchPaths_Impl.hpp index 21551c4780..5afa04eedb 100644 --- a/src/model/PythonPluginSearchPaths_Impl.hpp +++ b/src/model/PythonPluginSearchPaths_Impl.hpp @@ -32,6 +32,7 @@ #include #include "ModelObject_Impl.hpp" +#include "../utilities/core/Filesystem.hpp" namespace openstudio { namespace model { @@ -71,7 +72,7 @@ namespace model { bool addepinEnvironmentVariabletoSearchPath() const; - std::vector searchPaths() const; + std::vector searchPaths() const; //@} /** @name Setters */ @@ -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& searchPaths); + bool setSearchPaths(const std::vector& searchPaths); void clearSearchPaths();